BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_numericparseutil.h
Go to the documentation of this file.
1/// @file bdlb_numericparseutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_numericparseutil.h -*-C++-*-
8#ifndef INCLUDED_BDLB_NUMERICPARSEUTIL
9#define INCLUDED_BDLB_NUMERICPARSEUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_numericparseutil bdlb_numericparseutil
15/// @brief Provide conversions from text into fundamental numeric types.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_numericparseutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_numericparseutil-purpose"> Purpose</a>
25/// * <a href="#bdlb_numericparseutil-classes"> Classes </a>
26/// * <a href="#bdlb_numericparseutil-description"> Description </a>
27/// * <a href="#bdlb_numericparseutil-definition-of-symbols-used-in-production-rules"> Definition of Symbols Used in Production Rules </a>
28/// * <a href="#bdlb_numericparseutil-grammar-production-rules"> Grammar Production Rules </a>
29/// * <a href="#bdlb_numericparseutil-remainder-output-parameter"> Remainder Output Parameter </a>
30/// * <a href="#bdlb_numericparseutil-floating-point-values"> Floating Point Values </a>
31/// * <a href="#bdlb_numericparseutil-special-floating-point-values"> Special Floating Point Values </a>
32/// * <a href="#bdlb_numericparseutil-usage-example"> Usage Example </a>
33/// * <a href="#bdlb_numericparseutil-example-1-parsing-an-integer-value-from-a-string_view"> Example 1: Parsing an Integer Value from a string_view </a>
34///
35/// # Purpose {#bdlb_numericparseutil-purpose}
36/// Provide conversions from text into fundamental numeric types.
37///
38/// # Classes {#bdlb_numericparseutil-classes}
39///
40/// - bdlb::NumericParseUtil: namespace for parsing functions
41///
42/// # Description {#bdlb_numericparseutil-description}
43/// This component provides a namespace, `bdlb::NumericParseUtil`,
44/// containing utility functions for parsing ascii text representations of
45/// numeric values into the corresponding value of a fundamental C++ type (like
46/// `int` or `double`).
47///
48/// None of the parsing functions in this component consume leading whitespace.
49/// For parsing to succeed, the sought item must be found at the beginning of
50/// the input string.
51///
52/// The following two subsections describe the grammar defining the parsing
53/// rules.
54///
55/// ## Definition of Symbols Used in Production Rules {#bdlb_numericparseutil-definition-of-symbols-used-in-production-rules}
56///
57///
58///
59/// The following grammar is used to specify regular expressions:
60/// @code
61/// - Within brackets the minus means through. For example, [a-z] is
62/// equivalent to [abcd...xyz]. The - can appear as itself only if used
63/// as the first or last character. For example, the character class
64/// expression []-] matches the characters ] and -.
65///
66/// | Logical OR between two expressions means one must be present.
67///
68/// ( ... ) Parentheses are used for grouping. An operator, for example, *,
69/// +, {}, can work on a single character or on a regular expression
70/// enclosed in parentheses. For example, (a*(cb+)*)$.
71/// @endcode
72///
73/// ## Grammar Production Rules {#bdlb_numericparseutil-grammar-production-rules}
74///
75///
76/// @code
77/// <NUMBER> ::= <OPTIONAL_SIGN><DIGIT>+
78///
79/// <DECIMAL_NUMBER> ::= <OPTIONAL_SIGN><DECIMAL_DIGIT>+
80///
81/// <POSITIVE_NUMBER> ::= <DIGIT>+
82///
83/// <OPTIONAL_SIGN> ::= (+|-)?
84///
85/// <DIGIT> ::= depending on base can include characters 0-9 and case-
86/// insensitive letters. For example, octal digit is in the range
87/// [0 .. 7].
88///
89/// <DECIMAL_DIGIT> ::= [0123456789]
90/// <OCTAL_DIGIT> ::= [01234567]
91/// <HEX_DIGIT> ::= [0123456789abcdefABCDEF]
92///
93/// <SHORT> ::= <NUMBER>
94/// <SHORT> must be in range [SHRT_MIN .. SHRT_MAX].
95/// <USHORT> ::= <NUMBER>
96/// <USHORT> must be in range [0 .. USHRT_MAX].
97///
98/// <INT> ::= <NUMBER>
99/// <INT> must be in range [INT_MIN .. INT_MAX].
100///
101/// <INT64> ::= <NUMBER>
102/// <INT64> must be in range
103/// [-0x8000000000000000uLL .. 0x7FFFFFFFFFFFFFFFuLL].
104///
105/// <UNSIGNED> ::= <NUMBER>
106/// <UNSIGNED> must be in range [0 .. UINT_MAX].
107///
108/// <UNSIGNED64> ::= <NUMBER>
109/// <UNSIGNED64> must be in range
110/// [0 .. 0xFFFFFFFFFFFFFFFFuLL].
111///
112/// <DECIMAL_EXPONENT> ::= <DECIMAL_NUMBER>
113///
114/// <REAL> ::= (<DECIMAL_DIGIT>+ (. <DECIMAL_DIGIT>*)? | . <DECIMAL_DIGIT>+)
115/// (e|E <DECIMAL_EXPONENT>)
116///
117/// <INF> ::= infinity | inf
118/// all case insensitive
119///
120/// <NAN-SEQUENCE> ::= [abcdefghijklmnopqrstuvwxyz0123456789_]*
121/// case insensitive
122///
123/// <NAN> ::= nan(<NAN-SEQUENCE>) | nan
124/// all case insensitive
125///
126/// <DOUBLE> ::= <OPTIONAL_SIGN> (<REAL> | <INF> | <NAN>)
127/// <DOUBLE> must be in range [DBL_MIN .. DBL_MAX], or Nan, or Infinity.
128/// @endcode
129///
130/// ## Remainder Output Parameter {#bdlb_numericparseutil-remainder-output-parameter}
131///
132///
133/// The parsing functions provided by `bdlb::NumericParseUtil` typically return
134/// an optional, second, output parameter named `remainder`. The output
135/// parameter `remainder` is loaded with a string reference starting at the
136/// character following the last character successfully parsed as part of the
137/// numeric value, and ending at the character one past the end of the input
138/// string. If the entire input string is parsed successfully, `remainder` is
139/// loaded with an empty string reference. However, if the parse function is
140/// not successful (i.e., it returns a non-zero error status), then it will not
141/// modify the value of `remainder`.
142///
143/// ## Floating Point Values {#bdlb_numericparseutil-floating-point-values}
144///
145///
146/// The conversion from text to values of type `double` results in the closest
147/// representable value to the decimal text. Note that this is the same as for
148/// the standard library function `strtod`. For example, the ASCII string
149/// "3.14159" is converted, on some platforms, to 3.1415899999999999.
150///
151/// The `strtod` function is locale-dependent. It uses the `LC_CTYPE` and
152/// `LC_NUMERIC` locale categories from the C standard global locale established
153/// by `setlocale`. `LC_CTYPE` is used by `strtod` to skip leading whitespace,
154/// whereas `LC_NUMERIC` is used in the actual parsing of the number. Our
155/// implementation forbids leading whitespace. When verifying the lack of
156/// leading whitespace we use both our own locale-independent character
157/// classification function (in case `LC_CTYPE` would not classify ASCII
158/// whitespace properly), as well as the C global locale-dependent
159/// `bsl::isspace` to ensure that `strtod` will not skip some special whitespace
160/// characters and parse a string as fully-a-number by mistake. That allows us
161/// to ignore the `LC_CTYPE` locale category, however we still have to require
162/// `LC_NUMERIC` to be set to the "C" locale for `strtod` itself.
163///
164/// ### Special Floating Point Values {#bdlb_numericparseutil-special-floating-point-values}
165///
166///
167/// The IEEE-754 (double precision) floating point format supports the following
168/// special values: Not-a-Number (NaN) and Infinity, both in positive or
169/// negative. `parseDouble` allows expressions for both:
170///
171/// * **infinity-expression**: results in negative of positive
172/// bsl::numeric_limits<double>::infinity() value. The expresssion
173/// consists of the following elements:
174/// - an optional plus (`+`) or minus (`-`) sign
175/// - the word "INF" or "INFINITY", ignoring case
176///
177/// * **not-a-number-expression**: results in a negative or positive
178/// bsl::numeric_limits<double>::quiet_NaN() value. The expresssion consists
179/// of the following elements:
180/// - an optional plus (`+`) or minus (`-`) sign
181/// - "NAN" or "NAN(char-sequence)" ignoring the case of "NAN". The
182/// char-sequence may be empty or contain digits, letters from the Latin
183/// alphabet and underscores.
184///
185/// ## Usage Example {#bdlb_numericparseutil-usage-example}
186///
187///
188/// In this section, we show the intended usage of this component.
189///
190/// ### Example 1: Parsing an Integer Value from a string_view {#bdlb_numericparseutil-example-1-parsing-an-integer-value-from-a-string_view}
191///
192///
193/// Suppose that we have a @ref string_view that presumably contains a (not
194/// necessarily NUL terminated) string representing a 32-bit integer value and
195/// we want to convert that string into an `int` (32-bit integer).
196///
197/// First, we create the string:
198/// @code
199/// const bsl::string_view input("20171024", 4);
200/// @endcode
201/// Then we create the output variables for the parser:
202/// @code
203/// int year;
204/// bsl::string_view rest;
205/// @endcode
206/// Next we call the parser function:
207/// @code
208/// const int rv = bdlb::NumericParseUtil::parseInt(&year, &rest, input);
209/// @endcode
210/// Then we verify the results:
211/// @code
212/// assert(0 == rv);
213/// assert(2017 == year);
214/// assert(rest.empty());
215/// @endcode
216/// @}
217/** @} */
218/** @} */
219
220/** @addtogroup bdl
221 * @{
222 */
223/** @addtogroup bdlb
224 * @{
225 */
226/** @addtogroup bdlb_numericparseutil
227 * @{
228 */
229
230#include <bdlscm_version.h>
231
232#include <bsls_assert.h>
233#include <bsls_types.h>
234
235#include <bsl_string.h>
236#include <bsl_string_view.h>
237
238
239
240namespace bdlb {
241 // =======================
242 // struct NumericParseUtil
243 // =======================
244
245/// This `struct` provides a namespace for a suite of stateless procedures
246/// that perform parsing functionality for numbers.
247///
248/// See @ref bdlb_numericparseutil
250
251 public:
252 // TYPES
253
254 /// Shorter name for readability.
256
257 // CLASS METHODS
258
259 /// Determine whether the specified `character` represents a digit in
260 /// the specified `base`; return the numeric equivalent if so, and -1 otherwise.
261 ///
262 /// \pre The behavior is undefined if either `character` or
263 /// `base` is 0 and unless `2 <= base` and `base <= 36` (i.e., bases
264 /// where digits are representable by characters in the range [`0`-`9`],
265 /// [`a`-`z`], or [`A`-`Z`]).
266 static int characterToDigit(char character, int base);
267
268 /// Parse the specified `inputString` for a sequence of characters
269 /// matching the production rule <DOUBLE> (see {Grammar Production
270 /// Rules}) and place into the specified `result` the corresponding
271 /// value. Optionally specify `remainder`, in which to store the
272 /// remainder of the `inputString` immediately following the
273 /// successfully parsed text, or the position at which a parse failure
274 /// was detected. Return zero on success, and a non-zero value
275 /// otherwise. The value of `result` is unchanged if a parse failure
276 /// occurs unless it is a range error (value `ERANGE` from `<cerrno>`).
277 /// In case of `ERANGE` return value `result` will be set to zero on
278 /// underflow and infinity in case of overflow, with the appropriate sign.
279 ///
280 /// \pre The behavior is undefined unless the current numeric locale
281 /// is the `"C"` locale, such that
282 /// `strcmp(setlocale(LC_NUMERIC, 0), "C") == 0`. For more information
283 /// see {Floating Point Values}.
284 static int parseDouble(double *result,
285 const bsl::string_view& inputString);
286 static int parseDouble(double *result,
287 bsl::string_view *remainder,
288 const bsl::string_view& inputString);
289
290 /// Parse the specified `inputString` for the maximal sequence of
291 /// characters forming an <INT> (see {Grammar Production Rules}) in the
292 /// optionally specified `base` or in base 10 if `base` is not
293 /// specified, and place into the specified `result` the corresponding
294 /// value. Optionally specify `remainder`, in which to store the
295 /// remainder of the `inputString` immediately following the
296 /// successfully parsed text, or the position at which a parse failure
297 /// was detected. If the parsed number is outside of the
298 /// `[INT_MIN .. INT_MAX]` range the `result` will be the longest number
299 /// that does not over/underflow and `remainder` start at the first
300 /// digit that would make the number too large/small. Return zero on
301 /// success, and a non-zero value otherwise. The value of `result` is
302 /// unchanged if a parse failure occurs.
303 ///
304 /// \pre The behavior is undefined unless `2 <= base` and `base <= 36` (i.e., bases where digits are
305 /// representable by characters in the range [`0`-`9`], [`a`-`z`], or
306 /// [`A`-`Z`]).
307 ///
308 /// A parse failure can occur for the following reasons:
309 /// 1. The `inputString` is empty.
310 /// 2. The first character of `inputString` is not a valid digit in
311 /// `base`, or an optional sign followed by a valid digit.
312 static int parseInt(int *result,
313 const bsl::string_view& inputString,
314 int base = 10);
315 static int parseInt(int *result,
316 bsl::string_view *remainder,
317 const bsl::string_view& inputString,
318 int base = 10);
319
320 /// Parse the specified `inputString` for the maximal sequence of
321 /// characters forming a valid <INT64> (see {Grammar Production Rules})
322 /// in the optionally specified `base` or in base 10 if `base` is not
323 /// specified, and place into the specified `result` the corresponding
324 /// value. Optionally specify `remainder`, in which to store the
325 /// remainder of the `inputString` immediately following the
326 /// successfully parsed text, or the position at which a parse failure
327 /// was detected. If the parsed number is outside of the
328 /// `[-0x8000000000000000uLL .. 0x7FFFFFFFFFFFFFFFull]` range the
329 /// `result` will be the longest number that does not over/underflow and
330 /// `remainder` will start at the first digit that would make the number
331 /// too large/small. Return zero on success, and a non-zero value
332 /// otherwise. The value of `result` is unchanged if a parse failure occurs.
333 ///
334 /// \pre The behavior is undefined unless `2 <= base` and
335 /// `base <= 36` (i.e., bases where digits are representable by
336 /// characters in the range [`0`-`9`], [`a`-`z`], or [`A`-`Z`]).
337 ///
338 /// A parse failure can occur for the following reasons:
339 /// 1. The `inputString` is empty.
340 /// 2. The first character of `inputString` is not a valid digit in
341 /// `base`, or an optional sign followed by a valid digit.
342 static int parseInt64(bsls::Types::Int64 *result,
343 const bsl::string_view& inputString,
344 int base = 10);
345 static int parseInt64(bsls::Types::Int64 *result,
346 bsl::string_view *remainder,
347 const bsl::string_view& inputString,
348 int base = 10);
349
350 /// Parse the specified `inputString` for the maximal sequence of
351 /// characters forming a valid <SHORT> (see {Grammar Production Rules})
352 /// in the optionally specified `base` or in base 10 if `base` is not
353 /// specified, and place into the specified `result` the corresponding
354 /// value. Optionally specify `remainder`, in which to store the
355 /// remainder of the `inputString` immediately following the
356 /// successfully parsed text, or the position at which a parse failure
357 /// was detected. If the parsed number is outside of the
358 /// `[SHRT_MIN .. SHRT_MAX]` range the `result` will be the longest
359 /// number that does not over/underflow and `remainder` will start at
360 /// the first digit that would make the number too large/small. Return
361 /// zero on success, and a non-zero value otherwise. The value of
362 /// `result` is unchanged if a parse failure occurs.
363 ///
364 /// \pre The behavior is undefined unless `2 <= base` and `base <= 36` (i.e., bases where
365 /// digits are representable by characters in the range [`0`-`9`],
366 /// [`a`-`z`] or [`A`-`Z`]).
367 ///
368 /// A parse failure can occur for the following reasons:
369 /// 1. The `inputString` is empty.
370 /// 2. The first character of `inputString` is not a valid digit in
371 /// `base`, or an optional sign followed by a valid digit.
372 static int parseShort(short *result,
373 const bsl::string_view& inputString,
374 int base = 10);
375 static int parseShort(short *result,
376 bsl::string_view *remainder,
377 const bsl::string_view& inputString,
378 int base = 10);
379
380 /// Parse the specified `inputString` for the maximal sequence of
381 /// characters forming an <UNSIGNED> (see {Grammar Production Rules}) in
382 /// the optionally specified `base` or in base 10 if `base` is not
383 /// specified, and place into the specified `result` the corresponding
384 /// value. Optionally specify `remainder`, in which to store the
385 /// remainder of the `inputString` immediately following the
386 /// successfully parsed text, or the position at which a parse failure
387 /// was detected. If the parsed number is outside of the
388 /// `[0 .. UINT_MAX]` range the `result` will be the longest number that
389 /// does not overflow and `remainder` will start at the first digit that
390 /// would make the number too large. Return zero on success, and a
391 /// non-zero value otherwise. The value of `result` is unchanged if a parse failure occurs.
392 ///
393 /// \pre The behavior is undefined unless `2 <= base`
394 /// and `base <= 36` (i.e., bases where digits are representable by
395 /// characters in the range [`0`-`9`], [`a`-`z`], or [`A`-`Z`]).
396 ///
397 /// A parse failure can occur for the following reasons:
398 /// 1. The `inputString` is empty.
399 /// 2. The first character of `inputString` is not a valid digit in
400 /// `base`, or an optional sign followed by a valid digit.
401 static int parseUint(unsigned int *result,
402 const bsl::string_view& inputString,
403 int base = 10);
404 static int parseUint(unsigned int *result,
405 bsl::string_view *remainder,
406 const bsl::string_view& inputString,
407 int base = 10);
408
409 /// Parse the specified `inputString` for the maximal sequence of
410 /// characters forming a valid <UNSIGNED64> (see {Grammar Production
411 /// Rules}) in the optionally specified `base` or in base 10 if `base`
412 /// is not specified, and place into the specified `result` the
413 /// corresponding value. Optionally specify `remainder`, in which to
414 /// store the remainder of the `inputString` immediately following the
415 /// successfully parsed text, or the position at which a parse failure
416 /// was detected. If the parsed number is outside of the
417 /// `[0 .. 0XFFFFFFFFFFFFFFFF]` range the `result` will be the longest
418 /// number that does not overflow and `remainder` will start at the
419 /// first digit that would make the number too large. Return zero on
420 /// success, and a non-zero value otherwise. The value of `result` is
421 /// unchanged if a parse failure occurs.
422 ///
423 /// \pre The behavior is undefined unless `2 <= base` and `base <= 36` (i.e., bases where digits are
424 /// representable by characters in the range [`0`-`9`], [`a`-`z`], or
425 /// [`A`-`Z`]).
426 ///
427 /// A parse failure can occur for the following reasons:
428 /// 1. The `inputString` is empty.
429 /// 2. The first character of `inputString` is not a valid digit in
430 /// `base`, or an optional sign followed by a valid digit.
431 static int parseUint64(bsls::Types::Uint64 *result,
432 const bsl::string_view& inputString,
433 int base = 10);
435 bsl::string_view *remainder,
436 const bsl::string_view& inputString,
437 int base = 10);
438
439 /// Parse the specified `inputString` for the maximal sequence of
440 /// characters forming a valid <USHORT> (see {Grammar Production Rules})
441 /// in the optionally specified `base` or in base 10 if `base` is not
442 /// specified, and place into the specified `result` the corresponding
443 /// value. Optionally specify `remainder`, in which to store the
444 /// remainder of the `inputString` immediately following the
445 /// successfully parsed text, or the position at which a parse failure
446 /// was detected. If the parsed number is outside of the
447 /// `[0 .. USHRT_MAX]` range the `result` will be the longest number
448 /// that does not over/underflow and `remainder` will start at the first
449 /// digit that would make the number too large/small. Return zero on
450 /// success, and a non-zero value otherwise. The value of `result` is
451 /// unchanged if a parse failure occurs.
452 ///
453 /// \pre The behavior is undefined unless `2 <= base` and `base <= 36` (i.e., bases where digits are
454 /// representable by characters in the range [`0`-`9`], [`a`-`z`] or
455 /// [`A`-`Z`]).
456 ///
457 /// A parse failure can occur for the following reasons:
458 /// 1. The `inputString` is empty.
459 /// 2. The first character of `inputString` is not a valid digit in
460 /// `base`, or an optional sign followed by a valid digit.
461 static int parseUshort(unsigned short *result,
462 const bsl::string_view& inputString,
463 int base = 10);
464 static int parseUshort(unsigned short *result,
465 bsl::string_view *remainder,
466 const bsl::string_view& inputString,
467 int base = 10);
468
469 /// Parse the specified `inputString` for an optional sign followed by a
470 /// sequence of characters representing digits in the specified `base`,
471 /// consuming the maximum that will form a number whose value is less
472 /// than or equal to the specified `maxValue` and greater than or equal
473 /// to the specified `minValue`. Place into the specified `result` the
474 /// extracted value. Optionally specify `remainder`, in which to store
475 /// the remainder of the `inputString` immediately following the
476 /// successfully parsed text, or the position at which a parse failure
477 /// was detected. Return 0 on success, and a non-zero value otherwise.
478 /// The value of `result` is unchanged if a parse failure occurs.
479 ///
480 /// \pre The behavior is undefined unless `maxValue` a positive integer, and
481 /// `minValue` is negative (this is required to allow for efficient
482 /// implementation). The behavior is also undefined unless `2 <= base`
483 /// and `base <= 36`, (i.e., bases where digits are representable by
484 /// characters `[ 0 .. 9 ]`, `[ a .. z ]` or `[ A .. Z ]`).
485 ///
486 /// A parse failure can occur for the following reasons:
487 /// 1. The parsed string is not a `<NUMBER>`, i.e., does not contain
488 /// an optional sign followed by at least one digit.
489 /// 2. The first digit in `inputString` is larger than `maxValue` or
490 /// smaller than `minValue`.
491 /// 3. The first digit is not a valid number for the `base`.
492 static int parseSignedInteger(bsls::Types::Int64 *result,
493 const bsl::string_view& inputString,
494 int base,
495 const bsls::Types::Int64 minValue,
496 const bsls::Types::Int64 maxValue);
498 bsl::string_view *remainder,
499 const bsl::string_view& inputString,
500 int base,
501 const bsls::Types::Int64 minValue,
502 const bsls::Types::Int64 maxValue);
503
504 /// Parse the specified `inputString` for a sequence of characters
505 /// representing digits in the specified `base`, consuming the maximum
506 /// up to the optionally specified `maxNumDigits` that form a number
507 /// whose value does not exceed the specified `maxValue`. Place into
508 /// the specified `result` the extracted value. Optionally specify
509 /// `remainder`, in which to store the remainder of the `inputString`
510 /// immediately following the successfully parsed text, or the position
511 /// at which a parse failure was detected. Return 0 on success, and a
512 /// non-zero value otherwise. The value of `result` is unchanged if a
513 /// parse failure occurs. If `maxNumDigits` is not specified, it
514 /// defaults to a number larger than the number of possible digits in an unsigned 64-bit integer.
515 ///
516 /// \pre The behavior is undefined unless
517 /// `2 < = base` and `base <= 36` (i.e., bases where digits are
518 /// representable by characters `[ 0 .. 9 ]`, `[ a .. z ]` or
519 /// `[ A .. Z ]`).
520 ///
521 /// A parse failure can occur for the following reasons:
522 /// 1. The `inputString` is not a `<POSITIVE_NUMBER>`, i.e., does
523 /// not begin with a digit.
524 /// 2. The first digit in `inputString` is larger than `maxValue`.
525 /// 3. The first digit is not a valid number for the `base`.
526 static int parseUnsignedInteger(bsls::Types::Uint64 *result,
527 const bsl::string_view& inputString,
528 int base,
529 const bsls::Types::Uint64 maxValue);
531 bsl::string_view *remainder,
532 const bsl::string_view& inputString,
533 int base,
534 const bsls::Types::Uint64 maxValue);
535 static int parseUnsignedInteger(bsls::Types::Uint64 *result,
536 const bsl::string_view& inputString,
537 int base,
538 const bsls::Types::Uint64 maxValue,
539 int maxNumDigits);
541 bsl::string_view *remainder,
542 const bsl::string_view& inputString,
543 int base,
544 const bsls::Types::Uint64 maxValue,
545 int maxNumDigits);
546};
547
548// ============================================================================
549// INLINE AND TEMPLATE FUNCTION DEFINITIONS
550// ============================================================================
551
552 // -----------------------
553 // struct NumericParseUtil
554 // -----------------------
555
556inline
558 const bsl::string_view& inputString)
559{
560 BSLS_ASSERT(result);
561
562 bsl::string_view rest;
563 return parseDouble(result, &rest, inputString);
564}
565
566inline
568 const bsl::string_view& inputString,
569 int base)
570{
571 BSLS_ASSERT(result);
572
573 bsl::string_view rest;
574 return parseInt(result, &rest, inputString, base);
575}
576
577inline
579 const bsl::string_view& inputString,
580 int base)
581{
582 BSLS_ASSERT(result);
583
584 bsl::string_view rest;
585 return parseInt64(result, &rest, inputString, base);
586}
587
588inline
590 const bsl::string_view& inputString,
591 int base)
592{
593 BSLS_ASSERT(result);
594
595 bsl::string_view rest;
596 return parseShort(result, &rest, inputString, base);
597}
598
599inline
600int NumericParseUtil::parseUint(unsigned int *result,
601 const bsl::string_view& inputString,
602 int base)
603{
604 BSLS_ASSERT(result);
605
606 bsl::string_view rest;
607 return parseUint(result, &rest, inputString, base);
608}
609
610inline
612 const bsl::string_view& inputString,
613 int base)
614{
615 BSLS_ASSERT(result);
616
617 bsl::string_view rest;
618 return parseUint64(result, &rest, inputString, base);
619}
620
621inline
622int NumericParseUtil::parseUshort(unsigned short *result,
623 const bsl::string_view& inputString,
624 int base)
625{
626 BSLS_ASSERT(result);
627
628 bsl::string_view rest;
629 return parseUshort(result, &rest, inputString, base);
630}
631
632inline
634 const bsl::string_view& inputString,
635 int base,
636 const bsls::Types::Int64 minValue,
637 const bsls::Types::Int64 maxValue)
638{
639 BSLS_ASSERT(result);
640
641 bsl::string_view rest;
642 return parseSignedInteger(
643 result, &rest, inputString, base, minValue, maxValue);
644}
645
646inline
648 bsls::Types::Uint64 *result,
649 const bsl::string_view& inputString,
650 int base,
651 const bsls::Types::Uint64 maxValue)
652{
653 BSLS_ASSERT(result);
654
655 bsl::string_view rest;
656 return parseUnsignedInteger(result, &rest, inputString, base, maxValue);
657}
658
659inline
661 bsls::Types::Uint64 *result,
662 const bsl::string_view& inputString,
663 int base,
664 const bsls::Types::Uint64 maxValue,
665 int maxNumDigits)
666{
667 BSLS_ASSERT(result);
668
669 bsl::string_view rest;
671 result, &rest, inputString, base, maxValue, maxNumDigits);
672}
673
674} // close package namespace
675
676
677#endif
678
679// ----------------------------------------------------------------------------
680// Copyright 2017 Bloomberg Finance L.P.
681//
682// Licensed under the Apache License, Version 2.0 (the "License");
683// you may not use this file except in compliance with the License.
684// You may obtain a copy of the License at
685//
686// http://www.apache.org/licenses/LICENSE-2.0
687//
688// Unless required by applicable law or agreed to in writing, software
689// distributed under the License is distributed on an "AS IS" BASIS,
690// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
691// See the License for the specific language governing permissions and
692// limitations under the License.
693// ----------------------------- END-OF-FILE ----------------------------------
694
695/** @} */
696/** @} */
697/** @} */
Definition bslstl_stringview.h:471
std::size_t size_type
Definition bslstl_stringview.h:487
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_numericparseutil.h:249
static int parseUint64(bsls::Types::Uint64 *result, const bsl::string_view &inputString, int base=10)
Definition bdlb_numericparseutil.h:611
bsl::string_view::size_type size_type
Shorter name for readability.
Definition bdlb_numericparseutil.h:255
static int parseShort(short *result, bsl::string_view *remainder, const bsl::string_view &inputString, int base=10)
static int parseDouble(double *result, bsl::string_view *remainder, const bsl::string_view &inputString)
static int parseUint64(bsls::Types::Uint64 *result, bsl::string_view *remainder, const bsl::string_view &inputString, int base=10)
static int parseUnsignedInteger(bsls::Types::Uint64 *result, const bsl::string_view &inputString, int base, const bsls::Types::Uint64 maxValue)
Definition bdlb_numericparseutil.h:647
static int parseUshort(unsigned short *result, const bsl::string_view &inputString, int base=10)
Definition bdlb_numericparseutil.h:622
static int parseInt64(bsls::Types::Int64 *result, const bsl::string_view &inputString, int base=10)
Definition bdlb_numericparseutil.h:578
static int parseDouble(double *result, const bsl::string_view &inputString)
Definition bdlb_numericparseutil.h:557
static int parseInt(int *result, const bsl::string_view &inputString, int base=10)
Definition bdlb_numericparseutil.h:567
static int parseUshort(unsigned short *result, bsl::string_view *remainder, const bsl::string_view &inputString, int base=10)
static int parseUnsignedInteger(bsls::Types::Uint64 *result, bsl::string_view *remainder, const bsl::string_view &inputString, int base, const bsls::Types::Uint64 maxValue, int maxNumDigits)
static int parseUint(unsigned int *result, bsl::string_view *remainder, const bsl::string_view &inputString, int base=10)
static int parseUint(unsigned int *result, const bsl::string_view &inputString, int base=10)
Definition bdlb_numericparseutil.h:600
static int characterToDigit(char character, int base)
static int parseInt64(bsls::Types::Int64 *result, bsl::string_view *remainder, const bsl::string_view &inputString, int base=10)
static int parseInt(int *result, bsl::string_view *remainder, const bsl::string_view &inputString, int base=10)
static int parseShort(short *result, const bsl::string_view &inputString, int base=10)
Definition bdlb_numericparseutil.h:589
static int parseUnsignedInteger(bsls::Types::Uint64 *result, bsl::string_view *remainder, const bsl::string_view &inputString, int base, const bsls::Types::Uint64 maxValue)
static int parseSignedInteger(bsls::Types::Int64 *result, const bsl::string_view &inputString, int base, const bsls::Types::Int64 minValue, const bsls::Types::Int64 maxValue)
Definition bdlb_numericparseutil.h:633
static int parseSignedInteger(bsls::Types::Int64 *result, bsl::string_view *remainder, const bsl::string_view &inputString, int base, const bsls::Types::Int64 minValue, const bsls::Types::Int64 maxValue)
unsigned long long Uint64
Definition bsls_types.h:139
long long Int64
Definition bsls_types.h:134