diff options
author | jungshik@google.com <jungshik@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-25 21:42:00 +0000 |
---|---|---|
committer | jungshik@google.com <jungshik@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-25 21:42:00 +0000 |
commit | c9ec45429c64884c35f83b74131c0e3ae5b2bbe9 (patch) | |
tree | 39007373603b8e75f53fa5e4d0c1586b1a3a56b5 /base/string_util.cc | |
parent | 7e2fa03804bef4bff9c5bb941f2edf09b6d234c0 (diff) | |
download | chromium_src-c9ec45429c64884c35f83b74131c0e3ae5b2bbe9.zip chromium_src-c9ec45429c64884c35f83b74131c0e3ae5b2bbe9.tar.gz chromium_src-c9ec45429c64884c35f83b74131c0e3ae5b2bbe9.tar.bz2 |
Add UTF-8 check for JSON deserializer.
Add tests for IsStringUTF8
Make IsStringUTF8 accept std::string/std::wstring rather than char*/wchar_t*
Review URL: http://codereview.chromium.org/4268
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2610 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.cc')
-rw-r--r-- | base/string_util.cc | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 2f39104..223c485 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -517,7 +517,7 @@ static inline bool IsInUTF8Sequence(int c) { // originally been UTF-8, but has been converted to wide characters because // that's what we (and Windows) use internally. template<typename CHAR> -static bool IsStringUTF8T(const CHAR* str) { +static bool IsStringUTF8T(const CHAR* str, int length) { bool overlong = false; bool surrogate = false; bool nonchar = false; @@ -532,7 +532,7 @@ static bool IsStringUTF8T(const CHAR* str) { // are left in the sequence int positions_left = 0; - for (int i = 0; str[i] != 0; i++) { + for (int i = 0; i < length; i++) { // This whole function assume an unsigned value so force its conversion to // an unsigned value. typename ToUnsigned<CHAR>::Unsigned c = str[i]; @@ -556,6 +556,7 @@ static bool IsStringUTF8T(const CHAR* str) { slower = 0xA0; } else if (c == 0xEF) { // EF BF [BE-BF] : non-character + // TODO(jungshik): EF B7 [90-AF] should be checked as well. nonchar = true; } } else if (c <= 0xF4) { @@ -599,12 +600,12 @@ static bool IsStringUTF8T(const CHAR* str) { return true; } -bool IsStringUTF8(const char* str) { - return IsStringUTF8T(str); +bool IsStringUTF8(const std::string& str) { + return IsStringUTF8T(str.data(), str.length()); } -bool IsStringWideUTF8(const wchar_t* str) { - return IsStringUTF8T(str); +bool IsStringWideUTF8(const std::wstring& str) { + return IsStringUTF8T(str.data(), str.length()); } template<typename Iter> |