NumberParser.h 6.6 KB

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