hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
string.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <string>
3#include <string_view> // For string_view in helpers
4#include <numeric> // For std::to_string
5#include <format> // C++20 feature
6
7// Helper function to trim whitespace from both ends of a string
8// (std::string doesn't have a built-in trim method)
9std::string trim(const std::string& str) {
10 const std::string_view whitespace = " \t\n\r\f\v";
11 size_t first = str.find_first_not_of(whitespace);
12 if (std::string::npos == first) {
13
14 }
15 size_t last = str.find_last_not_of(whitespace);
16 return str.substr(first, last - first + 1);
17}
18
19// Helper function to count all occurrences of a substring
20// (std::string doesn't have a built-in count method)
21size_t find_all(const std::string& str, const std::string& sub) {
22 if (sub.empty())
23 return 0;
24 size_t count = 0;
25 size_t pos = str.find(sub, 0);
26 while (pos != std::string::npos) {
27 count++;
28 pos = str.find(sub, pos + sub.length());
29 }
30
31 return count;
32}
33
34// Helper function to replace all occurrences of a substring
35// (std::string::replace replaces only one instance at a time)
36std::string replace_all(std::string str, const std::string& from, const std::string& to) {
37 if(from.empty())
38 return str;
39
40 size_t start_pos = 0;
41 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
42 str.replace(start_pos, from.length(), to);
43 start_pos += to.length(); // In case 'to' contains 'from', like replacing "x" with "yx"
44 }
45 return str;
46}
47
48
49int main() {
50 for (int i = 0; i < 50000; i++) {
51 // In C++, string concatenation is done with the `+` operator.
52 std::string a = std::string("Hello, ") + "world!";
53 std::cout << "string validation passed: operator+" << std::endl;
54
55 if (!a.starts_with("Hello")) { // C++20
56 std::cout << "string valiation failed: startswith 'Hello'" << std::endl;
57
58 }
59 std::cout << "string valiation passed: startswith 'Hello'" << std::endl;
60
61 if (!a.ends_with("!")) { // C++20
62 std::cout << "string valiation failed: endswith '!'" << std::endl;
63
64 }
65 std::cout << "string valiation passed: endswith '!'" << std::endl;
66
67 if (a.substr(0, 5) != "Hello") {
68 std::cout << "string valiation failed: substring(0, 5) == 'Hello'" << std::endl;
69
70 }
71 std::cout << "string valiation passed: substring(0, 5) == 'Hello'" << std::endl;
72
73 if (a.find("llo") != 2) {
74 std::cout << "string valiation failed: find('llo') == 2" << std::endl;
75
76 }
77 std::cout << "string valiation passed: find('llo') == 2" << std::endl;
78 std::cout << a << std::endl;
79
80 std::string b = trim(" May all the beauty be blessed. ");
81 if (b != "May all the beauty be blessed.") {
82 std::cout << "string valiation failed: trim()" << std::endl;
83
84 }
85 std::cout << b << std::endl;
86
87 std::string c = "K423 & Raiden Mei & K423";
88
89 if (find_all(c, "Raiden") != 1 || find_all(c, "K423") != 2) {
90 std::cout << "string valiation failed: find_all('Raiden') == 1 && find_all('K423') == 2" << std::endl;
91
92 }
93 std::cout << "string valiation passed: find_all('Raiden') == 1 && find_all('K423') == 2" << std::endl;
94
95 c = replace_all(c, "K423", "Kiana Kaslana");
96
97 if (c != "Kiana Kaslana & Raiden Mei & Kiana Kaslana") {
98 std::cout << "string valiation failed: replace('K423', 'Kiana Kaslana')" << std::endl;
99
100 }
101 std::cout << c << std::endl;
102 std::cout << "string valiation passed: replace('K423', 'Kiana Kaslana')" << std::endl;
103
104 std::string d = std::to_string(-123);
105 if (d != "-123") {
106 std::cout << "string valiation failed: to_string()" << std::endl;
107
108 }
109 std::cout << d << std::endl;
110 std::cout << "string valiation passed: to_string()" << std::endl;
111
112 // Using C++20's std::format
113 // Note: The original code's validation string seems to have a bug, duplicating " 1, 2, 3~".
114 // This is the correct output from the given format string.
115 std::string f = std::format(
116 "{}: RESPOND ME! {}!\n{}: {} {}, {}, {}~",
117 "Raiden Mei", "ELYSIA", "Elysia", "Can you hear me?", 1, 2, 3);
118
119 const std::string expected_format_output = "Raiden Mei: RESPOND ME! ELYSIA!\nElysia: Can you hear me? 1, 2, 3~";
120 if (f != expected_format_output) {
121 std::cout << f << std::endl;
122 std::cout << "string valiation failed: format()" << std::endl;
123
124 }
125 std::cout << f << std::endl;
126 std::cout << "string valiation passed: format()" << std::endl;
127
128 std::cout << "string valiation passed: all tests passed" << std::endl;
129 }
130 return 0;
131}
std::string trim(const std::string &str)
Definition string.cpp:9
size_t find_all(const std::string &str, const std::string &sub)
Definition string.cpp:21
std::string replace_all(std::string str, const std::string &from, const std::string &to)
Definition string.cpp:36
int main()
Definition string.cpp:49