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.
|
namespace | sparta |
| Macros for handling exponential backoff.
|
|
|
template<typename T > |
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) |
|
Smart lexical casting supporting prefixes, separator ignoring, and suffixes.
Definition in file SmartLexicalCast.hpp.
◆ 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
-
s | Input string |
pos | Position in input string to begin parsing at |
digits | Allowed 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.
◆ smartLexicalCast() [1/10]
template<>
uint64_t sparta::utils::smartLexicalCast |
( |
const std::string & |
s, |
|
|
size_t & |
end_pos, |
|
|
bool |
allow_recursion, |
|
|
bool |
allow_prefix |
|
) |
| |
|
inline |
◆ smartLexicalCast() [2/10]
template<>
uint32_t sparta::utils::smartLexicalCast |
( |
const std::string & |
s, |
|
|
size_t & |
end_pos, |
|
|
bool |
allow_recursion, |
|
|
bool |
allow_prefix |
|
) |
| |
|
inline |
◆ smartLexicalCast() [3/10]
template<>
uint16_t sparta::utils::smartLexicalCast |
( |
const std::string & |
s, |
|
|
size_t & |
end_pos, |
|
|
bool |
allow_recursion, |
|
|
bool |
allow_prefix |
|
) |
| |
|
inline |
◆ smartLexicalCast() [4/10]
template<>
uint8_t sparta::utils::smartLexicalCast |
( |
const std::string & |
s, |
|
|
size_t & |
end_pos, |
|
|
bool |
allow_recursion, |
|
|
bool |
allow_prefix |
|
) |
| |
|
inline |
◆ smartLexicalCast() [5/10]
template<>
int64_t sparta::utils::smartLexicalCast |
( |
const std::string & |
s, |
|
|
size_t & |
end_pos, |
|
|
bool |
allow_recursion, |
|
|
bool |
allow_prefix |
|
) |
| |
|
inline |
◆ smartLexicalCast() [6/10]
template<>
int32_t sparta::utils::smartLexicalCast |
( |
const std::string & |
s, |
|
|
size_t & |
end_pos, |
|
|
bool |
allow_recursion, |
|
|
bool |
allow_prefix |
|
) |
| |
|
inline |
◆ smartLexicalCast() [7/10]
template<>
int16_t sparta::utils::smartLexicalCast |
( |
const std::string & |
s, |
|
|
size_t & |
end_pos, |
|
|
bool |
allow_recursion, |
|
|
bool |
allow_prefix |
|
) |
| |
|
inline |
◆ smartLexicalCast() [8/10]
template<>
int8_t sparta::utils::smartLexicalCast |
( |
const std::string & |
s, |
|
|
size_t & |
end_pos, |
|
|
bool |
allow_recursion, |
|
|
bool |
allow_prefix |
|
) |
| |
|
inline |
◆ smartLexicalCast() [9/10]
template<>
double sparta::utils::smartLexicalCast |
( |
const std::string & |
s, |
|
|
size_t & |
end_pos, |
|
|
bool |
allow_recursion, |
|
|
bool |
allow_prefix |
|
) |
| |
|
inline |
◆ 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
-
s | String 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_recursion | Allow 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_prefix | Allow 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:
5b1 => five billion
5t10k2 => five trillion, 10 thousdand, and 2
0xcccb1 => hex value 0xcccb1
0xcccb1g50 => (hex value 0xcccb1 * one billion) + decimal 50
5t0xc00 => ERROR: Prefixes are not allowed on trailing values
5b => five billion
0x500 => hex value 0x500
5b100 => ERROR: Recursion disabled
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.
◆ DECIMAL_DIGITS
const char* sparta::utils::DECIMAL_DIGITS = "0123456789." |
|
static |
◆ DEFAULT_RADIX
const uint32_t sparta::utils::DEFAULT_RADIX = 10 |
|
static |
◆ 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 |