Bitcoin
Functions | Variables
strencodings.cpp File Reference
#include <util/strencodings.h>
#include <tinyformat.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <limits>

Functions

std::string SanitizeString (const std::string &str, int rule)
 
signed char HexDigit (char c)
 
bool IsHex (const std::string &str)
 
bool IsHexNumber (const std::string &str)
 
std::vector< unsigned char > ParseHex (const char *psz)
 
std::vector< unsigned char > ParseHex (const std::string &str)
 
void SplitHostPort (std::string in, int &portOut, std::string &hostOut)
 
std::string EncodeBase64 (const unsigned char *pch, size_t len)
 
std::string EncodeBase64 (const std::string &str)
 
std::vector< unsigned char > DecodeBase64 (const char *p, bool *pf_invalid)
 
std::string DecodeBase64 (const std::string &str, bool *pf_invalid)
 
std::string EncodeBase32 (const unsigned char *pch, size_t len)
 
std::string EncodeBase32 (const std::string &str)
 
std::vector< unsigned char > DecodeBase32 (const char *p, bool *pf_invalid)
 
std::string DecodeBase32 (const std::string &str, bool *pf_invalid)
 
static NODISCARD bool ParsePrechecks (const std::string &str)
 
bool ParseInt32 (const std::string &str, int32_t *out)
 
bool ParseInt64 (const std::string &str, int64_t *out)
 
bool ParseUInt32 (const std::string &str, uint32_t *out)
 
bool ParseUInt64 (const std::string &str, uint64_t *out)
 
bool ParseDouble (const std::string &str, double *out)
 
std::string FormatParagraph (const std::string &in, size_t width, size_t indent)
 
std::string i64tostr (int64_t n)
 
std::string itostr (int n)
 
int64_t atoi64 (const char *psz)
 
int64_t atoi64 (const std::string &str)
 
int atoi (const std::string &str)
 
static bool ProcessMantissaDigit (char ch, int64_t &mantissa, int &mantissa_tzeros)
 
bool ParseFixedPoint (const std::string &val, int decimals, int64_t *amount_out)
 
void Downcase (std::string &str)
 
std::string Capitalize (std::string str)
 

Variables

static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
 
static const std::string SAFE_CHARS []
 
const signed char p_util_hexdigit [256]
 
static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL
 

Function Documentation

◆ atoi()

int atoi ( const std::string &  str)

◆ atoi64() [1/2]

int64_t atoi64 ( const char *  psz)

◆ atoi64() [2/2]

int64_t atoi64 ( const std::string &  str)

◆ Capitalize()

std::string Capitalize ( std::string  str)

Capitalizes the first character of the given string. This function is locale independent. It only capitalizes the first character of the argument if it has an uppercase equivalent in the standard 7-bit ASCII range.

Parameters
[in]strthe string to capitalize.
Returns
string with the first letter capitalized.

◆ DecodeBase32() [1/2]

std::vector<unsigned char> DecodeBase32 ( const char *  p,
bool *  pf_invalid 
)

◆ DecodeBase32() [2/2]

std::string DecodeBase32 ( const std::string &  str,
bool *  pf_invalid 
)

◆ DecodeBase64() [1/2]

std::vector<unsigned char> DecodeBase64 ( const char *  p,
bool *  pf_invalid 
)

◆ DecodeBase64() [2/2]

std::string DecodeBase64 ( const std::string &  str,
bool *  pf_invalid 
)

◆ Downcase()

void Downcase ( std::string &  str)

Converts the given string to its lowercase equivalent. This function is locale independent. It only converts uppercase characters in the standard 7-bit ASCII range.

Parameters
[in,out]strthe string to convert to lowercase.

◆ EncodeBase32() [1/2]

std::string EncodeBase32 ( const unsigned char *  pch,
size_t  len 
)

◆ EncodeBase32() [2/2]

std::string EncodeBase32 ( const std::string &  str)

◆ EncodeBase64() [1/2]

std::string EncodeBase64 ( const unsigned char *  pch,
size_t  len 
)

◆ EncodeBase64() [2/2]

std::string EncodeBase64 ( const std::string &  str)

◆ FormatParagraph()

std::string FormatParagraph ( const std::string &  in,
size_t  width = 79,
size_t  indent = 0 
)

Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.

◆ HexDigit()

signed char HexDigit ( char  c)

◆ i64tostr()

std::string i64tostr ( int64_t  n)

◆ IsHex()

bool IsHex ( const std::string &  str)

◆ IsHexNumber()

bool IsHexNumber ( const std::string &  str)

Return true if the string is a hex number, optionally prefixed with "0x"

◆ itostr()

std::string itostr ( int  n)

◆ ParseDouble()

bool ParseDouble ( const std::string &  str,
double *  out 
)

Convert string to double with strict parse error feedback.

Returns
true if the entire string could be parsed as valid double, false if not the entire string could be parsed or when overflow or underflow occurred.

◆ ParseFixedPoint()

bool ParseFixedPoint ( const std::string &  val,
int  decimals,
int64_t amount_out 
)

Parse number as fixed point according to JSON number syntax. See http://json.org/number.gif

Returns
true on success, false on error.
Note
The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.

◆ ParseHex() [1/2]

std::vector<unsigned char> ParseHex ( const char *  psz)

◆ ParseHex() [2/2]

std::vector<unsigned char> ParseHex ( const std::string &  str)

◆ ParseInt32()

bool ParseInt32 ( const std::string &  str,
int32_t out 
)

Convert string to signed 32-bit integer with strict parse error feedback.

Returns
true if the entire string could be parsed as valid integer, false if not the entire string could be parsed or when overflow or underflow occurred.

◆ ParseInt64()

bool ParseInt64 ( const std::string &  str,
int64_t out 
)

Convert string to signed 64-bit integer with strict parse error feedback.

Returns
true if the entire string could be parsed as valid integer, false if not the entire string could be parsed or when overflow or underflow occurred.

◆ ParsePrechecks()

static NODISCARD bool ParsePrechecks ( const std::string &  str)
static

◆ ParseUInt32()

bool ParseUInt32 ( const std::string &  str,
uint32_t out 
)

Convert decimal string to unsigned 32-bit integer with strict parse error feedback.

Returns
true if the entire string could be parsed as valid integer, false if not the entire string could be parsed or when overflow or underflow occurred.

◆ ParseUInt64()

bool ParseUInt64 ( const std::string &  str,
uint64_t out 
)

Convert decimal string to unsigned 64-bit integer with strict parse error feedback.

Returns
true if the entire string could be parsed as valid integer, false if not the entire string could be parsed or when overflow or underflow occurred.

◆ ProcessMantissaDigit()

static bool ProcessMantissaDigit ( char  ch,
int64_t mantissa,
int &  mantissa_tzeros 
)
inlinestatic

Helper function for ParseFixedPoint

◆ SanitizeString()

std::string SanitizeString ( const std::string &  str,
int  rule = SAFE_CHARS_DEFAULT 
)

Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email addresses, but avoid anything even possibly remotely dangerous like & or >

Parameters
[in]strThe string to sanitize
[in]ruleThe set of safe chars to choose (default: least restrictive)
Returns
A new string without unsafe chars

◆ SplitHostPort()

void SplitHostPort ( std::string  in,
int &  portOut,
std::string &  hostOut 
)

Variable Documentation

◆ CHARS_ALPHA_NUM

const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
static

◆ p_util_hexdigit

const signed char p_util_hexdigit[256]
Initial value:
=
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, }

◆ SAFE_CHARS

const std::string SAFE_CHARS[]
static
Initial value:
=
{
CHARS_ALPHA_NUM + " .,;-_/:?@()",
CHARS_ALPHA_NUM + " .,;-_?@",
CHARS_ALPHA_NUM + ".-_",
CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%",
}
static const std::string CHARS_ALPHA_NUM
Definition: strencodings.cpp:16

◆ UPPER_BOUND

const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL
static

Upper bound for mantissa. 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer. Larger integers cannot consist of arbitrary combinations of 0-9:

999999999999999999 1^18-1 9223372036854775807 (1<<63)-1 (max int64_t) 9999999999999999999 1^19-1 (would overflow)