| 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 <algorithm> | 10 #include <algorithm> |
| (...skipping 976 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 | 987 |
| 988 TEST(StringUtilTest, ReplaceStringPlaceholdersConsecutiveDollarSigns) { | 988 TEST(StringUtilTest, ReplaceStringPlaceholdersConsecutiveDollarSigns) { |
| 989 std::vector<std::string> subst; | 989 std::vector<std::string> subst; |
| 990 subst.push_back("a"); | 990 subst.push_back("a"); |
| 991 subst.push_back("b"); | 991 subst.push_back("b"); |
| 992 subst.push_back("c"); | 992 subst.push_back("c"); |
| 993 EXPECT_EQ(ReplaceStringPlaceholders("$$1 $$$2 $$$$3", subst, NULL), | 993 EXPECT_EQ(ReplaceStringPlaceholders("$$1 $$$2 $$$$3", subst, NULL), |
| 994 "$1 $$2 $$$3"); | 994 "$1 $$2 $$$3"); |
| 995 } | 995 } |
| 996 | 996 |
| 997 TEST(StringUtilTest, MatchPatternTest) { | |
| 998 EXPECT_TRUE(MatchPattern("www.google.com", "*.com")); | |
| 999 EXPECT_TRUE(MatchPattern("www.google.com", "*")); | |
| 1000 EXPECT_FALSE(MatchPattern("www.google.com", "www*.g*.org")); | |
| 1001 EXPECT_TRUE(MatchPattern("Hello", "H?l?o")); | |
| 1002 EXPECT_FALSE(MatchPattern("www.google.com", "http://*)")); | |
| 1003 EXPECT_FALSE(MatchPattern("www.msn.com", "*.COM")); | |
| 1004 EXPECT_TRUE(MatchPattern("Hello*1234", "He??o\\*1*")); | |
| 1005 EXPECT_FALSE(MatchPattern("", "*.*")); | |
| 1006 EXPECT_TRUE(MatchPattern("", "*")); | |
| 1007 EXPECT_TRUE(MatchPattern("", "?")); | |
| 1008 EXPECT_TRUE(MatchPattern("", "")); | |
| 1009 EXPECT_FALSE(MatchPattern("Hello", "")); | |
| 1010 EXPECT_TRUE(MatchPattern("Hello*", "Hello*")); | |
| 1011 // Stop after a certain recursion depth. | |
| 1012 EXPECT_FALSE(MatchPattern("123456789012345678", "?????????????????*")); | |
| 1013 | |
| 1014 // Test UTF8 matching. | |
| 1015 EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0", "*\xe2\x99\xa0")); | |
| 1016 EXPECT_TRUE(MatchPattern("heart: \xe2\x99\xa0.", "heart: ?.")); | |
| 1017 EXPECT_TRUE(MatchPattern("hearts: \xe2\x99\xa0\xe2\x99\xa0", "*")); | |
| 1018 // Invalid sequences should be handled as a single invalid character. | |
| 1019 EXPECT_TRUE(MatchPattern("invalid: \xef\xbf\xbe", "invalid: ?")); | |
| 1020 // If the pattern has invalid characters, it shouldn't match anything. | |
| 1021 EXPECT_FALSE(MatchPattern("\xf4\x90\x80\x80", "\xf4\x90\x80\x80")); | |
| 1022 | |
| 1023 // Test UTF16 character matching. | |
| 1024 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("www.google.com"), | |
| 1025 UTF8ToUTF16("*.com"))); | |
| 1026 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("Hello*1234"), | |
| 1027 UTF8ToUTF16("He??o\\*1*"))); | |
| 1028 | |
| 1029 // This test verifies that consecutive wild cards are collapsed into 1 | |
| 1030 // wildcard (when this doesn't occur, MatchPattern reaches it's maximum | |
| 1031 // recursion depth). | |
| 1032 EXPECT_TRUE(MatchPattern(UTF8ToUTF16("Hello"), | |
| 1033 UTF8ToUTF16("He********************************o"))); | |
| 1034 } | |
| 1035 | |
| 1036 TEST(StringUtilTest, LcpyTest) { | 997 TEST(StringUtilTest, LcpyTest) { |
| 1037 // Test the normal case where we fit in our buffer. | 998 // Test the normal case where we fit in our buffer. |
| 1038 { | 999 { |
| 1039 char dst[10]; | 1000 char dst[10]; |
| 1040 wchar_t wdst[10]; | 1001 wchar_t wdst[10]; |
| 1041 EXPECT_EQ(7U, strlcpy(dst, "abcdefg", arraysize(dst))); | 1002 EXPECT_EQ(7U, strlcpy(dst, "abcdefg", arraysize(dst))); |
| 1042 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8)); | 1003 EXPECT_EQ(0, memcmp(dst, "abcdefg", 8)); |
| 1043 EXPECT_EQ(7U, wcslcpy(wdst, L"abcdefg", arraysize(wdst))); | 1004 EXPECT_EQ(7U, wcslcpy(wdst, L"abcdefg", arraysize(wdst))); |
| 1044 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8)); | 1005 EXPECT_EQ(0, memcmp(wdst, L"abcdefg", sizeof(wchar_t) * 8)); |
| 1045 } | 1006 } |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1226 const std::string live = kLive; | 1187 const std::string live = kLive; |
| 1227 std::string dead = live; | 1188 std::string dead = live; |
| 1228 strncpy(WriteInto(&dead, 5), kDead, 4); | 1189 strncpy(WriteInto(&dead, 5), kDead, 4); |
| 1229 EXPECT_EQ(kDead, dead); | 1190 EXPECT_EQ(kDead, dead); |
| 1230 EXPECT_EQ(4u, dead.size()); | 1191 EXPECT_EQ(4u, dead.size()); |
| 1231 EXPECT_EQ(kLive, live); | 1192 EXPECT_EQ(kLive, live); |
| 1232 EXPECT_EQ(4u, live.size()); | 1193 EXPECT_EQ(4u, live.size()); |
| 1233 } | 1194 } |
| 1234 | 1195 |
| 1235 } // namespace base | 1196 } // namespace base |
| OLD | NEW |