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