Chromium Code Reviews| 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 | 10 #include <stdarg.h> // va_list |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/string16.h" | 16 #include "base/string16.h" |
| 17 #include "base/string_piece.h" // For implicit conversions. | |
| 17 | 18 |
| 18 // Safe standard library wrappers for all platforms. | 19 // Safe standard library wrappers for all platforms. |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 | 22 |
| 22 // C standard-library functions like "strncasecmp" and "snprintf" that aren't | 23 // C standard-library functions like "strncasecmp" and "snprintf" that aren't |
| 23 // cross-platform are provided as "base::strncasecmp", and their prototypes | 24 // cross-platform are provided as "base::strncasecmp", and their prototypes |
| 24 // are listed below. These functions are then implemented as inline calls | 25 // are listed below. These functions are then implemented as inline calls |
| 25 // to the platform-specific equivalents in the platform-specific headers. | 26 // to the platform-specific equivalents in the platform-specific headers. |
| 26 | 27 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 | 167 |
| 167 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, | 168 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, |
| 168 // so avoid unnecessary conversions. The low-level versions return a boolean | 169 // so avoid unnecessary conversions. The low-level versions return a boolean |
| 169 // indicating whether the conversion was 100% valid. In this case, it will still | 170 // indicating whether the conversion was 100% valid. In this case, it will still |
| 170 // do the best it can and put the result in the output buffer. The versions that | 171 // do the best it can and put the result in the output buffer. The versions that |
| 171 // return strings ignore this error and just return the best conversion | 172 // return strings ignore this error and just return the best conversion |
| 172 // possible. | 173 // possible. |
| 173 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); | 174 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); |
| 174 std::string WideToUTF8(const std::wstring& wide); | 175 std::string WideToUTF8(const std::wstring& wide); |
| 175 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); | 176 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); |
| 176 std::wstring UTF8ToWide(const std::string& utf8); | 177 std::wstring UTF8ToWide(const StringPiece& utf8); |
| 177 | 178 |
| 178 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output); | 179 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output); |
| 179 string16 WideToUTF16(const std::wstring& wide); | 180 string16 WideToUTF16(const std::wstring& wide); |
| 180 bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output); | 181 bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output); |
| 181 std::wstring UTF16ToWide(const string16& utf16); | 182 std::wstring UTF16ToWide(const string16& utf16); |
| 182 | 183 |
| 183 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); | 184 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); |
| 184 string16 UTF8ToUTF16(const std::string& utf8); | 185 string16 UTF8ToUTF16(const std::string& utf8); |
|
darin (slow to review)
2009/03/04 16:33:23
this looks like another good place to use this.
i
| |
| 185 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output); | 186 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output); |
| 186 std::string UTF16ToUTF8(const string16& utf16); | 187 std::string UTF16ToUTF8(const string16& utf16); |
| 187 | 188 |
| 188 // We are trying to get rid of wstring as much as possible, but it's too big | 189 // We are trying to get rid of wstring as much as possible, but it's too big |
| 189 // a mess to do it all at once. These conversions should be used when we | 190 // a mess to do it all at once. These conversions should be used when we |
| 190 // really should just be passing a string16 around, but we haven't finished | 191 // really should just be passing a string16 around, but we haven't finished |
| 191 // porting whatever module uses wstring and the conversion is being used as a | 192 // porting whatever module uses wstring and the conversion is being used as a |
| 192 // stopcock. This makes it easy to grep for the ones that should be removed. | 193 // stopcock. This makes it easy to grep for the ones that should be removed. |
| 193 #if defined(OS_WIN) | 194 #if defined(OS_WIN) |
| 194 # define WideToUTF16Hack | 195 # define WideToUTF16Hack |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 592 // The returned hex string will be in upper case. | 593 // The returned hex string will be in upper case. |
| 593 // This function does not check if |size| is within reasonable limits since | 594 // This function does not check if |size| is within reasonable limits since |
| 594 // it's written with trusted data in mind. | 595 // it's written with trusted data in mind. |
| 595 // If you suspect that the data you want to format might be large, | 596 // If you suspect that the data you want to format might be large, |
| 596 // the absolute max size for |size| should be is | 597 // the absolute max size for |size| should be is |
| 597 // std::numeric_limits<size_t>::max() / 2 | 598 // std::numeric_limits<size_t>::max() / 2 |
| 598 std::string HexEncode(const void* bytes, size_t size); | 599 std::string HexEncode(const void* bytes, size_t size); |
| 599 | 600 |
| 600 | 601 |
| 601 #endif // BASE_STRING_UTIL_H_ | 602 #endif // BASE_STRING_UTIL_H_ |
| OLD | NEW |