Coverage Report

Created: 2023-09-22 06:27

/src/poco/Foundation/src/NumberParser.cpp
Line
Count
Source (jump to first uncovered line)
1
//
2
// NumberParser.cpp
3
//
4
// Library: Foundation
5
// Package: Core
6
// Module:  NumberParser
7
//
8
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9
// and Contributors.
10
//
11
// SPDX-License-Identifier: BSL-1.0
12
//
13
14
15
#include "Poco/NumberParser.h"
16
#include "Poco/Exception.h"
17
#include "Poco/String.h"
18
#include "Poco/NumericString.h"
19
#include <cstdio>
20
#include <cctype>
21
#include <stdlib.h>
22
#if !defined(POCO_NO_LOCALE)
23
  #include <locale>
24
#endif
25
26
27
#if defined(POCO_LONG_IS_64_BIT)
28
  #define I64_FMT "l"
29
#elif defined(_MSC_VER) || defined(__MINGW32__)
30
  #define I64_FMT "I64"
31
#elif defined(__APPLE__)
32
  #define I64_FMT "q"
33
#else
34
  #define I64_FMT "ll"
35
#endif
36
37
38
namespace Poco {
39
40
41
int NumberParser::parse(const std::string& s, char thSep)
42
0
{
43
0
  int result;
44
0
  if (tryParse(s, result, thSep))
45
0
    return result;
46
0
  else
47
0
    throw SyntaxException("Not a valid integer", s);
48
0
}
49
50
51
bool NumberParser::tryParse(const std::string& s, int& value, char thSep)
52
0
{
53
0
  return strToInt(s.c_str(), value, NUM_BASE_DEC, thSep);
54
0
}
55
56
57
unsigned NumberParser::parseUnsigned(const std::string& s, char thSep)
58
0
{
59
0
  unsigned result;
60
0
  if (tryParseUnsigned(s, result, thSep))
61
0
    return result;
62
0
  else
63
0
    throw SyntaxException("Not a valid unsigned integer", s);
64
0
}
65
66
67
bool NumberParser::tryParseUnsigned(const std::string& s, unsigned& value, char thSep)
68
0
{
69
0
  return strToInt(s.c_str(), value, NUM_BASE_DEC, thSep);
70
0
}
71
72
73
unsigned NumberParser::parseHex(const std::string& s)
74
0
{
75
0
  unsigned result;
76
0
  if (tryParseHex(s, result))
77
0
    return result;
78
0
  else
79
0
    throw SyntaxException("Not a valid hexadecimal integer", s);
80
0
}
81
82
83
bool NumberParser::tryParseHex(const std::string& s, unsigned& value)
84
0
{
85
0
  int offset = 0;
86
0
  if (s.size() > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) offset = 2;
87
0
  return strToInt(s.c_str() + offset, value, NUM_BASE_HEX);
88
0
}
89
90
91
unsigned NumberParser::parseOct(const std::string& s)
92
0
{
93
0
  unsigned result;
94
0
  if (tryParseOct(s, result))
95
0
    return result;
96
0
  else
97
0
    throw SyntaxException("Not a valid hexadecimal integer", s);
98
0
}
99
100
101
bool NumberParser::tryParseOct(const std::string& s, unsigned& value)
102
0
{
103
0
  return strToInt(s.c_str(), value, NUM_BASE_OCT);
104
0
}
105
106
107
#if defined(POCO_HAVE_INT64)
108
109
110
Int64 NumberParser::parse64(const std::string& s, char thSep)
111
0
{
112
0
  Int64 result;
113
0
  if (tryParse64(s, result, thSep))
114
0
    return result;
115
0
  else
116
0
    throw SyntaxException("Not a valid integer", s);
117
0
}
118
119
120
bool NumberParser::tryParse64(const std::string& s, Int64& value, char thSep)
121
2.82M
{
122
2.82M
  return strToInt(s.c_str(), value, NUM_BASE_DEC, thSep);
123
2.82M
}
124
125
126
UInt64 NumberParser::parseUnsigned64(const std::string& s, char thSep)
127
892
{
128
892
  UInt64 result;
129
892
  if (tryParseUnsigned64(s, result, thSep))
130
834
    return result;
131
58
  else
132
58
    throw SyntaxException("Not a valid unsigned integer", s);
133
892
}
134
135
136
bool NumberParser::tryParseUnsigned64(const std::string& s, UInt64& value, char thSep)
137
892
{
138
892
  return strToInt(s.c_str(), value, NUM_BASE_DEC, thSep);
139
892
}
140
141
142
UInt64 NumberParser::parseHex64(const std::string& s)
143
0
{
144
0
  UInt64 result;
145
0
  if (tryParseHex64(s, result))
146
0
    return result;
147
0
  else
148
0
    throw SyntaxException("Not a valid hexadecimal integer", s);
149
0
}
150
151
152
bool NumberParser::tryParseHex64(const std::string& s, UInt64& value)
153
0
{
154
0
  int offset = 0;
155
0
  if (s.size() > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) offset = 2;
156
0
  return strToInt(s.c_str() + offset, value, NUM_BASE_HEX);
157
0
}
158
159
160
UInt64 NumberParser::parseOct64(const std::string& s)
161
0
{
162
0
  UInt64 result;
163
0
  if (tryParseOct64(s, result))
164
0
    return result;
165
0
  else
166
0
    throw SyntaxException("Not a valid hexadecimal integer", s);
167
0
}
168
169
170
bool NumberParser::tryParseOct64(const std::string& s, UInt64& value)
171
0
{
172
0
  return strToInt(s.c_str(), value, NUM_BASE_OCT);
173
0
}
174
175
176
#endif // defined(POCO_HAVE_INT64)
177
178
179
double NumberParser::parseFloat(const std::string& s, char decSep, char thSep)
180
14.3k
{
181
14.3k
  double result;
182
14.3k
  if (tryParseFloat(s, result, decSep, thSep))
183
14.2k
    return result;
184
172
  else
185
172
    throw SyntaxException("Not a valid floating-point number", s);
186
14.3k
}
187
188
189
bool NumberParser::tryParseFloat(const std::string& s, double& value, char decSep, char thSep)
190
14.3k
{
191
14.3k
  return strToDouble(s, value, decSep, thSep);
192
14.3k
}
193
194
195
bool NumberParser::parseBool(const std::string& s)
196
0
{
197
0
  bool result;
198
0
  if (tryParseBool(s, result))
199
0
    return result;
200
0
  else
201
0
    throw SyntaxException("Not a valid bool number", s);
202
0
}
203
204
205
bool NumberParser::tryParseBool(const std::string& s, bool& value)
206
0
{
207
0
  int n;
208
0
  if (NumberParser::tryParse(s, n))
209
0
  {
210
0
    value = (n != 0);
211
0
    return true;
212
0
  }
213
214
0
  if (icompare(s, "true") == 0)
215
0
  {
216
0
    value = true;
217
0
    return true;
218
0
  }
219
0
  else if (icompare(s, "yes") == 0)
220
0
  {
221
0
    value = true;
222
0
    return true;
223
0
  }
224
0
  else if (icompare(s, "on") == 0)
225
0
  {
226
0
    value = true;
227
0
    return true;
228
0
  }
229
230
0
  if (icompare(s, "false") == 0)
231
0
  {
232
0
    value = false;
233
0
    return true;
234
0
  }
235
0
  else if (icompare(s, "no") == 0)
236
0
  {
237
0
    value = false;
238
0
    return true;
239
0
  }
240
0
  else if (icompare(s, "off") == 0)
241
0
  {
242
0
    value = false;
243
0
    return true;
244
0
  }
245
246
0
  return false;
247
0
}
248
249
250
} // namespace Poco