| 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 #include <math.h> | 5 #include <math.h> |
| 6 #include <stdarg.h> | 6 #include <stdarg.h> |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 | 10 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 93 |
| 94 std::string output_ascii; | 94 std::string output_ascii; |
| 95 for (size_t i = 0; i < arraysize(trim_cases_ascii); ++i) { | 95 for (size_t i = 0; i < arraysize(trim_cases_ascii); ++i) { |
| 96 const trim_case_ascii& value = trim_cases_ascii[i]; | 96 const trim_case_ascii& value = trim_cases_ascii[i]; |
| 97 EXPECT_EQ(value.return_value, | 97 EXPECT_EQ(value.return_value, |
| 98 TrimWhitespace(value.input, value.positions, &output_ascii)); | 98 TrimWhitespace(value.input, value.positions, &output_ascii)); |
| 99 EXPECT_EQ(value.output, output_ascii); | 99 EXPECT_EQ(value.output, output_ascii); |
| 100 } | 100 } |
| 101 } | 101 } |
| 102 | 102 |
| 103 static const struct trim_case_utf8 { | |
| 104 const char* input; | |
| 105 const TrimPositions positions; | |
| 106 const char* output; | |
| 107 const TrimPositions return_value; | |
| 108 } trim_cases_utf8[] = { | |
| 109 // UTF-8 strings that start (and end) with Unicode space characters | |
| 110 // (including zero-width spaces). | |
| 111 {"\xE2\x80\x80Test String\xE2\x80\x81", TRIM_ALL, "Test String", TRIM_ALL}, | |
| 112 {"\xE2\x80\x82Test String\xE2\x80\x83", TRIM_ALL, "Test String", TRIM_ALL}, | |
| 113 {"\xE2\x80\x84Test String\xE2\x80\x85", TRIM_ALL, "Test String", TRIM_ALL}, | |
| 114 {"\xE2\x80\x86Test String\xE2\x80\x87", TRIM_ALL, "Test String", TRIM_ALL}, | |
| 115 {"\xE2\x80\x88Test String\xE2\x80\x8A", TRIM_ALL, "Test String", TRIM_ALL}, | |
| 116 {"\xE3\x80\x80Test String\xE3\x80\x80", TRIM_ALL, "Test String", TRIM_ALL}, | |
| 117 // UTF-8 strings that end with 0x85 (NEL in ISO-8859). | |
| 118 {"\xD0\x85", TRIM_TRAILING, "\xD0\x85", TRIM_NONE}, | |
| 119 {"\xD9\x85", TRIM_TRAILING, "\xD9\x85", TRIM_NONE}, | |
| 120 {"\xEC\x97\x85", TRIM_TRAILING, "\xEC\x97\x85", TRIM_NONE}, | |
| 121 {"\xF0\x90\x80\x85", TRIM_TRAILING, "\xF0\x90\x80\x85", TRIM_NONE}, | |
| 122 // UTF-8 strings that end with 0xA0 (non-break space in ISO-8859-1). | |
| 123 {"\xD0\xA0", TRIM_TRAILING, "\xD0\xA0", TRIM_NONE}, | |
| 124 {"\xD9\xA0", TRIM_TRAILING, "\xD9\xA0", TRIM_NONE}, | |
| 125 {"\xEC\x97\xA0", TRIM_TRAILING, "\xEC\x97\xA0", TRIM_NONE}, | |
| 126 {"\xF0\x90\x80\xA0", TRIM_TRAILING, "\xF0\x90\x80\xA0", TRIM_NONE}, | |
| 127 }; | |
| 128 | |
| 129 TEST(StringUtilTest, TrimWhitespaceUTF8) { | |
| 130 std::string output_ascii; | |
| 131 for (size_t i = 0; i < arraysize(trim_cases_ascii); ++i) { | |
| 132 const trim_case_ascii& value = trim_cases_ascii[i]; | |
| 133 EXPECT_EQ(value.return_value, | |
| 134 TrimWhitespaceASCII(value.input, value.positions, &output_ascii)); | |
| 135 EXPECT_EQ(value.output, output_ascii); | |
| 136 } | |
| 137 | |
| 138 // Test that TrimWhiteSpaceUTF8() can remove Unicode space characters and | |
| 139 // prevent from removing UTF-8 characters that end with an ISO-8859 NEL. | |
| 140 std::string output_utf8; | |
| 141 for (size_t i = 0; i < arraysize(trim_cases_utf8); ++i) { | |
| 142 const trim_case_utf8& value = trim_cases_utf8[i]; | |
| 143 EXPECT_EQ(value.return_value, | |
| 144 TrimWhitespaceUTF8(value.input, value.positions, &output_utf8)); | |
| 145 EXPECT_EQ(value.output, output_utf8); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 static const struct collapse_case { | 103 static const struct collapse_case { |
| 150 const wchar_t* input; | 104 const wchar_t* input; |
| 151 const bool trim; | 105 const bool trim; |
| 152 const wchar_t* output; | 106 const wchar_t* output; |
| 153 } collapse_cases[] = { | 107 } collapse_cases[] = { |
| 154 {L" Google Video ", false, L"Google Video"}, | 108 {L" Google Video ", false, L"Google Video"}, |
| 155 {L"Google Video", false, L"Google Video"}, | 109 {L"Google Video", false, L"Google Video"}, |
| 156 {L"", false, L""}, | 110 {L"", false, L""}, |
| 157 {L" ", false, L""}, | 111 {L" ", false, L""}, |
| 158 {L"\t\rTest String\n", false, L"Test String"}, | 112 {L"\t\rTest String\n", false, L"Test String"}, |
| (...skipping 1680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1839 } | 1793 } |
| 1840 } | 1794 } |
| 1841 | 1795 |
| 1842 TEST(StringUtilTest, HexEncode) { | 1796 TEST(StringUtilTest, HexEncode) { |
| 1843 std::string hex(HexEncode(NULL, 0)); | 1797 std::string hex(HexEncode(NULL, 0)); |
| 1844 EXPECT_EQ(hex.length(), 0U); | 1798 EXPECT_EQ(hex.length(), 0U); |
| 1845 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; | 1799 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; |
| 1846 hex = HexEncode(bytes, sizeof(bytes)); | 1800 hex = HexEncode(bytes, sizeof(bytes)); |
| 1847 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); | 1801 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); |
| 1848 } | 1802 } |
| OLD | NEW |