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

Detailed Description

Text utilities.

Namespaces

namespace  utf8
 UTF8 text utilities.
 

Enumerations

enum class  SplitFlags {
  kNone = 0 ,
  kCompressAdjacentSeparators = 1 << 0
}
 

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, SplitFlags split_flags=SplitFlags::kCompressAdjacentSeparators)
 
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, int ndigits)
 Return number formatted.
 
constexpr bool StartsWith (std::string_view hay, std::string_view needle) noexcept
 Return true if hay starts with needle, false otherwise.
 
constexpr bool EndsWith (std::string_view hay, std::string_view needle) noexcept
 Return true if hay ends with needle, false otherwise.
 
bool ICaseStartsWith (std::string_view hay, std::string_view needle) noexcept
 Case insensitive (ASCII only) variant of StartsWith()
 
bool ICaseEndsWith (std::string_view hay, std::string_view needle) noexcept
 Case insensitive (ASCII only) variant of EndsWith()
 
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.
 
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)
 
std::string Format (double value, const std::string &locale, int ndigits=0, bool is_fixed=true)
 Return number formatted with specified locale.
 
std::string ToLower (std::string_view str, const std::string &locale=kEnLocale)
 Transform letters to lower case.
 
std::string ToUpper (std::string_view str, const std::string &locale=kEnLocale)
 Transform letters to upper case.
 
std::string Capitalize (std::string_view str, const std::string &locale)
 Capitalizes the first letter of the str.
 
const std::locale & GetLocale (const std::string &name)
 Returns a locale with the specified name.
 

Variables

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

Enumeration Type Documentation

◆ SplitFlags

enum class utils::text::SplitFlags
strong

Definition at line 22 of file text_light.hpp.

Function Documentation

◆ EndsWith()

constexpr bool utils::text::EndsWith ( std::string_view hay,
std::string_view needle )
constexprnoexcept

Return true if hay ends with needle, false otherwise.

Definition at line 55 of file text_light.hpp.

◆ 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,
SplitFlags split_flags = SplitFlags::kCompressAdjacentSeparators )

Split string by separators

std::string input = "1,22#333";
auto tokens = utils::text::Split(input, "#,");
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 = utils::text::SplitIntoStringViewVector(input, "#,");
ASSERT_EQ(tokens.size(), 3);
EXPECT_EQ(tokens[0], "1");
EXPECT_EQ(tokens[1], "22");
EXPECT_EQ(tokens[2], "333");

◆ StartsWith()

constexpr bool utils::text::StartsWith ( std::string_view hay,
std::string_view needle )
constexprnoexcept

Return true if hay starts with needle, false otherwise.

Examples
samples/multipart_service/service.cpp.

Definition at line 50 of file text_light.hpp.

Variable Documentation

◆ kEnLocale

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

Definition at line 15 of file text.hpp.