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.
7
#ifndef BASE_STRINGS_STRING_UTIL_H_
8
#define BASE_STRINGS_STRING_UTIL_H_
11
#include <stdarg.h> // va_list
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.
22
// Safe standard library wrappers for all platforms.
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.
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);
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);
41
// Same as strncmp but for char16 strings.
42
int strncmp16(const char16* s1, const char16* s2, size_t count);
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)
50
// Some of these implementations need to be inlined.
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, ...)
56
inline int snprintf(char* buffer, size_t size, const char* format, ...) {
58
va_start(arguments, format);
59
int result = vsnprintf(buffer, size, format, arguments);
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);
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.
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
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.
90
// Note that there is no portable conversion specifier for char data when
91
// working with wprintf.
93
// This function is intended to be called from base::vswprintf.
94
BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format);
96
// ASCII-specific tolower. The standard library's tolower is locale sensitive,
97
// so we don't want to use it here.
98
template <class Char> inline Char ToLowerASCII(Char c) {
99
return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
102
// ASCII-specific toupper. The standard library's toupper is locale sensitive,
103
// so we don't want to use it here.
104
template <class Char> inline Char ToUpperASCII(Char c) {
105
return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
108
// Function objects to aid in comparing/searching strings.
110
template<typename Char> struct CaseInsensitiveCompare {
112
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);
119
template<typename Char> struct CaseInsensitiveCompareASCII {
121
bool operator()(Char x, Char y) const {
122
return ToLowerASCII(x) == ToLowerASCII(y);
126
// These threadsafe functions return references to globally unique empty
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).
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();
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[];
148
// Null-terminated string representing the UTF-8 byte order mark.
149
BASE_EXPORT extern const char kUtf8ByteOrderMark[];
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[],
157
BASE_EXPORT bool RemoveChars(const std::string& input,
158
const char remove_chars[],
159
std::string* output);
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,
170
BASE_EXPORT bool ReplaceChars(const std::string& input,
171
const char replace_chars[],
172
const std::string& replace_with,
173
std::string* output);
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[],
181
BASE_EXPORT bool TrimString(const std::string& input,
182
const char trim_chars[],
183
std::string* output);
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);
191
// Trims any whitespace from either end of the input string. Returns where
192
// whitespace was found.
193
// The non-wide version has two functions:
194
// * TrimWhitespaceASCII()
195
// This function is for ASCII strings and only looks for ASCII whitespace;
196
// Please choose the best one according to your usage.
197
// NOTE: Safe to use the same variable for both input and output.
200
TRIM_LEADING = 1 << 0,
201
TRIM_TRAILING = 1 << 1,
202
TRIM_ALL = TRIM_LEADING | TRIM_TRAILING,
204
BASE_EXPORT TrimPositions TrimWhitespace(const string16& input,
205
TrimPositions positions,
206
base::string16* output);
207
BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input,
208
TrimPositions positions,
209
std::string* output);
211
// Deprecated. This function is only for backward compatibility and calls
212
// TrimWhitespaceASCII().
213
BASE_EXPORT TrimPositions TrimWhitespace(const std::string& input,
214
TrimPositions positions,
215
std::string* output);
220
#include "base/strings/string_util_win.h"
221
#elif defined(OS_POSIX)
222
#include "base/strings/string_util_posix.h"
224
#error Define string operations appropriately for your platform
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);
242
// Returns true if the passed string is empty or contains only white-space
244
BASE_EXPORT bool ContainsOnlyWhitespaceASCII(const std::string& str);
245
BASE_EXPORT bool ContainsOnlyWhitespace(const base::string16& str);
247
// Returns true if |input| is empty or contains only characters found in
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);
254
// Converts to 7-bit ASCII by truncating. The result must be known to be ASCII
256
BASE_EXPORT std::string WideToASCII(const std::wstring& wide);
257
BASE_EXPORT std::string UTF16ToASCII(const base::string16& utf16);
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).
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);
274
// Converts the elements of the given string. This version uses a pointer to
275
// clearly differentiate it from the non-pointer variant.
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);
281
template <class str> inline str StringToLowerASCII(const str& s) {
282
// for std::string and std::wstring
284
StringToLowerASCII(&output);
288
// Converts the elements of the given string. This version uses a pointer to
289
// clearly differentiate it from the non-pointer variant.
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);
295
template <class str> inline str StringToUpperASCII(const str& s) {
296
// for std::string and std::wstring
298
StringToUpperASCII(&output);
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);
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,
313
BASE_EXPORT bool LowerCaseEqualsASCII(base::string16::const_iterator a_begin,
314
base::string16::const_iterator a_end,
316
BASE_EXPORT bool LowerCaseEqualsASCII(const char* a_begin,
319
BASE_EXPORT bool LowerCaseEqualsASCII(const base::char16* a_begin,
320
const base::char16* a_end,
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);
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);
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);
344
// Determines the type of ASCII character, independent of locale (the C
345
// library versions will change based on locale).
346
template <typename Char>
347
inline bool IsAsciiWhitespace(Char c) {
348
return c == ' ' || c == '\r' || c == '\n' || c == '\t';
350
template <typename Char>
351
inline bool IsAsciiAlpha(Char c) {
352
return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
354
template <typename Char>
355
inline bool IsAsciiDigit(Char c) {
356
return c >= '0' && c <= '9';
359
template <typename Char>
360
inline bool IsHexDigit(Char c) {
361
return (c >= '0' && c <= '9') ||
362
(c >= 'A' && c <= 'F') ||
363
(c >= 'a' && c <= 'f');
366
template <typename Char>
367
inline Char HexDigitToInt(Char c) {
368
DCHECK(IsHexDigit(c));
369
if (c >= '0' && c <= '9')
371
if (c >= 'A' && c <= 'F')
373
if (c >= 'a' && c <= 'f')
378
// Returns true if it's a whitespace character.
379
inline bool IsWhitespace(wchar_t c) {
380
return wcschr(base::kWhitespaceWide, c) != NULL;
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);
389
// Starting at |start_offset| (usually 0), replace the first instance of
390
// |find_this| with |replace_with|.
391
BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
393
base::string16::size_type start_offset,
394
const base::string16& find_this,
395
const base::string16& replace_with);
396
BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
398
std::string::size_type start_offset,
399
const std::string& find_this,
400
const std::string& replace_with);
402
// Starting at |start_offset| (usually 0), look through |str| and replace all
403
// instances of |find_this| with |replace_with|.
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(
410
base::string16::size_type start_offset,
411
const base::string16& find_this,
412
const base::string16& replace_with);
413
BASE_EXPORT void ReplaceSubstringsAfterOffset(
415
std::string::size_type start_offset,
416
const std::string& find_this,
417
const std::string& replace_with);
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.
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.
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>
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);
448
//-----------------------------------------------------------------------------
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);
463
// Does the opposite of SplitString().
464
BASE_EXPORT base::string16 JoinString(const std::vector<base::string16>& parts,
466
BASE_EXPORT std::string JoinString(
467
const std::vector<std::string>& parts, char s);
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);
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);
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);
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,
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);
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
516
struct ToUnsigned<char> {
517
typedef unsigned char Unsigned;
520
struct ToUnsigned<signed char> {
521
typedef unsigned char Unsigned;
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;
532
struct ToUnsigned<short> {
533
typedef unsigned short Unsigned;
536
#endif // BASE_STRINGS_STRING_UTIL_H_