2
0

blitz_string_ex.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. Copyright 2024 Bruce A Henderson
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. */
  12. #include "fast_float/fast_float.h"
  13. #include "blitz_debug.h"
  14. #ifdef _WIN32
  15. #if defined(_WIN64)
  16. typedef __int64 LONG_PTR;
  17. typedef unsigned __int64 UINT_PTR;
  18. #else
  19. typedef long LONG_PTR;
  20. typedef unsigned int UINT_PTR;
  21. #endif
  22. typedef UINT_PTR WPARAM;
  23. typedef LONG_PTR LPARAM;
  24. #endif
  25. #include "blitz_string.h"
  26. // extracts a double from a string, from the range startPos to endPos
  27. // endPos of -1 means the end of the string
  28. // returns 0 if the string is not a valid double, or the position of the first character after the double otherwise
  29. int bbStringToDoubleEx( BBString *str, double * val, int startPos, int endPos, BBULONG format, BBString* sep ) {
  30. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  31. return 0;
  32. }
  33. if (endPos == -1) {
  34. endPos = str->length;
  35. }
  36. const char16_t * start = (char16_t*)str->buf;
  37. const char16_t * end = start + str->length;
  38. const char16_t * p = start + startPos;
  39. const char16_t * e = start + endPos;
  40. const char16_t sepChar = sep->length > 0 ? sep->buf[0] : '.';
  41. double result;
  42. if ( sepChar != 0 && sepChar != '.' ) {
  43. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), sepChar};
  44. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars_advanced(p, e, result, options);
  45. if (res.ptr != nullptr) {
  46. *val = result;
  47. return res.ptr - start;
  48. }
  49. }
  50. else {
  51. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars(p, e, result, static_cast<fast_float::chars_format>(format));
  52. if (res.ptr != nullptr) {
  53. *val = result;
  54. return res.ptr - start;
  55. }
  56. }
  57. return 0;
  58. }
  59. // extracts a float from a string, from the range startPos to endPos
  60. // endPos of -1 means the end of the string
  61. // returns 0 if the string is not a valid float, or the position of the first character after the float otherwise
  62. int bbStringToFloatEx( BBString *str, float * val, int startPos, int endPos, BBULONG format, BBString* sep ) {
  63. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  64. return 0;
  65. }
  66. if (endPos == -1) {
  67. endPos = str->length;
  68. }
  69. const char16_t * start = (char16_t*)str->buf;
  70. const char16_t * end = start + str->length;
  71. const char16_t * p = start + startPos;
  72. const char16_t * e = start + endPos;
  73. const char16_t sepChar = sep->length > 0 ? sep->buf[0] : '.';
  74. float result;
  75. if ( sepChar != 0 && sepChar != '.' ) {
  76. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), sepChar};
  77. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars_advanced(p, e, result, options);
  78. if (res.ptr != nullptr) {
  79. *val = result;
  80. return res.ptr - start;
  81. }
  82. }
  83. else {
  84. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars(p, e, result, static_cast<fast_float::chars_format>(format));
  85. if (res.ptr != nullptr) {
  86. *val = result;
  87. return res.ptr - start;
  88. }
  89. }
  90. return 0;
  91. }
  92. // extracts a int from a string, from the range startPos to endPos
  93. // endPos of -1 means the end of the string
  94. // returns 0 if the string is not a valid int, or the position of the first character after the int otherwise
  95. int bbStringToIntEx( BBString *str, int * val, int startPos, int endPos, BBULONG format, int base ) {
  96. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  97. return 0;
  98. }
  99. if (endPos == -1) {
  100. endPos = str->length;
  101. }
  102. const char16_t * start = (char16_t*)str->buf;
  103. const char16_t * end = start + str->length;
  104. const char16_t * p = start + startPos;
  105. const char16_t * e = start + endPos;
  106. int result;
  107. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  108. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars_advanced(p, e, result, options);
  109. if (res.ptr != nullptr) {
  110. *val = result;
  111. return res.ptr - start;
  112. }
  113. return 0;
  114. }
  115. // extracts a UInt from a string, from the range startPos to endPos
  116. // endPos of -1 means the end of the string
  117. // returns 0 if the string is not a valid UInt, or the position of the first character after the UInt otherwise
  118. int bbStringToUIntEx( BBString *str, unsigned int * val, int startPos, int endPos, BBULONG format, int base ) {
  119. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  120. return 0;
  121. }
  122. if (endPos == -1) {
  123. endPos = str->length;
  124. }
  125. const char16_t * start = (char16_t*)str->buf;
  126. const char16_t * end = start + str->length;
  127. const char16_t * p = start + startPos;
  128. const char16_t * e = start + endPos;
  129. unsigned int result;
  130. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  131. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars_advanced(p, e, result, options);
  132. if (res.ptr != nullptr) {
  133. *val = result;
  134. return res.ptr - start;
  135. }
  136. return 0;
  137. }
  138. // extracts a Long from a string, from the range startPos to endPos
  139. // endPos of -1 means the end of the string
  140. // returns 0 if the string is not a valid Long, or the position of the first character after the Long otherwise
  141. int bbStringToLongEx( BBString *str, BBInt64 * val, int startPos, int endPos, BBULONG format, int base ) {
  142. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  143. return 0;
  144. }
  145. if (endPos == -1) {
  146. endPos = str->length;
  147. }
  148. const char16_t * start = (char16_t*)str->buf;
  149. const char16_t * end = start + str->length;
  150. const char16_t * p = start + startPos;
  151. const char16_t * e = start + endPos;
  152. BBInt64 result;
  153. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  154. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars_advanced(p, e, result, options);
  155. if (res.ptr != nullptr) {
  156. *val = result;
  157. return res.ptr - start;
  158. }
  159. return 0;
  160. }
  161. // extracts a ULong from a string, from the range startPos to endPos
  162. // endPos of -1 means the end of the string
  163. // returns 0 if the string is not a valid ULong, or the position of the first character after the ULong otherwise
  164. int bbStringToULongEx( BBString *str, BBUInt64 * val, int startPos, int endPos, BBULONG format, int base ) {
  165. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  166. return 0;
  167. }
  168. if (endPos == -1) {
  169. endPos = str->length;
  170. }
  171. const char16_t * start = (char16_t*)str->buf;
  172. const char16_t * end = start + str->length;
  173. const char16_t * p = start + startPos;
  174. const char16_t * e = start + endPos;
  175. BBUInt64 result;
  176. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  177. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars_advanced(p, e, result, options);
  178. if (res.ptr != nullptr) {
  179. *val = result;
  180. return res.ptr - start;
  181. }
  182. return 0;
  183. }
  184. // extracts a Size_T from a string, from the range startPos to endPos
  185. // endPos of -1 means the end of the string
  186. // returns 0 if the string is not a valid Size_T, or the position of the first character after the Size_T otherwise
  187. int bbStringToSizeTEx( BBString *str, BBSIZET * val, int startPos, int endPos, BBULONG format, int base ) {
  188. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  189. return 0;
  190. }
  191. if (endPos == -1) {
  192. endPos = str->length;
  193. }
  194. const char16_t * start = (char16_t*)str->buf;
  195. const char16_t * end = start + str->length;
  196. const char16_t * p = start + startPos;
  197. const char16_t * e = start + endPos;
  198. BBSIZET result;
  199. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  200. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars_advanced(p, e, result, options);
  201. if (res.ptr != nullptr) {
  202. *val = result;
  203. return res.ptr - start;
  204. }
  205. return 0;
  206. }
  207. // extracts a LongInt from a string, from the range startPos to endPos
  208. // endPos of -1 means the end of the string
  209. // returns -1 if the string is not a valid LongInt, or the position of the first character after the LongInt otherwise
  210. int bbStringToLongIntEx( BBString *str, BBLONGINT * val, int startPos, int endPos, BBULONG format, int base ) {
  211. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  212. return 0;
  213. }
  214. if (endPos == -1) {
  215. endPos = str->length;
  216. }
  217. const char16_t * start = (char16_t*)str->buf;
  218. const char16_t * end = start + str->length;
  219. const char16_t * p = start + startPos;
  220. const char16_t * e = start + endPos;
  221. BBLONGINT result;
  222. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  223. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars_advanced(p, e, result, options);
  224. if (res.ptr != nullptr) {
  225. *val = result;
  226. return res.ptr - start;
  227. }
  228. return 0;
  229. }
  230. // extracts a ULongInt from a string, from the range startPos to endPos
  231. // endPos of -1 means the end of the string
  232. // returns 0 if the string is not a valid ULongInt, or the position of the first character after the ULongInt otherwise
  233. int bbStringToULongIntEx( BBString *str, BBULONGINT * val, int startPos, int endPos, BBULONG format, int base ) {
  234. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  235. return 0;
  236. }
  237. if (endPos == -1) {
  238. endPos = str->length;
  239. }
  240. const char16_t * start = (char16_t*)str->buf;
  241. const char16_t * end = start + str->length;
  242. const char16_t * p = start + startPos;
  243. const char16_t * e = start + endPos;
  244. BBULONGINT result;
  245. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  246. fast_float::from_chars_result_t<char16_t> res = fast_float::from_chars_advanced(p, e, result, options);
  247. if (res.ptr != nullptr) {
  248. *val = result;
  249. return res.ptr - start;
  250. }
  251. return 0;
  252. }