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.
5// This file defines utility functions for working with strings.
10#include <ctype.h>
11#include <stdarg.h>   // va_list
13#include <string>
14#include <vector>
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.
22namespace base {
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.
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.
32int strcasecmp(const char* s1, const char* s2);
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.
37int strncasecmp(const char* s1, const char* s2, size_t count);
39// Same as strncmp but for char16 strings.
40int strncmp16(const char16* s1, const char16* s2, size_t count);
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.
45int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments)
46    PRINTF_FORMAT(3, 0);
48// Some of these implementations need to be inlined.
50// We separate the declaration from the implementation of this inline
51// function just so the PRINTF_FORMAT works.
52inline int snprintf(char* buffer, size_t size, const char* format, ...)
53    PRINTF_FORMAT(3, 4);
54inline 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;
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.
68BASE_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size);
69BASE_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size);
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.
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.
88// Note that there is no portable conversion specifier for char data when
89// working with wprintf.
91// This function is intended to be called from base::vswprintf.
92BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format);
94// ASCII-specific tolower.  The standard library's tolower is locale sensitive,
95// so we don't want to use it here.
96template <class Char> inline Char ToLowerASCII(Char c) {
97  return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
100// ASCII-specific toupper.  The standard library's toupper is locale sensitive,
101// so we don't want to use it here.
102template <class Char> inline Char ToUpperASCII(Char c) {
103  return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
106// Function objects to aid in comparing/searching strings.
108template<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  }
117template<typename Char> struct CaseInsensitiveCompareASCII {
118 public:
119  bool operator()(Char x, Char y) const {
120    return ToLowerASCII(x) == ToLowerASCII(y);
121  }
124// These threadsafe functions return references to globally unique empty
125// strings.
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).
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.
137BASE_EXPORT const std::string& EmptyString();
138BASE_EXPORT const string16& EmptyString16();
140// Contains the set of characters representing whitespace in the corresponding
141// encoding. Null-terminated.
142BASE_EXPORT extern const wchar_t kWhitespaceWide[];
143BASE_EXPORT extern const char16 kWhitespaceUTF16[];
144BASE_EXPORT extern const char kWhitespaceASCII[];
146// Null-terminated string representing the UTF-8 byte order mark.
147BASE_EXPORT extern const char kUtf8ByteOrderMark[];
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|.
152BASE_EXPORT bool RemoveChars(const string16& input,
153                             const base::StringPiece16& remove_chars,
154                             string16* output);
155BASE_EXPORT bool RemoveChars(const std::string& input,
156                             const base::StringPiece& remove_chars,
157                             std::string* output);
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|.
164BASE_EXPORT bool ReplaceChars(const string16& input,
165                              const base::StringPiece16& replace_chars,
166                              const string16& replace_with,
167                              string16* output);
168BASE_EXPORT bool ReplaceChars(const std::string& input,
169                              const base::StringPiece& replace_chars,
170                              const std::string& replace_with,
171                              std::string* output);
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|.
176BASE_EXPORT bool TrimString(const string16& input,
177                            const base::StringPiece16& trim_chars,
178                            string16* output);
179BASE_EXPORT bool TrimString(const std::string& input,
180                            const base::StringPiece& trim_chars,
181                            std::string* output);
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.
185BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input,
186                                        const size_t byte_size,
187                                        std::string* output);
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.
196enum TrimPositions {
197  TRIM_NONE     = 0,
198  TRIM_LEADING  = 1 << 0,
199  TRIM_TRAILING = 1 << 1,
202BASE_EXPORT TrimPositions TrimWhitespace(const string16& input,
203                                         TrimPositions positions,
204                                         base::string16* output);
205BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input,
206                                              TrimPositions positions,
207                                              std::string* output);
209// Deprecated. This function is only for backward compatibility and calls
210// TrimWhitespaceASCII().
211BASE_EXPORT TrimPositions TrimWhitespace(const std::string& input,
212                                         TrimPositions positions,
213                                         std::string* output);
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.
223BASE_EXPORT string16 CollapseWhitespace(
224    const string16& text,
225    bool trim_sequences_with_line_breaks);
226BASE_EXPORT std::string CollapseWhitespaceASCII(
227    const std::string& text,
228    bool trim_sequences_with_line_breaks);
230// Returns true if |input| is empty or contains only characters found in
231// |characters|.
232BASE_EXPORT bool ContainsOnlyChars(const StringPiece& input,
233                                   const StringPiece& characters);
234BASE_EXPORT bool ContainsOnlyChars(const StringPiece16& input,
235                                   const StringPiece16& characters);
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).
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.
248BASE_EXPORT bool IsStringUTF8(const std::string& str);
249BASE_EXPORT bool IsStringASCII(const StringPiece& str);
250BASE_EXPORT bool IsStringASCII(const string16& str);
252// Converts the elements of the given string.  This version uses a pointer to
253// clearly differentiate it from the non-pointer variant.
254template <class str> inline void StringToLowerASCII(str* s) {
255  for (typename str::iterator i = s->begin(); i != s->end(); ++i)
256    *i = ToLowerASCII(*i);
259template <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;
266}  // namespace base
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"
273#error Define string operations appropriately for your platform
276// Converts the elements of the given string.  This version uses a pointer to
277// clearly differentiate it from the non-pointer variant.
278template <class str> inline void StringToUpperASCII(str* s) {
279  for (typename str::iterator i = s->begin(); i != s->end(); ++i)
280    *i = base::ToUpperASCII(*i);
283template <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;
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.
294BASE_EXPORT bool LowerCaseEqualsASCII(const std::string& a, const char* b);
295BASE_EXPORT bool LowerCaseEqualsASCII(const base::string16& a, const char* b);
297// Same thing, but with string iterators instead.
298BASE_EXPORT bool LowerCaseEqualsASCII(std::string::const_iterator a_begin,
299                                      std::string::const_iterator a_end,
300                                      const char* b);
301BASE_EXPORT bool LowerCaseEqualsASCII(base::string16::const_iterator a_begin,
302                                      base::string16::const_iterator a_end,
303                                      const char* b);
304BASE_EXPORT bool LowerCaseEqualsASCII(const char* a_begin,
305                                      const char* a_end,
306                                      const char* b);
307BASE_EXPORT bool LowerCaseEqualsASCII(const base::char16* a_begin,
308                                      const base::char16* a_end,
309                                      const char* b);
311// Performs a case-sensitive string compare. The behavior is undefined if both
312// strings are not ASCII.
313BASE_EXPORT bool EqualsASCII(const base::string16& a, const base::StringPiece& b);
315// Returns true if str starts with search, or false otherwise.
316BASE_EXPORT bool StartsWithASCII(const std::string& str,
317                                 const std::string& search,
318                                 bool case_sensitive);
319BASE_EXPORT bool StartsWith(const base::string16& str,
320                            const base::string16& search,
321                            bool case_sensitive);
323// Returns true if str ends with search, or false otherwise.
324BASE_EXPORT bool EndsWith(const std::string& str,
325                          const std::string& search,
326                          bool case_sensitive);
327BASE_EXPORT bool EndsWith(const base::string16& str,
328                          const base::string16& search,
329                          bool case_sensitive);
332// Determines the type of ASCII character, independent of locale (the C
333// library versions will change based on locale).
334template <typename Char>
335inline bool IsAsciiWhitespace(Char c) {
336  return c == ' ' || c == '\r' || c == '\n' || c == '\t';
338template <typename Char>
339inline bool IsAsciiAlpha(Char c) {
340  return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
342template <typename Char>
343inline bool IsAsciiDigit(Char c) {
344  return c >= '0' && c <= '9';
347template <typename Char>
348inline bool IsHexDigit(Char c) {
349  return (c >= '0' && c <= '9') ||
350         (c >= 'A' && c <= 'F') ||
351         (c >= 'a' && c <= 'f');
354template <typename Char>
355inline 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;
366// Returns true if it's a whitespace character.
367inline bool IsWhitespace(wchar_t c) {
368  return wcschr(base::kWhitespaceWide, c) != NULL;
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.
375BASE_EXPORT base::string16 FormatBytesUnlocalized(int64 bytes);
377// Starting at |start_offset| (usually 0), replace the first instance of
378// |find_this| with |replace_with|.
379BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
380    base::string16* str,
381    size_t start_offset,
382    const base::string16& find_this,
383    const base::string16& replace_with);
384BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
385    std::string* str,
386    size_t start_offset,
387    const std::string& find_this,
388    const std::string& replace_with);
390// Starting at |start_offset| (usually 0), look through |str| and replace all
391// instances of |find_this| with |replace_with|.
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');
396BASE_EXPORT void ReplaceSubstringsAfterOffset(
397    base::string16* str,
398    size_t start_offset,
399    const base::string16& find_this,
400    const base::string16& replace_with);
401BASE_EXPORT void ReplaceSubstringsAfterOffset(std::string* str,
402                                              size_t start_offset,
403                                              const std::string& find_this,
404                                              const std::string& replace_with);
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.
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.
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).
426template <class string_type>
427inline 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]);
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.
440BASE_EXPORT size_t Tokenize(const base::string16& str,
441                            const base::string16& delimiters,
442                            std::vector<base::string16>* tokens);
443BASE_EXPORT size_t Tokenize(const std::string& str,
444                            const std::string& delimiters,
445                            std::vector<std::string>* tokens);
446BASE_EXPORT size_t Tokenize(const base::StringPiece& str,
447                            const base::StringPiece& delimiters,
448                            std::vector<base::StringPiece>* tokens);
450// Does the opposite of SplitString().
451BASE_EXPORT base::string16 JoinString(const std::vector<base::string16>& parts,
452                                      base::char16 s);
453BASE_EXPORT std::string JoinString(
454    const std::vector<std::string>& parts, char s);
456// Join |parts| using |separator|.
457BASE_EXPORT std::string JoinString(
458    const std::vector<std::string>& parts,
459    const std::string& separator);
460BASE_EXPORT base::string16 JoinString(
461    const std::vector<base::string16>& parts,
462    const base::string16& separator);
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.
468BASE_EXPORT base::string16 ReplaceStringPlaceholders(
469    const base::string16& format_string,
470    const std::vector<base::string16>& subst,
471    std::vector<size_t>* offsets);
473BASE_EXPORT std::string ReplaceStringPlaceholders(
474    const base::StringPiece& format_string,
475    const std::vector<std::string>& subst,
476    std::vector<size_t>* offsets);
478// Single-string shortcut for ReplaceStringHolders. |offset| may be NULL.
479BASE_EXPORT base::string16 ReplaceStringPlaceholders(
480    const base::string16& format_string,
481    const base::string16& a,
482    size_t* offset);
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.
489BASE_EXPORT bool MatchPattern(const base::StringPiece& string,
490                              const base::StringPiece& pattern);
491BASE_EXPORT bool MatchPattern(const base::string16& string,
492                              const base::string16& pattern);
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.
497template<typename T>
498struct ToUnsigned {
499  typedef T Unsigned;
503struct ToUnsigned<char> {
504  typedef unsigned char Unsigned;
507struct ToUnsigned<signed char> {
508  typedef unsigned char Unsigned;
511struct 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;
519struct ToUnsigned<short> {
520  typedef unsigned short Unsigned;