-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.cc
142 lines (126 loc) · 4.21 KB
/
replace.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <text_processing/replace.hpp>
#include <gtest/gtest.h>
TEST(Replace, SimpleWithViews)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace(simple, " "sv, "_"sv);
EXPECT_EQ(result, 3);
EXPECT_EQ(simple, "simple_string_with_words"s);
}
TEST(Replace, ConstVersion)
{
using namespace std::literals;
std::string_view simple("simple string with words");
auto result = Text::replaced(simple, " "sv, "_"sv);
EXPECT_EQ(result, "simple_string_with_words");
EXPECT_EQ(simple, "simple string with words");
}
TEST(Replace, OutOfBoundsStart)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace(simple, " "sv, "_"sv, 100);
EXPECT_EQ(result, 0);
EXPECT_EQ(simple, "simple string with words");
}
TEST(Replace, WideView)
{
using namespace std::literals;
std::wstring simple(L"simple string with words");
auto result = Text::replace(simple, L" "sv, L"_"sv);
EXPECT_EQ(result, 3);
EXPECT_EQ(simple, L"simple_string_with_words"s);
}
TEST(Replace, SimpleWithViewsAndStart)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace(simple, " "sv, "_"sv, 7);
EXPECT_EQ(result, 2);
EXPECT_EQ(simple, "simple string_with_words"s);
}
TEST(Replace, CaseInsensitive)
{
using namespace std::literals;
std::string simple("simple String with words");
auto result = Text::replace(simple, "s"sv, "_"sv, Text::Start, Text::End, Text::Step, Text::Case::Insensitive);
EXPECT_EQ(result, 3);
EXPECT_EQ(simple, "_imple _tring with word_"s);
}
TEST(Replace, WithSteps)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace(simple, "s"sv, "_"sv, 1, Text::End, 2);
EXPECT_EQ(result, 2);
EXPECT_EQ(simple, "simple _tring with word_"s);
}
TEST(ReplaceIf, ValueLargerThan)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace_if(
simple, [](auto ch) { return ch > 'u'; }, "_"sv, 7);
EXPECT_EQ(result, 2);
EXPECT_EQ(simple, "simple string _ith _ords"s);
}
TEST(ReplaceIf, ConstVersion)
{
using namespace std::literals;
std::string_view simple("simple string with words");
auto result = Text::replaced_if(
simple, [](auto ch) { return ch > 'u'; }, "_"sv, 7);
EXPECT_EQ(result, "simple string _ith _ords");
EXPECT_EQ(simple, "simple string with words");
}
TEST(ReplaceIf, OutOfBoundsStart)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace_if(
simple, [](auto ch) { return ch > 'u'; }, "_"sv, 100);
EXPECT_EQ(result, 0);
EXPECT_EQ(simple, "simple string with words");
}
TEST(ReplaceIf, WithSteps)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace_if(
simple, [](auto ch) { return ch > 'u'; }, "_"sv, Text::Start, Text::End, 2);
EXPECT_EQ(result, 1);
EXPECT_EQ(simple, "simple string _ith words"s);
}
TEST(ReplaceAny, ValueLargerThan)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace_any(simple, "tg"sv, "_"sv);
EXPECT_EQ(result, 3);
EXPECT_EQ(simple, "simple s_rin_ wi_h words"s);
}
TEST(ReplaceAny, ConstVersion)
{
using namespace std::literals;
std::string_view simple("simple string with words");
auto result = Text::replaced_any(simple, "tg"sv, "_"sv);
EXPECT_EQ(result, "simple s_rin_ wi_h words");
EXPECT_EQ(simple, "simple string with words");
}
TEST(ReplaceAny, OutOfBoundsStart)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace_any(simple, "tg"sv, "_"sv, 100);
EXPECT_EQ(result, 0);
EXPECT_EQ(simple, "simple string with words");
}
TEST(ReplaceAny, WithSteps)
{
using namespace std::literals;
std::string simple("simple string with words");
auto result = Text::replace_any(simple, "sw"sv, "_"sv, 1, Text::End, 2);
EXPECT_EQ(result, 3);
EXPECT_EQ(simple, "simple _tring with _ord_"s);
}