Home | History | Annotate | Download | only in strings
      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 base::StringPiece16& remove_chars,
    154                              string16* output);
    155 BASE_EXPORT bool RemoveChars(const std::string& input,
    156                              const base::StringPiece& 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 base::StringPiece16& replace_chars,
    166                               const string16& replace_with,
    167                               string16* output);
    168 BASE_EXPORT bool ReplaceChars(const std::string& input,
    169                               const base::StringPiece& 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 base::StringPiece16& trim_chars,
    178                             string16* output);
    179 BASE_EXPORT bool TrimString(const std::string& input,
    180                             const base::StringPiece& 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 // Converts the elements of the given string.  This version uses a pointer to
    253 // clearly differentiate it from the non-pointer variant.
    254 template <class str> inline void StringToLowerASCII(str* s) {
    255   for (typename str::iterator i = s->begin(); i != s->end(); ++i)
    256     *i = ToLowerASCII(*i);
    257 }
    258 
    259 template <class str> inline str StringToLowerASCII(const str& s) {
    260   // for std::string and std::wstring
    261   str output(s);
    262   StringToLowerASCII(&output);
    263   return output;
    264 }
    265 
    266 }  // namespace base
    267 
    268 #if defined(OS_WIN)
    269 #include "base/strings/string_util_win.h"
    270 #elif defined(OS_POSIX)
    271 #include "base/strings/string_util_posix.h"
    272 #else
    273 #error Define string operations appropriately for your platform
    274 #endif
    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     size_t start_offset,
    382     const base::string16& find_this,
    383     const base::string16& replace_with);
    384 BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
    385     std::string* str,
    386     size_t 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     size_t start_offset,
    399     const base::string16& find_this,
    400     const base::string16& replace_with);
    401 BASE_EXPORT void ReplaceSubstringsAfterOffset(std::string* str,
    402                                               size_t start_offset,
    403                                               const std::string& find_this,
    404                                               const std::string& replace_with);
    405 
    406 // Reserves enough memory in |str| to accommodate |length_with_null| characters,
    407 // sets the size of |str| to |length_with_null - 1| characters, and returns a
    408 // pointer to the underlying contiguous array of characters.  This is typically
    409 // used when calling a function that writes results into a character array, but
    410 // the caller wants the data to be managed by a string-like object.  It is
    411 // convenient in that is can be used inline in the call, and fast in that it
    412 // avoids copying the results of the call from a char* into a string.
    413 //
    414 // |length_with_null| must be at least 2, since otherwise the underlying string
    415 // would have size 0, and trying to access &((*str)[0]) in that case can result
    416 // in a number of problems.
    417 //
    418 // Internally, this takes linear time because the resize() call 0-fills the
    419 // underlying array for potentially all
    420 // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes.  Ideally we
    421 // could avoid this aspect of the resize() call, as we expect the caller to
    422 // immediately write over this memory, but there is no other way to set the size
    423 // of the string, and not doing that will mean people who access |str| rather
    424 // than str.c_str() will get back a string of whatever size |str| had on entry
    425 // to this function (probably 0).
    426 template <class string_type>
    427 inline typename string_type::value_type* WriteInto(string_type* str,
    428                                                    size_t length_with_null) {
    429   DCHECK_GT(length_with_null, 1u);
    430   str->reserve(length_with_null);
    431   str->resize(length_with_null - 1);
    432   return &((*str)[0]);
    433 }
    434 
    435 //-----------------------------------------------------------------------------
    436 
    437 // Splits a string into its fields delimited by any of the characters in
    438 // |delimiters|.  Each field is added to the |tokens| vector.  Returns the
    439 // number of tokens found.
    440 BASE_EXPORT size_t Tokenize(const base::string16& str,
    441                             const base::string16& delimiters,
    442                             std::vector<base::string16>* tokens);
    443 BASE_EXPORT size_t Tokenize(const std::string& str,
    444                             const std::string& delimiters,
    445                             std::vector<std::string>* tokens);
    446 BASE_EXPORT size_t Tokenize(const base::StringPiece& str,
    447                             const base::StringPiece& delimiters,
    448                             std::vector<base::StringPiece>* tokens);
    449 
    450 // Does the opposite of SplitString().
    451 BASE_EXPORT base::string16 JoinString(const std::vector<base::string16>& parts,
    452                                       base::char16 s);
    453 BASE_EXPORT std::string JoinString(
    454     const std::vector<std::string>& parts, char s);
    455 
    456 // Join |parts| using |separator|.
    457 BASE_EXPORT std::string JoinString(
    458     const std::vector<std::string>& parts,
    459     const std::string& separator);
    460 BASE_EXPORT base::string16 JoinString(
    461     const std::vector<base::string16>& parts,
    462     const base::string16& separator);
    463 
    464 // Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively.
    465 // Additionally, any number of consecutive '$' characters is replaced by that
    466 // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be
    467 // NULL. This only allows you to use up to nine replacements.
    468 BASE_EXPORT base::string16 ReplaceStringPlaceholders(
    469     const base::string16& format_string,
    470     const std::vector<base::string16>& subst,
    471     std::vector<size_t>* offsets);
    472 
    473 BASE_EXPORT std::string ReplaceStringPlaceholders(
    474     const base::StringPiece& format_string,
    475     const std::vector<std::string>& subst,
    476     std::vector<size_t>* offsets);
    477 
    478 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL.
    479 BASE_EXPORT base::string16 ReplaceStringPlaceholders(
    480     const base::string16& format_string,
    481     const base::string16& a,
    482     size_t* offset);
    483 
    484 // Returns true if the string passed in matches the pattern. The pattern
    485 // string can contain wildcards like * and ?
    486 // The backslash character (\) is an escape character for * and ?
    487 // We limit the patterns to having a max of 16 * or ? characters.
    488 // ? matches 0 or 1 character, while * matches 0 or more characters.
    489 BASE_EXPORT bool MatchPattern(const base::StringPiece& string,
    490                               const base::StringPiece& pattern);
    491 BASE_EXPORT bool MatchPattern(const base::string16& string,
    492                               const base::string16& pattern);
    493 
    494 // Hack to convert any char-like type to its unsigned counterpart.
    495 // For example, it will convert char, signed char and unsigned char to unsigned
    496 // char.
    497 template<typename T>
    498 struct ToUnsigned {
    499   typedef T Unsigned;
    500 };
    501 
    502 template<>
    503 struct ToUnsigned<char> {
    504   typedef unsigned char Unsigned;
    505 };
    506 template<>
    507 struct ToUnsigned<signed char> {
    508   typedef unsigned char Unsigned;
    509 };
    510 template<>
    511 struct ToUnsigned<wchar_t> {
    512 #if defined(WCHAR_T_IS_UTF16)
    513   typedef unsigned short Unsigned;
    514 #elif defined(WCHAR_T_IS_UTF32)
    515   typedef uint32 Unsigned;
    516 #endif
    517 };
    518 template<>
    519 struct ToUnsigned<short> {
    520   typedef unsigned short Unsigned;
    521 };
    522 
    523 #endif  // BASE_STRINGS_STRING_UTIL_H_
    524