Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: base/strings/string_util.h

Issue 1224553010: Replace base::str[n]casecmp with helper functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/file_path.cc ('k') | base/strings/string_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // This file defines utility functions for working with strings. 5 // This file defines utility functions for working with strings.
6 6
7 #ifndef BASE_STRINGS_STRING_UTIL_H_ 7 #ifndef BASE_STRINGS_STRING_UTIL_H_
8 #define BASE_STRINGS_STRING_UTIL_H_ 8 #define BASE_STRINGS_STRING_UTIL_H_
9 9
10 #include <ctype.h> 10 #include <ctype.h>
11 #include <stdarg.h> // va_list 11 #include <stdarg.h> // va_list
12 12
13 #include <string> 13 #include <string>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/base_export.h" 16 #include "base/base_export.h"
17 #include "base/basictypes.h" 17 #include "base/basictypes.h"
18 #include "base/compiler_specific.h" 18 #include "base/compiler_specific.h"
19 #include "base/strings/string16.h" 19 #include "base/strings/string16.h"
20 #include "base/strings/string_piece.h" // For implicit conversions. 20 #include "base/strings/string_piece.h" // For implicit conversions.
21 21
22 namespace base { 22 namespace base {
23 23
24 // C standard-library functions like "strncasecmp" and "snprintf" that aren't 24 // C standard-library functions that aren't cross-platform are provided as
25 // cross-platform are provided as "base::strncasecmp", and their prototypes 25 // "base::...", and their prototypes are listed below. These functions are
26 // are listed below. These functions are then implemented as inline calls 26 // then implemented as inline calls to the platform-specific equivalents in the
27 // to the platform-specific equivalents in the platform-specific headers. 27 // 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 28
39 // Wrapper for vsnprintf that always null-terminates and always returns the 29 // Wrapper for vsnprintf that always null-terminates and always returns the
40 // number of characters that would be in an untruncated formatted 30 // number of characters that would be in an untruncated formatted
41 // string, even when truncation occurs. 31 // string, even when truncation occurs.
42 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments) 32 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments)
43 PRINTF_FORMAT(3, 0); 33 PRINTF_FORMAT(3, 0);
44 34
45 // Some of these implementations need to be inlined. 35 // Some of these implementations need to be inlined.
46 36
47 // We separate the declaration from the implementation of this inline 37 // We separate the declaration from the implementation of this inline
48 // function just so the PRINTF_FORMAT works. 38 // function just so the PRINTF_FORMAT works.
49 inline int snprintf(char* buffer, size_t size, const char* format, ...) 39 inline int snprintf(char* buffer, size_t size, const char* format, ...)
50 PRINTF_FORMAT(3, 4); 40 PRINTF_FORMAT(3, 4);
51 inline int snprintf(char* buffer, size_t size, const char* format, ...) { 41 inline int snprintf(char* buffer, size_t size, const char* format, ...) {
52 va_list arguments; 42 va_list arguments;
53 va_start(arguments, format); 43 va_start(arguments, format);
54 int result = vsnprintf(buffer, size, format, arguments); 44 int result = vsnprintf(buffer, size, format, arguments);
55 va_end(arguments); 45 va_end(arguments);
56 return result; 46 return result;
57 } 47 }
58 48
49 // TODO(mark) http://crbug.com/472900 crashpad shouldn't use base while
50 // being DEPSed in. This backwards-compat hack is provided until crashpad is
51 // updated.
52 #if defined(OS_WIN)
53 inline int strcasecmp(const char* s1, const char* s2) {
54 return _stricmp(s1, s2);
55 }
56 #else // Posix
57 inline int strcasecmp(const char* string1, const char* string2) {
58 return ::strcasecmp(string1, string2);
59 }
60 #endif
61
59 // BSD-style safe and consistent string copy functions. 62 // BSD-style safe and consistent string copy functions.
60 // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|. 63 // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|.
61 // Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as 64 // Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as
62 // long as |dst_size| is not 0. Returns the length of |src| in characters. 65 // long as |dst_size| is not 0. Returns the length of |src| in characters.
63 // If the return value is >= dst_size, then the output was truncated. 66 // If the return value is >= dst_size, then the output was truncated.
64 // NOTE: All sizes are in number of characters, NOT in bytes. 67 // NOTE: All sizes are in number of characters, NOT in bytes.
65 BASE_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size); 68 BASE_EXPORT size_t strlcpy(char* dst, const char* src, size_t dst_size);
66 BASE_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size); 69 BASE_EXPORT size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size);
67 70
68 // Scan a wprintf format string to determine whether it's portable across a 71 // Scan a wprintf format string to determine whether it's portable across a
(...skipping 26 matching lines...) Expand all
95 } 98 }
96 99
97 // ASCII-specific toupper. The standard library's toupper is locale sensitive, 100 // ASCII-specific toupper. The standard library's toupper is locale sensitive,
98 // so we don't want to use it here. 101 // so we don't want to use it here.
99 template <class Char> inline Char ToUpperASCII(Char c) { 102 template <class Char> inline Char ToUpperASCII(Char c) {
100 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; 103 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
101 } 104 }
102 105
103 // Function objects to aid in comparing/searching strings. 106 // Function objects to aid in comparing/searching strings.
104 107
108 // DO NOT USE. tolower() will given incorrect results for non-ASCII characters.
109 // Use the ASCII version, base::i18n::ToLower, or base::i18n::FoldCase.
105 template<typename Char> struct CaseInsensitiveCompare { 110 template<typename Char> struct CaseInsensitiveCompare {
106 public: 111 public:
107 bool operator()(Char x, Char y) const { 112 bool operator()(Char x, Char y) const {
108 // TODO(darin): Do we really want to do locale sensitive comparisons here? 113 // TODO(darin): Do we really want to do locale sensitive comparisons here?
114 // ANSWER(brettw): No.
109 // See http://crbug.com/24917 115 // See http://crbug.com/24917
110 return tolower(x) == tolower(y); 116 return tolower(x) == tolower(y);
111 } 117 }
112 }; 118 };
113 119
114 template<typename Char> struct CaseInsensitiveCompareASCII { 120 template<typename Char> struct CaseInsensitiveCompareASCII {
115 public: 121 public:
116 bool operator()(Char x, Char y) const { 122 bool operator()(Char x, Char y) const {
117 return ToLowerASCII(x) == ToLowerASCII(y); 123 return ToLowerASCII(x) == ToLowerASCII(y);
118 } 124 }
119 }; 125 };
120 126
127 // Like strcasecmp for case-insensitive ASCII characters only. Returns:
128 // -1 (a < b)
129 // 0 (a == b)
130 // 1 (a > b)
131 // (unlike strcasecmp which can return values greater or less than 1/-1). For
132 // full Unicode support, use base::i18n::ToLower or base::i18h::FoldCase
133 // and then just call the normal string operators on the result.
134 BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece a, StringPiece b);
135 BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b);
136
137 // Equality for ASCII case-insensitive comparisons. For full Unicode support,
138 // use base::i18n::ToLower or base::i18h::FoldCase and then compare with either
139 // == or !=.
140 BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece a, StringPiece b);
141 BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b);
142
121 // These threadsafe functions return references to globally unique empty 143 // These threadsafe functions return references to globally unique empty
122 // strings. 144 // strings.
123 // 145 //
124 // It is likely faster to construct a new empty string object (just a few 146 // It is likely faster to construct a new empty string object (just a few
125 // instructions to set the length to 0) than to get the empty string singleton 147 // instructions to set the length to 0) than to get the empty string singleton
126 // returned by these functions (which requires threadsafe singleton access). 148 // returned by these functions (which requires threadsafe singleton access).
127 // 149 //
128 // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT 150 // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT
129 // CONSTRUCTORS. There is only one case where you should use these: functions 151 // CONSTRUCTORS. There is only one case where you should use these: functions
130 // which need to return a string by reference (e.g. as a class member 152 // which need to return a string by reference (e.g. as a class member
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 const std::vector<std::string>& subst, 537 const std::vector<std::string>& subst,
516 std::vector<size_t>* offsets); 538 std::vector<size_t>* offsets);
517 539
518 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. 540 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL.
519 BASE_EXPORT base::string16 ReplaceStringPlaceholders( 541 BASE_EXPORT base::string16 ReplaceStringPlaceholders(
520 const base::string16& format_string, 542 const base::string16& format_string,
521 const base::string16& a, 543 const base::string16& a,
522 size_t* offset); 544 size_t* offset);
523 545
524 #endif // BASE_STRINGS_STRING_UTIL_H_ 546 #endif // BASE_STRINGS_STRING_UTIL_H_
OLDNEW
« no previous file with comments | « base/files/file_path.cc ('k') | base/strings/string_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698