The Sparta Modeling Framework
Loading...
Searching...
No Matches
SmartLexicalCast.hpp File Reference

Smart lexical casting supporting prefixes, separator ignoring, and suffixes. More...

#include <iostream>
#include <utility>
#include <memory>
#include <boost/lexical_cast.hpp>
#include "sparta/utils/LexicalCast.hpp"
#include "sparta/utils/SpartaException.hpp"
#include "sparta/utils/Utils.hpp"
#include "sparta/utils/ValidValue.hpp"

Go to the source code of this file.

Classes

struct  sparta::utils::Modifier
 Modifier instance - Associates some suffix strings (e.g. "b") with a semantic (e.g. multiply by one billion) More...
 
struct  sparta::utils::RadixPrefix
 Prefix instance - Associates some prefix strings (e.g. "0x") with a radix and a set of allowed digit characters for parsing a string following this radix. More...
 

Namespaces

namespace  sparta
 Macros for handling exponential backoff.
 

Functions

template<typename T >
sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion=true, bool allow_prefix=true)
 Performs a lexical cast on a string while attempting to interpret radix prefixes (.
 
std::string sparta::utils::parseNumericString (const std::string &s, size_t pos, const char *digits, size_t &after_numeric)
 Parse and return a numeric string from after a certain position in an input string.
 
template<>
uint64_t sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion, bool allow_prefix)
 
template<>
uint32_t sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion, bool allow_prefix)
 
template<>
uint16_t sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion, bool allow_prefix)
 
template<>
uint8_t sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion, bool allow_prefix)
 
template<>
int64_t sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion, bool allow_prefix)
 
template<>
int32_t sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion, bool allow_prefix)
 
template<>
int16_t sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion, bool allow_prefix)
 
template<>
int8_t sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion, bool allow_prefix)
 
template<>
double sparta::utils::smartLexicalCast (const std::string &s, size_t &end_pos, bool allow_recursion, bool allow_prefix)
 

Variables

static const uint32_t sparta::utils::DEFAULT_RADIX = 10
 
static const std::vector< RadixPrefixsparta::utils::prefixes
 Radixes supported.
 
static const std::vector< Modifiersparta::utils::suffixes
 Suffixes supported.
 
static const char * sparta::utils::WHITESPACE = " _,\t\n"
 All whitespace characters allowed.
 
static const char * sparta::utils::DECIMAL_DIGITS = "0123456789."
 All decimal (base 10) digits allowed and '.'.
 

Detailed Description

Smart lexical casting supporting prefixes, separator ignoring, and suffixes.

Definition in file SmartLexicalCast.hpp.

Function Documentation

◆ parseNumericString()

std::string sparta::utils::parseNumericString ( const std::string &  s,
size_t  pos,
const char *  digits,
size_t &  after_numeric 
)
inline

Parse and return a numeric string from after a certain position in an input string.

Parameters
sInput string
posPosition in input string to begin parsing at
digitsAllowed digits in numeric string (in addition to WHITESPACE)
after_numeric[out] Position of string s after numeric string is extracted
Returns
Parsed numeric string (if possible) Returns "" if not.

Definition at line 185 of file SmartLexicalCast.hpp.

Here is the call graph for this function:

◆ smartLexicalCast() [1/10]

template<>
uint64_t sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion,
bool  allow_prefix 
)
inline

Definition at line 212 of file SmartLexicalCast.hpp.

◆ smartLexicalCast() [2/10]

template<>
uint32_t sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion,
bool  allow_prefix 
)
inline

Definition at line 395 of file SmartLexicalCast.hpp.

◆ smartLexicalCast() [3/10]

template<>
uint16_t sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion,
bool  allow_prefix 
)
inline

Definition at line 408 of file SmartLexicalCast.hpp.

◆ smartLexicalCast() [4/10]

template<>
uint8_t sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion,
bool  allow_prefix 
)
inline

Definition at line 421 of file SmartLexicalCast.hpp.

◆ smartLexicalCast() [5/10]

template<>
int64_t sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion,
bool  allow_prefix 
)
inline

Definition at line 434 of file SmartLexicalCast.hpp.

◆ smartLexicalCast() [6/10]

template<>
int32_t sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion,
bool  allow_prefix 
)
inline

Definition at line 464 of file SmartLexicalCast.hpp.

◆ smartLexicalCast() [7/10]

template<>
int16_t sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion,
bool  allow_prefix 
)
inline

Definition at line 478 of file SmartLexicalCast.hpp.

◆ smartLexicalCast() [8/10]

template<>
int8_t sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion,
bool  allow_prefix 
)
inline

Definition at line 492 of file SmartLexicalCast.hpp.

◆ smartLexicalCast() [9/10]

template<>
double sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion,
bool  allow_prefix 
)
inline

Definition at line 506 of file SmartLexicalCast.hpp.

◆ smartLexicalCast() [10/10]

template<typename T >
T sparta::utils::smartLexicalCast ( const std::string &  s,
size_t &  end_pos,
bool  allow_recursion = true,
bool  allow_prefix = true 
)
inline

Performs a lexical cast on a string while attempting to interpret radix prefixes (.

See also
prefixes) and multiplying suffixes (
suffixes)
Parameters
sString to parse. String must contain no extra non-witespace characters where "whitespace" is defined as any character in the WHITESPACE string.
end_pos[out] Position of string after parsing. All characters after point will be whitespace. If all characters of s are consumed, this will be std::string::npos
allow_recursionAllow recursion during parsing. This allows stringing together values to add together in the way the large values are spoken such as 1b5k (for one billion, five thousand). Note that a prefix on the initial value does not apply to any subsequent values encountered. For example, 0xck22 is (hex 0xc) * (decimal 1000) + (decimal 22) When recursing intenally, this method is always reentered with allow_prefix=false, meaning values parsed after the first are NOT allowed to have prefixes.
allow_prefixAllow parsing of prefixes. If false, prefixes are not allowed in s. this is typiclly useful when recursively parsing the end of the string because of ambiguity between certain suffixes (e.g. 50b and 0b111 could not be joined together because of the '0b').

Example:

// With allow_recursion=true && allow_prefix=true
5b1 => five billion
5t10k2 => five trillion, 10 thousdand, and 2
0xcccb1 => hex value 0xcccb1
0xcccb1g50 => (hex value 0xcccb1 * one billion) + decimal 50
// Always illegal
5t0xc00 => ERROR: Prefixes are not allowed on trailing values
// With allow_recursion=false && allow_prefix=true
5b => five billion
0x500 => hex value 0x500
5b100 => ERROR: Recursion disabled
// With allow_recursion=true && allow_prefix=false
5b100 => five billion, one hundred
0x5b2k => ERROR: prefixes disabled

// With period (and allow_recursion) 0.5M => 500,000 .5m => 500,000 .5m2k => 502,000 .5m.2k => 500,200

Todo:
To resolve prefix/suffix ambiguity and allow stringing together multiple value with prefixes support could be added for '+' as a disamgiuator (e.g. 50+0b111)

Definition at line 166 of file SmartLexicalCast.hpp.

Here is the call graph for this function:

Variable Documentation

◆ DECIMAL_DIGITS

const char* sparta::utils::DECIMAL_DIGITS = "0123456789."
static

All decimal (base 10) digits allowed and '.'.

Definition at line 116 of file SmartLexicalCast.hpp.

◆ DEFAULT_RADIX

const uint32_t sparta::utils::DEFAULT_RADIX = 10
static

Definition at line 64 of file SmartLexicalCast.hpp.

◆ prefixes

const std::vector<RadixPrefix> sparta::utils::prefixes
static
Initial value:
= {
{{"0x", "0X"}, 16, "0123456789abcdefABCDEF"},
{{"0b", "0B"}, 2, "01"},
{{"0"}, 8, "01234567"}
}

Radixes supported.

Note
The digits field will have precidence over any suffix after the number. Specifically, hex digits b or B will be treated as a hex digit and NEVER as a suffix (billion). If the billion suffix were desired, the g or G suffix would need to be used

Definition at line 73 of file SmartLexicalCast.hpp.

◆ suffixes

const std::vector<Modifier> sparta::utils::suffixes
static
Initial value:
= {
{{"ki", "Ki",
"kI", "KI"}, 1_u64<<10},
{{"mi", "Mi",
"mI", "MI"}, 1_u64<<20},
{{"gi", "Gi", "bi", "Bi",
"gI", "GI", "bI", "BI"}, 1_u64<<30},
{{"ti", "Ti",
"tI", "TI"}, 1_u64<<40},
{{"pi", "Pi",
"pI", "PI"}, 1_u64<<50},
{{"n", "N"}, 1},
{{"k", "K"}, 1000},
{{"m", "M"}, 1000000},
{{"g", "G", "b", "B"}, 1000000000},
{{"t", "T"}, 1000000000000},
{{"p", "P"}, 1000000000000000}
}

Suffixes supported.

Definition at line 82 of file SmartLexicalCast.hpp.

◆ WHITESPACE

const char* sparta::utils::WHITESPACE = " _,\t\n"
static

All whitespace characters allowed.

Definition at line 111 of file SmartLexicalCast.hpp.