blitz_string_ex.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include "blitz_string.h"
  15. // extracts a double from a string, from the range startPos to endPos
  16. // endPos of -1 means the end of the string
  17. // returns 0 if the string is not a valid double, or the position of the first character after the double otherwise
  18. int bbStringToDoubleEx( BBString *str, double * val, int startPos, int endPos, BBULONG format, BBString* sep ) {
  19. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  20. return 0;
  21. }
  22. if (endPos == -1) {
  23. endPos = str->length;
  24. }
  25. const char16_t * start = (char16_t*)str->buf;
  26. const char16_t * end = start + str->length;
  27. const char16_t * p = start + startPos;
  28. const char16_t * e = start + endPos;
  29. const char16_t sepChar = sep->length > 0 ? sep->buf[0] : '.';
  30. double result;
  31. if ( sepChar != 0 && sepChar != '.' ) {
  32. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), sepChar};
  33. auto [ptr, ec] = fast_float::from_chars_advanced(p, e, result, options);
  34. if (ptr != nullptr) {
  35. *val = result;
  36. return ptr - start;
  37. }
  38. }
  39. else {
  40. auto [ptr, ec] = fast_float::from_chars(p, e, result, static_cast<fast_float::chars_format>(format));
  41. if (ptr != nullptr) {
  42. *val = result;
  43. return ptr - start;
  44. }
  45. }
  46. return 0;
  47. }
  48. // extracts a float from a string, from the range startPos to endPos
  49. // endPos of -1 means the end of the string
  50. // returns 0 if the string is not a valid float, or the position of the first character after the float otherwise
  51. int bbStringToFloatEx( BBString *str, float * val, int startPos, int endPos, BBULONG format, BBString* sep ) {
  52. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  53. return 0;
  54. }
  55. if (endPos == -1) {
  56. endPos = str->length;
  57. }
  58. const char16_t * start = (char16_t*)str->buf;
  59. const char16_t * end = start + str->length;
  60. const char16_t * p = start + startPos;
  61. const char16_t * e = start + endPos;
  62. const char16_t sepChar = sep->length > 0 ? sep->buf[0] : '.';
  63. float result;
  64. if ( sepChar != 0 && sepChar != '.' ) {
  65. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), sepChar};
  66. auto [ptr, ec] = fast_float::from_chars_advanced(p, e, result, options);
  67. if (ptr != nullptr) {
  68. *val = result;
  69. return ptr - start;
  70. }
  71. }
  72. else {
  73. auto [ptr, ec] = fast_float::from_chars(p, e, result, static_cast<fast_float::chars_format>(format));
  74. if (ptr != nullptr) {
  75. *val = result;
  76. return ptr - start;
  77. }
  78. }
  79. return 0;
  80. }
  81. // extracts a int from a string, from the range startPos to endPos
  82. // endPos of -1 means the end of the string
  83. // returns 0 if the string is not a valid int, or the position of the first character after the int otherwise
  84. int bbStringToIntEx( BBString *str, int * val, int startPos, int endPos, BBULONG format, int base ) {
  85. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  86. return 0;
  87. }
  88. if (endPos == -1) {
  89. endPos = str->length;
  90. }
  91. const char16_t * start = (char16_t*)str->buf;
  92. const char16_t * end = start + str->length;
  93. const char16_t * p = start + startPos;
  94. const char16_t * e = start + endPos;
  95. int result;
  96. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  97. auto [ptr, ec] = fast_float::from_chars_advanced(p, e, result, options);
  98. if (ptr != nullptr) {
  99. *val = result;
  100. return ptr - start;
  101. }
  102. return 0;
  103. }
  104. // extracts a UInt from a string, from the range startPos to endPos
  105. // endPos of -1 means the end of the string
  106. // returns 0 if the string is not a valid UInt, or the position of the first character after the UInt otherwise
  107. int bbStringToUIntEx( BBString *str, unsigned int * val, int startPos, int endPos, BBULONG format, int base ) {
  108. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  109. return 0;
  110. }
  111. if (endPos == -1) {
  112. endPos = str->length;
  113. }
  114. const char16_t * start = (char16_t*)str->buf;
  115. const char16_t * end = start + str->length;
  116. const char16_t * p = start + startPos;
  117. const char16_t * e = start + endPos;
  118. unsigned int result;
  119. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  120. auto [ptr, ec] = fast_float::from_chars_advanced(p, e, result, options);
  121. if (ptr != nullptr) {
  122. *val = result;
  123. return ptr - start;
  124. }
  125. return 0;
  126. }
  127. // extracts a Long from a string, from the range startPos to endPos
  128. // endPos of -1 means the end of the string
  129. // returns 0 if the string is not a valid Long, or the position of the first character after the Long otherwise
  130. int bbStringToLongEx( BBString *str, BBInt64 * val, int startPos, int endPos, BBULONG format, int base ) {
  131. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  132. return 0;
  133. }
  134. if (endPos == -1) {
  135. endPos = str->length;
  136. }
  137. const char16_t * start = (char16_t*)str->buf;
  138. const char16_t * end = start + str->length;
  139. const char16_t * p = start + startPos;
  140. const char16_t * e = start + endPos;
  141. BBInt64 result;
  142. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  143. auto [ptr, ec] = fast_float::from_chars_advanced(p, e, result, options);
  144. if (ptr != nullptr) {
  145. *val = result;
  146. return ptr - start;
  147. }
  148. return 0;
  149. }
  150. // extracts a ULong from a string, from the range startPos to endPos
  151. // endPos of -1 means the end of the string
  152. // returns 0 if the string is not a valid ULong, or the position of the first character after the ULong otherwise
  153. int bbStringToULongEx( BBString *str, BBUInt64 * val, int startPos, int endPos, BBULONG format, int base ) {
  154. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  155. return 0;
  156. }
  157. if (endPos == -1) {
  158. endPos = str->length;
  159. }
  160. const char16_t * start = (char16_t*)str->buf;
  161. const char16_t * end = start + str->length;
  162. const char16_t * p = start + startPos;
  163. const char16_t * e = start + endPos;
  164. BBUInt64 result;
  165. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  166. auto [ptr, ec] = fast_float::from_chars_advanced(p, e, result, options);
  167. if (ptr != nullptr) {
  168. *val = result;
  169. return ptr - start;
  170. }
  171. return 0;
  172. }
  173. // extracts a Size_T from a string, from the range startPos to endPos
  174. // endPos of -1 means the end of the string
  175. // returns 0 if the string is not a valid Size_T, or the position of the first character after the Size_T otherwise
  176. int bbStringToSizeTEx( BBString *str, BBSIZET * val, int startPos, int endPos, BBULONG format, int base ) {
  177. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  178. return 0;
  179. }
  180. if (endPos == -1) {
  181. endPos = str->length;
  182. }
  183. const char16_t * start = (char16_t*)str->buf;
  184. const char16_t * end = start + str->length;
  185. const char16_t * p = start + startPos;
  186. const char16_t * e = start + endPos;
  187. BBSIZET result;
  188. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  189. auto [ptr, ec] = fast_float::from_chars_advanced(p, e, result, options);
  190. if (ptr != nullptr) {
  191. *val = result;
  192. return ptr - start;
  193. }
  194. return 0;
  195. }
  196. // extracts a LongInt from a string, from the range startPos to endPos
  197. // endPos of -1 means the end of the string
  198. // returns -1 if the string is not a valid LongInt, or the position of the first character after the LongInt otherwise
  199. int bbStringToLongIntEx( BBString *str, BBLONGINT * val, int startPos, int endPos, BBULONG format, int base ) {
  200. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  201. return 0;
  202. }
  203. if (endPos == -1) {
  204. endPos = str->length;
  205. }
  206. const char16_t * start = (char16_t*)str->buf;
  207. const char16_t * end = start + str->length;
  208. const char16_t * p = start + startPos;
  209. const char16_t * e = start + endPos;
  210. BBLONGINT result;
  211. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  212. auto [ptr, ec] = fast_float::from_chars_advanced(p, e, result, options);
  213. if (ptr != nullptr) {
  214. *val = result;
  215. return ptr - start;
  216. }
  217. return 0;
  218. }
  219. // extracts a ULongInt from a string, from the range startPos to endPos
  220. // endPos of -1 means the end of the string
  221. // returns 0 if the string is not a valid ULongInt, or the position of the first character after the ULongInt otherwise
  222. int bbStringToULongIntEx( BBString *str, BBULONGINT * val, int startPos, int endPos, BBULONG format, int base ) {
  223. if ( startPos < 0 || startPos >= str->length || endPos < -1 || endPos > str->length ) {
  224. return 0;
  225. }
  226. if (endPos == -1) {
  227. endPos = str->length;
  228. }
  229. const char16_t * start = (char16_t*)str->buf;
  230. const char16_t * end = start + str->length;
  231. const char16_t * p = start + startPos;
  232. const char16_t * e = start + endPos;
  233. BBULONGINT result;
  234. fast_float::parse_options_t<char16_t> options{static_cast<fast_float::chars_format>(format), '.', base};
  235. auto [ptr, ec] = fast_float::from_chars_advanced(p, e, result, options);
  236. if (ptr != nullptr) {
  237. *val = result;
  238. return ptr - start;
  239. }
  240. return 0;
  241. }