Fix sort order of unlaunched apps on app list start page.
[chromium-blink-merge.git] / base / strings / string_util.h
blob5ab2ad5d5477f2a648aa081dda0ad2a5e859754d
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.
7 #ifndef BASE_STRINGS_STRING_UTIL_H_
8 #define BASE_STRINGS_STRING_UTIL_H_
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.
22 namespace 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.
32 int 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.
37 int strncasecmp(const char* s1, const char* s2, size_t count);
39 // Same as strncmp but for char16 strings.
40 int 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.
45 int 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.
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;
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);
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.
92 BASE_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.
96 template <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.
102 template <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.
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);
117 template<typename Char> struct CaseInsensitiveCompareASCII {
118 public:
119 bool operator()(Char x, Char y) const {
120 return ToLowerASCII(x) == ToLowerASCII(y);
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).
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();
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[];
146 // Null-terminated string representing the UTF-8 byte order mark.
147 BASE_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|.
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);
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);
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);
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);
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,
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);
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);
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);
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);
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.
249 // IsStringASCII assumes the input is likely all ASCII, and does not leave early
250 // if it is not the case.
251 BASE_EXPORT bool IsStringUTF8(const StringPiece& str);
252 BASE_EXPORT bool IsStringASCII(const StringPiece& str);
253 BASE_EXPORT bool IsStringASCII(const StringPiece16& str);
254 // A convenience adaptor for WebStrings, as they don't convert into
255 // StringPieces directly.
256 BASE_EXPORT bool IsStringASCII(const string16& str);
257 #if defined(WCHAR_T_IS_UTF32)
258 BASE_EXPORT bool IsStringASCII(const std::wstring& str);
259 #endif
261 // Converts the elements of the given string. This version uses a pointer to
262 // clearly differentiate it from the non-pointer variant.
263 template <class str> inline void StringToLowerASCII(str* s) {
264 for (typename str::iterator i = s->begin(); i != s->end(); ++i)
265 *i = ToLowerASCII(*i);
268 template <class str> inline str StringToLowerASCII(const str& s) {
269 // for std::string and std::wstring
270 str output(s);
271 StringToLowerASCII(&output);
272 return output;
275 } // namespace base
277 #if defined(OS_WIN)
278 #include "base/strings/string_util_win.h"
279 #elif defined(OS_POSIX)
280 #include "base/strings/string_util_posix.h"
281 #else
282 #error Define string operations appropriately for your platform
283 #endif
285 // Converts the elements of the given string. This version uses a pointer to
286 // clearly differentiate it from the non-pointer variant.
287 template <class str> inline void StringToUpperASCII(str* s) {
288 for (typename str::iterator i = s->begin(); i != s->end(); ++i)
289 *i = base::ToUpperASCII(*i);
292 template <class str> inline str StringToUpperASCII(const str& s) {
293 // for std::string and std::wstring
294 str output(s);
295 StringToUpperASCII(&output);
296 return output;
299 // Compare the lower-case form of the given string against the given ASCII
300 // string. This is useful for doing checking if an input string matches some
301 // token, and it is optimized to avoid intermediate string copies. This API is
302 // borrowed from the equivalent APIs in Mozilla.
303 BASE_EXPORT bool LowerCaseEqualsASCII(const std::string& a, const char* b);
304 BASE_EXPORT bool LowerCaseEqualsASCII(const base::string16& a, const char* b);
306 // Same thing, but with string iterators instead.
307 BASE_EXPORT bool LowerCaseEqualsASCII(std::string::const_iterator a_begin,
308 std::string::const_iterator a_end,
309 const char* b);
310 BASE_EXPORT bool LowerCaseEqualsASCII(base::string16::const_iterator a_begin,
311 base::string16::const_iterator a_end,
312 const char* b);
313 BASE_EXPORT bool LowerCaseEqualsASCII(const char* a_begin,
314 const char* a_end,
315 const char* b);
316 BASE_EXPORT bool LowerCaseEqualsASCII(const base::char16* a_begin,
317 const base::char16* a_end,
318 const char* b);
320 // Performs a case-sensitive string compare. The behavior is undefined if both
321 // strings are not ASCII.
322 BASE_EXPORT bool EqualsASCII(const base::string16& a, const base::StringPiece& b);
324 // Returns true if str starts with search, or false otherwise.
325 BASE_EXPORT bool StartsWithASCII(const std::string& str,
326 const std::string& search,
327 bool case_sensitive);
328 BASE_EXPORT bool StartsWith(const base::string16& str,
329 const base::string16& search,
330 bool case_sensitive);
332 // Returns true if str ends with search, or false otherwise.
333 BASE_EXPORT bool EndsWith(const std::string& str,
334 const std::string& search,
335 bool case_sensitive);
336 BASE_EXPORT bool EndsWith(const base::string16& str,
337 const base::string16& search,
338 bool case_sensitive);
341 // Determines the type of ASCII character, independent of locale (the C
342 // library versions will change based on locale).
343 template <typename Char>
344 inline bool IsAsciiWhitespace(Char c) {
345 return c == ' ' || c == '\r' || c == '\n' || c == '\t';
347 template <typename Char>
348 inline bool IsAsciiAlpha(Char c) {
349 return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
351 template <typename Char>
352 inline bool IsAsciiDigit(Char c) {
353 return c >= '0' && c <= '9';
356 template <typename Char>
357 inline bool IsHexDigit(Char c) {
358 return (c >= '0' && c <= '9') ||
359 (c >= 'A' && c <= 'F') ||
360 (c >= 'a' && c <= 'f');
363 template <typename Char>
364 inline char HexDigitToInt(Char c) {
365 DCHECK(IsHexDigit(c));
366 if (c >= '0' && c <= '9')
367 return static_cast<char>(c - '0');
368 if (c >= 'A' && c <= 'F')
369 return static_cast<char>(c - 'A' + 10);
370 if (c >= 'a' && c <= 'f')
371 return static_cast<char>(c - 'a' + 10);
372 return 0;
375 // Returns true if it's a whitespace character.
376 inline bool IsWhitespace(wchar_t c) {
377 return wcschr(base::kWhitespaceWide, c) != NULL;
380 // Return a byte string in human-readable format with a unit suffix. Not
381 // appropriate for use in any UI; use of FormatBytes and friends in ui/base is
382 // highly recommended instead. TODO(avi): Figure out how to get callers to use
383 // FormatBytes instead; remove this.
384 BASE_EXPORT base::string16 FormatBytesUnlocalized(int64 bytes);
386 // Starting at |start_offset| (usually 0), replace the first instance of
387 // |find_this| with |replace_with|.
388 BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
389 base::string16* str,
390 size_t start_offset,
391 const base::string16& find_this,
392 const base::string16& replace_with);
393 BASE_EXPORT void ReplaceFirstSubstringAfterOffset(
394 std::string* str,
395 size_t start_offset,
396 const std::string& find_this,
397 const std::string& replace_with);
399 // Starting at |start_offset| (usually 0), look through |str| and replace all
400 // instances of |find_this| with |replace_with|.
402 // This does entire substrings; use std::replace in <algorithm> for single
403 // characters, for example:
404 // std::replace(str.begin(), str.end(), 'a', 'b');
405 BASE_EXPORT void ReplaceSubstringsAfterOffset(
406 base::string16* str,
407 size_t start_offset,
408 const base::string16& find_this,
409 const base::string16& replace_with);
410 BASE_EXPORT void ReplaceSubstringsAfterOffset(std::string* str,
411 size_t start_offset,
412 const std::string& find_this,
413 const std::string& replace_with);
415 // Reserves enough memory in |str| to accommodate |length_with_null| characters,
416 // sets the size of |str| to |length_with_null - 1| characters, and returns a
417 // pointer to the underlying contiguous array of characters. This is typically
418 // used when calling a function that writes results into a character array, but
419 // the caller wants the data to be managed by a string-like object. It is
420 // convenient in that is can be used inline in the call, and fast in that it
421 // avoids copying the results of the call from a char* into a string.
423 // |length_with_null| must be at least 2, since otherwise the underlying string
424 // would have size 0, and trying to access &((*str)[0]) in that case can result
425 // in a number of problems.
427 // Internally, this takes linear time because the resize() call 0-fills the
428 // underlying array for potentially all
429 // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes. Ideally we
430 // could avoid this aspect of the resize() call, as we expect the caller to
431 // immediately write over this memory, but there is no other way to set the size
432 // of the string, and not doing that will mean people who access |str| rather
433 // than str.c_str() will get back a string of whatever size |str| had on entry
434 // to this function (probably 0).
435 template <class string_type>
436 inline typename string_type::value_type* WriteInto(string_type* str,
437 size_t length_with_null) {
438 DCHECK_GT(length_with_null, 1u);
439 str->reserve(length_with_null);
440 str->resize(length_with_null - 1);
441 return &((*str)[0]);
444 //-----------------------------------------------------------------------------
446 // Splits a string into its fields delimited by any of the characters in
447 // |delimiters|. Each field is added to the |tokens| vector. Returns the
448 // number of tokens found.
449 BASE_EXPORT size_t Tokenize(const base::string16& str,
450 const base::string16& delimiters,
451 std::vector<base::string16>* tokens);
452 BASE_EXPORT size_t Tokenize(const std::string& str,
453 const std::string& delimiters,
454 std::vector<std::string>* tokens);
455 BASE_EXPORT size_t Tokenize(const base::StringPiece& str,
456 const base::StringPiece& delimiters,
457 std::vector<base::StringPiece>* tokens);
459 // Does the opposite of SplitString().
460 BASE_EXPORT base::string16 JoinString(const std::vector<base::string16>& parts,
461 base::char16 s);
462 BASE_EXPORT std::string JoinString(
463 const std::vector<std::string>& parts, char s);
465 // Join |parts| using |separator|.
466 BASE_EXPORT std::string JoinString(
467 const std::vector<std::string>& parts,
468 const std::string& separator);
469 BASE_EXPORT base::string16 JoinString(
470 const std::vector<base::string16>& parts,
471 const base::string16& separator);
473 // Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively.
474 // Additionally, any number of consecutive '$' characters is replaced by that
475 // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be
476 // NULL. This only allows you to use up to nine replacements.
477 BASE_EXPORT base::string16 ReplaceStringPlaceholders(
478 const base::string16& format_string,
479 const std::vector<base::string16>& subst,
480 std::vector<size_t>* offsets);
482 BASE_EXPORT std::string ReplaceStringPlaceholders(
483 const base::StringPiece& format_string,
484 const std::vector<std::string>& subst,
485 std::vector<size_t>* offsets);
487 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL.
488 BASE_EXPORT base::string16 ReplaceStringPlaceholders(
489 const base::string16& format_string,
490 const base::string16& a,
491 size_t* offset);
493 // Returns true if the string passed in matches the pattern. The pattern
494 // string can contain wildcards like * and ?
495 // The backslash character (\) is an escape character for * and ?
496 // We limit the patterns to having a max of 16 * or ? characters.
497 // ? matches 0 or 1 character, while * matches 0 or more characters.
498 BASE_EXPORT bool MatchPattern(const base::StringPiece& string,
499 const base::StringPiece& pattern);
500 BASE_EXPORT bool MatchPattern(const base::string16& string,
501 const base::string16& pattern);
503 // Hack to convert any char-like type to its unsigned counterpart.
504 // For example, it will convert char, signed char and unsigned char to unsigned
505 // char.
506 template<typename T>
507 struct ToUnsigned {
508 typedef T Unsigned;
511 template<>
512 struct ToUnsigned<char> {
513 typedef unsigned char Unsigned;
515 template<>
516 struct ToUnsigned<signed char> {
517 typedef unsigned char Unsigned;
519 template<>
520 struct ToUnsigned<wchar_t> {
521 #if defined(WCHAR_T_IS_UTF16)
522 typedef unsigned short Unsigned;
523 #elif defined(WCHAR_T_IS_UTF32)
524 typedef uint32 Unsigned;
525 #endif
527 template<>
528 struct ToUnsigned<short> {
529 typedef unsigned short Unsigned;
532 #endif // BASE_STRINGS_STRING_UTIL_H_