-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStringView.cpp
More file actions
112 lines (84 loc) · 3.13 KB
/
StringView.cpp
File metadata and controls
112 lines (84 loc) · 3.13 KB
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
// =====================================================================================
// StringView.cpp // Klasse std::string_view
// =====================================================================================
module modern_cpp:string_view;
namespace StringViewDemonstration {
static void test_01()
{
std::string_view sv{ "AAAAAAAAAAAAAAAAAAAAAAAAAAAA" }; // Konstante Zeichenkette
std::string s{ "AAAAAAAAAAAAAAAAAAAAAAAAAAAA" }; // Heap
// sv[0] = '?'; // error
char ch{ sv[0] };
}
static void test_02()
{
std::string s{ "AAAAAAAAAAAAAAAAAAAAAAAAAAAA" };
std::string_view sv{ s };
std::println("{}", sv);
s += "BBBBBBBBBBBBBBBBBBBBBBBBBBBB"; // Caution: the content of s is reallocated !
std::println("{}", sv);
}
static void test_03()
{
std::string s{ "This is a std::string object" };
std::println("{}", s);
std::println("{}", s.size());
std::string sub{ s.substr(5, 23) };
std::println("{}", sub);
std::println("{}", sub.size());
std::string_view sv{ "This is a std::string_view object" };
std::println("{}", sv);
std::println("{}", sv.size());
std::string_view sv_sub = sv.substr(5, 28);
std::println("{}", sv_sub);
std::println("{}", sv_sub.size());
}
static void test_04()
{
using namespace std::literals; // easiest way to access the s and sv suffixes
auto s1 = "ABC"; // no suffix: C-style string literal
auto s2 = "DEF"s; // s suffix: std::string literal
auto s3 = "GHI"sv; // sv suffix: std::string_view literal
std::println("{}", s1);
std::println("{}", s2);
std::println("{}", s3);
}
static std::size_t countUpperCaseChars(std::string_view sv) {
std::size_t result{};
for (char c : sv) {
if (std::isupper(c)) {
++result;
}
}
return result;
}
static void test_05()
{
std::string_view sv{ "DiesIstEinLangerSatz" };
std::size_t count{ countUpperCaseChars(sv) };
std::println("countUpperCaseChars: {}", count);
std::string s{ "AuchDasWiederIstEinLangerSatz" };
count = countUpperCaseChars(s);
std::println("countUpperCaseChars: {}", count);
count = countUpperCaseChars("NurKurzJetzt");
std::println("countUpperCaseChars: {}", count);
// works too
count = countUpperCaseChars({ &s[25] }); // "Satz"
std::println("countUpperCaseChars: {}", count);
// works too
count = countUpperCaseChars({ &s[26], 2 }); // "at"
std::println("countUpperCaseChars: {}", count);
}
}
void main_string_view()
{
using namespace StringViewDemonstration;
test_01();
test_02();
test_03();
test_04();
test_05();
}
// =====================================================================================
// End-of-File
// =====================================================================================