Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
Loading...
Searching...
No Matches
string-utilities.h
Go to the documentation of this file.
1// License: Apache 2.0. See LICENSE file in root directory.
2// Copyright(c) 2021 Intel Corporation. All Rights Reserved.
3
4#pragma once
5
6#include <algorithm>
7#include <string>
8#include <sstream>
9#include <cmath> // std::isfinite
10
11namespace utilities {
12namespace string {
13 inline std::string hexify(unsigned char n)
14 {
15 std::string res;
16
17 do
18 {
19 res += "0123456789ABCDEF"[n % 16];
20 n >>= 4;
21 } while (n);
22
23 std::reverse(res.begin(), res.end());
24
25 if (res.size() == 1)
26 {
27 res.insert(0, "0");
28 }
29
30 return res;
31 }
32
33 inline std::string to_lower(std::string x)
34 {
35 std::transform(x.begin(), x.end(), x.begin(), tolower);
36 return x;
37 }
38
39 inline std::string to_upper(std::string x)
40 {
41 std::transform(x.begin(), x.end(), x.begin(), toupper);
42 return x;
43 }
44
45 inline bool string_to_bool(const std::string& x)
46 {
47 return (to_lower(x) == "true");
48 }
49
50 // Pass by-value to perform in-place modifications
51 inline unsigned int ascii_hex_string_to_uint(std::string str)
52 {
53 std::string delimiter = "x";
54
55 auto pos = str.find(delimiter);
56 str.erase(0, pos + delimiter.length());
57
58 std::stringstream ss;
59 unsigned int value;
60 ss << str;
61 ss >> std::hex >> value;
62
63 return value;
64 }
65
66 template < typename T,
67 typename std::enable_if< std::is_arithmetic< T >::value >::
68 type* = nullptr > // check if T is arithmetic during compile
69 inline bool string_to_value(const std::string& str, T& result)
70 {
71 // Converts string to value via a given argument with desire type
72 // Input:
73 // - string of a number
74 // - argument with T type
75 // returns:true if conversion succeeded
76 // NOTE for unsigned types:
77 // if T is unsinged and the given string represents a negative number (meaning it starts with
78 // '-'), then string_to_value returns false (rather than send the string to the std conversion
79 // function). The reason for that is, stoul will return return a number in that case rather than
80 // throwing an exception.
81
82 try
83 {
84 size_t last_char_idx = 0;
85 if (std::is_integral< T >::value)
86 {
87 if (std::is_same< T, unsigned long >::value)
88 {
89 if (str[0] == '-')
90 return false; // for explanation see 'NOTE for unsigned types'
91 result = static_cast<T>(std::stoul(str, &last_char_idx));
92 }
93 else if (std::is_same< T, unsigned long long >::value)
94 {
95 if (str[0] == '-')
96 return false;
97 result = static_cast<T>(std::stoull(str, &last_char_idx));
98 }
99 else if (std::is_same< T, long >::value)
100 {
101 result = static_cast<T>(std::stol(str, &last_char_idx));
102 }
103 else if (std::is_same< T, long long >::value)
104 {
105 result = static_cast<T>(std::stoll(str, &last_char_idx));
106 }
107 else if (std::is_same< T, int >::value)
108 {
109 result = static_cast<T>(std::stoi(str, &last_char_idx));
110 }
111 else if (std::is_same< T, short >::value)
112 { // no dedicated function fot short in std
113
114 int check_value = std::stoi(str, &last_char_idx);
115
116 if (check_value > std::numeric_limits< short >::max()
117 || check_value < std::numeric_limits< short >::min())
118 throw std::out_of_range("short");
119
120 result = static_cast<T>(check_value);
121 }
122 else if (std::is_same< T, unsigned int >::value)
123 { // no dedicated function in std - unsgined corresponds to 16 bit, and unsigned long
124 // corresponds to 32 bit
125 if (str[0] == '-')
126 return false;
127
128 unsigned long check_value = std::stoul(str, &last_char_idx);
129
130 if (check_value > std::numeric_limits< unsigned int >::max())
131 throw std::out_of_range("unsigned int");
132
133 result = static_cast<T>(check_value);
134 }
135 else if (std::is_same< T, unsigned short >::value
136 || std::is_same< T, unsigned short >::value)
137 { // no dedicated function in std - unsgined corresponds to 16 bit, and unsigned long
138 // corresponds to 32 bit
139 if (str[0] == '-')
140 return false;
141
142 unsigned long check_value = std::stoul(str, &last_char_idx);
143
144 if (check_value > std::numeric_limits< unsigned short >::max())
145 throw std::out_of_range("unsigned short");
146
147 result = static_cast<T>(check_value);
148 }
149 else
150 {
151 return false;
152 }
153 }
154 else if (std::is_floating_point< T >::value)
155 {
156 if (std::is_same< T, float >::value)
157 {
158 result = static_cast<T>(std::stof(str, &last_char_idx));
159 if (!std::isfinite((float)result))
160 return false;
161 }
162 else if (std::is_same< T, double >::value)
163 {
164 result = static_cast<T>(std::stod(str, &last_char_idx));
165 if (!std::isfinite((double)result))
166 return false;
167 }
168 else if (std::is_same< T, long double >::value)
169 {
170 result = static_cast<T>(std::stold(str, &last_char_idx));
171 if (!std::isfinite((long double)result))
172 return false;
173 }
174 else
175 {
176 return false;
177 }
178 }
179 return str.size()
180 == last_char_idx; // all the chars in the string are converted to the value (thus there
181 // are no other chars other than numbers)
182 }
183 catch (const std::invalid_argument&)
184 {
185 return false;
186 }
187 catch (const std::out_of_range&)
188 {
189 return false;
190 }
191 }
192
193} // namespace string
194} // namespace utilities
unsigned int ascii_hex_string_to_uint(std::string str)
Definition: string-utilities.h:51
std::string hexify(unsigned char n)
Definition: string-utilities.h:13
std::string to_upper(std::string x)
Definition: string-utilities.h:39
bool string_to_value(const std::string &str, T &result)
Definition: string-utilities.h:69
std::string to_lower(std::string x)
Definition: string-utilities.h:33
bool string_to_bool(const std::string &x)
Definition: string-utilities.h:45
Definition: stabilized-value.h:12