trim¶
- group turbo_strings_trim
Functions
-
inline bool consume_prefix(std::string_view *str, std::string_view expected)¶
Strips the
expectedprefix, if found, from the start ofstr.If the operation succeeded,
trueis returned. If not,falseis returned andstris not modified. Example:std::string_view input("abc"); EXPECT_TRUE(turbo::consume_prefix(&input, "a")); EXPECT_EQ(input, "bc");
- Parameters:
str – The string to strip from. After the function returns, this will be the remaining string after the prefix is stripped.
expected – The prefix to strip.
- Returns:
true if the prefix is stripped successfully, false otherwise.
-
inline bool consume_suffix(std::string_view *str, std::string_view expected)¶
Strips the
expectedsuffix, if found, from the end ofstr.If the operation succeeded,
trueis returned. If not,falseis returned andstris not modified. Example:std::string_view input("abcdef"); EXPECT_TRUE(turbo::consume_suffix(&input, "def")); EXPECT_EQ(input, "abc");
- Parameters:
str – The string to strip from. After the function returns, this will be the remaining string after the suffix is stripped.
expected – The suffix to strip.
- Returns:
true if the suffix is stripped successfully, false otherwise.
-
inline std::string_view strip_prefix(std::string_view str, std::string_view prefix)¶
Returns a view into the input string
strwith the givenprefixremoved, but leaving the original string intact.If the prefix does not match at the start of the string, returns the original string instead. Example:
std::string_view input("abc"); EXPECT_EQ(turbo::strip_prefix(input, "a"), "bc"); EXPECT_EQ(turbo::strip_prefix(input, "b"), "abc");
- Parameters:
str – The string to strip from.
prefix – The prefix to strip.
- Returns:
A view into the input string
strwith the givenprefixremoved.
-
inline std::string_view strip_suffix(std::string_view str, std::string_view suffix)¶
Returns a view into the input string
strwith the givensuffixremoved, but leaving the original string intact.If the suffix does not match at the end of the string, returns the original string instead. Example:
std::string_view input("abcdef"); EXPECT_EQ(turbo::strip_suffix(input, "def"), "abc"); EXPECT_EQ(turbo::strip_suffix(input, "de"), "abcdef");
- Parameters:
str – The string to strip from.
suffix – The suffix to strip.
- Returns:
A view into the input string
strwith the givensuffixremoved.
-
template<typename Pred = by_white_space>
inline std::string_view trim_left(std::string_view str, Pred pred = Pred())¶ trim_left() removes whitespace from the beginning of the given string.
pred defaults to by_white_space. Example:
std::string_view input("\t abc"); EXPECT_EQ(turbo::trim_left(input), "abc"); EXPECT_EQ(turbo::trim_left(input, turbo::by_any_of("\t a")), "bc");
- Parameters:
str – The string to trim.
pred – The predicate to use to determine if a character should be trimmed. Defaults to by_white_space.
- Returns:
A std::string_view with whitespace stripped from the beginning of the given std::string_view.
-
template<typename String, typename Pred = by_white_space>
inline std::enable_if<turbo::is_string_type<String>::value>::type trim_left(String *str, Pred pred = Pred())¶ trim_left() removes specified characters that match the predicate from the beginning of the given string.
pred defaults to by_white_space. Example:
std::string input("\t abc"); turbo::trim_left(&input); EXPECT_EQ(input, "abc"); std::string input2("\t abc"); turbo::trim_left(&input2, turbo::by_any_of("\t a")); EXPECT_EQ(input2, "bc");
- Attention
This function will modify the given string.
- Parameters:
str – The string to trim.
pred – The predicate to use to determine if a character should be trimmed. Defaults to by_white_space.
-
template<typename Pred = by_white_space>
inline std::string_view trim_right(std::string_view str, Pred pred = Pred())¶ trim_right() removes whitespace from the end of the given string.
pred defaults to by_white_space. Example:
std::string_view input("abc \t"); EXPECT_EQ(turbo::trim_right(input), "abc"); EXPECT_EQ(turbo::trim_right(input, turbo::by_any_of("\t a")), "abc");
- Parameters:
str – The string to trim.
pred – The predicate to use to determine if a character should be trimmed. Defaults to by_white_space.
- Returns:
A std::string_view with whitespace stripped from the end of the given std::string_view.
-
template<typename String, typename Pred = by_white_space>
inline std::enable_if<turbo::is_string_type<String>::value>::type trim_right(String *str, Pred pred = Pred())¶ trim_right() removes specified characters that match the predicate from the end of the given string.
pred defaults to by_white_space. Example:
std::string input("abc \t"); turbo::trim_right(&input); EXPECT_EQ(input, "abc"); std::string input2("abc \t"); turbo::trim_right(&input2, turbo::by_any_of("\t a")); EXPECT_EQ(input2, "abc");
- Attention
This function will modify the given string.
- Parameters:
str – The string to trim.
pred – The predicate to use to determine if a character should be trimmed. Defaults to by_white_space.
-
template<typename Pred = by_white_space>
inline std::string_view trim_all(std::string_view str, Pred pred = Pred())¶ trim_all() removes specified characters that match the predicate from both ends of the given string.
pred defaults to by_white_space. Example:
std::string_view input(" \t abc \t"); EXPECT_EQ(turbo::trim_all(input), "abc"); EXPECT_EQ(turbo::trim_all(input, turbo::by_any_of("\t a")), "bc");
- Parameters:
str – The string to trim.
pred – The predicate to use to determine if a character should be trimmed. Defaults to by_white_space.
- Returns:
A std::string_view with whitespace stripped from both ends of the given std::string_view.
-
template<typename String, typename Pred = by_white_space>
inline std::enable_if<turbo::is_string_type<String>::value>::type trim_all(String *str, Pred pred = Pred())¶ trim_all() removes specified characters that match the predicate from both ends of the given string.
pred defaults to by_white_space. Example:
std::string input(" \t abc \t"); turbo::trim_all(&input); EXPECT_EQ(input, "abc"); std::string input2(" \t abc \t"); turbo::trim_all(&input2, turbo::by_any_of("\t a")); EXPECT_EQ(input2, "bc");
- Attention
This function will modify the given string.
- Parameters:
str – The string to trim.
pred – The predicate to use to determine if a character should be trimmed. Defaults to by_white_space.
-
template<typename String>
std::enable_if<turbo::is_string_type<String>::value>::type trim_complete(String*)¶ trim_complete() removes leading, trailing, and consecutive internal whitespace.
Example:
std::string input(" \t ab c \t"); turbo::trim_complete(&input); EXPECT_EQ(input, "abc");
- Attention
This function will modify the given string.
- Parameters:
str – The string to trim.
-
struct by_any_of¶
- #include <str_trim.h>
operator()() is used to check if a character is in the given string.
-
struct by_white_space¶
- #include <str_trim.h>
operator()() is used to check if a character is a whitespace.
-
inline bool consume_prefix(std::string_view *str, std::string_view expected)¶