NumberParser.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // NumberParser.h
  3. //
  4. // Library: Foundation
  5. // Package: Core
  6. // Module: NumberParser
  7. //
  8. // Definition of the NumberParser class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_NumberParser_INCLUDED
  16. #define Foundation_NumberParser_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include <string>
  19. #undef min
  20. #undef max
  21. #include <limits>
  22. namespace Poco {
  23. class Foundation_API NumberParser
  24. /// The NumberParser class provides static methods
  25. /// for parsing numbers out of strings.
  26. ///
  27. /// Note that leading or trailing whitespace is not allowed
  28. /// in the string. Poco::trim() or Poco::trimInPlace()
  29. /// can be used to remove leading or trailing whitespace.
  30. {
  31. public:
  32. static const unsigned short NUM_BASE_OCT = 010;
  33. static const unsigned short NUM_BASE_DEC = 10;
  34. static const unsigned short NUM_BASE_HEX = 0x10;
  35. static int parse(const std::string& s, char thousandSeparator = ',');
  36. /// Parses an integer value in decimal notation from the given string.
  37. /// Throws a SyntaxException if the string does not hold a number in decimal notation.
  38. static bool tryParse(const std::string& s, int& value, char thousandSeparator = ',');
  39. /// Parses an integer value in decimal notation from the given string.
  40. /// Returns true if a valid integer has been found, false otherwise.
  41. /// If parsing was not successful, value is undefined.
  42. static unsigned parseUnsigned(const std::string& s, char thousandSeparator = ',');
  43. /// Parses an unsigned integer value in decimal notation from the given string.
  44. /// Throws a SyntaxException if the string does not hold a number in decimal notation.
  45. static bool tryParseUnsigned(const std::string& s, unsigned& value, char thousandSeparator = ',');
  46. /// Parses an unsigned integer value in decimal notation from the given string.
  47. /// Returns true if a valid integer has been found, false otherwise.
  48. /// If parsing was not successful, value is undefined.
  49. static unsigned parseHex(const std::string& s);
  50. /// Parses an integer value in hexadecimal notation from the given string.
  51. /// Throws a SyntaxException if the string does not hold a number in
  52. /// hexadecimal notation.
  53. static bool tryParseHex(const std::string& s, unsigned& value);
  54. /// Parses an unsigned integer value in hexadecimal notation from the given string.
  55. /// Returns true if a valid integer has been found, false otherwise.
  56. /// If parsing was not successful, value is undefined.
  57. static unsigned parseOct(const std::string& s);
  58. /// Parses an integer value in octal notation from the given string.
  59. /// Throws a SyntaxException if the string does not hold a number in
  60. /// hexadecimal notation.
  61. static bool tryParseOct(const std::string& s, unsigned& value);
  62. /// Parses an unsigned integer value in octal notation from the given string.
  63. /// Returns true if a valid integer has been found, false otherwise.
  64. /// If parsing was not successful, value is undefined.
  65. #if defined(POCO_HAVE_INT64)
  66. static Int64 parse64(const std::string& s, char thousandSeparator = ',');
  67. /// Parses a 64-bit integer value in decimal notation from the given string.
  68. /// Throws a SyntaxException if the string does not hold a number in decimal notation.
  69. static bool tryParse64(const std::string& s, Int64& value, char thousandSeparator = ',');
  70. /// Parses a 64-bit integer value in decimal notation from the given string.
  71. /// Returns true if a valid integer has been found, false otherwise.
  72. /// If parsing was not successful, value is undefined.
  73. static UInt64 parseUnsigned64(const std::string& s, char thousandSeparator = ',');
  74. /// Parses an unsigned 64-bit integer value in decimal notation from the given string.
  75. /// Throws a SyntaxException if the string does not hold a number in decimal notation.
  76. static bool tryParseUnsigned64(const std::string& s, UInt64& value, char thousandSeparator = ',');
  77. /// Parses an unsigned 64-bit integer value in decimal notation from the given string.
  78. /// Returns true if a valid integer has been found, false otherwise.
  79. /// If parsing was not successful, value is undefined.
  80. static UInt64 parseHex64(const std::string& s);
  81. /// Parses a 64 bit-integer value in hexadecimal notation from the given string.
  82. /// Throws a SyntaxException if the string does not hold a number in hexadecimal notation.
  83. static bool tryParseHex64(const std::string& s, UInt64& value);
  84. /// Parses an unsigned 64-bit integer value in hexadecimal notation from the given string.
  85. /// Returns true if a valid integer has been found, false otherwise.
  86. /// If parsing was not successful, value is undefined.
  87. static UInt64 parseOct64(const std::string& s);
  88. /// Parses a 64 bit-integer value in octal notation from the given string.
  89. /// Throws a SyntaxException if the string does not hold a number in hexadecimal notation.
  90. static bool tryParseOct64(const std::string& s, UInt64& value);
  91. /// Parses an unsigned 64-bit integer value in octal notation from the given string.
  92. /// Returns true if a valid integer has been found, false otherwise.
  93. /// If parsing was not successful, value is undefined.
  94. #endif // defined(POCO_HAVE_INT64)
  95. static double parseFloat(const std::string& s, char decimalSeparator = '.', char thousandSeparator = ',');
  96. /// Parses a double value in decimal floating point notation
  97. /// from the given string.
  98. /// Throws a SyntaxException if the string does not hold a floating-point
  99. /// number in decimal notation.
  100. static bool tryParseFloat(const std::string& s, double& value, char decimalSeparator = '.', char thousandSeparator = ',');
  101. /// Parses a double value in decimal floating point notation
  102. /// from the given string.
  103. /// Returns true if a valid floating point number has been found,
  104. /// false otherwise.
  105. /// If parsing was not successful, value is undefined.
  106. static bool parseBool(const std::string& s);
  107. /// Parses a bool value in decimal or string notation
  108. /// from the given string.
  109. /// Valid forms are: "0", "1", "true", "on", false", "yes", "no", "off".
  110. /// String forms are NOT case sensitive.
  111. /// Throws a SyntaxException if the string does not hold a valid bool number
  112. static bool tryParseBool(const std::string& s, bool& value);
  113. /// Parses a bool value in decimal or string notation
  114. /// from the given string.
  115. /// Valid forms are: "0", "1", "true", "on", false", "yes", "no", "off".
  116. /// String forms are NOT case sensitive.
  117. /// Returns true if a valid bool number has been found,
  118. /// false otherwise.
  119. /// If parsing was not successful, value is undefined.
  120. };
  121. } // namespace Poco
  122. #endif // Foundation_NumberParser_INCLUDED