tvl-depot/absl/strings/str_replace_test.cc

342 lines
9.9 KiB
C++
Raw Normal View History

2017-09-19 22:54:40 +02:00
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
2017-09-19 22:54:40 +02:00
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/strings/str_replace.h"
#include <list>
Changes imported from Abseil "staging" branch: - b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Fix some typos in the usage examples by Jorg Brown <jorg@google.com> - 82be4a9adf3bb0ddafc0d46274969c99afffe870 Fix typo in optional.h comment. by Abseil Team <absl-team@google.com> - d6ee63bf8fc51fba074c23b33cebc28c808d7f07 Remove internal-only identifiers from code. by Daniel Katz <katzdm@google.com> - f9c3ad2f0d73f53b21603638af8b4bed636e79f4 Use easier understandable names for absl::StartsWith and ... by Abseil Team <absl-team@google.com> - 7c16c14fefee89c927b8789d6043c4691bcffc9b Add -Wno-missing-prototypes back to the LLVM copts. by Derek Mauro <dmauro@google.com> - 2f4b7d2e50c7023240242f1e15db60ccd7e8768d IWYU | absl/strings by Juemin Yang <jueminyang@google.com> - a99cbcc1daa34a2d6a2bb26de275e05173cc77e9 IWYU | absl/type by Juemin Yang <jueminyang@google.com> - 12e1146d0fc76c071d7e0ebaabb62f0a984fae66 Use LLVM_FLAGS and LLVM_TEST_FLAGS when --compiler=llvm. by Derek Mauro <dmauro@google.com> - cd6bea616abda558d0bace5bd77455662a233688 IWYU | absl/debugging by Juemin Yang <jueminyang@google.com> - d9a7382e59d46a8581b6b7a31cd5a48bb89326e9 IWYU | absl/synchronization by Juemin Yang <jueminyang@google.com> - 07ec7d6d5a4a666f4183c5d0ed9c342baa7b24bc IWYU | absl/numeric by Juemin Yang <jueminyang@google.com> - 12bfe40051f4270f8707e191af5652f83f2f750c Remove the RoundTrip{Float,Double}ToBuffer routines from ... by Jorg Brown <jorg@google.com> - eeb4fd67c9d97f66cb9475c3c5e51ab132f1c810 Adds conversion functions for converting between absl/tim... by Greg Miller <jgm@google.com> - 59a2108d05d4ea85dc5cc11e49b2cd2335d4295a Change Substitute to use %.6g formatting rather than 15/1... by Jorg Brown <jorg@google.com> - 394becb48e0fcd161642cdaac5120d32567e0ef8 IWYU | absl/meta by Juemin Yang <jueminyang@google.com> - 1e5da6e8da336699b2469dcf6dda025b9b0ec4c9 Rewrite atomic_hook.h to not use std::atomic<T*> under Wi... by Greg Falcon <gfalcon@google.com> GitOrigin-RevId: b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Change-Id: I14e331d91c956ef045ac7927091a9f179716de0c
2017-09-24 17:20:48 +02:00
#include <map>
2017-09-19 22:54:40 +02:00
#include <tuple>
#include "gtest/gtest.h"
#include "absl/strings/str_cat.h"
Changes imported from Abseil "staging" branch: - b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Fix some typos in the usage examples by Jorg Brown <jorg@google.com> - 82be4a9adf3bb0ddafc0d46274969c99afffe870 Fix typo in optional.h comment. by Abseil Team <absl-team@google.com> - d6ee63bf8fc51fba074c23b33cebc28c808d7f07 Remove internal-only identifiers from code. by Daniel Katz <katzdm@google.com> - f9c3ad2f0d73f53b21603638af8b4bed636e79f4 Use easier understandable names for absl::StartsWith and ... by Abseil Team <absl-team@google.com> - 7c16c14fefee89c927b8789d6043c4691bcffc9b Add -Wno-missing-prototypes back to the LLVM copts. by Derek Mauro <dmauro@google.com> - 2f4b7d2e50c7023240242f1e15db60ccd7e8768d IWYU | absl/strings by Juemin Yang <jueminyang@google.com> - a99cbcc1daa34a2d6a2bb26de275e05173cc77e9 IWYU | absl/type by Juemin Yang <jueminyang@google.com> - 12e1146d0fc76c071d7e0ebaabb62f0a984fae66 Use LLVM_FLAGS and LLVM_TEST_FLAGS when --compiler=llvm. by Derek Mauro <dmauro@google.com> - cd6bea616abda558d0bace5bd77455662a233688 IWYU | absl/debugging by Juemin Yang <jueminyang@google.com> - d9a7382e59d46a8581b6b7a31cd5a48bb89326e9 IWYU | absl/synchronization by Juemin Yang <jueminyang@google.com> - 07ec7d6d5a4a666f4183c5d0ed9c342baa7b24bc IWYU | absl/numeric by Juemin Yang <jueminyang@google.com> - 12bfe40051f4270f8707e191af5652f83f2f750c Remove the RoundTrip{Float,Double}ToBuffer routines from ... by Jorg Brown <jorg@google.com> - eeb4fd67c9d97f66cb9475c3c5e51ab132f1c810 Adds conversion functions for converting between absl/tim... by Greg Miller <jgm@google.com> - 59a2108d05d4ea85dc5cc11e49b2cd2335d4295a Change Substitute to use %.6g formatting rather than 15/1... by Jorg Brown <jorg@google.com> - 394becb48e0fcd161642cdaac5120d32567e0ef8 IWYU | absl/meta by Juemin Yang <jueminyang@google.com> - 1e5da6e8da336699b2469dcf6dda025b9b0ec4c9 Rewrite atomic_hook.h to not use std::atomic<T*> under Wi... by Greg Falcon <gfalcon@google.com> GitOrigin-RevId: b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Change-Id: I14e331d91c956ef045ac7927091a9f179716de0c
2017-09-24 17:20:48 +02:00
#include "absl/strings/str_split.h"
2017-09-19 22:54:40 +02:00
TEST(StrReplaceAll, OneReplacement) {
std::string s;
// Empty string.
2017-09-19 22:54:40 +02:00
s = absl::StrReplaceAll(s, {{"", ""}});
EXPECT_EQ(s, "");
s = absl::StrReplaceAll(s, {{"x", ""}});
EXPECT_EQ(s, "");
s = absl::StrReplaceAll(s, {{"", "y"}});
EXPECT_EQ(s, "");
s = absl::StrReplaceAll(s, {{"x", "y"}});
EXPECT_EQ(s, "");
// Empty substring.
s = absl::StrReplaceAll("abc", {{"", ""}});
EXPECT_EQ(s, "abc");
s = absl::StrReplaceAll("abc", {{"", "y"}});
EXPECT_EQ(s, "abc");
s = absl::StrReplaceAll("abc", {{"x", ""}});
EXPECT_EQ(s, "abc");
// Substring not found.
s = absl::StrReplaceAll("abc", {{"xyz", "123"}});
EXPECT_EQ(s, "abc");
// Replace entire string.
2017-09-19 22:54:40 +02:00
s = absl::StrReplaceAll("abc", {{"abc", "xyz"}});
EXPECT_EQ(s, "xyz");
// Replace once at the start.
s = absl::StrReplaceAll("abc", {{"a", "x"}});
EXPECT_EQ(s, "xbc");
// Replace once in the middle.
s = absl::StrReplaceAll("abc", {{"b", "x"}});
EXPECT_EQ(s, "axc");
// Replace once at the end.
s = absl::StrReplaceAll("abc", {{"c", "x"}});
EXPECT_EQ(s, "abx");
// Replace multiple times with varying lengths of original/replacement.
s = absl::StrReplaceAll("ababa", {{"a", "xxx"}});
EXPECT_EQ(s, "xxxbxxxbxxx");
s = absl::StrReplaceAll("ababa", {{"b", "xxx"}});
EXPECT_EQ(s, "axxxaxxxa");
s = absl::StrReplaceAll("aaabaaabaaa", {{"aaa", "x"}});
EXPECT_EQ(s, "xbxbx");
s = absl::StrReplaceAll("abbbabbba", {{"bbb", "x"}});
EXPECT_EQ(s, "axaxa");
// Overlapping matches are replaced greedily.
s = absl::StrReplaceAll("aaa", {{"aa", "x"}});
EXPECT_EQ(s, "xa");
// The replacements are not recursive.
s = absl::StrReplaceAll("aaa", {{"aa", "a"}});
EXPECT_EQ(s, "aa");
}
TEST(StrReplaceAll, ManyReplacements) {
std::string s;
// Empty string.
2017-09-19 22:54:40 +02:00
s = absl::StrReplaceAll("", {{"", ""}, {"x", ""}, {"", "y"}, {"x", "y"}});
EXPECT_EQ(s, "");
// Empty substring.
s = absl::StrReplaceAll("abc", {{"", ""}, {"", "y"}, {"x", ""}});
EXPECT_EQ(s, "abc");
// Replace entire string, one char at a time
2017-09-19 22:54:40 +02:00
s = absl::StrReplaceAll("abc", {{"a", "x"}, {"b", "y"}, {"c", "z"}});
EXPECT_EQ(s, "xyz");
s = absl::StrReplaceAll("zxy", {{"z", "x"}, {"x", "y"}, {"y", "z"}});
EXPECT_EQ(s, "xyz");
// Replace once at the start (longer matches take precedence)
s = absl::StrReplaceAll("abc", {{"a", "x"}, {"ab", "xy"}, {"abc", "xyz"}});
EXPECT_EQ(s, "xyz");
// Replace once in the middle.
s = absl::StrReplaceAll(
"Abc!", {{"a", "x"}, {"ab", "xy"}, {"b", "y"}, {"bc", "yz"}, {"c", "z"}});
EXPECT_EQ(s, "Ayz!");
// Replace once at the end.
s = absl::StrReplaceAll(
"Abc!",
{{"a", "x"}, {"ab", "xy"}, {"b", "y"}, {"bc!", "yz?"}, {"c!", "z;"}});
EXPECT_EQ(s, "Ayz?");
// Replace multiple times with varying lengths of original/replacement.
s = absl::StrReplaceAll("ababa", {{"a", "xxx"}, {"b", "XXXX"}});
EXPECT_EQ(s, "xxxXXXXxxxXXXXxxx");
// Overlapping matches are replaced greedily.
s = absl::StrReplaceAll("aaa", {{"aa", "x"}, {"a", "X"}});
EXPECT_EQ(s, "xX");
s = absl::StrReplaceAll("aaa", {{"a", "X"}, {"aa", "x"}});
EXPECT_EQ(s, "xX");
// Two well-known sentences
s = absl::StrReplaceAll("the quick brown fox jumped over the lazy dogs",
{
{"brown", "box"},
{"dogs", "jugs"},
{"fox", "with"},
{"jumped", "five"},
{"over", "dozen"},
{"quick", "my"},
{"the", "pack"},
{"the lazy", "liquor"},
});
EXPECT_EQ(s, "pack my box with five dozen liquor jugs");
}
TEST(StrReplaceAll, ManyReplacementsInMap) {
std::map<const char *, const char *> replacements;
replacements["$who"] = "Bob";
replacements["$count"] = "5";
replacements["#Noun"] = "Apples";
std::string s = absl::StrReplaceAll("$who bought $count #Noun. Thanks $who!",
replacements);
2017-09-19 22:54:40 +02:00
EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
}
TEST(StrReplaceAll, ReplacementsInPlace) {
std::string s = std::string("$who bought $count #Noun. Thanks $who!");
int count;
count = absl::StrReplaceAll({{"$count", absl::StrCat(5)},
{"$who", "Bob"},
{"#Noun", "Apples"}}, &s);
EXPECT_EQ(count, 4);
EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
}
TEST(StrReplaceAll, ReplacementsInPlaceInMap) {
std::string s = std::string("$who bought $count #Noun. Thanks $who!");
std::map<absl::string_view, absl::string_view> replacements;
replacements["$who"] = "Bob";
replacements["$count"] = "5";
replacements["#Noun"] = "Apples";
int count;
count = absl::StrReplaceAll(replacements, &s);
EXPECT_EQ(count, 4);
EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
}
struct Cont {
Cont() {}
explicit Cont(absl::string_view src) : data(src) {}
absl::string_view data;
};
template <int index>
absl::string_view get(const Cont& c) {
auto splitter = absl::StrSplit(c.data, ':');
auto it = splitter.begin();
for (int i = 0; i < index; ++i) ++it;
return *it;
}
TEST(StrReplaceAll, VariableNumber) {
std::string s;
{
std::vector<std::pair<std::string, std::string>> replacements;
s = "abc";
EXPECT_EQ(0, absl::StrReplaceAll(replacements, &s));
EXPECT_EQ("abc", s);
s = "abc";
replacements.push_back({"a", "A"});
EXPECT_EQ(1, absl::StrReplaceAll(replacements, &s));
EXPECT_EQ("Abc", s);
s = "abc";
replacements.push_back({"b", "B"});
EXPECT_EQ(2, absl::StrReplaceAll(replacements, &s));
EXPECT_EQ("ABc", s);
s = "abc";
replacements.push_back({"d", "D"});
EXPECT_EQ(2, absl::StrReplaceAll(replacements, &s));
EXPECT_EQ("ABc", s);
EXPECT_EQ("ABcABc", absl::StrReplaceAll("abcabc", replacements));
}
{
std::map<const char*, const char*> replacements;
replacements["aa"] = "x";
replacements["a"] = "X";
s = "aaa";
EXPECT_EQ(2, absl::StrReplaceAll(replacements, &s));
EXPECT_EQ("xX", s);
EXPECT_EQ("xxX", absl::StrReplaceAll("aaaaa", replacements));
}
{
std::list<std::pair<absl::string_view, absl::string_view>> replacements = {
{"a", "x"}, {"b", "y"}, {"c", "z"}};
std::string s = absl::StrReplaceAll("abc", replacements);
EXPECT_EQ(s, "xyz");
}
{
using X = std::tuple<absl::string_view, std::string, int>;
std::vector<X> replacements(3);
replacements[0] = X{"a", "x", 1};
replacements[1] = X{"b", "y", 0};
replacements[2] = X{"c", "z", -1};
std::string s = absl::StrReplaceAll("abc", replacements);
EXPECT_EQ(s, "xyz");
}
{
std::vector<Cont> replacements(3);
replacements[0] = Cont{"a:x"};
replacements[1] = Cont{"b:y"};
replacements[2] = Cont{"c:z"};
std::string s = absl::StrReplaceAll("abc", replacements);
EXPECT_EQ(s, "xyz");
}
}
// Same as above, but using the in-place variant of absl::StrReplaceAll,
// that returns the # of replacements performed.
TEST(StrReplaceAll, Inplace) {
std::string s;
int reps;
// Empty string.
2017-09-19 22:54:40 +02:00
s = "";
reps = absl::StrReplaceAll({{"", ""}, {"x", ""}, {"", "y"}, {"x", "y"}}, &s);
EXPECT_EQ(reps, 0);
EXPECT_EQ(s, "");
// Empty substring.
s = "abc";
reps = absl::StrReplaceAll({{"", ""}, {"", "y"}, {"x", ""}}, &s);
EXPECT_EQ(reps, 0);
EXPECT_EQ(s, "abc");
// Replace entire string, one char at a time
2017-09-19 22:54:40 +02:00
s = "abc";
reps = absl::StrReplaceAll({{"a", "x"}, {"b", "y"}, {"c", "z"}}, &s);
EXPECT_EQ(reps, 3);
EXPECT_EQ(s, "xyz");
s = "zxy";
reps = absl::StrReplaceAll({{"z", "x"}, {"x", "y"}, {"y", "z"}}, &s);
EXPECT_EQ(reps, 3);
EXPECT_EQ(s, "xyz");
// Replace once at the start (longer matches take precedence)
s = "abc";
reps = absl::StrReplaceAll({{"a", "x"}, {"ab", "xy"}, {"abc", "xyz"}}, &s);
EXPECT_EQ(reps, 1);
EXPECT_EQ(s, "xyz");
// Replace once in the middle.
s = "Abc!";
reps = absl::StrReplaceAll(
{{"a", "x"}, {"ab", "xy"}, {"b", "y"}, {"bc", "yz"}, {"c", "z"}}, &s);
EXPECT_EQ(reps, 1);
EXPECT_EQ(s, "Ayz!");
// Replace once at the end.
s = "Abc!";
reps = absl::StrReplaceAll(
{{"a", "x"}, {"ab", "xy"}, {"b", "y"}, {"bc!", "yz?"}, {"c!", "z;"}}, &s);
EXPECT_EQ(reps, 1);
EXPECT_EQ(s, "Ayz?");
// Replace multiple times with varying lengths of original/replacement.
s = "ababa";
reps = absl::StrReplaceAll({{"a", "xxx"}, {"b", "XXXX"}}, &s);
EXPECT_EQ(reps, 5);
EXPECT_EQ(s, "xxxXXXXxxxXXXXxxx");
// Overlapping matches are replaced greedily.
s = "aaa";
reps = absl::StrReplaceAll({{"aa", "x"}, {"a", "X"}}, &s);
EXPECT_EQ(reps, 2);
EXPECT_EQ(s, "xX");
s = "aaa";
reps = absl::StrReplaceAll({{"a", "X"}, {"aa", "x"}}, &s);
EXPECT_EQ(reps, 2);
EXPECT_EQ(s, "xX");
// Two well-known sentences
s = "the quick brown fox jumped over the lazy dogs";
reps = absl::StrReplaceAll(
{
{"brown", "box"},
{"dogs", "jugs"},
{"fox", "with"},
{"jumped", "five"},
{"over", "dozen"},
{"quick", "my"},
{"the", "pack"},
{"the lazy", "liquor"},
},
&s);
EXPECT_EQ(reps, 8);
EXPECT_EQ(s, "pack my box with five dozen liquor jugs");
}