| 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 |  | 
|---|
| 16 |  | 
|---|
| 17 | #ifndef Foundation_NumberParser_INCLUDED | 
|---|
| 18 | #define Foundation_NumberParser_INCLUDED | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | #include "Poco/Foundation.h" | 
|---|
| 22 | #include <string> | 
|---|
| 23 | #undef min | 
|---|
| 24 | #undef max | 
|---|
| 25 | #include <limits> | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | namespace Poco { | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | class Foundation_API NumberParser | 
|---|
| 32 | /// The NumberParser class provides static methods | 
|---|
| 33 | /// for parsing numbers out of strings. | 
|---|
| 34 | /// | 
|---|
| 35 | /// Note that leading or trailing whitespace is not allowed | 
|---|
| 36 | /// in the string. Poco::trim() or Poco::trimInPlace() | 
|---|
| 37 | /// can be used to remove leading or trailing whitespace. | 
|---|
| 38 | { | 
|---|
| 39 | public: | 
|---|
| 40 | static const unsigned short NUM_BASE_OCT = 010; | 
|---|
| 41 | static const unsigned short NUM_BASE_DEC = 10; | 
|---|
| 42 | static const unsigned short NUM_BASE_HEX = 0x10; | 
|---|
| 43 |  | 
|---|
| 44 | static int parse(const std::string& s, char thousandSeparator = ','); | 
|---|
| 45 | /// Parses an 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 |  | 
|---|
| 48 | static bool tryParse(const std::string& s, int& value, char thousandSeparator = ','); | 
|---|
| 49 | /// Parses an integer value in decimal notation from the given string. | 
|---|
| 50 | /// Returns true if a valid integer has been found, false otherwise. | 
|---|
| 51 | /// If parsing was not successful, value is undefined. | 
|---|
| 52 |  | 
|---|
| 53 | static unsigned parseUnsigned(const std::string& s, char thousandSeparator = ','); | 
|---|
| 54 | /// Parses an unsigned integer value in decimal notation from the given string. | 
|---|
| 55 | /// Throws a SyntaxException if the string does not hold a number in decimal notation. | 
|---|
| 56 |  | 
|---|
| 57 | static bool tryParseUnsigned(const std::string& s, unsigned& value, char thousandSeparator = ','); | 
|---|
| 58 | /// Parses an unsigned integer value in decimal notation from the given string. | 
|---|
| 59 | /// Returns true if a valid integer has been found, false otherwise. | 
|---|
| 60 | /// If parsing was not successful, value is undefined. | 
|---|
| 61 |  | 
|---|
| 62 | static unsigned parseHex(const std::string& s); | 
|---|
| 63 | /// Parses an integer value in hexadecimal notation from the given string. | 
|---|
| 64 | /// Throws a SyntaxException if the string does not hold a number in | 
|---|
| 65 | /// hexadecimal notation. | 
|---|
| 66 |  | 
|---|
| 67 | static bool tryParseHex(const std::string& s, unsigned& value); | 
|---|
| 68 | /// Parses an unsigned integer value in hexadecimal notation from the given string. | 
|---|
| 69 | /// Returns true if a valid integer has been found, false otherwise. | 
|---|
| 70 | /// If parsing was not successful, value is undefined. | 
|---|
| 71 |  | 
|---|
| 72 | static unsigned parseOct(const std::string& s); | 
|---|
| 73 | /// Parses an integer value in octal notation from the given string. | 
|---|
| 74 | /// Throws a SyntaxException if the string does not hold a number in | 
|---|
| 75 | /// hexadecimal notation. | 
|---|
| 76 |  | 
|---|
| 77 | static bool tryParseOct(const std::string& s, unsigned& value); | 
|---|
| 78 | /// Parses an unsigned integer value in octal notation from the given string. | 
|---|
| 79 | /// Returns true if a valid integer has been found, false otherwise. | 
|---|
| 80 | /// If parsing was not successful, value is undefined. | 
|---|
| 81 |  | 
|---|
| 82 | #if defined(POCO_HAVE_INT64) | 
|---|
| 83 |  | 
|---|
| 84 | static Int64 parse64(const std::string& s, char thousandSeparator = ','); | 
|---|
| 85 | /// Parses a 64-bit integer value in decimal notation from the given string. | 
|---|
| 86 | /// Throws a SyntaxException if the string does not hold a number in decimal notation. | 
|---|
| 87 |  | 
|---|
| 88 | static bool tryParse64(const std::string& s, Int64& value, char thousandSeparator = ','); | 
|---|
| 89 | /// Parses a 64-bit integer value in decimal notation from the given string. | 
|---|
| 90 | /// Returns true if a valid integer has been found, false otherwise. | 
|---|
| 91 | /// If parsing was not successful, value is undefined. | 
|---|
| 92 |  | 
|---|
| 93 | static UInt64 parseUnsigned64(const std::string& s, char thousandSeparator = ','); | 
|---|
| 94 | /// Parses an unsigned 64-bit integer value in decimal notation from the given string. | 
|---|
| 95 | /// Throws a SyntaxException if the string does not hold a number in decimal notation. | 
|---|
| 96 |  | 
|---|
| 97 | static bool tryParseUnsigned64(const std::string& s, UInt64& value, char thousandSeparator = ','); | 
|---|
| 98 | /// Parses an unsigned 64-bit integer value in decimal notation from the given string. | 
|---|
| 99 | /// Returns true if a valid integer has been found, false otherwise. | 
|---|
| 100 | /// If parsing was not successful, value is undefined. | 
|---|
| 101 |  | 
|---|
| 102 | static UInt64 parseHex64(const std::string& s); | 
|---|
| 103 | /// Parses a 64 bit-integer value in hexadecimal notation from the given string. | 
|---|
| 104 | /// Throws a SyntaxException if the string does not hold a number in hexadecimal notation. | 
|---|
| 105 |  | 
|---|
| 106 | static bool tryParseHex64(const std::string& s, UInt64& value); | 
|---|
| 107 | /// Parses an unsigned 64-bit integer value in hexadecimal notation from the given string. | 
|---|
| 108 | /// Returns true if a valid integer has been found, false otherwise. | 
|---|
| 109 | /// If parsing was not successful, value is undefined. | 
|---|
| 110 |  | 
|---|
| 111 | static UInt64 parseOct64(const std::string& s); | 
|---|
| 112 | /// Parses a 64 bit-integer value in octal notation from the given string. | 
|---|
| 113 | /// Throws a SyntaxException if the string does not hold a number in hexadecimal notation. | 
|---|
| 114 |  | 
|---|
| 115 | static bool tryParseOct64(const std::string& s, UInt64& value); | 
|---|
| 116 | /// Parses an unsigned 64-bit integer value in octal notation from the given string. | 
|---|
| 117 | /// Returns true if a valid integer has been found, false otherwise. | 
|---|
| 118 | /// If parsing was not successful, value is undefined. | 
|---|
| 119 |  | 
|---|
| 120 | #endif // defined(POCO_HAVE_INT64) | 
|---|
| 121 |  | 
|---|
| 122 | static double parseFloat(const std::string& s, char decimalSeparator = '.', char thousandSeparator = ','); | 
|---|
| 123 | /// Parses a double value in decimal floating point notation | 
|---|
| 124 | /// from the given string. | 
|---|
| 125 | /// Throws a SyntaxException if the string does not hold a floating-point | 
|---|
| 126 | /// number in decimal notation. | 
|---|
| 127 |  | 
|---|
| 128 | static bool tryParseFloat(const std::string& s, double& value, char decimalSeparator = '.', char thousandSeparator = ','); | 
|---|
| 129 | /// Parses a double value in decimal floating point notation | 
|---|
| 130 | /// from the given string. | 
|---|
| 131 | /// Returns true if a valid floating point number has been found, | 
|---|
| 132 | /// false otherwise. | 
|---|
| 133 | /// If parsing was not successful, value is undefined. | 
|---|
| 134 |  | 
|---|
| 135 | static bool parseBool(const std::string& s); | 
|---|
| 136 | /// Parses a bool value in decimal or string notation | 
|---|
| 137 | /// from the given string. | 
|---|
| 138 | /// Valid forms are: "0", "1", "true", "on", false", "yes", "no", "off". | 
|---|
| 139 | /// String forms are NOT case sensitive. | 
|---|
| 140 | /// Throws a SyntaxException if the string does not hold a valid bool number | 
|---|
| 141 |  | 
|---|
| 142 | static bool tryParseBool(const std::string& s, bool& value); | 
|---|
| 143 | /// Parses a bool value in decimal or string notation | 
|---|
| 144 | /// from the given string. | 
|---|
| 145 | /// Valid forms are: "0", "1", "true", "on", false", "yes", "no", "off". | 
|---|
| 146 | /// String forms are NOT case sensitive. | 
|---|
| 147 | /// Returns true if a valid bool number has been found, | 
|---|
| 148 | /// false otherwise. | 
|---|
| 149 | /// If parsing was not successful, value is undefined. | 
|---|
| 150 | }; | 
|---|
| 151 |  | 
|---|
| 152 |  | 
|---|
| 153 | } // namespace Poco | 
|---|
| 154 |  | 
|---|
| 155 |  | 
|---|
| 156 | #endif // Foundation_NumberParser_INCLUDED | 
|---|
| 157 |  | 
|---|