melder_atof.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* melder_atof.cpp
  2. *
  3. * Copyright (C) 2003-2008,2011,2015-2018 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "melder.h"
  19. /**
  20. Assume that the next thing that follows is a numeric string,
  21. and find the end of it.
  22. Return null on error.
  23. */
  24. template <typename T>
  25. static const T *findEndOfNumericString (const T *string) noexcept {
  26. const T *p = & string [0];
  27. /*
  28. Leading white space is OK.
  29. */
  30. while (Melder_isAsciiHorizontalOrVerticalSpace (*p))
  31. p ++;
  32. /*
  33. Next we accept an optional leading plus or minus.
  34. */
  35. if (*p == '+' || *p == '-')
  36. p ++;
  37. /*
  38. The next character has to be a decimal digit.
  39. So we don't allow things like ".5".
  40. */
  41. if (! Melder_isAsciiDecimalNumber (*p))
  42. return nullptr; // string is not numeric
  43. p ++;
  44. /*
  45. Then we accept any number of decimal digits.
  46. */
  47. while (Melder_isAsciiDecimalNumber (*p)) p ++;
  48. /*
  49. Next we accept an optional decimal point.
  50. */
  51. if (*p == '.') {
  52. p ++;
  53. /*
  54. We accept any number of (even zero) decimal digits after the decimal point.
  55. */
  56. while (Melder_isAsciiDecimalNumber (*p)) p ++;
  57. }
  58. // Next we accept an optional exponential E or e.
  59. if (*p == 'e' || *p == 'E') {
  60. p ++;
  61. /*
  62. In the exponent we accept an optional leading plus or minus.
  63. */
  64. if (*p == '+' || *p == '-')
  65. p ++;
  66. /*
  67. The exponent shall contain at least one decimal digit.
  68. So we don't allow things like "+2.1E".
  69. */
  70. if (! Melder_isAsciiDecimalNumber (*p))
  71. return nullptr; // string is not numeric
  72. p ++; // skip first decimal digit
  73. /*
  74. Then we accept any number of decimal digits.
  75. */
  76. while (Melder_isAsciiDecimalNumber (*p)) p ++;
  77. }
  78. /*
  79. Next we accept an optional percent sign.
  80. */
  81. if (*p == '%')
  82. p ++;
  83. /*
  84. We have found the end of the numeric string.
  85. */
  86. return p;
  87. }
  88. bool Melder_isStringNumeric (conststring32 string) noexcept {
  89. if (! string)
  90. return false;
  91. const char32 *p = findEndOfNumericString (string);
  92. if (! p)
  93. return false;
  94. /*
  95. After the numeric string, we accept only white space.
  96. */
  97. while (Melder_isAsciiHorizontalOrVerticalSpace (*p))
  98. p ++; // not Unicode-savvy
  99. return *p == U'\0';
  100. }
  101. double Melder_a8tof (conststring8 string) noexcept {
  102. if (! string)
  103. return undefined;
  104. const char *p = findEndOfNumericString (string);
  105. if (! p)
  106. return undefined;
  107. Melder_assert (p - & string [0] > 0);
  108. return p [-1] == '%' ? 0.01 * strtod (string, nullptr) : strtod (string, nullptr);
  109. }
  110. double Melder_atof (conststring32 string) noexcept {
  111. return Melder_a8tof (Melder_peek32to8 (string));
  112. }
  113. int64 Melder_atoi (conststring32 string) noexcept {
  114. return strtoll (Melder_peek32to8 (string), nullptr, 10);
  115. }
  116. /* End of file melder_atof.cpp */