diff options
Diffstat (limited to 'chromium/base/strings/string_util.h')
-rw-r--r-- | chromium/base/strings/string_util.h | 134 |
1 files changed, 26 insertions, 108 deletions
diff --git a/chromium/base/strings/string_util.h b/chromium/base/strings/string_util.h index f9f5e10ade9..e5a0487cff1 100644 --- a/chromium/base/strings/string_util.h +++ b/chromium/base/strings/string_util.h @@ -14,10 +14,12 @@ #include <initializer_list> #include <string> +#include <type_traits> #include <vector> #include "base/base_export.h" #include "base/compiler_specific.h" +#include "base/containers/span.h" #include "base/stl_util.h" #include "base/strings/string16.h" #include "base/strings/string_piece.h" // For implicit conversions. @@ -84,19 +86,17 @@ BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format); // ASCII-specific tolower. The standard library's tolower is locale sensitive, // so we don't want to use it here. -inline char ToLowerASCII(char c) { - return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; -} -inline char16 ToLowerASCII(char16 c) { +template <typename CharT, + typename = std::enable_if_t<std::is_integral<CharT>::value>> +CharT ToLowerASCII(CharT c) { return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; } // ASCII-specific toupper. The standard library's toupper is locale sensitive, // so we don't want to use it here. -inline char ToUpperASCII(char c) { - return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; -} -inline char16 ToUpperASCII(char16 c) { +template <typename CharT, + typename = std::enable_if_t<std::is_integral<CharT>::value>> +CharT ToUpperASCII(CharT c) { return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; } @@ -170,10 +170,10 @@ BASE_EXPORT extern const char kUtf8ByteOrderMark[]; // Removes characters in |remove_chars| from anywhere in |input|. Returns true // if any characters were removed. |remove_chars| must be null-terminated. // NOTE: Safe to use the same variable for both |input| and |output|. -BASE_EXPORT bool RemoveChars(const string16& input, +BASE_EXPORT bool RemoveChars(StringPiece16 input, StringPiece16 remove_chars, string16* output); -BASE_EXPORT bool RemoveChars(const std::string& input, +BASE_EXPORT bool RemoveChars(StringPiece input, StringPiece remove_chars, std::string* output); @@ -182,11 +182,11 @@ BASE_EXPORT bool RemoveChars(const std::string& input, // the |replace_with| string. Returns true if any characters were replaced. // |replace_chars| must be null-terminated. // NOTE: Safe to use the same variable for both |input| and |output|. -BASE_EXPORT bool ReplaceChars(const string16& input, +BASE_EXPORT bool ReplaceChars(StringPiece16 input, StringPiece16 replace_chars, StringPiece16 replace_with, string16* output); -BASE_EXPORT bool ReplaceChars(const std::string& input, +BASE_EXPORT bool ReplaceChars(StringPiece input, StringPiece replace_chars, StringPiece replace_with, std::string* output); @@ -226,69 +226,6 @@ BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, const size_t byte_size, std::string* output); -#if defined(WCHAR_T_IS_UTF16) -// Utility functions to access the underlying string buffer as a wide char -// pointer. -// -// Note: These functions violate strict aliasing when char16 and wchar_t are -// unrelated types. We thus pass -fno-strict-aliasing to the compiler on -// non-Windows platforms [1], and rely on it being off in Clang's CL mode [2]. -// -// [1] https://crrev.com/b9a0976622/build/config/compiler/BUILD.gn#244 -// [2] -// https://github.com/llvm/llvm-project/blob/1e28a66/clang/lib/Driver/ToolChains/Clang.cpp#L3949 -inline wchar_t* as_writable_wcstr(char16* str) { - return reinterpret_cast<wchar_t*>(str); -} - -inline wchar_t* as_writable_wcstr(string16& str) { - return reinterpret_cast<wchar_t*>(data(str)); -} - -inline const wchar_t* as_wcstr(const char16* str) { - return reinterpret_cast<const wchar_t*>(str); -} - -inline const wchar_t* as_wcstr(StringPiece16 str) { - return reinterpret_cast<const wchar_t*>(str.data()); -} - -// Utility functions to access the underlying string buffer as a char16 pointer. -inline char16* as_writable_u16cstr(wchar_t* str) { - return reinterpret_cast<char16*>(str); -} - -inline char16* as_writable_u16cstr(std::wstring& str) { - return reinterpret_cast<char16*>(data(str)); -} - -inline const char16* as_u16cstr(const wchar_t* str) { - return reinterpret_cast<const char16*>(str); -} - -inline const char16* as_u16cstr(WStringPiece str) { - return reinterpret_cast<const char16*>(str.data()); -} - -// Utility functions to convert between base::WStringPiece and -// base::StringPiece16. -inline WStringPiece AsWStringPiece(StringPiece16 str) { - return WStringPiece(as_wcstr(str.data()), str.size()); -} - -inline StringPiece16 AsStringPiece16(WStringPiece str) { - return StringPiece16(as_u16cstr(str.data()), str.size()); -} - -inline std::wstring AsWString(StringPiece16 str) { - return std::wstring(as_wcstr(str.data()), str.size()); -} - -inline string16 AsString16(WStringPiece str) { - return string16(as_u16cstr(str.data()), str.size()); -} -#endif // defined(WCHAR_T_IS_UTF16) - // Trims any whitespace from either end of the input string. // // The StringPiece versions return a substring referencing the input buffer. @@ -315,11 +252,10 @@ BASE_EXPORT StringPiece TrimWhitespaceASCII(StringPiece input, // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace // sequences containing a CR or LF are trimmed. // (3) All other whitespace sequences are converted to single spaces. -BASE_EXPORT string16 CollapseWhitespace( - const string16& text, - bool trim_sequences_with_line_breaks); +BASE_EXPORT string16 CollapseWhitespace(StringPiece16 text, + bool trim_sequences_with_line_breaks); BASE_EXPORT std::string CollapseWhitespaceASCII( - const std::string& text, + StringPiece text, bool trim_sequences_with_line_breaks); // Returns true if |input| is empty or contains only characters found in @@ -347,6 +283,7 @@ BASE_EXPORT bool IsStringUTF8AllowingNoncharacters(StringPiece str); // does not leave early if it is not the case. BASE_EXPORT bool IsStringASCII(StringPiece str); BASE_EXPORT bool IsStringASCII(StringPiece16 str); + #if defined(WCHAR_T_IS_UTF32) BASE_EXPORT bool IsStringASCII(WStringPiece str); #endif @@ -488,8 +425,8 @@ BASE_EXPORT void ReplaceSubstringsAfterOffset( BASE_EXPORT char* WriteInto(std::string* str, size_t length_with_null); BASE_EXPORT char16* WriteInto(string16* str, size_t length_with_null); -// Joins a vector or list of strings into a single string, inserting |separator| -// (which may be empty) in between all elements. +// Joins a list of strings into a single string, inserting |separator| (which +// may be empty) in between all elements. // // Note this is inverse of SplitString()/SplitStringPiece() defined in // string_split.h. @@ -501,13 +438,13 @@ BASE_EXPORT char16* WriteInto(string16* str, size_t length_with_null); // copies of those strings are created until the final join operation. // // Use StrCat (in base/strings/strcat.h) if you don't need a separator. -BASE_EXPORT std::string JoinString(const std::vector<std::string>& parts, +BASE_EXPORT std::string JoinString(span<const std::string> parts, StringPiece separator); -BASE_EXPORT string16 JoinString(const std::vector<string16>& parts, +BASE_EXPORT string16 JoinString(span<const string16> parts, StringPiece16 separator); -BASE_EXPORT std::string JoinString(const std::vector<StringPiece>& parts, +BASE_EXPORT std::string JoinString(span<const StringPiece> parts, StringPiece separator); -BASE_EXPORT string16 JoinString(const std::vector<StringPiece16>& parts, +BASE_EXPORT string16 JoinString(span<const StringPiece16> parts, StringPiece16 separator); // Explicit initializer_list overloads are required to break ambiguity when used // with a literal initializer list (otherwise the compiler would not be able to @@ -521,10 +458,10 @@ BASE_EXPORT string16 JoinString(std::initializer_list<StringPiece16> parts, // Additionally, any number of consecutive '$' characters is replaced by that // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be // NULL. This only allows you to use up to nine replacements. -BASE_EXPORT string16 ReplaceStringPlaceholders( - const string16& format_string, - const std::vector<string16>& subst, - std::vector<size_t>* offsets); +BASE_EXPORT string16 +ReplaceStringPlaceholders(StringPiece16 format_string, + const std::vector<string16>& subst, + std::vector<size_t>* offsets); BASE_EXPORT std::string ReplaceStringPlaceholders( StringPiece format_string, @@ -536,25 +473,6 @@ BASE_EXPORT string16 ReplaceStringPlaceholders(const string16& format_string, const string16& a, size_t* offset); -#if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING) -BASE_EXPORT TrimPositions TrimWhitespace(WStringPiece input, - TrimPositions positions, - std::wstring* output); - -BASE_EXPORT WStringPiece TrimWhitespace(WStringPiece input, - TrimPositions positions); - -BASE_EXPORT bool TrimString(WStringPiece input, - WStringPiece trim_chars, - std::wstring* output); - -BASE_EXPORT WStringPiece TrimString(WStringPiece input, - WStringPiece trim_chars, - TrimPositions positions); - -BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null); -#endif - } // namespace base #if defined(OS_WIN) |