BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdljsn::NumberUtil Struct Reference

#include <bdljsn_numberutil.h>

Detailed Description

This struct provides a namespace for a suite of functions that convert between a JSON formated numeric value and various numerical types. The valid syntax for a JSON formatted numeric value is spelled out in https://www.rfc-editor.org/rfc/rfc8259#section-6.

See bdljsn_numberutil

Public Types

enum  { k_OVERFLOW = -1 , k_UNDERFLOW = -2 , k_NOT_INTEGRAL = -3 , k_INEXACT = -4 }
 
typedef bsls::Types::Uint64 Uint64
 
typedef bsls::Types::Int64 Int64
 

Static Public Member Functions

static bool isIntegralNumber (const bsl::string_view &value)
 
static bool isValidNumber (const bsl::string_view &value)
 
static bdldfp::Decimal64 asDecimal64 (const bsl::string_view &value)
 
static double asDouble (const bsl::string_view &value)
 
static float asFloat (const bsl::string_view &value)
 
static int asDecimal64Exact (bdldfp::Decimal64 *result, const bsl::string_view &value)
 
static int asShort (short *result, const bsl::string_view &value)
 
static int asUshort (unsigned short *result, const bsl::string_view &value)
 
static int asInt (int *result, const bsl::string_view &value)
 
static int asUint (unsigned int *result, const bsl::string_view &value)
 
static int asLong (long *result, const bsl::string_view &value)
 
static int asUlong (unsigned long *result, const bsl::string_view &value)
 
static int asLonglong (long long *result, const bsl::string_view &value)
 
static int asUlonglong (unsigned long long *result, const bsl::string_view &value)
 
static int asInt64 (Int64 *result, const bsl::string_view &value)
 
static int asUint64 (Uint64 *result, const bsl::string_view &value)
 
template<class t_INTEGER_TYPE >
static int asInteger (t_INTEGER_TYPE *result, const bsl::string_view &value)
 
static void stringify (bsl::string *result, long long value)
 
static void stringify (bsl::string *result, unsigned long long value)
 
static void stringify (bsl::string *result, double value)
 
static void stringify (bsl::string *result, const bdldfp::Decimal64 &value)
 
static bool areEqual (const bsl::string_view &lhs, const bsl::string_view &rhs)
 

Member Typedef Documentation

◆ Int64

◆ Uint64

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
k_OVERFLOW 
k_UNDERFLOW 
k_NOT_INTEGRAL 
k_INEXACT 

Member Function Documentation

◆ areEqual()

static bool bdljsn::NumberUtil::areEqual ( const bsl::string_view lhs,
const bsl::string_view rhs 
)
static

Return true if the specified lhs and rhs represent the same numeric value, and false otherwise. This function will return true for differing representations of the same number (e.g., 1.0, "1", "0.1e+1" are all equivalent) except in cases where the exponent cannot be represented by a 64-bit integer. If the exponent is outside the range of a 64-bit integer, true will be returned if lhs == rhs. For example, comparing "1e18446744073709551615" with itself will return true, but comparing it to "10e18446744073709551614" will return false.

Precondition
The behavior is undefined unless isValidNumber(lhs) and isValidNumber(rhs).

◆ asDecimal64()

static bdldfp::Decimal64 bdljsn::NumberUtil::asDecimal64 ( const bsl::string_view value)
static

Return the closest floating point representation to the specified value. If value is outside the representable range, return +INF or -INF (as appropriate).

Precondition
The behavior is undefined unless isValidNumber(value) is true.

◆ asDecimal64Exact()

static int bdljsn::NumberUtil::asDecimal64Exact ( bdldfp::Decimal64 result,
const bsl::string_view value 
)
static

Load the specified result with the specified value, even if a non-zero status is returned. Return 0 if value can be represented exactly, and return k_INEXACT and load result with the closest approximation of value if value cannot be represented exactly. A value can be represented exactly as a Decimal64 if, for the significand and exponent of value, abs(significand) <= 9,999,999,999,999,999 and -398 <= exponent <= 369.

Precondition
The behavior is undefined unless isValidNumber(value) is true.

◆ asDouble()

double bdljsn::NumberUtil::asDouble ( const bsl::string_view value)
inlinestatic

◆ asFloat()

float bdljsn::NumberUtil::asFloat ( const bsl::string_view value)
inlinestatic

◆ asInt()

int bdljsn::NumberUtil::asInt ( int *  result,
const bsl::string_view value 
)
inlinestatic

◆ asInt64()

int bdljsn::NumberUtil::asInt64 ( Int64 result,
const bsl::string_view value 
)
inlinestatic

◆ asInteger()

template<class t_INTEGER_TYPE >
int bdljsn::NumberUtil::asInteger ( t_INTEGER_TYPE *  result,
const bsl::string_view value 
)
static

Load into the specified result (of the template parameter type t_INTEGER_TYPE) with the specified value, even if a non-zero status is returned (truncating fractional digits if necessary). Return 0 on success, k_OVERFLOW if value is larger than can be represented by result, k_UNDERFLOW if value is smaller than can be represented by result, and k_NOT_INTEGRAL if value is not an integral number (i.e., there is a fractional part). For underflow, result will be loaded with the minimum representable value, for overflow, result will be loaded with the maximum representable value, for non-integral values result will be loaded with the integer part of value (truncating the value to the nearest integer). If the result is not an integer and also either overflows or underflows, it is treated as an overflow or underflow (respectively). The (template parameter) t_INTEGER_TYPE shall be either a signed or unsigned integer type (that is not bool) where sizeof(t_INTEGER_TYPE) <= 8.

Precondition
The behavior is undefined unless isValidNumber(value) is true.
Note
Note that this operation will correctly handle exponents (e.g., a value of "0.00000000000000000001e20" will produce a result of 1).

◆ asLong()

int bdljsn::NumberUtil::asLong ( long *  result,
const bsl::string_view value 
)
inlinestatic

◆ asLonglong()

int bdljsn::NumberUtil::asLonglong ( long long *  result,
const bsl::string_view value 
)
inlinestatic

◆ asShort()

int bdljsn::NumberUtil::asShort ( short *  result,
const bsl::string_view value 
)
inlinestatic

Load the specified result with the specified value, even if a non-zero status is returned (truncating fractional digits if necessary). Return 0 on success, k_OVERFLOW if value is larger than can be represented by result, k_UNDERFLOW if value is smaller than can be represented by result, and k_NOT_INTEGRAL if value is not an integral number (i.e., there is a fractional part). For underflow, result will be loaded with the minimum representable value, for overflow, result will be loaded with the maximum representable value, for non-integral values result will be loaded with the integer part of value (truncating the fractional part of value).

Precondition
The behavior is undefined unless isValidNumber(value) is true.
Note
Note that this operation will correctly handle exponents (e.g., a value of "0.00000000000000000001e20" will produce a result of 1).

◆ asUint()

int bdljsn::NumberUtil::asUint ( unsigned int *  result,
const bsl::string_view value 
)
inlinestatic

◆ asUint64()

static int bdljsn::NumberUtil::asUint64 ( Uint64 result,
const bsl::string_view value 
)
static

◆ asUlong()

int bdljsn::NumberUtil::asUlong ( unsigned long *  result,
const bsl::string_view value 
)
inlinestatic

◆ asUlonglong()

int bdljsn::NumberUtil::asUlonglong ( unsigned long long *  result,
const bsl::string_view value 
)
inlinestatic

◆ asUshort()

int bdljsn::NumberUtil::asUshort ( unsigned short *  result,
const bsl::string_view value 
)
inlinestatic

◆ isIntegralNumber()

static bool bdljsn::NumberUtil::isIntegralNumber ( const bsl::string_view value)
static

Return true if the specified value is a valid integral JSON number.

Note
Note that this function may return true even if value cannot be represented in a fundamental integral type.
Precondition
The behavior is undefined unless isValidNumber(value) is true.

◆ isValidNumber()

static bool bdljsn::NumberUtil::isValidNumber ( const bsl::string_view value)
static

Return true if the specified value is a valid JSON number.

Note
Note that this function may return true even if value cannot be represented in any particular number type.

◆ stringify() [1/4]

static void bdljsn::NumberUtil::stringify ( bsl::string result,
const bdldfp::Decimal64 value 
)
static

◆ stringify() [2/4]

static void bdljsn::NumberUtil::stringify ( bsl::string result,
double  value 
)
static

◆ stringify() [3/4]

static void bdljsn::NumberUtil::stringify ( bsl::string result,
long long  value 
)
static

Load into the specified result a string representation of specified numerical value.

◆ stringify() [4/4]

static void bdljsn::NumberUtil::stringify ( bsl::string result,
unsigned long long  value 
)
static

The documentation for this struct was generated from the following file: