stc
Loading...
Searching...
No Matches
StringUtil.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <algorithm>
8#include <string>
9#include <sstream>
10#include <vector>
11#include <iostream>
12
13namespace stc::string {
14
23inline std::vector<std::string> split(const std::string& input, const char delimiter, int64_t limit = -1) {
24 if (delimiter == 0) {
25 std::vector<std::string> out;
26 std::transform(input.begin(), input.end(), std::back_inserter(out), [](const char& chr) {
27 return std::string(1, chr);
28 });
29 return out;
30 } else if (limit == 0) {
31 return {input};
32 } else if (input.empty()) {
33 return {};
34 }
35
36 std::vector<std::string> out;
37 std::stringstream stream(input);
38 std::string line;
39 int64_t count = 0;
40 while (getline(stream, line, delimiter)) {
41 out.push_back(line);
42 count++;
43 if (count == limit) {
44 break;
45 }
46 }
47 std::stringstream remainder;
48 remainder << stream.rdbuf();
49 std::string res = remainder.str();
50 if (res != "") {
51 out.push_back(res);
52 } else if (input.at(input.size() - 1) == delimiter) {
53 // Edge-case; delimiter last
54 out.push_back("");
55 }
56
57 return out;
58}
59
68inline std::vector<std::string> split(const std::string& input, const std::string& delimiter, int64_t limit = -1) {
69 if (delimiter.size() == 1) {
70 return split(input, delimiter[0], limit);
71 } else if (delimiter.size() == 0) {
72 return split(input, 0, limit);
73 } else if (limit == 0) {
74 return {input};
75 } else if (input.empty()) {
76 return {};
77 }
78
79 std::vector<std::string> out;
80 size_t pos = 0;
81 size_t index = 0;
82 std::string token;
83 int64_t count = 0;
84 while ((pos = input.find(delimiter, index)) != std::string::npos) {
85 // pos - index, because this shit operates on length rather than indices.
86 token = input.substr(index, pos - index);
87 index = pos + delimiter.size();
88 out.push_back(token);
89 count++;
90 if (count == limit) {
91 break;
92 }
93 }
94 if (index < input.size()) {
95 out.push_back(input.substr(index));
96 } else if (index == input.size()) {
97 // A delimiter was the last token
98 out.push_back("");
99 }
100 return out;
101
102}
103
104inline std::vector<int> byteArrayOf(const std::string& input) {
105 std::vector<int> out;
106 for (auto& chr : input) {
107 out.push_back(chr);
108 }
109 return out;
110}
111
115inline std::string getByteString(const std::string& input) {
116 std::string output;
117 for (const char c : input) {
118 output += std::to_string(c) + " ";
119 }
120 return output;
121}
122
123inline void replaceAll(std::string& input, const std::string& find, const std::string& replaceWith, size_t limit = 0) {
124 if (find.empty()) {
125 return;
126 }
127
128 size_t pos = 0;
129 size_t count = 0;
130 while ((pos = input.find(find, pos)) != std::string::npos) {
131 input.replace(pos, find.length(), replaceWith);
132 pos += replaceWith.length();
133
134 if (++count == limit) {
135 break;
136 }
137 }
138}
139
143inline void removeDuplicateWhitespace(const std::string& input, std::string& output) {
144 std::unique_copy(
145 input.begin(), input.end(), std::back_inserter(output),
146 [](char a, char b) {
147 return std::isspace(a) != 0 && std::isspace(b) != 0;
148 });
149}
150
151}
Definition StringUtil.hpp:13
std::vector< std::string > split(const std::string &input, const char delimiter, int64_t limit=-1)
Definition StringUtil.hpp:23
std::string getByteString(const std::string &input)
Definition StringUtil.hpp:115
void removeDuplicateWhitespace(const std::string &input, std::string &output)
Definition StringUtil.hpp:143
std::vector< int > byteArrayOf(const std::string &input)
Definition StringUtil.hpp:104
void replaceAll(std::string &input, const std::string &find, const std::string &replaceWith, size_t limit=0)
Definition StringUtil.hpp:123