123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- #ifndef BASE_STRINGS_STRING_UTIL_H_
- #define BASE_STRINGS_STRING_UTIL_H_
- #include <ctype.h>
- #include <stdarg.h> // va_list
- #include <stddef.h>
- #include <stdint.h>
- #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"
- #include "build/build_config.h"
- namespace base {
- int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments)
- PRINTF_FORMAT(3, 0);
- inline int snprintf(char* buffer, size_t size, const char* format, ...)
- PRINTF_FORMAT(3, 4);
- inline int snprintf(char* buffer, size_t size, const char* format, ...) {
- va_list arguments;
- va_start(arguments, format);
- int result = vsnprintf(buffer, size, format, arguments);
- va_end(arguments);
- return result;
- }
- BASE_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size);
- BASE_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size);
- BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format);
- 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;
- }
- 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;
- }
- BASE_EXPORT std::string ToLowerASCII(StringPiece str);
- BASE_EXPORT string16 ToLowerASCII(StringPiece16 str);
- BASE_EXPORT std::string ToUpperASCII(StringPiece str);
- BASE_EXPORT string16 ToUpperASCII(StringPiece16 str);
- template<typename Char> struct CaseInsensitiveCompareASCII {
- public:
- bool operator()(Char x, Char y) const {
- return ToLowerASCII(x) == ToLowerASCII(y);
- }
- };
- BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece a, StringPiece b);
- BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b);
- BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece a, StringPiece b);
- BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b);
- BASE_EXPORT const std::string& EmptyString();
- BASE_EXPORT const string16& EmptyString16();
- BASE_EXPORT extern const wchar_t kWhitespaceWide[];
- BASE_EXPORT extern const char16 kWhitespaceUTF16[];
- BASE_EXPORT extern const char16 kWhitespaceNoCrLfUTF16[];
- BASE_EXPORT extern const char kWhitespaceASCII[];
- BASE_EXPORT extern const char16 kWhitespaceASCIIAs16[];
- BASE_EXPORT extern const char kUtf8ByteOrderMark[];
- BASE_EXPORT bool RemoveChars(StringPiece16 input,
- StringPiece16 remove_chars,
- string16* output);
- BASE_EXPORT bool RemoveChars(StringPiece input,
- StringPiece remove_chars,
- std::string* output);
- BASE_EXPORT bool ReplaceChars(StringPiece16 input,
- StringPiece16 replace_chars,
- StringPiece16 replace_with,
- string16* output);
- BASE_EXPORT bool ReplaceChars(StringPiece input,
- StringPiece replace_chars,
- StringPiece replace_with,
- std::string* output);
- enum TrimPositions {
- TRIM_NONE = 0,
- TRIM_LEADING = 1 << 0,
- TRIM_TRAILING = 1 << 1,
- TRIM_ALL = TRIM_LEADING | TRIM_TRAILING,
- };
- BASE_EXPORT bool TrimString(StringPiece16 input,
- StringPiece16 trim_chars,
- string16* output);
- BASE_EXPORT bool TrimString(StringPiece input,
- StringPiece trim_chars,
- std::string* output);
- BASE_EXPORT StringPiece16 TrimString(StringPiece16 input,
- StringPiece16 trim_chars,
- TrimPositions positions);
- BASE_EXPORT StringPiece TrimString(StringPiece input,
- StringPiece trim_chars,
- TrimPositions positions);
- BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input,
- const size_t byte_size,
- std::string* output);
- BASE_EXPORT TrimPositions TrimWhitespace(StringPiece16 input,
- TrimPositions positions,
- string16* output);
- BASE_EXPORT StringPiece16 TrimWhitespace(StringPiece16 input,
- TrimPositions positions);
- BASE_EXPORT TrimPositions TrimWhitespaceASCII(StringPiece input,
- TrimPositions positions,
- std::string* output);
- BASE_EXPORT StringPiece TrimWhitespaceASCII(StringPiece input,
- TrimPositions positions);
- BASE_EXPORT string16 CollapseWhitespace(StringPiece16 text,
- bool trim_sequences_with_line_breaks);
- BASE_EXPORT std::string CollapseWhitespaceASCII(
- StringPiece text,
- bool trim_sequences_with_line_breaks);
- BASE_EXPORT bool ContainsOnlyChars(StringPiece input, StringPiece characters);
- BASE_EXPORT bool ContainsOnlyChars(StringPiece16 input,
- StringPiece16 characters);
- BASE_EXPORT bool IsStringUTF8(StringPiece str);
- BASE_EXPORT bool IsStringUTF8AllowingNoncharacters(StringPiece str);
- 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
- BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece str,
- StringPiece lowecase_ascii);
- BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece16 str,
- StringPiece lowecase_ascii);
- BASE_EXPORT bool EqualsASCII(StringPiece16 str, StringPiece ascii);
- enum class CompareCase {
- SENSITIVE,
- INSENSITIVE_ASCII,
- };
- BASE_EXPORT bool StartsWith(StringPiece str,
- StringPiece search_for,
- CompareCase case_sensitivity);
- BASE_EXPORT bool StartsWith(StringPiece16 str,
- StringPiece16 search_for,
- CompareCase case_sensitivity);
- BASE_EXPORT bool EndsWith(StringPiece str,
- StringPiece search_for,
- CompareCase case_sensitivity);
- BASE_EXPORT bool EndsWith(StringPiece16 str,
- StringPiece16 search_for,
- CompareCase case_sensitivity);
- template <typename Char>
- inline bool IsAsciiWhitespace(Char c) {
- return c == ' ' || c == '\r' || c == '\n' || c == '\t' || c == '\f';
- }
- template <typename Char>
- inline bool IsAsciiAlpha(Char c) {
- return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
- }
- template <typename Char>
- inline bool IsAsciiUpper(Char c) {
- return c >= 'A' && c <= 'Z';
- }
- template <typename Char>
- inline bool IsAsciiLower(Char c) {
- return c >= 'a' && c <= 'z';
- }
- template <typename Char>
- inline bool IsAsciiDigit(Char c) {
- return c >= '0' && c <= '9';
- }
- template <typename Char>
- inline bool IsAsciiPrintable(Char c) {
- return c >= ' ' && c <= '~';
- }
- template <typename Char>
- inline bool IsHexDigit(Char c) {
- return (c >= '0' && c <= '9') ||
- (c >= 'A' && c <= 'F') ||
- (c >= 'a' && c <= 'f');
- }
- BASE_EXPORT char HexDigitToInt(wchar_t c);
- BASE_EXPORT bool IsUnicodeWhitespace(wchar_t c);
- BASE_EXPORT string16 FormatBytesUnlocalized(int64_t bytes);
- BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
- base::string16* str,
- size_t start_offset,
- StringPiece16 find_this,
- StringPiece16 replace_with);
- BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
- std::string* str,
- size_t start_offset,
- StringPiece find_this,
- StringPiece replace_with);
- BASE_EXPORT void ReplaceSubstringsAfterOffset(
- string16* str,
- size_t start_offset,
- StringPiece16 find_this,
- StringPiece16 replace_with);
- BASE_EXPORT void ReplaceSubstringsAfterOffset(
- std::string* str,
- size_t start_offset,
- StringPiece find_this,
- StringPiece replace_with);
- BASE_EXPORT char* WriteInto(std::string* str, size_t length_with_null);
- BASE_EXPORT char16* WriteInto(string16* str, size_t length_with_null);
- BASE_EXPORT std::string JoinString(span<const std::string> parts,
- StringPiece separator);
- BASE_EXPORT string16 JoinString(span<const string16> parts,
- StringPiece16 separator);
- BASE_EXPORT std::string JoinString(span<const StringPiece> parts,
- StringPiece separator);
- BASE_EXPORT string16 JoinString(span<const StringPiece16> parts,
- StringPiece16 separator);
- BASE_EXPORT std::string JoinString(std::initializer_list<StringPiece> parts,
- StringPiece separator);
- BASE_EXPORT string16 JoinString(std::initializer_list<StringPiece16> parts,
- StringPiece16 separator);
- 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,
- const std::vector<std::string>& subst,
- std::vector<size_t>* offsets);
- BASE_EXPORT string16 ReplaceStringPlaceholders(const string16& format_string,
- const string16& a,
- size_t* offset);
- }
- #if defined(OS_WIN)
- #include "base/strings/string_util_win.h"
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- #include "base/strings/string_util_posix.h"
- #else
- #error Define string operations appropriately for your platform
- #endif
- #endif
|