convert¶
- group turbo_strings_convert
Enums
Functions
-
std::string c_encode(std::string_view src)¶
c_encode escapes a
srcstring using C-style escapes sequences (https://en.cppreference.com/w/cpp/language/escape), escaping other non-printable/non-whitespace bytes as octal sequences (e.g.“\377”). Example:
std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n"; std::string escaped_s = turbo::c_encode(s); EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\010\\t\\n\\013\\014\\r\\n");
- Parameters:
src – [input] the string to escape.
- Returns:
the escaped string.
-
std::string c_hex_encode(std::string_view src)¶
c_hex_encode escapes a
srcstring using C-style escape sequences, escaping other non-printable/non-whitespace bytes as hexadecimal sequences (e.g.“\xFF”).
Example:
std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n"; std::string escaped_s = turbo::c_hex_encode(s); EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\x08\\t\\n\\x0b\\x0c\\r\\n");
- Parameters:
src – [input] the string to escape.
- Returns:
the escaped string.
-
std::string utf8_safe_c_encode(std::string_view src)¶
utf8_safe_c_encode escapes a
srcstring using C-style escape sequences, escaping bytes as octal sequences, and passing through UTF-8 characters without conversion.I.e., when encountering any bytes with their high bit set, this function will not escape those values, whether or not they are valid UTF-8.
- Parameters:
src – [input] the string to escape.
- Returns:
the escaped string.
-
std::string utf8_safe_c_hex_encode(std::string_view src)¶
utf8_safe_c_hex_encode escapes a
srcstring using C-style escape sequences, escaping bytes as hexadecimal sequences, and passing through UTF-8 characters without conversion.I.e., when encountering any bytes with their high bit set, this function will not escape those values, whether or not they are valid UTF-8.
- Parameters:
src – [input] the string to escape.
- Returns:
the escaped string.
-
template<typename String>
std::enable_if<turbo::is_string_type<String>::value>::type base64_encode(std::string_view src, String *dest)¶ base64_encode encodes a
srcstring into a base64-encoded ‘dest’ string with padding characters.This function conforms with RFC 4648 section 4 (base64) and RFC 2045.
- Parameters:
src – [input] the string to encode.
dest – [output] the encoded string.
-
template<typename String>
std::enable_if<turbo::is_string_type<String>::value>::type web_safe_base64_encode(std::string_view src, String *dest)¶ web_safe_base64_encode encodes a
srcstring into a base64-encoded ‘dest’ string without padding characters.This function conforms with RFC 4648 section 5 (base64url).
- Parameters:
src – [input] the string to encode.
dest – [output] the encoded string.
-
template<typename String = std::string>
std::enable_if<turbo::is_string_type<String>::value, String>::type web_safe_base64_encode(std::string_view src)¶ web_safe_base64_encode encodes a
srcstring into a base64-encoded ‘dest’ string without padding characters.This function conforms with RFC 4648 section 5 (base64url).
- Parameters:
src – [input] the string to encode.
dest – [output] the encoded string.
-
template<typename String>
std::enable_if<turbo::is_string_type<String>::value, bool>::type base64_decode(std::string_view src, String *dest)¶ base64_decode decodes a
srcstring encoded in Base64 (RFC 4648 section 4) to its binary equivalent, writing it to adestbuffer, returningtrueon success.If
srccontains invalid characters,destis cleared and returnsfalse. If padding is included (note thatbase64_encode()does produce it), it must be correct. In the padding, ‘=’ and ‘.’ are treated identically.- Parameters:
src – [input] the string to decode.
dest – [output] the decoded string.
- Returns:
true if successful, otherwise false.
-
template<typename String>
std::enable_if<turbo::is_string_type<String>::value, bool>::type web_safe_base64_decode(std::string_view src, String *dest)¶ web_safe_base64_decode decodes a
srcstring encoded in “web safe” Base64 (RFC 4648 section 5) to its binary equivalent, writing it to adestbuffer.If
srccontains invalid characters,destis cleared and returnsfalse. If padding is included (note thatweb_safe_base64_encode()does not produce it), it must be correct. In the padding, ‘=’ and ‘.’ are treated identically.- Parameters:
src – [input] the string to decode.
dest – [output] the decoded string.
- Returns:
true if successful, otherwise false.
-
template<typename String>
std::enable_if<turbo::is_string_type<String>::value>::type hex_to_bytes(std::string_view from, String *dest)¶ hex_to_bytes converts an ASCII hex string into bytes, returning binary data of length
from.size()/2.- Parameters:
from – [input] the string to convert.
dest – [output] the converted string.
-
template<typename String>
std::enable_if<turbo::is_string_type<String>::value>::type bytes_to_hex(std::string_view from, String *dest)¶ bytes_to_hex converts binary data into an ASCII text string, returning a string of size
2*from.size().- Parameters:
from – [input] the string to convert.
dest – [output] the converted string.
-
template<typename int_type>
bool simple_atoi(std::string_view str, int_type *out, int base = 10)¶ Converts the given string (optionally followed or preceded by ASCII whitespace) into an integer value, returning
trueif successful.The string must reflect a base-10 integer whose value falls within the range of the integer type (optionally preceded by a
+or-). If any errors are encountered, this function returnsfalse, leavingoutin an unspecified state. Example:int32_t i; EXPECT_TRUE(turbo::simple_atoi("123", &i, 10)); EXPECT_EQ(i, 123); EXPECT_TRUE(turbo::simple_atoi("123", &i, 16)); EXPECT_EQ(i, 291); EXPECT_TRUE(turbo::simple_atoi("123", &i, 8)); EXPECT_EQ(i, 83); EXPECT_TRUE(turbo::simple_atoi("123", &i, 0)); EXPECT_EQ(i, 123); EXPECT_TRUE(turbo::simple_atoi("-123", &i, 2)); EXPECT_EQ(i, -3); EXPECT_TRUE(turbo::simple_atoi(" 123", &i, 10)); EXPECT_EQ(i, 123);
- Parameters:
str – [input] The string to convert.
out – [output] The converted integer.
base – [input] The base of the string to convert. default is 10. if base is 0, the base is determined by the format in the string. if str begins with “0x” or “0X”, base is 16, else if str begins with “0”, base is 8, else if str begins with “0b” or “0B”, base is 2, else base is 10.
- Returns:
true if the string is converted successfully, false otherwise.
-
bool simple_atof(std::string_view str, float *out)¶
Converts the given string (optionally followed or preceded by ASCII whitespace) into a float, which may be rounded on overflow or underflow, returning
trueif successful.See https://en.cppreference.com/w/c/string/byte/strtof for details about the allowed formats for
str, except simple_atof() is locale-independent and will always use the “C” locale. If any errors are encountered, this function returnsfalse, leavingoutin an unspecified state. Example:float f; EXPECT_TRUE(turbo::simple_atof("123.456", &f)); EXPECT_EQ(f, 123.456f);
- Parameters:
str – [input] The string to convert.
out – [output] The converted float.
- Returns:
true if the string is converted successfully, false otherwise.
-
bool simple_atod(std::string_view str, double *out)¶
Converts the given string (optionally followed or preceded by ASCII whitespace) into a double, which may be rounded on overflow or underflow, returning
trueif successful.See https://en.cppreference.com/w/c/string/byte/strtof for details about the allowed formats for
str, except simple_atod is locale-independent and will always use the “C” locale. If any errors are encountered, this function returnsfalse, leavingoutin an unspecified state. Example:double d; EXPECT_TRUE(turbo::simple_atod("123.456", &d)); EXPECT_EQ(d, 123.456);
- Parameters:
str – [input] The string to convert.
out – [output] The converted double.
- Returns:
true if the string is converted successfully, false otherwise.
-
bool simple_atob(std::string_view str, bool *out)¶
Converts the given string into a boolean, returning
trueif successful.The following case-insensitive strings are interpreted as boolean
true: “true”, “t”, “yes”, “y”, “1”. The following case-insensitive strings are interpreted as booleanfalse: “false”, “f”, “no”, “n”, “0”. If any errors are encountered, this function returnsfalse, leavingoutin an unspecified state. Example:bool b; EXPECT_TRUE(turbo::simple_atob("true", &b)); EXPECT_EQ(b, true); EXPECT_TRUE(turbo::simple_atob("false", &b)); EXPECT_EQ(b, false);
- Parameters:
str – [input] The string to convert.
out – [output] The converted boolean.
- Returns:
true if the string is converted successfully, false otherwise.
-
template<typename int_type>
bool simple_hex_atoi(std::string_view str, int_type *out, int base = 16)¶ Converts a hexadecimal string (optionally followed or preceded by ASCII whitespace) to an integer, returning
trueif successful.Only valid base-16 hexadecimal integers whose value falls within the range of the integer type (optionally preceded by a
+or-) can be converted. A valid hexadecimal value may include both upper and lowercase character symbols, and may optionally include a leading “0x” (or “0X”) number prefix, which is ignored by this function. If any errors are encountered, this function returnsfalse, leavingoutin an unspecified state. Example:int32_t i; EXPECT_TRUE(turbo::simple_hex_atoi("123", &i)); EXPECT_EQ(i, 291);
- Parameters:
str – [input] The string to convert.
out – [output] The converted integer.
base – [input] The base of the string to convert. default is 16.
- Returns:
true if the string is converted successfully, false otherwise.
-
turbo::from_chars_result from_chars(const char *first, const char *last, double &value, chars_format fmt = chars_format::general)¶
from_chars convert string to number.
This function is a workalike compatibilty version of std::from_chars from C++17. Currently this only supports the
doubleandfloattypes.This interface incorporates the proposed resolutions for library issues DR 3080 and DR 3081. If these are adopted with different wording, Turbo’s behavior will change to match the standard. (The behavior most likely to change is for DR 3081, which says what
valuewill be set to in the case of overflow and underflow. Code that wants to avoid possible breaking changes in this area should not depend onvaluewhen the returned from_chars_result indicates a range error.)Searches the range [first, last) for the longest matching pattern beginning at
firstthat represents a floating point number. If one is found, store the result invalue.The matching pattern format is almost the same as that of strtod(), except that (1) C locale is not respected, (2) an initial ‘+’ character in the input range will never be matched, and (3) leading whitespaces are not ignored.
If
fmtis set, it must be one of the enumerator values of the chars_format. (This is despite the fact that chars_format is a bitmask type.) If set toscientific, a matching number must contain an exponent. If set tofixed, then an exponent will never match. (For example, the string “1e5” will be parsed as “1”.) If set tohex, then a hexadecimal float is parsed in the format that strtod() accepts, except that a “0x” prefix is NOT matched. (In particular, inhexmode, the input “0xff” results in the largest matching pattern “0”.)If a matching pattern is found, the function returns {last, std::errc()}. Otherwise, it returns {first, std::errc::invalid_argument}.
If a matching pattern is found, but the value is out of range for the requested type, the function returns {last, std::errc::result_out_of_range}. In this case,
valueis set to the appropriate signed or unsigned infinity, or to the largest finite value representable by the requested type, depending on the sign of the value.Note
use the result of this function should check the status of the operation first. if the status is not ok, the result is invalid.
- Parameters:
first – [input] the first character of the string to convert.
last – [input] one past the last character of the string to convert.
value – [output] the converted value.
fmt – [input] the format of the string to convert.
- Returns:
the result of the operation.
-
struct from_chars_result¶
- #include <charconv.h>
from_chars_result is the result of a string-to-number conversion.
ecwill be set toinvalid_argumentif a well-formed number was not found at the start of the input range,result_out_of_rangeif a well-formed number was found, but it was out of the representable range of the requested type, or to std::errc() otherwise.If a well-formed number was found,
ptris set to one past the sequence of characters that were successfully parsed. If none was found,ptris set to thefirstargument to from_chars.
-
std::string c_encode(std::string_view src)¶