OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_STRING_UTIL_H_ | 7 #ifndef BASE_STRING_UTIL_H_ |
8 #define BASE_STRING_UTIL_H_ | 8 #define BASE_STRING_UTIL_H_ |
9 | 9 |
| 10 #include <stdarg.h> // va_list |
| 11 |
10 #include <string> | 12 #include <string> |
11 #include <vector> | 13 #include <vector> |
12 #include <stdarg.h> // va_list | |
13 | 14 |
14 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
15 #include "base/string16.h" | 16 #include "base/string16.h" |
16 | 17 |
17 // Safe standard library wrappers for all platforms. | 18 // Safe standard library wrappers for all platforms. |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
21 // C standard-library functions like "strncasecmp" and "snprintf" that aren't | 22 // C standard-library functions like "strncasecmp" and "snprintf" that aren't |
22 // cross-platform are provided as "base::strncasecmp", and their prototypes | 23 // cross-platform are provided as "base::strncasecmp", and their prototypes |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 // return strings ignore this error and just return the best conversion | 168 // return strings ignore this error and just return the best conversion |
168 // possible. | 169 // possible. |
169 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); | 170 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); |
170 std::string WideToUTF8(const std::wstring& wide); | 171 std::string WideToUTF8(const std::wstring& wide); |
171 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); | 172 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); |
172 std::wstring UTF8ToWide(const std::string& utf8); | 173 std::wstring UTF8ToWide(const std::string& utf8); |
173 | 174 |
174 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output); | 175 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output); |
175 string16 WideToUTF16(const std::wstring& wide); | 176 string16 WideToUTF16(const std::wstring& wide); |
176 bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output); | 177 bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output); |
177 std::wstring UTF16ToWide(const string16& utf8); | 178 std::wstring UTF16ToWide(const string16& utf16); |
178 | 179 |
179 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); | 180 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); |
180 string16 UTF8ToUTF16(const std::string& utf8); | 181 string16 UTF8ToUTF16(const std::string& utf8); |
181 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output); | 182 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output); |
182 std::string UTF16ToUTF8(const string16& utf16); | 183 std::string UTF16ToUTF8(const string16& utf16); |
183 | 184 |
184 // Defines the error handling modes of WideToCodepage and CodepageToWide. | 185 // Defines the error handling modes of WideToCodepage and CodepageToWide. |
185 class OnStringUtilConversionError { | 186 class OnStringUtilConversionError { |
186 public: | 187 public: |
187 enum Type { | 188 enum Type { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 *i = ToLowerASCII(*i); | 237 *i = ToLowerASCII(*i); |
237 } | 238 } |
238 | 239 |
239 template <class str> inline str StringToLowerASCII(const str& s) { | 240 template <class str> inline str StringToLowerASCII(const str& s) { |
240 // for std::string and std::wstring | 241 // for std::string and std::wstring |
241 str output(s); | 242 str output(s); |
242 StringToLowerASCII(&output); | 243 StringToLowerASCII(&output); |
243 return output; | 244 return output; |
244 } | 245 } |
245 | 246 |
| 247 // ASCII-specific toupper. The standard library's toupper is locale sensitive, |
| 248 // so we don't want to use it here. |
| 249 template <class Char> inline Char ToUpperASCII(Char c) { |
| 250 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; |
| 251 } |
| 252 |
| 253 // Converts the elements of the given string. This version uses a pointer to |
| 254 // clearly differentiate it from the non-pointer variant. |
| 255 template <class str> inline void StringToUpperASCII(str* s) { |
| 256 for (typename str::iterator i = s->begin(); i != s->end(); ++i) |
| 257 *i = ToUpperASCII(*i); |
| 258 } |
| 259 |
| 260 template <class str> inline str StringToUpperASCII(const str& s) { |
| 261 // for std::string and std::wstring |
| 262 str output(s); |
| 263 StringToUpperASCII(&output); |
| 264 return output; |
| 265 } |
| 266 |
246 // Compare the lower-case form of the given string against the given ASCII | 267 // Compare the lower-case form of the given string against the given ASCII |
247 // string. This is useful for doing checking if an input string matches some | 268 // string. This is useful for doing checking if an input string matches some |
248 // token, and it is optimized to avoid intermediate string copies. This API is | 269 // token, and it is optimized to avoid intermediate string copies. This API is |
249 // borrowed from the equivalent APIs in Mozilla. | 270 // borrowed from the equivalent APIs in Mozilla. |
250 bool LowerCaseEqualsASCII(const std::string& a, const char* b); | 271 bool LowerCaseEqualsASCII(const std::string& a, const char* b); |
251 bool LowerCaseEqualsASCII(const std::wstring& a, const char* b); | 272 bool LowerCaseEqualsASCII(const std::wstring& a, const char* b); |
252 | 273 |
253 // Same thing, but with string iterators instead. | 274 // Same thing, but with string iterators instead. |
254 bool LowerCaseEqualsASCII(std::string::const_iterator a_begin, | 275 bool LowerCaseEqualsASCII(std::string::const_iterator a_begin, |
255 std::string::const_iterator a_end, | 276 std::string::const_iterator a_end, |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 // Returns a hex string representation of a binary buffer. | 574 // Returns a hex string representation of a binary buffer. |
554 // The returned hex string will be in upper case. | 575 // The returned hex string will be in upper case. |
555 // This function does not check if |size| is within reasonable limits since | 576 // This function does not check if |size| is within reasonable limits since |
556 // it's written with trusted data in mind. | 577 // it's written with trusted data in mind. |
557 // If you suspect that the data you want to format might be large, | 578 // If you suspect that the data you want to format might be large, |
558 // the absolute max size for |size| should be is | 579 // the absolute max size for |size| should be is |
559 // std::numeric_limits<size_t>::max() / 2 | 580 // std::numeric_limits<size_t>::max() / 2 |
560 std::string HexEncode(const void* bytes, size_t size); | 581 std::string HexEncode(const void* bytes, size_t size); |
561 | 582 |
562 #endif // BASE_STRING_UTIL_H_ | 583 #endif // BASE_STRING_UTIL_H_ |
OLD | NEW |