| 6 |
| 7 | #ifndef BASE_STRINGS_STRING_UTIL_H_ |
| 8 | #define BASE_STRINGS_STRING_UTIL_H_ |
| 9 |
| 10 | #include <ctype.h> |
| 11 | #include <stdarg.h> // va_list |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 |
| 15 | #include <initializer_list> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "base/base_export.h" |
| 20 | #include "base/compiler_specific.h" |
| 21 | #include "base/stl_util.h" |
| 22 | #include "base/strings/string16.h" |
| 23 | #include "base/strings/string_piece.h" // For implicit conversions. |
| 24 | #include "build/build_config.h" |
| 25 |
| 26 | namespace base { |
| 27 |
| 28 | // C standard-library functions that aren't cross-platform are provided as |
| 29 | // "base::...", and their prototypes are listed below. These functions are |
| 30 | // then implemented as inline calls to the platform-specific equivalents in the |
| 31 | // platform-specific headers. |
| 32 |
| 33 | // Wrapper for vsnprintf that always null-terminates and always returns the |
| 34 | // number of characters that would be in an untruncated formatted |
| 35 | // string, even when truncation occurs. |
| 36 | int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments) |
| 37 | PRINTF_FORMAT(3, 0); |
| 38 |
| 39 | // Some of these implementations need to be inlined. |
| 40 |
| 41 | // We separate the declaration from the implementation of this inline |
| 42 | // function just so the PRINTF_FORMAT works. |
| 43 | inline int snprintf(char* buffer, size_t size, const char* format, ...) |
| 44 | PRINTF_FORMAT(3, 4); |
| 45 | inline int snprintf(char* buffer, size_t size, const char* format, ...) { |
| 46 | va_list arguments; |
| 47 | va_start(arguments, format); |
| 48 | int result = vsnprintf(buffer, size, format, arguments); |
| 49 | va_end(arguments); |
| 50 | return result; |
| 51 | } |
| 52 |
| 53 | // BSD-style safe and consistent string copy functions. |
| 54 | // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|. |
| 55 | // Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as |
| 56 | // long as |dst_size| is not 0. Returns the length of |src| in characters. |
| 57 | // If the return value is >= dst_size, then the output was truncated. |
| 58 | // NOTE: All sizes are in number of characters, NOT in bytes. |
| 59 | BASE_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size); |
| 60 | BASE_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size); |
| 61 |
| 62 | // Scan a wprintf format string to determine whether it's portable across a |
| 63 | // variety of systems. This function only checks that the conversion |
| 64 | // specifiers used by the format string are supported and have the same meaning |
| 65 | // on a variety of systems. It doesn't check for other errors that might occur |
| 66 | // within a format string. |
| 67 | // |
| 68 | // Nonportable conversion specifiers for wprintf are: |
| 69 | // - 's' and 'c' without an 'l' length modifier. %s and %c operate on char |
| 70 | // data on all systems except Windows, which treat them as wchar_t data. |
| 71 | // Use %ls and %lc for wchar_t data instead. |
| 72 | // - 'S' and 'C', which operate on wchar_t data on all systems except Windows, |
| 73 | // which treat them as char data. Use %ls and %lc for wchar_t data |
| 74 | // instead. |
| 75 | // - 'F', which is not identified by Windows wprintf documentation. |
| 76 | // - 'D', 'O', and 'U', which are deprecated and not available on all systems. |
| 77 | // Use %ld, %lo, and %lu instead. |
| 78 | // |
| 79 | // Note that there is no portable conversion specifier for char data when |
| 80 | // working with wprintf. |
| 81 | // |
| 82 | // This function is intended to be called from base::vswprintf. |
| 83 | BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format); |
| 84 |
| 85 | // ASCII-specific tolower. The standard library's tolower is locale sensitive, |
| 86 | // so we don't want to use it here. |
| 87 | inline char ToLowerASCII(char c) { |
| 88 | return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; |
| 89 | } |
| 90 | inline char16 ToLowerASCII(char16 c) { |
| 91 | return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; |
| 92 | } |
| 93 |
| 94 | // ASCII-specific toupper. The standard library's toupper is locale sensitive, |
| 95 | // so we don't want to use it here. |
| 96 | inline char ToUpperASCII(char c) { |
| 97 | return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; |
| 98 | } |
| 99 | inline char16 ToUpperASCII(char16 c) { |
| 100 | return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; |
| 101 | } |
| 102 |
| 103 | // Converts the given string to it's ASCII-lowercase equivalent. |
| 104 | BASE_EXPORT std::string ToLowerASCII(StringPiece str); |
| 105 | BASE_EXPORT string16 ToLowerASCII(StringPiece16 str); |
| 106 |
| 107 | // Converts the given string to it's ASCII-uppercase equivalent. |
| 108 | BASE_EXPORT std::string ToUpperASCII(StringPiece str); |
| 109 | BASE_EXPORT string16 ToUpperASCII(StringPiece16 str); |
| 110 |
| 111 | // Functor for case-insensitive ASCII comparisons for STL algorithms like |
| 112 | // std::search. |
| 113 | // |
| 114 | // Note that a full Unicode version of this functor is not possible to write |
| 115 | // because case mappings might change the number of characters, depend on |
| 116 | // context (combining accents), and require handling UTF-16. If you need |
| 117 | // proper Unicode support, use base::i18n::ToLower/FoldCase and then just |
| 118 | // use a normal operator== on the result. |
| 119 | template<typename Char> struct CaseInsensitiveCompareASCII { |
| 120 | public: |
| 121 | bool operator()(Char x, Char y) const { |
| 122 | return ToLowerASCII(x) == ToLowerASCII(y); |
| 123 | } |
| 124 | }; |
| 125 |
| 126 | // Like strcasecmp for case-insensitive ASCII characters only. Returns: |
| 127 | // -1 (a < b) |
| 128 | // 0 (a == b) |
| 129 | // 1 (a > b) |
| 130 | // (unlike strcasecmp which can return values greater or less than 1/-1). For |
| 131 | // full Unicode support, use base::i18n::ToLower or base::i18h::FoldCase |
| 132 | // and then just call the normal string operators on the result. |
| 133 | BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece a, StringPiece b); |
| 134 | BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b); |
| 135 |
| 136 | // Equality for ASCII case-insensitive comparisons. For full Unicode support, |
| 137 | // use base::i18n::ToLower or base::i18h::FoldCase and then compare with either |
| 138 | // == or !=. |
| 139 | BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece a, StringPiece b); |
| 140 | BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b); |
| 141 |
| 142 | // These threadsafe functions return references to globally unique empty |
| 143 | // strings. |
| 144 | // |
| 145 | // It is likely faster to construct a new empty string object (just a few |
| 146 | // instructions to set the length to 0) than to get the empty string instance |
| 147 | // returned by these functions (which requires threadsafe static access). |
| 148 | // |
| 149 | // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT |
| 150 | // CONSTRUCTORS. There is only one case where you should use these: functions |
| 151 | // which need to return a string by reference (e.g. as a class member |
| 152 | // accessor), and don't have an empty string to use (e.g. in an error case). |
| 153 | // These should not be used as initializers, function arguments, or return |
| 154 | // values for functions which return by value or outparam. |
| 155 | BASE_EXPORT const std::string& EmptyString(); |
| 156 | BASE_EXPORT const string16& EmptyString16(); |
| 157 |
| 158 | // Contains the set of characters representing whitespace in the corresponding |
| 159 | // encoding. Null-terminated. The ASCII versions are the whitespaces as defined |
| 160 | // by HTML5, and don't include control characters. |
| 161 | BASE_EXPORT extern const wchar_t kWhitespaceWide[]; // Includes Unicode. |
| 162 | BASE_EXPORT extern const char16 kWhitespaceUTF16[]; // Includes Unicode. |
| 163 | BASE_EXPORT extern const char16 kWhitespaceNoCrLfUTF16[]; // Unicode w/o CR/LF. |
| 164 | BASE_EXPORT extern const char kWhitespaceASCII[]; |
| 165 | BASE_EXPORT extern const char16 kWhitespaceASCIIAs16[]; // No unicode. |
| 166 |
| 167 | // Null-terminated string representing the UTF-8 byte order mark. |
| 168 | BASE_EXPORT extern const char kUtf8ByteOrderMark[]; |
| 169 |
| 170 | // Removes characters in |remove_chars| from anywhere in |input|. Returns true |
| 171 | // if any characters were removed. |remove_chars| must be null-terminated. |
| 172 | // NOTE: Safe to use the same variable for both |input| and |output|. |
| 173 | BASE_EXPORT bool RemoveChars(const string16& input, |
| 174 | StringPiece16 remove_chars, |
| 175 | string16* output); |
| 176 | BASE_EXPORT bool RemoveChars(const std::string& input, |
| 177 | StringPiece remove_chars, |
| 178 | std::string* output); |
| 179 |
| 180 | // Replaces characters in |replace_chars| from anywhere in |input| with |
| 181 | // |replace_with|. Each character in |replace_chars| will be replaced with |
| 182 | // the |replace_with| string. Returns true if any characters were replaced. |
| 183 | // |replace_chars| must be null-terminated. |
| 184 | // NOTE: Safe to use the same variable for both |input| and |output|. |
| 185 | BASE_EXPORT bool ReplaceChars(const string16& input, |
| 186 | StringPiece16 replace_chars, |
| 187 | StringPiece16 replace_with, |
| 188 | string16* output); |
| 189 | BASE_EXPORT bool ReplaceChars(const std::string& input, |
| 190 | StringPiece replace_chars, |
| 191 | StringPiece replace_with, |
| 192 | std::string* output); |
| 193 |
| 194 | enum TrimPositions { |
| 195 | TRIM_NONE = 0, |
| 196 | TRIM_LEADING = 1 << 0, |
| 197 | TRIM_TRAILING = 1 << 1, |
| 198 | TRIM_ALL = TRIM_LEADING | TRIM_TRAILING, |
| 199 | }; |
| 200 |
| 201 | // Removes characters in |trim_chars| from the beginning and end of |input|. |
| 202 | // The 8-bit version only works on 8-bit characters, not UTF-8. Returns true if |
| 203 | // any characters were removed. |
| 204 | // |
| 205 | // It is safe to use the same variable for both |input| and |output| (this is |
| 206 | // the normal usage to trim in-place). |
| 207 | BASE_EXPORT bool TrimString(StringPiece16 input, |
| 208 | StringPiece16 trim_chars, |
| 209 | string16* output); |
| 210 | BASE_EXPORT bool TrimString(StringPiece input, |
| 211 | StringPiece trim_chars, |
| 212 | std::string* output); |
| 213 |
| 214 | // StringPiece versions of the above. The returned pieces refer to the original |
| 215 | // buffer. |
| 216 | BASE_EXPORT StringPiece16 TrimString(StringPiece16 input, |
| 217 | StringPiece16 trim_chars, |
| 218 | TrimPositions positions); |
| 219 | BASE_EXPORT StringPiece TrimString(StringPiece input, |
| 220 | StringPiece trim_chars, |
| 221 | TrimPositions positions); |
| 222 |
| 223 | // Truncates a string to the nearest UTF-8 character that will leave |
| 224 | // the string less than or equal to the specified byte size. |
| 225 | BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, |
| 226 | const size_t byte_size, |
| 227 | std::string* output); |
| 228 |
| 229 | #if defined(WCHAR_T_IS_UTF16) |
| 230 | // Utility functions to access the underlying string buffer as a wide char |
| 231 | // pointer. |
| 232 | // |
| 233 | // Note: These functions violate strict aliasing when char16 and wchar_t are |
| 234 | // unrelated types. We thus pass -fno-strict-aliasing to the compiler on |
| 235 | // non-Windows platforms [1], and rely on it being off in Clang's CL mode [2]. |
| 236 | // |
| 237 | // [1] https://crrev.com/b9a0976622/build/config/compiler/BUILD.gn#244 |
| 238 | // [2] |
| 239 | // https://github.com/llvm/llvm-project/blob/1e28a66/clang/lib/Driver/ToolChains/Clang.cpp#L3949 |
| 240 | inline wchar_t* as_writable_wcstr(char16* str) { |
| 241 | return reinterpret_cast<wchar_t*>(str); |
| 242 | } |
| 243 |
| 244 | inline wchar_t* as_writable_wcstr(string16& str) { |
| 245 | return reinterpret_cast<wchar_t*>(data(str)); |
| 246 | } |
| 247 |
| 248 | inline const wchar_t* as_wcstr(const char16* str) { |
| 249 | return reinterpret_cast<const wchar_t*>(str); |
| 250 | } |
| 251 |
| 252 | inline const wchar_t* as_wcstr(StringPiece16 str) { |
| 253 | return reinterpret_cast<const wchar_t*>(str.data()); |
| 254 | } |
| 255 |
| 256 | // Utility functions to access the underlying string buffer as a char16 pointer. |
| 257 | inline char16* as_writable_u16cstr(wchar_t* str) { |
| 258 | return reinterpret_cast<char16*>(str); |
| 259 | } |
| 260 |
| 261 | inline char16* as_writable_u16cstr(std::wstring& str) { |
| 262 | return reinterpret_cast<char16*>(data(str)); |
| 263 | } |
| 264 |
| 265 | inline const char16* as_u16cstr(const wchar_t* str) { |
| 266 | return reinterpret_cast<const char16*>(str); |
| 267 | } |
| 268 |
| 269 | inline const char16* as_u16cstr(WStringPiece str) { |
| 270 | return reinterpret_cast<const char16*>(str.data()); |
| 271 | } |
| 272 |
| 273 | // Utility functions to convert between base::WStringPiece and |
| 274 | // base::StringPiece16. |
| 275 | inline WStringPiece AsWStringPiece(StringPiece16 str) { |
| 276 | return WStringPiece(as_wcstr(str.data()), str.size()); |
| 277 | } |
| 278 |
| 279 | inline StringPiece16 AsStringPiece16(WStringPiece str) { |
| 280 | return StringPiece16(as_u16cstr(str.data()), str.size()); |
| 281 | } |
| 282 |
| 283 | inline std::wstring AsWString(StringPiece16 str) { |
| 284 | return std::wstring(as_wcstr(str.data()), str.size()); |
| 285 | } |
| 286 |
| 287 | inline string16 AsString16(WStringPiece str) { |
| 288 | return string16(as_u16cstr(str.data()), str.size()); |
| 289 | } |
| 290 | #endif // defined(WCHAR_T_IS_UTF16) |
| 291 |
| 292 | // Trims any whitespace from either end of the input string. |
| 293 | // |
| 294 | // The StringPiece versions return a substring referencing the input buffer. |
| 295 | // The ASCII versions look only for ASCII whitespace. |
| 296 | // |
| 297 | // The std::string versions return where whitespace was found. |
| 298 | // NOTE: Safe to use the same variable for both input and output. |
| 299 | BASE_EXPORT TrimPositions TrimWhitespace(StringPiece16 input, |
| 300 | TrimPositions positions, |
| 301 | string16* output); |
| 302 | BASE_EXPORT StringPiece16 TrimWhitespace(StringPiece16 input, |
| 303 | TrimPositions positions); |
| 304 | BASE_EXPORT TrimPositions TrimWhitespaceASCII(StringPiece input, |
| 305 | TrimPositions positions, |
| 306 | std::string* output); |
| 307 | BASE_EXPORT StringPiece TrimWhitespaceASCII(StringPiece input, |
| 308 | TrimPositions positions); |
| 309 |
| 310 | // Searches for CR or LF characters. Removes all contiguous whitespace |
| 311 | // strings that contain them. This is useful when trying to deal with text |
| 312 | // copied from terminals. |
| 313 | // Returns |text|, with the following three transformations: |
| 314 | // (1) Leading and trailing whitespace is trimmed. |
| 315 | // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace |
| 316 | // sequences containing a CR or LF are trimmed. |
| 317 | // (3) All other whitespace sequences are converted to single spaces. |
| 318 | BASE_EXPORT string16 CollapseWhitespace( |
| 319 | const string16& text, |
| 320 | bool trim_sequences_with_line_breaks); |
| 321 | BASE_EXPORT std::string CollapseWhitespaceASCII( |
| 322 | const std::string& text, |
| 323 | bool trim_sequences_with_line_breaks); |
| 324 |
| 325 | // Returns true if |input| is empty or contains only characters found in |
| 326 | // |characters|. |
| 327 | BASE_EXPORT bool ContainsOnlyChars(StringPiece input, StringPiece characters); |
| 328 | BASE_EXPORT bool ContainsOnlyChars(StringPiece16 input, |
| 329 | StringPiece16 characters); |
| 330 |
| 331 | // Returns true if |str| is structurally valid UTF-8 and also doesn't |
| 332 | // contain any non-character code point (e.g. U+10FFFE). Prohibiting |
| 333 | // non-characters increases the likelihood of detecting non-UTF-8 in |
| 334 | // real-world text, for callers which do not need to accept |
| 335 | // non-characters in strings. |
| 336 | BASE_EXPORT bool IsStringUTF8(StringPiece str); |
| 337 |
| 338 | // Returns true if |str| contains valid UTF-8, allowing non-character |
| 339 | // code points. |
| 340 | BASE_EXPORT bool IsStringUTF8AllowingNoncharacters(StringPiece str); |
| 341 |
| 342 | // Returns true if |str| contains only valid ASCII character values. |
| 343 | // Note 1: IsStringASCII executes in time determined solely by the |
| 344 | // length of the string, not by its contents, so it is robust against |
| 345 | // timing attacks for all strings of equal length. |
| 346 | // Note 2: IsStringASCII assumes the input is likely all ASCII, and |
| 347 | // does not leave early if it is not the case. |
| 348 | BASE_EXPORT bool IsStringASCII(StringPiece str); |
| 349 | BASE_EXPORT bool IsStringASCII(StringPiece16 str); |
| 350 | #if defined(WCHAR_T_IS_UTF32) |
| 351 | BASE_EXPORT bool IsStringASCII(WStringPiece str); |
| 352 | #endif |
| 353 |
| 354 | // Compare the lower-case form of the given string against the given |
| 355 | // previously-lower-cased ASCII string (typically a constant). |
| 356 | BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece str, |
| 357 | StringPiece lowecase_ascii); |
| 358 | BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece16 str, |
| 359 | StringPiece lowecase_ascii); |
| 360 |
| 361 | // Performs a case-sensitive string compare of the given 16-bit string against |
| 362 | // the given 8-bit ASCII string (typically a constant). The behavior is |
| 363 | // undefined if the |ascii| string is not ASCII. |
| 364 | BASE_EXPORT bool EqualsASCII(StringPiece16 str, StringPiece ascii); |
| 365 |
| 366 | // Indicates case sensitivity of comparisons. Only ASCII case insensitivity |
| 367 | // is supported. Full Unicode case-insensitive conversions would need to go in |
| 368 | // base/i18n so it can use ICU. |
| 369 | // |
| 370 | // If you need to do Unicode-aware case-insensitive StartsWith/EndsWith, it's |
| 371 | // best to call base::i18n::ToLower() or base::i18n::FoldCase() (see |
| 372 | // base/i18n/case_conversion.h for usage advice) on the arguments, and then use |
| 373 | // the results to a case-sensitive comparison. |
| 374 | enum class CompareCase { |
| 375 | SENSITIVE, |
| 376 | INSENSITIVE_ASCII, |
| 377 | }; |
| 378 |
| 379 | BASE_EXPORT bool StartsWith(StringPiece str, |
| 380 | StringPiece search_for, |
| 381 | CompareCase case_sensitivity); |
| 382 | BASE_EXPORT bool StartsWith(StringPiece16 str, |
| 383 | StringPiece16 search_for, |
| 384 | CompareCase case_sensitivity); |
| 385 | BASE_EXPORT bool EndsWith(StringPiece str, |
| 386 | StringPiece search_for, |
| 387 | CompareCase case_sensitivity); |
| 388 | BASE_EXPORT bool EndsWith(StringPiece16 str, |
| 389 | StringPiece16 search_for, |
| 390 | CompareCase case_sensitivity); |
| 391 |
| 392 | // Determines the type of ASCII character, independent of locale (the C |
| 393 | // library versions will change based on locale). |
| 394 | template <typename Char> |
| 395 | inline bool IsAsciiWhitespace(Char c) { |
| 396 | return c == ' ' || c == '\r' || c == '\n' || c == '\t' || c == '\f'; |
| 397 | } |
| 398 | template <typename Char> |
| 399 | inline bool IsAsciiAlpha(Char c) { |
| 400 | return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); |
| 401 | } |
| 402 | template <typename Char> |
| 403 | inline bool IsAsciiUpper(Char c) { |
| 404 | return c >= 'A' && c <= 'Z'; |
| 405 | } |
| 406 | template <typename Char> |
| 407 | inline bool IsAsciiLower(Char c) { |
| 408 | return c >= 'a' && c <= 'z'; |
| 409 | } |
| 410 | template <typename Char> |
| 411 | inline bool IsAsciiDigit(Char c) { |
| 412 | return c >= '0' && c <= '9'; |
| 413 | } |
| 414 | template <typename Char> |
| 415 | inline bool IsAsciiPrintable(Char c) { |
| 416 | return c >= ' ' && c <= '~'; |
| 417 | } |
| 418 |
| 419 | template <typename Char> |
| 420 | inline bool IsHexDigit(Char c) { |
| 421 | return (c >= '0' && c <= '9') || |
| 422 | (c >= 'A' && c <= 'F') || |
| 423 | (c >= 'a' && c <= 'f'); |
| 424 | } |
| 425 |
| 426 | // Returns the integer corresponding to the given hex character. For example: |
| 427 | // '4' -> 4 |
| 428 | // 'a' -> 10 |
| 429 | // 'B' -> 11 |
| 430 | // Assumes the input is a valid hex character. DCHECKs in debug builds if not. |
| 431 | BASE_EXPORT char HexDigitToInt(wchar_t c); |
| 432 |
| 433 | // Returns true if it's a Unicode whitespace character. |
| 434 | BASE_EXPORT bool IsUnicodeWhitespace(wchar_t c); |
| 435 |
| 436 | // Return a byte string in human-readable format with a unit suffix. Not |
| 437 | // appropriate for use in any UI; use of FormatBytes and friends in ui/base is |
| 438 | // highly recommended instead. TODO(avi): Figure out how to get callers to use |
| 439 | // FormatBytes instead; remove this. |
| 440 | BASE_EXPORT string16 FormatBytesUnlocalized(int64_t bytes); |
| 441 |
| 442 | // Starting at |start_offset| (usually 0), replace the first instance of |
| 443 | // |find_this| with |replace_with|. |
| 444 | BASE_EXPORT void ReplaceFirstSubstringAfterOffset( |
| 445 | base::string16* str, |
| 446 | size_t start_offset, |
| 447 | StringPiece16 find_this, |
| 448 | StringPiece16 replace_with); |
| 449 | BASE_EXPORT void ReplaceFirstSubstringAfterOffset( |
| 450 | std::string* str, |
| 451 | size_t start_offset, |
| 452 | StringPiece find_this, |
| 453 | StringPiece replace_with); |
| 454 |
| 455 | // Starting at |start_offset| (usually 0), look through |str| and replace all |
| 456 | // instances of |find_this| with |replace_with|. |
| 457 | // |
| 458 | // This does entire substrings; use std::replace in <algorithm> for single |
| 459 | // characters, for example: |
| 460 | // std::replace(str.begin(), str.end(), 'a', 'b'); |
| 461 | BASE_EXPORT void ReplaceSubstringsAfterOffset( |
| 462 | string16* str, |
| 463 | size_t start_offset, |
| 464 | StringPiece16 find_this, |
| 465 | StringPiece16 replace_with); |
| 466 | BASE_EXPORT void ReplaceSubstringsAfterOffset( |
| 467 | std::string* str, |
| 468 | size_t start_offset, |
| 469 | StringPiece find_this, |
| 470 | StringPiece replace_with); |
| 471 |
| 472 | // Reserves enough memory in |str| to accommodate |length_with_null| characters, |
| 473 | // sets the size of |str| to |length_with_null - 1| characters, and returns a |
| 474 | // pointer to the underlying contiguous array of characters. This is typically |
| 475 | // used when calling a function that writes results into a character array, but |
| 476 | // the caller wants the data to be managed by a string-like object. It is |
| 477 | // convenient in that is can be used inline in the call, and fast in that it |
| 478 | // avoids copying the results of the call from a char* into a string. |
| 479 | // |
| 480 | // Internally, this takes linear time because the resize() call 0-fills the |
| 481 | // underlying array for potentially all |
| 482 | // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes. Ideally we |
| 483 | // could avoid this aspect of the resize() call, as we expect the caller to |
| 484 | // immediately write over this memory, but there is no other way to set the size |
| 485 | // of the string, and not doing that will mean people who access |str| rather |
| 486 | // than str.c_str() will get back a string of whatever size |str| had on entry |
| 487 | // to this function (probably 0). |
| 488 | BASE_EXPORT char* WriteInto(std::string* str, size_t length_with_null); |
| 489 | BASE_EXPORT char16* WriteInto(string16* str, size_t length_with_null); |
| 490 |
| 491 | // Joins a vector or list of strings into a single string, inserting |separator| |
| 492 | // (which may be empty) in between all elements. |
| 493 | // |
| 494 | // Note this is inverse of SplitString()/SplitStringPiece() defined in |
| 495 | // string_split.h. |
| 496 | // |
| 497 | // If possible, callers should build a vector of StringPieces and use the |
| 498 | // StringPiece variant, so that they do not create unnecessary copies of |
| 499 | // strings. For example, instead of using SplitString, modifying the vector, |
| 500 | // then using JoinString, use SplitStringPiece followed by JoinString so that no |
| 501 | // copies of those strings are created until the final join operation. |
| 502 | // |
| 503 | // Use StrCat (in base/strings/strcat.h) if you don't need a separator. |
| 504 | BASE_EXPORT std::string JoinString(const std::vector<std::string>& parts, |
| 505 | StringPiece separator); |
| 506 | BASE_EXPORT string16 JoinString(const std::vector<string16>& parts, |
| 507 | StringPiece16 separator); |
| 508 | BASE_EXPORT std::string JoinString(const std::vector<StringPiece>& parts, |
| 509 | StringPiece separator); |
| 510 | BASE_EXPORT string16 JoinString(const std::vector<StringPiece16>& parts, |
| 511 | StringPiece16 separator); |
| 512 | // Explicit initializer_list overloads are required to break ambiguity when used |
| 513 | // with a literal initializer list (otherwise the compiler would not be able to |
| 514 | // decide between the string and StringPiece overloads). |
| 515 | BASE_EXPORT std::string JoinString(std::initializer_list<StringPiece> parts, |
| 516 | StringPiece separator); |
| 517 | BASE_EXPORT string16 JoinString(std::initializer_list<StringPiece16> parts, |
| 518 | StringPiece16 separator); |
| 519 |
| 520 | // Replace $1-$2-$3..$9 in the format string with values from |subst|. |
| 521 | // Additionally, any number of consecutive '$' characters is replaced by that |
| 522 | // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be |
| 523 | // NULL. This only allows you to use up to nine replacements. |
| 524 | BASE_EXPORT string16 ReplaceStringPlaceholders( |
| 525 | const string16& format_string, |
| 526 | const std::vector<string16>& subst, |
| 527 | std::vector<size_t>* offsets); |
| 528 |
| 529 | BASE_EXPORT std::string ReplaceStringPlaceholders( |
| 530 | StringPiece format_string, |
| 531 | const std::vector<std::string>& subst, |
| 532 | std::vector<size_t>* offsets); |
| 533 |
| 534 | // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. |
| 535 | BASE_EXPORT string16 ReplaceStringPlaceholders(const string16& format_string, |
| 536 | const string16& a, |
| 537 | size_t* offset); |
| 538 |
| 539 | #if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING) |
| 540 | BASE_EXPORT TrimPositions TrimWhitespace(WStringPiece input, |
| 541 | TrimPositions positions, |
| 542 | std::wstring* output); |
| 543 |
| 544 | BASE_EXPORT WStringPiece TrimWhitespace(WStringPiece input, |
| 545 | TrimPositions positions); |
| 546 |
| 547 | BASE_EXPORT bool TrimString(WStringPiece input, |
| 548 | WStringPiece trim_chars, |
| 549 | std::wstring* output); |
| 550 |
| 551 | BASE_EXPORT WStringPiece TrimString(WStringPiece input, |
| 552 | WStringPiece trim_chars, |
| 553 | TrimPositions positions); |
| 554 |
| 555 | BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null); |
| 556 | #endif |
| 557 |
| 558 | } // namespace base |
| 559 |
| 560 | #if defined(OS_WIN) |
| 561 | #include "base/strings/string_util_win.h" |
| 562 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 563 | #include "base/strings/string_util_posix.h" |
| 564 | #else |
| 565 | #error Define string operations appropriately for your platform |
| 566 | #endif |
| 567 |
| 568 | #endif // BASE_STRINGS_STRING_UTIL_H_ |
| 569 |