| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/strings/string_util.h" | 5 #include "base/strings/string_util.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <stdarg.h> | 8 #include <stdarg.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <algorithm> |
| 11 #include <sstream> | |
| 12 | 11 |
| 13 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 14 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 using ::testing::ElementsAre; | 18 using ::testing::ElementsAre; |
| 20 | 19 |
| 21 namespace base { | 20 namespace base { |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 {L"\nFoo", true, L"Foo"}, | 276 {L"\nFoo", true, L"Foo"}, |
| 278 {L"\r Foo ", true, L"Foo"}, | 277 {L"\r Foo ", true, L"Foo"}, |
| 279 {L" Foo bar ", true, L"Foo bar"}, | 278 {L" Foo bar ", true, L"Foo bar"}, |
| 280 {L" \tFoo bar \n", true, L"Foo bar"}, | 279 {L" \tFoo bar \n", true, L"Foo bar"}, |
| 281 {L" a \r b\n c \r\n d \t\re \t f \n ", true, L"abcde f"}, | 280 {L" a \r b\n c \r\n d \t\re \t f \n ", true, L"abcde f"}, |
| 282 }; | 281 }; |
| 283 | 282 |
| 284 TEST(StringUtilTest, CollapseWhitespace) { | 283 TEST(StringUtilTest, CollapseWhitespace) { |
| 285 for (size_t i = 0; i < arraysize(collapse_cases); ++i) { | 284 for (size_t i = 0; i < arraysize(collapse_cases); ++i) { |
| 286 const collapse_case& value = collapse_cases[i]; | 285 const collapse_case& value = collapse_cases[i]; |
| 287 EXPECT_EQ(value.output, CollapseWhitespace(value.input, value.trim)); | 286 EXPECT_EQ(WideToUTF16(value.output), |
| 287 CollapseWhitespace(WideToUTF16(value.input), value.trim)); |
| 288 } | 288 } |
| 289 } | 289 } |
| 290 | 290 |
| 291 static const struct collapse_case_ascii { | 291 static const struct collapse_case_ascii { |
| 292 const char* input; | 292 const char* input; |
| 293 const bool trim; | 293 const bool trim; |
| 294 const char* output; | 294 const char* output; |
| 295 } collapse_cases_ascii[] = { | 295 } collapse_cases_ascii[] = { |
| 296 {" Google Video ", false, "Google Video"}, | 296 {" Google Video ", false, "Google Video"}, |
| 297 {"Google Video", false, "Google Video"}, | 297 {"Google Video", false, "Google Video"}, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 L"Google Video", | 414 L"Google Video", |
| 415 L"Hello, world\n", | 415 L"Hello, world\n", |
| 416 L"0123ABCDwxyz \a\b\t\r\n!+,.~" | 416 L"0123ABCDwxyz \a\b\t\r\n!+,.~" |
| 417 }; | 417 }; |
| 418 | 418 |
| 419 for (size_t i = 0; i < arraysize(char_cases); ++i) { | 419 for (size_t i = 0; i < arraysize(char_cases); ++i) { |
| 420 EXPECT_TRUE(IsStringASCII(char_cases[i])); | 420 EXPECT_TRUE(IsStringASCII(char_cases[i])); |
| 421 std::wstring wide = ASCIIToWide(char_cases[i]); | 421 std::wstring wide = ASCIIToWide(char_cases[i]); |
| 422 EXPECT_EQ(wchar_cases[i], wide); | 422 EXPECT_EQ(wchar_cases[i], wide); |
| 423 | 423 |
| 424 EXPECT_TRUE(IsStringASCII(wchar_cases[i])); | |
| 425 std::string ascii = WideToASCII(wchar_cases[i]); | 424 std::string ascii = WideToASCII(wchar_cases[i]); |
| 426 EXPECT_EQ(char_cases[i], ascii); | 425 EXPECT_EQ(char_cases[i], ascii); |
| 427 } | 426 } |
| 428 | 427 |
| 429 EXPECT_FALSE(IsStringASCII("Google \x80Video")); | 428 EXPECT_FALSE(IsStringASCII("Google \x80Video")); |
| 430 EXPECT_FALSE(IsStringASCII(L"Google \x80Video")); | |
| 431 | 429 |
| 432 // Convert empty strings. | 430 // Convert empty strings. |
| 433 std::wstring wempty; | 431 std::wstring wempty; |
| 434 std::string empty; | 432 std::string empty; |
| 435 EXPECT_EQ(empty, WideToASCII(wempty)); | 433 EXPECT_EQ(empty, WideToASCII(wempty)); |
| 436 EXPECT_EQ(wempty, ASCIIToWide(empty)); | 434 EXPECT_EQ(wempty, ASCIIToWide(empty)); |
| 437 | 435 |
| 438 // Convert strings with an embedded NUL character. | 436 // Convert strings with an embedded NUL character. |
| 439 const char chars_with_nul[] = "test\0string"; | 437 const char chars_with_nul[] = "test\0string"; |
| 440 const int length_with_nul = arraysize(chars_with_nul) - 1; | 438 const int length_with_nul = arraysize(chars_with_nul) - 1; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 469 std::string upper_a = StringToUpperASCII(original_a); | 467 std::string upper_a = StringToUpperASCII(original_a); |
| 470 EXPECT_EQ("CC2", upper_a); | 468 EXPECT_EQ("CC2", upper_a); |
| 471 | 469 |
| 472 std::wstring original_w(L"Cc2"); | 470 std::wstring original_w(L"Cc2"); |
| 473 std::wstring upper_w = StringToUpperASCII(original_w); | 471 std::wstring upper_w = StringToUpperASCII(original_w); |
| 474 EXPECT_EQ(L"CC2", upper_w); | 472 EXPECT_EQ(L"CC2", upper_w); |
| 475 } | 473 } |
| 476 | 474 |
| 477 TEST(StringUtilTest, LowerCaseEqualsASCII) { | 475 TEST(StringUtilTest, LowerCaseEqualsASCII) { |
| 478 static const struct { | 476 static const struct { |
| 479 const wchar_t* src_w; | |
| 480 const char* src_a; | 477 const char* src_a; |
| 481 const char* dst; | 478 const char* dst; |
| 482 } lowercase_cases[] = { | 479 } lowercase_cases[] = { |
| 483 { L"FoO", "FoO", "foo" }, | 480 { "FoO", "foo" }, |
| 484 { L"foo", "foo", "foo" }, | 481 { "foo", "foo" }, |
| 485 { L"FOO", "FOO", "foo" }, | 482 { "FOO", "foo" }, |
| 486 }; | 483 }; |
| 487 | 484 |
| 488 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(lowercase_cases); ++i) { | 485 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(lowercase_cases); ++i) { |
| 489 EXPECT_TRUE(LowerCaseEqualsASCII(lowercase_cases[i].src_w, | 486 EXPECT_TRUE(LowerCaseEqualsASCII(ASCIIToUTF16(lowercase_cases[i].src_a), |
| 490 lowercase_cases[i].dst)); | 487 lowercase_cases[i].dst)); |
| 491 EXPECT_TRUE(LowerCaseEqualsASCII(lowercase_cases[i].src_a, | 488 EXPECT_TRUE(LowerCaseEqualsASCII(lowercase_cases[i].src_a, |
| 492 lowercase_cases[i].dst)); | 489 lowercase_cases[i].dst)); |
| 493 } | 490 } |
| 494 } | 491 } |
| 495 | 492 |
| 496 TEST(StringUtilTest, FormatBytesUnlocalized) { | 493 TEST(StringUtilTest, FormatBytesUnlocalized) { |
| 497 static const struct { | 494 static const struct { |
| 498 int64 bytes; | 495 int64 bytes; |
| 499 const char* expected; | 496 const char* expected; |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 EXPECT_FALSE(StartsWithASCII("JavaScript:url", "javascript", true)); | 808 EXPECT_FALSE(StartsWithASCII("JavaScript:url", "javascript", true)); |
| 812 EXPECT_TRUE(StartsWithASCII("javascript:url", "javascript", false)); | 809 EXPECT_TRUE(StartsWithASCII("javascript:url", "javascript", false)); |
| 813 EXPECT_TRUE(StartsWithASCII("JavaScript:url", "javascript", false)); | 810 EXPECT_TRUE(StartsWithASCII("JavaScript:url", "javascript", false)); |
| 814 EXPECT_FALSE(StartsWithASCII("java", "javascript", true)); | 811 EXPECT_FALSE(StartsWithASCII("java", "javascript", true)); |
| 815 EXPECT_FALSE(StartsWithASCII("java", "javascript", false)); | 812 EXPECT_FALSE(StartsWithASCII("java", "javascript", false)); |
| 816 EXPECT_FALSE(StartsWithASCII(std::string(), "javascript", false)); | 813 EXPECT_FALSE(StartsWithASCII(std::string(), "javascript", false)); |
| 817 EXPECT_FALSE(StartsWithASCII(std::string(), "javascript", true)); | 814 EXPECT_FALSE(StartsWithASCII(std::string(), "javascript", true)); |
| 818 EXPECT_TRUE(StartsWithASCII("java", std::string(), false)); | 815 EXPECT_TRUE(StartsWithASCII("java", std::string(), false)); |
| 819 EXPECT_TRUE(StartsWithASCII("java", std::string(), true)); | 816 EXPECT_TRUE(StartsWithASCII("java", std::string(), true)); |
| 820 | 817 |
| 821 EXPECT_TRUE(StartsWith(L"javascript:url", L"javascript", true)); | 818 EXPECT_TRUE(StartsWith(ASCIIToUTF16("javascript:url"), |
| 822 EXPECT_FALSE(StartsWith(L"JavaScript:url", L"javascript", true)); | 819 ASCIIToUTF16("javascript"), true)); |
| 823 EXPECT_TRUE(StartsWith(L"javascript:url", L"javascript", false)); | 820 EXPECT_FALSE(StartsWith(ASCIIToUTF16("JavaScript:url"), |
| 824 EXPECT_TRUE(StartsWith(L"JavaScript:url", L"javascript", false)); | 821 ASCIIToUTF16("javascript"), true)); |
| 825 EXPECT_FALSE(StartsWith(L"java", L"javascript", true)); | 822 EXPECT_TRUE(StartsWith(ASCIIToUTF16("javascript:url"), |
| 826 EXPECT_FALSE(StartsWith(L"java", L"javascript", false)); | 823 ASCIIToUTF16("javascript"), false)); |
| 827 EXPECT_FALSE(StartsWith(std::wstring(), L"javascript", false)); | 824 EXPECT_TRUE(StartsWith(ASCIIToUTF16("JavaScript:url"), |
| 828 EXPECT_FALSE(StartsWith(std::wstring(), L"javascript", true)); | 825 ASCIIToUTF16("javascript"), false)); |
| 829 EXPECT_TRUE(StartsWith(L"java", std::wstring(), false)); | 826 EXPECT_FALSE(StartsWith(ASCIIToUTF16("java"), |
| 830 EXPECT_TRUE(StartsWith(L"java", std::wstring(), true)); | 827 ASCIIToUTF16("javascript"), true)); |
| 828 EXPECT_FALSE(StartsWith(ASCIIToUTF16("java"), |
| 829 ASCIIToUTF16("javascript"), false)); |
| 830 EXPECT_FALSE(StartsWith(string16(), ASCIIToUTF16("javascript"), false)); |
| 831 EXPECT_FALSE(StartsWith(string16(), ASCIIToUTF16("javascript"), true)); |
| 832 EXPECT_TRUE(StartsWith(ASCIIToUTF16("java"), string16(), false)); |
| 833 EXPECT_TRUE(StartsWith(ASCIIToUTF16("java"), string16(), true)); |
| 831 } | 834 } |
| 832 | 835 |
| 833 TEST(StringUtilTest, EndsWith) { | 836 TEST(StringUtilTest, EndsWith) { |
| 834 EXPECT_TRUE(EndsWith(L"Foo.plugin", L".plugin", true)); | 837 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), |
| 835 EXPECT_FALSE(EndsWith(L"Foo.Plugin", L".plugin", true)); | 838 ASCIIToUTF16(".plugin"), true)); |
| 836 EXPECT_TRUE(EndsWith(L"Foo.plugin", L".plugin", false)); | 839 EXPECT_FALSE(EndsWith(ASCIIToUTF16("Foo.Plugin"), |
| 837 EXPECT_TRUE(EndsWith(L"Foo.Plugin", L".plugin", false)); | 840 ASCIIToUTF16(".plugin"), true)); |
| 838 EXPECT_FALSE(EndsWith(L".plug", L".plugin", true)); | 841 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), |
| 839 EXPECT_FALSE(EndsWith(L".plug", L".plugin", false)); | 842 ASCIIToUTF16(".plugin"), false)); |
| 840 EXPECT_FALSE(EndsWith(L"Foo.plugin Bar", L".plugin", true)); | 843 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.Plugin"), |
| 841 EXPECT_FALSE(EndsWith(L"Foo.plugin Bar", L".plugin", false)); | 844 ASCIIToUTF16(".plugin"), false)); |
| 842 EXPECT_FALSE(EndsWith(std::wstring(), L".plugin", false)); | 845 EXPECT_FALSE(EndsWith(ASCIIToUTF16(".plug"), ASCIIToUTF16(".plugin"), true)); |
| 843 EXPECT_FALSE(EndsWith(std::wstring(), L".plugin", true)); | 846 EXPECT_FALSE(EndsWith(ASCIIToUTF16(".plug"), ASCIIToUTF16(".plugin"), false)); |
| 844 EXPECT_TRUE(EndsWith(L"Foo.plugin", std::wstring(), false)); | 847 EXPECT_FALSE(EndsWith(ASCIIToUTF16("Foo.plugin Bar"), |
| 845 EXPECT_TRUE(EndsWith(L"Foo.plugin", std::wstring(), true)); | 848 ASCIIToUTF16(".plugin"), true)); |
| 846 EXPECT_TRUE(EndsWith(L".plugin", L".plugin", false)); | 849 EXPECT_FALSE(EndsWith(ASCIIToUTF16("Foo.plugin Bar"), |
| 847 EXPECT_TRUE(EndsWith(L".plugin", L".plugin", true)); | 850 ASCIIToUTF16(".plugin"), false)); |
| 848 EXPECT_TRUE(EndsWith(std::wstring(), std::wstring(), false)); | 851 EXPECT_FALSE(EndsWith(string16(), ASCIIToUTF16(".plugin"), false)); |
| 849 EXPECT_TRUE(EndsWith(std::wstring(), std::wstring(), true)); | 852 EXPECT_FALSE(EndsWith(string16(), ASCIIToUTF16(".plugin"), true)); |
| 853 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(), false)); |
| 854 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(), true)); |
| 855 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), |
| 856 ASCIIToUTF16(".plugin"), false)); |
| 857 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), ASCIIToUTF16(".plugin"), true)); |
| 858 EXPECT_TRUE(EndsWith(string16(), string16(), false)); |
| 859 EXPECT_TRUE(EndsWith(string16(), string16(), true)); |
| 850 } | 860 } |
| 851 | 861 |
| 852 TEST(StringUtilTest, GetStringFWithOffsets) { | 862 TEST(StringUtilTest, GetStringFWithOffsets) { |
| 853 std::vector<string16> subst; | 863 std::vector<string16> subst; |
| 854 subst.push_back(ASCIIToUTF16("1")); | 864 subst.push_back(ASCIIToUTF16("1")); |
| 855 subst.push_back(ASCIIToUTF16("2")); | 865 subst.push_back(ASCIIToUTF16("2")); |
| 856 std::vector<size_t> offsets; | 866 std::vector<size_t> offsets; |
| 857 | 867 |
| 858 ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $1. Your number is $2."), | 868 ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $1. Your number is $2."), |
| 859 subst, | 869 subst, |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1182 const std::string live = kLive; | 1192 const std::string live = kLive; |
| 1183 std::string dead = live; | 1193 std::string dead = live; |
| 1184 strncpy(WriteInto(&dead, 5), kDead, 4); | 1194 strncpy(WriteInto(&dead, 5), kDead, 4); |
| 1185 EXPECT_EQ(kDead, dead); | 1195 EXPECT_EQ(kDead, dead); |
| 1186 EXPECT_EQ(4u, dead.size()); | 1196 EXPECT_EQ(4u, dead.size()); |
| 1187 EXPECT_EQ(kLive, live); | 1197 EXPECT_EQ(kLive, live); |
| 1188 EXPECT_EQ(4u, live.size()); | 1198 EXPECT_EQ(4u, live.size()); |
| 1189 } | 1199 } |
| 1190 | 1200 |
| 1191 } // namespace base | 1201 } // namespace base |
| OLD | NEW |