userver: utils::text Namespace Reference
Loading...
Searching...
No Matches
utils::text Namespace Reference

Text utilities. More...

Namespaces

namespace  utf8
 UTF8 text utilities.
 

Functions

std::string Trim (const std::string &str)
 Return trimmed copy of string.
 
std::string Trim (std::string &&str)
 Trim string in-place.
 
std::vector< std::string > Split (std::string_view str, std::string_view separators)
 
std::vector< std::string_view > SplitIntoStringViewVector (std::string_view str, std::string_view separators)
 
std::string Join (const std::vector< std::string > &strs, std::string_view sep)
 Join string.
 
std::string Format (double value, const std::string &locale, int ndigits=0, bool is_fixed=true)
 Return number formatted with specified locale.
 
std::string Format (double value, int ndigits)
 Return number formatted.
 
std::string Format (boost::multiprecision::cpp_dec_float_50 value, int ndigits)
 Return cpp_dec_float_50 formatted.
 
bool StartsWith (std::string_view hay, std::string_view needle) noexcept
 Return true if hay starts with needle, false otherwise.
 
bool EndsWith (std::string_view hay, std::string_view needle) noexcept
 Return true if hay ends with needle, false otherwise.
 
std::string ToLower (std::string_view str, const std::string &locale=kEnLocale)
 Transform letters to lower case.
 
std::string Capitalize (std::string_view str, const std::string &locale)
 Capitalizes the first letter of the str.
 
std::string RemoveQuotes (std::string_view str)
 
bool IsAscii (char ch) noexcept
 Checks whether the character is an ASCII character.
 
bool IsAsciiSpace (char ch) noexcept
 Checks whether the character is a whitespace character in C locale.
 
bool IsAscii (std::string_view text) noexcept
 Checks if text contains only ASCII characters.
 
const std::locale & GetLocale (const std::string &name)
 Returns a locale with the specified name.
 
bool IsUtf8 (std::string_view text) noexcept
 Checks if text is in utf-8 encoding.
 
bool IsPrintable (std::string_view text, bool ascii_only=true) noexcept
 
bool IsCString (std::string_view text) noexcept
 Checks if there are no embedded null ('\0') characters in text.
 
std::string CamelCaseToSnake (std::string_view camel)
 convert CamelCase to snake_case(underscore)
 

Variables

const std::string kEnLocale {"en_US.UTF-8"}
 

Detailed Description

Text utilities.

Function Documentation

◆ IsPrintable()

bool utils::text::IsPrintable ( std::string_view  text,
bool  ascii_only = true 
)
noexcept

Checks text on matching to the following conditions:

  1. text is in utf-8 encoding
  2. text does not contain any of control ascii characters
  3. if flag ascii is true than text contains only ascii characters

◆ RemoveQuotes()

std::string utils::text::RemoveQuotes ( std::string_view  str)

Removes double quotes from front and back of string.

Examples:

RemoveQuotes("\"test\"") // returns "test"
RemoveQuotes("\"test") // returns "\"test"
RemoveQuotes("'test'") // returns "'test'"
RemoveQuotes("\"\"test\"\"") // returns "\"test\""

◆ Split()

std::vector< std::string > utils::text::Split ( std::string_view  str,
std::string_view  separators 
)

Split string by separators

std::string input = "1,22#333";
ASSERT_EQ(tokens.size(), 3);
EXPECT_EQ(tokens[0], "1");
EXPECT_EQ(tokens[1], "22");
EXPECT_EQ(tokens[2], "333");

◆ SplitIntoStringViewVector()

std::vector< std::string_view > utils::text::SplitIntoStringViewVector ( std::string_view  str,
std::string_view  separators 
)

Split string by separators and return a non-owning container of chunks.

Warning
Initial str should outlive the result of the function
std::string input = "1,22#333";
std::vector<std::string_view> tokens =
ASSERT_EQ(tokens.size(), 3);
EXPECT_EQ(tokens[0], "1");
EXPECT_EQ(tokens[1], "22");
EXPECT_EQ(tokens[2], "333");

Variable Documentation

◆ kEnLocale

const std::string utils::text::kEnLocale {"en_US.UTF-8"}
inline

Definition at line 18 of file text.hpp.