comparison Framework/Toolbox/GenericToolbox.h @ 1306:fef1ec42a7db broker

Some docs + headers added to CMake for easier VS browsing + tiny predecl
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 04 Mar 2020 10:07:14 +0100
parents 7ec8fea061b9
children fd616c4a5904
comparison
equal deleted inserted replaced
1305:a5326ce4f24b 1306:fef1ec42a7db
27 27
28 namespace OrthancStone 28 namespace OrthancStone
29 { 29 {
30 namespace GenericToolbox 30 namespace GenericToolbox
31 { 31 {
32 /**
33 Fast floating point string validation.
34 No trimming applied, so the input must match regex
35 /^[-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/
36 The following are allowed as edge cases: "" and "-"
37 */
32 inline bool LegitDoubleString(const char* text) 38 inline bool LegitDoubleString(const char* text)
33 { 39 {
34 const char* p = text; 40 const char* p = text;
35 if(*p == '-') 41 if(*p == '-')
36 p++; 42 p++;
68 } 74 }
69 } 75 }
70 return true; 76 return true;
71 } 77 }
72 78
79 /**
80 Fast integer string validation.
81 No trimming applied, so the input must match regex /^-?[0-9]*$/
82 The following are allowed as edge cases: "" and "-"
83 */
73 inline bool LegitIntegerString(const char* text) 84 inline bool LegitIntegerString(const char* text)
74 { 85 {
75 const char* p = text; 86 const char* p = text;
76 if (*p == '-') 87 if (*p == '-')
77 p++; 88 p++;
84 } 95 }
85 return true; 96 return true;
86 } 97 }
87 98
88 /* 99 /*
100 Fast string --> double conversion.
101 Must pass the LegitDoubleString test
102
89 String to doubles with at most 18 digits 103 String to doubles with at most 18 digits
90 */ 104 */
91 inline bool StringToDouble(double& r, const char* text) 105 inline bool StringToDouble(double& r, const char* text)
92 { 106 {
93 if(!LegitDoubleString(text)) 107 if(!LegitDoubleString(text))
208 inline bool StringToDouble(double& r, const std::string& text) 222 inline bool StringToDouble(double& r, const std::string& text)
209 { 223 {
210 return StringToDouble(r, text.c_str()); 224 return StringToDouble(r, text.c_str());
211 } 225 }
212 226
227 /**
228 Fast string to integer conversion. Leading zeroes and minus are accepted,
229 but a leading + sign is NOT.
230 Must pass the LegitIntegerString function test.
231 In addition, an empty string (or lone minus sign) yields 0.
232 */
233
213 template<typename T> 234 template<typename T>
214 inline bool StringToInteger(T& r, const char* text) 235 inline bool StringToInteger(T& r, const char* text)
215 { 236 {
216 if (!LegitDoubleString(text)) 237 if (!LegitIntegerString(text))
217 return false; 238 return false;
218 239
219 r = 0; 240 r = 0;
220 T neg = 1; 241 T neg = 1;
221 const char* p = text; 242 const char* p = text;
242 { 263 {
243 return StringToInteger<T>(r, text.c_str()); 264 return StringToInteger<T>(r, text.c_str());
244 } 265 }
245 266
246 /** 267 /**
247 "rgb(12,23,255)" --> red, green, blue and returns true 268 if input is "rgb(12,23,255)" --> function fills `red`, `green` and `blue` and returns true
248 "everything else" --> returns false (other values left untouched) 269 else ("everything else") --> function returns false and leaves all values untouched
249 */ 270 */
250 bool GetRgbValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, const char* text); 271 bool GetRgbValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, const char* text);
251 272
252 /** 273 /**
253 See other overload 274 See main overload
254 */ 275 */
255 inline bool GetRgbValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, const std::string& text) 276 inline bool GetRgbValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, const std::string& text)
256 { 277 {
257 return GetRgbValuesFromString(red, green, blue, text.c_str()); 278 return GetRgbValuesFromString(red, green, blue, text.c_str());
258 } 279 }
259 280
281 /**
282 Same as GetRgbValuesFromString
283 */
260 bool GetRgbaValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha, const char* text); 284 bool GetRgbaValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha, const char* text);
261 285
286 /**
287 Same as GetRgbValuesFromString
288 */
262 inline bool GetRgbaValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha, const std::string& text) 289 inline bool GetRgbaValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha, const std::string& text)
263 { 290 {
264 return GetRgbaValuesFromString(red, green, blue, alpha, text.c_str()); 291 return GetRgbaValuesFromString(red, green, blue, alpha, text.c_str());
265 } 292 }
266 } 293 }