• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file defines utility functions for working with strings.
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 
13 #include <string>
14 #include <vector>
15 
16 #include "base/base_export.h"
17 #include "base/basictypes.h"
18 #include "base/compiler_specific.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/string_piece.h"  // For implicit conversions.
21 
22 // Safe standard library wrappers for all platforms.
23 
24 namespace base {
25 
26 // C standard-library functions like "strncasecmp" and "snprintf" that aren't
27 // cross-platform are provided as "base::strncasecmp", and their prototypes
28 // are listed below.  These functions are then implemented as inline calls
29 // to the platform-specific equivalents in the platform-specific headers.
30 
31 // Compares the two strings s1 and s2 without regard to case using
32 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
33 // s2 > s1 according to a lexicographic comparison.
34 int strcasecmp(const char* s1, const char* s2);
35 
36 // Compares up to count characters of s1 and s2 without regard to case using
37 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
38 // s2 > s1 according to a lexicographic comparison.
39 int strncasecmp(const char* s1, const char* s2, size_t count);
40 
41 // Same as strncmp but for char16 strings.
42 int strncmp16(const char16* s1, const char16* s2, size_t count);
43 
44 // Wrapper for vsnprintf that always null-terminates and always returns the
45 // number of characters that would be in an untruncated formatted
46 // string, even when truncation occurs.
47 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments)
48     PRINTF_FORMAT(3, 0);
49 
50 // Some of these implementations need to be inlined.
51 
52 // We separate the declaration from the implementation of this inline
53 // function just so the PRINTF_FORMAT works.
54 inline int snprintf(char* buffer, size_t size, const char* format, ...)
55     PRINTF_FORMAT(3, 4);
snprintf(char * buffer,size_t size,const char * format,...)56 inline int snprintf(char* buffer, size_t size, const char* format, ...) {
57   va_list arguments;
58   va_start(arguments, format);
59   int result = vsnprintf(buffer, size, format, arguments);
60   va_end(arguments);
61   return result;
62 }
63 
64 // BSD-style safe and consistent string copy functions.
65 // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|.
66 // Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as
67 // long as |dst_size| is not 0.  Returns the length of |src| in characters.
68 // If the return value is >= dst_size, then the output was truncated.
69 // NOTE: All sizes are in number of characters, NOT in bytes.
70 BASE_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size);
71 BASE_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size);
72 
73 // Scan a wprintf format string to determine whether it's portable across a
74 // variety of systems.  This function only checks that the conversion
75 // specifiers used by the format string are supported and have the same meaning
76 // on a variety of systems.  It doesn't check for other errors that might occur
77 // within a format string.
78 //
79 // Nonportable conversion specifiers for wprintf are:
80 //  - 's' and 'c' without an 'l' length modifier.  %s and %c operate on char
81 //     data on all systems except Windows, which treat them as wchar_t data.
82 //     Use %ls and %lc for wchar_t data instead.
83 //  - 'S' and 'C', which operate on wchar_t data on all systems except Windows,
84 //     which treat them as char data.  Use %ls and %lc for wchar_t data
85 //     instead.
86 //  - 'F', which is not identified by Windows wprintf documentation.
87 //  - 'D', 'O', and 'U', which are deprecated and not available on all systems.
88 //     Use %ld, %lo, and %lu instead.
89 //
90 // Note that there is no portable conversion specifier for char data when
91 // working with wprintf.
92 //
93 // This function is intended to be called from base::vswprintf.
94 BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format);
95 
96 // ASCII-specific tolower.  The standard library's tolower is locale sensitive,
97 // so we don't want to use it here.
ToLowerASCII(Char c)98 template <class Char> inline Char ToLowerASCII(Char c) {
99   return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
100 }
101 
102 // ASCII-specific toupper.  The standard library's toupper is locale sensitive,
103 // so we don't want to use it here.
ToUpperASCII(Char c)104 template <class Char> inline Char ToUpperASCII(Char c) {
105   return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
106 }
107 
108 // Function objects to aid in comparing/searching strings.
109 
110 template<typename Char> struct CaseInsensitiveCompare {
111  public:
operatorCaseInsensitiveCompare112   bool operator()(Char x, Char y) const {
113     // TODO(darin): Do we really want to do locale sensitive comparisons here?
114     // See http://crbug.com/24917
115     return tolower(x) == tolower(y);
116   }
117 };
118 
119 template<typename Char> struct CaseInsensitiveCompareASCII {
120  public:
operatorCaseInsensitiveCompareASCII121   bool operator()(Char x, Char y) const {
122     return ToLowerASCII(x) == ToLowerASCII(y);
123   }
124 };
125 
126 // These threadsafe functions return references to globally unique empty
127 // strings.
128 //
129 // It is likely faster to construct a new empty string object (just a few
130 // instructions to set the length to 0) than to get the empty string singleton
131 // returned by these functions (which requires threadsafe singleton access).
132 //
133 // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT
134 // CONSTRUCTORS. There is only one case where you should use these: functions
135 // which need to return a string by reference (e.g. as a class member
136 // accessor), and don't have an empty string to use (e.g. in an error case).
137 // These should not be used as initializers, function arguments, or return
138 // values for functions which return by value or outparam.
139 BASE_EXPORT const std::string& EmptyString();
140 BASE_EXPORT const string16& EmptyString16();
141 
142 // Contains the set of characters representing whitespace in the corresponding
143 // encoding. Null-terminated.
144 BASE_EXPORT extern const wchar_t kWhitespaceWide[];
145 BASE_EXPORT extern const char16 kWhitespaceUTF16[];
146 BASE_EXPORT extern const char kWhitespaceASCII[];
147 
148 // Null-terminated string representing the UTF-8 byte order mark.
149 BASE_EXPORT extern const char kUtf8ByteOrderMark[];
150 
151 // Removes characters in |remove_chars| from anywhere in |input|.  Returns true
152 // if any characters were removed.  |remove_chars| must be null-terminated.
153 // NOTE: Safe to use the same variable for both |input| and |output|.
154 BASE_EXPORT bool RemoveChars(const string16& input,
155                              const char16 remove_chars[],
156                              string16* output);
157 BASE_EXPORT bool RemoveChars(const std::string& input,
158                              const char remove_chars[],
159                              std::string* output);
160 
161 // Replaces characters in |replace_chars| from anywhere in |input| with
162 // |replace_with|.  Each character in |replace_chars| will be replaced with
163 // the |replace_with| string.  Returns true if any characters were replaced.
164 // |replace_chars| must be null-terminated.
165 // NOTE: Safe to use the same variable for both |input| and |output|.
166 BASE_EXPORT bool ReplaceChars(const string16& input,
167                               const char16 replace_chars[],
168                               const string16& replace_with,
169                               string16* output);
170 BASE_EXPORT bool ReplaceChars(const std::string& input,
171                               const char replace_chars[],
172                               const std::string& replace_with,
173                               std::string* output);
174 
175 // Removes characters in |trim_chars| from the beginning and end of |input|.
176 // |trim_chars| must be null-terminated.
177 // NOTE: Safe to use the same variable for both |input| and |output|.
178 BASE_EXPORT bool TrimString(const string16& input,
179                             const char16 trim_chars[],
180                             string16* output);
181 BASE_EXPORT bool TrimString(const std::string& input,
182                             const char trim_chars[],
183                             std::string* output);
184 
185 // Truncates a string to the nearest UTF-8 character that will leave
186 // the string less than or equal to the specified byte size.
187 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input,
188                                         const size_t byte_size,
189                                         std::string* output);
190 
191 }  // namespace base
192 
193 #if defined(OS_WIN)
194 #include "base/strings/string_util_win.h"
195 #elif defined(OS_POSIX)
196 #include "base/strings/string_util_posix.h"
197 #else
198 #error Define string operations appropriately for your platform
199 #endif
200 
201 // Trims any whitespace from either end of the input string.  Returns where
202 // whitespace was found.
203 // The non-wide version has two functions:
204 // * TrimWhitespaceASCII()
205 //   This function is for ASCII strings and only looks for ASCII whitespace;
206 // Please choose the best one according to your usage.
207 // NOTE: Safe to use the same variable for both input and output.
208 enum TrimPositions {
209   TRIM_NONE     = 0,
210   TRIM_LEADING  = 1 << 0,
211   TRIM_TRAILING = 1 << 1,
212   TRIM_ALL      = TRIM_LEADING | TRIM_TRAILING,
213 };
214 BASE_EXPORT TrimPositions TrimWhitespace(const base::string16& input,
215                                          TrimPositions positions,
216                                          base::string16* output);
217 BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input,
218                                               TrimPositions positions,
219                                               std::string* output);
220 
221 // Deprecated. This function is only for backward compatibility and calls
222 // TrimWhitespaceASCII().
223 BASE_EXPORT TrimPositions TrimWhitespace(const std::string& input,
224                                          TrimPositions positions,
225                                          std::string* output);
226 
227 // Searches  for CR or LF characters.  Removes all contiguous whitespace
228 // strings that contain them.  This is useful when trying to deal with text
229 // copied from terminals.
230 // Returns |text|, with the following three transformations:
231 // (1) Leading and trailing whitespace is trimmed.
232 // (2) If |trim_sequences_with_line_breaks| is true, any other whitespace
233 //     sequences containing a CR or LF are trimmed.
234 // (3) All other whitespace sequences are converted to single spaces.
235 BASE_EXPORT base::string16 CollapseWhitespace(
236     const base::string16& text,
237     bool trim_sequences_with_line_breaks);
238 BASE_EXPORT std::string CollapseWhitespaceASCII(
239     const std::string& text,
240     bool trim_sequences_with_line_breaks);
241 
242 // Returns true if the passed string is empty or contains only white-space
243 // characters.
244 BASE_EXPORT bool ContainsOnlyWhitespaceASCII(const std::string& str);
245 BASE_EXPORT bool ContainsOnlyWhitespace(const base::string16& str);
246 
247 // Returns true if |input| is empty or contains only characters found in
248 // |characters|.
249 BASE_EXPORT bool ContainsOnlyChars(const base::string16& input,
250                                    const base::string16& characters);
251 BASE_EXPORT bool ContainsOnlyChars(const std::string& input,
252                                    const std::string& characters);
253 
254 // Converts to 7-bit ASCII by truncating. The result must be known to be ASCII
255 // beforehand.
256 BASE_EXPORT std::string WideToASCII(const std::wstring& wide);
257 BASE_EXPORT std::string UTF16ToASCII(const base::string16& utf16);
258 
259 // Returns true if the specified string matches the criteria. How can a wide
260 // string be 8-bit or UTF8? It contains only characters that are < 256 (in the
261 // first case) or characters that use only 8-bits and whose 8-bit
262 // representation looks like a UTF-8 string (the second case).
263 //
264 // Note that IsStringUTF8 checks not only if the input is structurally
265 // valid but also if it doesn't contain any non-character codepoint
266 // (e.g. U+FFFE). It's done on purpose because all the existing callers want
267 // to have the maximum 'discriminating' power from other encodings. If
268 // there's a use case for just checking the structural validity, we have to
269 // add a new function for that.
270 BASE_EXPORT bool IsStringUTF8(const std::string& str);
271 BASE_EXPORT bool IsStringASCII(const base::StringPiece& str);
272 BASE_EXPORT bool IsStringASCII(const base::string16& str);
273 
274 // Converts the elements of the given string.  This version uses a pointer to
275 // clearly differentiate it from the non-pointer variant.
StringToLowerASCII(str * s)276 template <class str> inline void StringToLowerASCII(str* s) {
277   for (typename str::iterator i = s->begin(); i != s->end(); ++i)
278     *i = base::ToLowerASCII(*i);
279 }
280 
StringToLowerASCII(const str & s)281 template <class str> inline str StringToLowerASCII(const str& s) {
282   // for std::string and std::wstring
283   str output(s);
284   StringToLowerASCII(&output);
285   return output;
286 }
287 
288 // Converts the elements of the given string.  This version uses a pointer to
289 // clearly differentiate it from the non-pointer variant.
StringToUpperASCII(str * s)290 template <class str> inline void StringToUpperASCII(str* s) {
291   for (typename str::iterator i = s->begin(); i != s->end(); ++i)
292     *i = base::ToUpperASCII(*i);
293 }
294 
StringToUpperASCII(const str & s)295 template <class str> inline str StringToUpperASCII(const str& s) {
296   // for std::string and std::wstring
297   str output(s);
298   StringToUpperASCII(&output);
299   return output;
300 }
301 
302 // Compare the lower-case form of the given string against the given ASCII
303 // string.  This is useful for doing checking if an input string matches some
304 // token, and it is optimized to avoid intermediate string copies.  This API is
305 // borrowed from the equivalent APIs in Mozilla.
306 BASE_EXPORT bool LowerCaseEqualsASCII(const std::string& a, const char* b);
307 BASE_EXPORT bool LowerCaseEqualsASCII(const base::string16& a, const char* b);
308 
309 // Same thing, but with string iterators instead.
310 BASE_EXPORT bool LowerCaseEqualsASCII(std::string::const_iterator a_begin,
311                                       std::string::const_iterator a_end,
312                                       const char* b);
313 BASE_EXPORT bool LowerCaseEqualsASCII(base::string16::const_iterator a_begin,
314                                       base::string16::const_iterator a_end,
315                                       const char* b);
316 BASE_EXPORT bool LowerCaseEqualsASCII(const char* a_begin,
317                                       const char* a_end,
318                                       const char* b);
319 BASE_EXPORT bool LowerCaseEqualsASCII(const base::char16* a_begin,
320                                       const base::char16* a_end,
321                                       const char* b);
322 
323 // Performs a case-sensitive string compare. The behavior is undefined if both
324 // strings are not ASCII.
325 BASE_EXPORT bool EqualsASCII(const base::string16& a, const base::StringPiece& b);
326 
327 // Returns true if str starts with search, or false otherwise.
328 BASE_EXPORT bool StartsWithASCII(const std::string& str,
329                                  const std::string& search,
330                                  bool case_sensitive);
331 BASE_EXPORT bool StartsWith(const base::string16& str,
332                             const base::string16& search,
333                             bool case_sensitive);
334 
335 // Returns true if str ends with search, or false otherwise.
336 BASE_EXPORT bool EndsWith(const std::string& str,
337                           const std::string& search,
338                           bool case_sensitive);
339 BASE_EXPORT bool EndsWith(const base::string16& str,
340                           const base::string16& search,
341                           bool case_sensitive);
342 
343 
344 // Determines the type of ASCII character, independent of locale (the C
345 // library versions will change based on locale).
346 template <typename Char>
IsAsciiWhitespace(Char c)347 inline bool IsAsciiWhitespace(Char c) {
348   return c == ' ' || c == '\r' || c == '\n' || c == '\t';
349 }
350 template <typename Char>
IsAsciiAlpha(Char c)351 inline bool IsAsciiAlpha(Char c) {
352   return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
353 }
354 template <typename Char>
IsAsciiDigit(Char c)355 inline bool IsAsciiDigit(Char c) {
356   return c >= '0' && c <= '9';
357 }
358 
359 template <typename Char>
IsHexDigit(Char c)360 inline bool IsHexDigit(Char c) {
361   return (c >= '0' && c <= '9') ||
362          (c >= 'A' && c <= 'F') ||
363          (c >= 'a' && c <= 'f');
364 }
365 
366 template <typename Char>
HexDigitToInt(Char c)367 inline Char HexDigitToInt(Char c) {
368   DCHECK(IsHexDigit(c));
369   if (c >= '0' && c <= '9')
370     return c - '0';
371   if (c >= 'A' && c <= 'F')
372     return c - 'A' + 10;
373   if (c >= 'a' && c <= 'f')
374     return c - 'a' + 10;
375   return 0;
376 }
377 
378 // Returns true if it's a whitespace character.
IsWhitespace(wchar_t c)379 inline bool IsWhitespace(wchar_t c) {
380   return wcschr(base::kWhitespaceWide, c) != NULL;
381 }
382 
383 // Return a byte string in human-readable format with a unit suffix. Not
384 // appropriate for use in any UI; use of FormatBytes and friends in ui/base is
385 // highly recommended instead. TODO(avi): Figure out how to get callers to use
386 // FormatBytes instead; remove this.
387 BASE_EXPORT base::string16 FormatBytesUnlocalized(int64 bytes);
388 
389 // Starting at |start_offset| (usually 0), replace the first instance of
390 // |find_this| with |replace_with|.
391 BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
392     base::string16* str,
393     base::string16::size_type start_offset,
394     const base::string16& find_this,
395     const base::string16& replace_with);
396 BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
397     std::string* str,
398     std::string::size_type start_offset,
399     const std::string& find_this,
400     const std::string& replace_with);
401 
402 // Starting at |start_offset| (usually 0), look through |str| and replace all
403 // instances of |find_this| with |replace_with|.
404 //
405 // This does entire substrings; use std::replace in <algorithm> for single
406 // characters, for example:
407 //   std::replace(str.begin(), str.end(), 'a', 'b');
408 BASE_EXPORT void ReplaceSubstringsAfterOffset(
409     base::string16* str,
410     base::string16::size_type start_offset,
411     const base::string16& find_this,
412     const base::string16& replace_with);
413 BASE_EXPORT void ReplaceSubstringsAfterOffset(
414     std::string* str,
415     std::string::size_type start_offset,
416     const std::string& find_this,
417     const std::string& replace_with);
418 
419 // Reserves enough memory in |str| to accommodate |length_with_null| characters,
420 // sets the size of |str| to |length_with_null - 1| characters, and returns a
421 // pointer to the underlying contiguous array of characters.  This is typically
422 // used when calling a function that writes results into a character array, but
423 // the caller wants the data to be managed by a string-like object.  It is
424 // convenient in that is can be used inline in the call, and fast in that it
425 // avoids copying the results of the call from a char* into a string.
426 //
427 // |length_with_null| must be at least 2, since otherwise the underlying string
428 // would have size 0, and trying to access &((*str)[0]) in that case can result
429 // in a number of problems.
430 //
431 // Internally, this takes linear time because the resize() call 0-fills the
432 // underlying array for potentially all
433 // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes.  Ideally we
434 // could avoid this aspect of the resize() call, as we expect the caller to
435 // immediately write over this memory, but there is no other way to set the size
436 // of the string, and not doing that will mean people who access |str| rather
437 // than str.c_str() will get back a string of whatever size |str| had on entry
438 // to this function (probably 0).
439 template <class string_type>
WriteInto(string_type * str,size_t length_with_null)440 inline typename string_type::value_type* WriteInto(string_type* str,
441                                                    size_t length_with_null) {
442   DCHECK_GT(length_with_null, 1u);
443   str->reserve(length_with_null);
444   str->resize(length_with_null - 1);
445   return &((*str)[0]);
446 }
447 
448 //-----------------------------------------------------------------------------
449 
450 // Splits a string into its fields delimited by any of the characters in
451 // |delimiters|.  Each field is added to the |tokens| vector.  Returns the
452 // number of tokens found.
453 BASE_EXPORT size_t Tokenize(const base::string16& str,
454                             const base::string16& delimiters,
455                             std::vector<base::string16>* tokens);
456 BASE_EXPORT size_t Tokenize(const std::string& str,
457                             const std::string& delimiters,
458                             std::vector<std::string>* tokens);
459 BASE_EXPORT size_t Tokenize(const base::StringPiece& str,
460                             const base::StringPiece& delimiters,
461                             std::vector<base::StringPiece>* tokens);
462 
463 // Does the opposite of SplitString().
464 BASE_EXPORT base::string16 JoinString(const std::vector<base::string16>& parts,
465                                       base::char16 s);
466 BASE_EXPORT std::string JoinString(
467     const std::vector<std::string>& parts, char s);
468 
469 // Join |parts| using |separator|.
470 BASE_EXPORT std::string JoinString(
471     const std::vector<std::string>& parts,
472     const std::string& separator);
473 BASE_EXPORT base::string16 JoinString(
474     const std::vector<base::string16>& parts,
475     const base::string16& separator);
476 
477 // Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively.
478 // Additionally, any number of consecutive '$' characters is replaced by that
479 // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be
480 // NULL. This only allows you to use up to nine replacements.
481 BASE_EXPORT base::string16 ReplaceStringPlaceholders(
482     const base::string16& format_string,
483     const std::vector<base::string16>& subst,
484     std::vector<size_t>* offsets);
485 
486 BASE_EXPORT std::string ReplaceStringPlaceholders(
487     const base::StringPiece& format_string,
488     const std::vector<std::string>& subst,
489     std::vector<size_t>* offsets);
490 
491 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL.
492 BASE_EXPORT base::string16 ReplaceStringPlaceholders(
493     const base::string16& format_string,
494     const base::string16& a,
495     size_t* offset);
496 
497 // Returns true if the string passed in matches the pattern. The pattern
498 // string can contain wildcards like * and ?
499 // The backslash character (\) is an escape character for * and ?
500 // We limit the patterns to having a max of 16 * or ? characters.
501 // ? matches 0 or 1 character, while * matches 0 or more characters.
502 BASE_EXPORT bool MatchPattern(const base::StringPiece& string,
503                               const base::StringPiece& pattern);
504 BASE_EXPORT bool MatchPattern(const base::string16& string,
505                               const base::string16& pattern);
506 
507 // Hack to convert any char-like type to its unsigned counterpart.
508 // For example, it will convert char, signed char and unsigned char to unsigned
509 // char.
510 template<typename T>
511 struct ToUnsigned {
512   typedef T Unsigned;
513 };
514 
515 template<>
516 struct ToUnsigned<char> {
517   typedef unsigned char Unsigned;
518 };
519 template<>
520 struct ToUnsigned<signed char> {
521   typedef unsigned char Unsigned;
522 };
523 template<>
524 struct ToUnsigned<wchar_t> {
525 #if defined(WCHAR_T_IS_UTF16)
526   typedef unsigned short Unsigned;
527 #elif defined(WCHAR_T_IS_UTF32)
528   typedef uint32 Unsigned;
529 #endif
530 };
531 template<>
532 struct ToUnsigned<short> {
533   typedef unsigned short Unsigned;
534 };
535 
536 #endif  // BASE_STRINGS_STRING_UTIL_H_
537