| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/logging.h" |
| 6 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 10 |
| 9 namespace base { | 11 namespace base { |
| 10 | 12 |
| 11 namespace { | 13 namespace { |
| 12 | 14 |
| 13 // Given a null-terminated string of wchar_t with each wchar_t representing | 15 // Given a null-terminated string of wchar_t with each wchar_t representing |
| 14 // a UTF-16 code unit, returns a string16 made up of wchar_t's in the input. | 16 // a UTF-16 code unit, returns a string16 made up of wchar_t's in the input. |
| 15 // Each wchar_t should be <= 0xFFFF and a non-BMP character (> U+FFFF) | 17 // Each wchar_t should be <= 0xFFFF and a non-BMP character (> U+FFFF) |
| 16 // should be represented as a surrogate pair (two UTF-16 units) | 18 // should be represented as a surrogate pair (two UTF-16 units) |
| 17 // *even* where wchar_t is 32-bit (Linux and Mac). | 19 // *even* where wchar_t is 32-bit (Linux and Mac). |
| 18 // | 20 // |
| 19 // This is to help write tests for functions with string16 params until | 21 // This is to help write tests for functions with string16 params until |
| 20 // the C++ 0x UTF-16 literal is well-supported by compilers. | 22 // the C++ 0x UTF-16 literal is well-supported by compilers. |
| 21 string16 BuildString16(const wchar_t* s) { | 23 string16 BuildString16(const wchar_t* s) { |
| 22 #if defined(WCHAR_T_IS_UTF16) | 24 #if defined(WCHAR_T_IS_UTF16) |
| 23 return string16(s); | 25 return string16(s); |
| 24 #elif defined(WCHAR_T_IS_UTF32) | 26 #elif defined(WCHAR_T_IS_UTF32) |
| 25 string16 u16; | 27 string16 u16; |
| 26 while (*s != 0) { | 28 while (*s != 0) { |
| 27 DCHECK(static_cast<unsigned int>(*s) <= 0xFFFFu); | 29 DCHECK_LE(static_cast<unsigned int>(*s), 0xFFFFu); |
| 28 u16.push_back(*s++); | 30 u16.push_back(*s++); |
| 29 } | 31 } |
| 30 return u16; | 32 return u16; |
| 31 #endif | 33 #endif |
| 32 } | 34 } |
| 33 | 35 |
| 34 const wchar_t* const kConvertRoundtripCases[] = { | 36 const wchar_t* const kConvertRoundtripCases[] = { |
| 35 L"Google Video", | 37 L"Google Video", |
| 36 // "网页 图片 资讯更多 »" | 38 // "网页 图片 资讯更多 »" |
| 37 L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb", | 39 L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb", |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 memcpy(WriteInto(&wmultistring, arraysize(wmulti)), wmulti, sizeof(wmulti)); | 221 memcpy(WriteInto(&wmultistring, arraysize(wmulti)), wmulti, sizeof(wmulti)); |
| 220 EXPECT_EQ(arraysize(wmulti) - 1, wmultistring.length()); | 222 EXPECT_EQ(arraysize(wmulti) - 1, wmultistring.length()); |
| 221 std::string expected; | 223 std::string expected; |
| 222 memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi)); | 224 memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi)); |
| 223 EXPECT_EQ(arraysize(multi) - 1, expected.length()); | 225 EXPECT_EQ(arraysize(multi) - 1, expected.length()); |
| 224 const std::string& converted = WideToUTF8(wmultistring); | 226 const std::string& converted = WideToUTF8(wmultistring); |
| 225 EXPECT_EQ(arraysize(multi) - 1, converted.length()); | 227 EXPECT_EQ(arraysize(multi) - 1, converted.length()); |
| 226 EXPECT_EQ(expected, converted); | 228 EXPECT_EQ(expected, converted); |
| 227 } | 229 } |
| 228 | 230 |
| 229 } // namaspace base | 231 } // base |
| OLD | NEW |