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