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

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: Revert CLD 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
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 // Same as strncmp but for char16 strings. 29 // Same as strncmp but for char16 strings.
40 int strncmp16(const char16* s1, const char16* s2, size_t count); 30 int strncmp16(const char16* s1, const char16* s2, size_t count);
41 31
42 // Wrapper for vsnprintf that always null-terminates and always returns the 32 // Wrapper for vsnprintf that always null-terminates and always returns the
43 // number of characters that would be in an untruncated formatted 33 // number of characters that would be in an untruncated formatted
44 // string, even when truncation occurs. 34 // string, even when truncation occurs.
45 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments) 35 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments)
46 PRINTF_FORMAT(3, 0); 36 PRINTF_FORMAT(3, 0);
47 37
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 } 88 }
99 89
100 // ASCII-specific toupper. The standard library's toupper is locale sensitive, 90 // ASCII-specific toupper. The standard library's toupper is locale sensitive,
101 // so we don't want to use it here. 91 // so we don't want to use it here.
102 template <class Char> inline Char ToUpperASCII(Char c) { 92 template <class Char> inline Char ToUpperASCII(Char c) {
103 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; 93 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
104 } 94 }
105 95
106 // Function objects to aid in comparing/searching strings. 96 // Function objects to aid in comparing/searching strings.
107 97
98 // DO NOT USE. tolower() will given incorrect results for non-ASCII characters.
Nico 2015/07/08 21:58:23 suggest what to do instead?
108 template<typename Char> struct CaseInsensitiveCompare { 99 template<typename Char> struct CaseInsensitiveCompare {
109 public: 100 public:
110 bool operator()(Char x, Char y) const { 101 bool operator()(Char x, Char y) const {
111 // TODO(darin): Do we really want to do locale sensitive comparisons here? 102 // TODO(darin): Do we really want to do locale sensitive comparisons here?
112 // See http://crbug.com/24917 103 // See http://crbug.com/24917
113 return tolower(x) == tolower(y); 104 return tolower(x) == tolower(y);
114 } 105 }
115 }; 106 };
116 107
117 template<typename Char> struct CaseInsensitiveCompareASCII { 108 template<typename Char> struct CaseInsensitiveCompareASCII {
118 public: 109 public:
119 bool operator()(Char x, Char y) const { 110 bool operator()(Char x, Char y) const {
120 return ToLowerASCII(x) == ToLowerASCII(y); 111 return ToLowerASCII(x) == ToLowerASCII(y);
121 } 112 }
122 }; 113 };
123 114
115 // Like strcasecmp for case-insensitive ASCII characters only. Returns:
116 // -1 (a < b)
117 // 0 (a == b)
118 // 1 (a > b)
119 // (unlike strcasecmp which can return values greater or less than 1/-1). For
120 // full Unicode support, use base::i18n::ToLower or base::i18h::FoldCase
121 // and then just call the normal string operators on the result.
122 BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece a, StringPiece b);
123 BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b);
124
125 // Equality for ASCII case-insensitive comparisons. For full Unicode support,
126 // use base::i18n::ToLower or base::i18h::FoldCase and then compare with either
127 // == or !=.
128 BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece a, StringPiece b);
129 BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b);
130
124 // These threadsafe functions return references to globally unique empty 131 // These threadsafe functions return references to globally unique empty
125 // strings. 132 // strings.
126 // 133 //
127 // It is likely faster to construct a new empty string object (just a few 134 // 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 135 // instructions to set the length to 0) than to get the empty string singleton
129 // returned by these functions (which requires threadsafe singleton access). 136 // returned by these functions (which requires threadsafe singleton access).
130 // 137 //
131 // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT 138 // 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 139 // 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 140 // which need to return a string by reference (e.g. as a class member
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 const std::vector<std::string>& subst, 527 const std::vector<std::string>& subst,
521 std::vector<size_t>* offsets); 528 std::vector<size_t>* offsets);
522 529
523 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. 530 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL.
524 BASE_EXPORT base::string16 ReplaceStringPlaceholders( 531 BASE_EXPORT base::string16 ReplaceStringPlaceholders(
525 const base::string16& format_string, 532 const base::string16& format_string,
526 const base::string16& a, 533 const base::string16& a,
527 size_t* offset); 534 size_t* offset);
528 535
529 #endif // BASE_STRINGS_STRING_UTIL_H_ 536 #endif // BASE_STRINGS_STRING_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698