CPP-TOOLBOX
Loading...
Searching...
No Matches
text_utils.hpp
Go to the documentation of this file.
1#ifndef TEXT_UTILS_HPP
2#define TEXT_UTILS_HPP
3
4#include <string>
5#include <sstream>
6#include <unordered_map>
7#include <vector>
8#include <stdexcept>
9
10namespace text_utils {
11
12inline const std::string double_quote = "\"";
13inline const std::string single_quote = "'";
14inline const std::string newline = "\n";
15inline const std::string tab = "\t";
16inline const std::string space = " ";
17inline const std::string empty = "";
18inline const std::string comma = ",";
19inline const std::string period = ".";
20inline const std::string colon = ":";
21inline const std::string semicolon = ";";
22inline const std::string dash = "-";
23inline const std::string underscore = "_";
24inline const std::string slash = "/";
25inline const std::string backslash = "\\";
26inline const std::string pipe = "|";
27inline const std::string ampersand = "&";
28inline const std::string at_sign = "@";
29inline const std::string hash = "#";
30inline const std::string dollar = "$";
31inline const std::string percent = "%";
32inline const std::string caret = "^";
33inline const std::string asterisk = "*";
34inline const std::string plus = "+";
35inline const std::string equals = "=";
36inline const std::string question_mark = "?";
37inline const std::string exclamation_mark = "!";
38inline const std::string left_paren = "(";
39inline const std::string right_paren = ")";
40inline const std::string left_bracket = "[";
41inline const std::string right_bracket = "]";
42inline const std::string left_brace = "{";
43inline const std::string right_brace = "}";
44inline const std::string less_than = "<";
45inline const std::string greater_than = ">";
46inline const std::string newline_windows = "\r\n";
47inline const std::string carriage_return = "\r";
48
49inline const std::string natural_numbers = "ℕ";
50inline const std::string element_of = "∈";
51
53 public:
62 template <typename... Args> void add(Args &&...args) {
63 std::ostringstream oss;
64 (oss << ... << args); // fold expression to stream all args
65 data_ += oss.str();
66 }
67
69 void clear() { data_.clear(); }
70
72 std::string str() const { return data_; }
73
75 size_t size() const { return data_.size(); }
76
77 private:
78 std::string data_;
79};
80
82 public:
83 MultilineStringAccumulator() : indent_level_(0), indent_size_(4) {}
84
86 void indent() { ++indent_level_; }
87
89 void unindent() {
90 if (indent_level_ > 0) {
91 --indent_level_;
92 }
93 }
94
100 template <typename... Args> void add(Args &&...args) {
101 std::ostringstream oss;
102 oss << std::string(indent_level_ * indent_size_, ' '); // indent prefix
103 (oss << ... << args); // fold expression (C++17+)
104 lines_.emplace_back(oss.str());
105 }
106
111 void add_multiline(const std::string &multiline_str) {
112 std::istringstream iss(multiline_str);
113 std::string line;
114 while (std::getline(iss, line)) {
115 lines_.emplace_back(std::string(indent_level_ * indent_size_, ' ') + line);
116 }
117 }
118
125 void insert_line(size_t index, const std::string &line) {
126 if (index > lines_.size()) {
127 throw std::out_of_range("insert_line: index out of range");
128 }
129 lines_.insert(lines_.begin() + index, std::string(indent_level_ * indent_size_, ' ') + line);
130 }
131
138 void insert_lines(size_t index, const MultilineStringAccumulator &other) {
139 if (index > lines_.size()) {
140 throw std::out_of_range("insert_lines: index out of range");
141 }
142 lines_.insert(lines_.begin() + index, other.lines_.begin(), other.lines_.end());
143 }
144
151 void insert_multiline(size_t index, const std::string &multiline_str) {
152 if (index > lines_.size()) {
153 throw std::out_of_range("insert_multiline: index out of range");
154 }
155
156 std::istringstream iss(multiline_str);
157 std::string line;
158 std::vector<std::string> new_lines;
159
160 while (std::getline(iss, line)) {
161 new_lines.emplace_back(std::string(indent_level_ * indent_size_, ' ') + line);
162 }
163
164 lines_.insert(lines_.begin() + index, new_lines.begin(), new_lines.end());
165 }
166
172 void remove_line(size_t index) {
173 if (index >= lines_.size()) {
174 throw std::out_of_range("remove_line: index out of range");
175 }
176 lines_.erase(lines_.begin() + index);
177 }
178
180 std::string str() const {
181 std::ostringstream oss;
182 for (size_t i = 0; i < lines_.size(); ++i) {
183 oss << lines_[i];
184 if (i + 1 < lines_.size()) {
185 oss << '\n';
186 }
187 }
188 return oss.str();
189 }
190
192 void clear() { lines_.clear(); }
193
195 size_t line_count() const { return lines_.size(); }
196
197 private:
198 std::vector<std::string> lines_;
199 size_t indent_level_;
200 size_t indent_size_;
201};
202
203// ---------------- Free functions ----------------
204
211std::string remove_consecutive_duplicates(const std::string &input, const std::string &dedup_chars = "");
212
218std::string abbreviate_snake_case(const std::string &input);
219
221bool is_integer(const std::string &str);
222
224bool is_rational(const std::string &str);
225
232std::string add_newlines_to_long_string(const std::string &text, size_t max_chars_per_line = 25);
233
240std::vector<std::string> split(const std::string &str, const std::string &delimiter);
241
248std::vector<std::string> split_once_from_right(const std::string &str, const std::string &delimiter);
249
256std::string join(const std::vector<std::string> &elements, const std::string &separator);
257
259std::string trim(const std::string &s);
260
268std::string surround(const std::string &str, const std::string &left, const std::string &right = "");
269
275std::string pascal_to_snake_case(const std::string &input);
276
282std::string snake_to_pascal_case(const std::string &input);
283
290std::string join_multiline(const std::string &input, bool replace_newlines_with_space = false);
291
293std::string replace_char(const std::string &input, char from_char, char to_char);
294
296std::string replace_chars(const std::string &input, const std::unordered_map<char, char> &mapping);
297
299std::string replace_substring(const std::string &input, const std::string &from_substr, const std::string &to_substr);
300
302bool starts_with(const std::string &str, const std::string &prefix);
303
305bool contains(const std::string &str, const std::string &substr);
306
308std::string get_substring(const std::string &input, size_t start, size_t end);
309
311std::string remove_newlines(const std::string &input);
312
314std::string collapse_whitespace(const std::string &input);
315
317std::string replace_literal_newlines_with_real(const std::string &input);
318
326std::string indent(const std::string &text, int indent_level, int spaces_per_indent = 4);
327
333std::unordered_map<std::string, std::string> map_words_to_abbreviations(const std::vector<std::string> &words);
334
335struct Node {
336 std::string key;
337 std::string value;
338 std::vector<Node> children;
339 bool is_block = false;
340};
341
342Node parse_block(const std::string &s, size_t &pos);
343std::string parse_token(const std::string &s, size_t &pos);
344std::vector<std::string> build_buffer(const Node &node);
345std::string format_nested_brace_string_recursive(const std::string &input);
346
347} // namespace text_utils
348
349#endif // TEXT_UTILS_HPP
void indent()
Increase indentation level.
Definition text_utils.hpp:86
void insert_multiline(size_t index, const std::string &multiline_str)
Insert multiple lines from a string.
Definition text_utils.hpp:151
void add(Args &&...args)
Add a line with current indentation applied.
Definition text_utils.hpp:100
void clear()
Clear all stored lines.
Definition text_utils.hpp:192
void unindent()
Decrease indentation level (no-op if already at 0).
Definition text_utils.hpp:89
size_t line_count() const
Get the number of stored lines.
Definition text_utils.hpp:195
void remove_line(size_t index)
Remove a line at the given index.
Definition text_utils.hpp:172
void add_multiline(const std::string &multiline_str)
Add multiple lines with indentation applied.
Definition text_utils.hpp:111
void insert_line(size_t index, const std::string &line)
Insert a line at the given index.
Definition text_utils.hpp:125
MultilineStringAccumulator()
Definition text_utils.hpp:83
std::string str() const
Get the accumulated text as a single string with newlines.
Definition text_utils.hpp:180
void insert_lines(size_t index, const MultilineStringAccumulator &other)
Insert all lines from another accumulator.
Definition text_utils.hpp:138
Definition text_utils.hpp:52
void add(Args &&...args)
Append values to the accumulator.
Definition text_utils.hpp:62
void clear()
Clear the accumulator.
Definition text_utils.hpp:69
std::string str() const
Get the accumulated string.
Definition text_utils.hpp:72
size_t size() const
Get the size of the accumulated string.
Definition text_utils.hpp:75
@ s
Definition input_state.hpp:45
@ i
Definition input_state.hpp:35
Definition text_utils.cpp:9
std::string format_nested_brace_string_recursive(const std::string &input)
Definition text_utils.cpp:525
const std::string question_mark
Definition text_utils.hpp:36
std::string replace_char(const std::string &input, char from_char, char to_char)
Replace a character with another in a string.
Definition text_utils.cpp:196
std::string remove_newlines(const std::string &input)
Remove all newlines from a string.
Definition text_utils.cpp:243
bool is_integer(const std::string &str)
Check if a string represents an integer.
Definition text_utils.cpp:49
std::string indent(const std::string &text, int indent_level, int spaces_per_indent)
Indent text by a given number of levels.
Definition text_utils.cpp:293
const std::string natural_numbers
Definition text_utils.hpp:49
const std::string period
Definition text_utils.hpp:19
const std::string exclamation_mark
Definition text_utils.hpp:37
const std::string element_of
Definition text_utils.hpp:50
std::string join_multiline(const std::string &input, bool replace_newlines_with_space)
Join a string with newlines removed or replaced.
Definition text_utils.cpp:165
const std::string hash
Definition text_utils.hpp:29
const std::string ampersand
Definition text_utils.hpp:27
const std::string newline
Definition text_utils.hpp:14
std::string join(const std::vector< std::string > &elements, const std::string &separator)
Join elements into a single string with a separator.
Definition text_utils.cpp:115
const std::string carriage_return
Definition text_utils.hpp:47
const std::string left_bracket
Definition text_utils.hpp:40
std::string parse_token(const std::string &s, size_t &pos)
Definition text_utils.cpp:364
std::string remove_consecutive_duplicates(const std::string &input, const std::string &dedup_chars)
Remove consecutive duplicate characters from a string.
Definition text_utils.cpp:11
const std::string percent
Definition text_utils.hpp:31
bool starts_with(const std::string &str, const std::string &prefix)
Check if a string starts with a prefix.
Definition text_utils.cpp:230
std::string replace_substring(const std::string &input, const std::string &from_substr, const std::string &to_substr)
Replace all occurrences of a substring with another substring.
Definition text_utils.cpp:216
const std::string space
Definition text_utils.hpp:16
std::vector< std::string > split_once_from_right(const std::string &str, const std::string &delimiter)
Split a string once from the right.
Definition text_utils.cpp:101
std::unordered_map< std::string, std::string > map_words_to_abbreviations(const std::vector< std::string > &words)
Create a map from words to their abbreviations.
Definition text_utils.cpp:352
std::string replace_literal_newlines_with_real(const std::string &input)
Replace literal "\n" sequences with real newlines.
Definition text_utils.cpp:278
const std::string empty
Definition text_utils.hpp:17
const std::string pipe
Definition text_utils.hpp:26
const std::string at_sign
Definition text_utils.hpp:28
std::string abbreviate_snake_case(const std::string &input)
Abbreviate a snake_case string by shortening each word.
Definition text_utils.cpp:35
std::string pascal_to_snake_case(const std::string &input)
Convert a PascalCase string to snake_case.
Definition text_utils.cpp:134
std::string snake_to_pascal_case(const std::string &input)
Convert a snake_case string to PascalCase.
Definition text_utils.cpp:155
const std::string newline_windows
Definition text_utils.hpp:46
const std::string equals
Definition text_utils.hpp:35
const std::string underscore
Definition text_utils.hpp:23
const std::string left_paren
Definition text_utils.hpp:38
std::vector< std::string > split(const std::string &str, const std::string &delimiter)
Split a string by a delimiter.
Definition text_utils.cpp:89
const std::string double_quote
Definition text_utils.hpp:12
const std::string semicolon
Definition text_utils.hpp:21
std::vector< std::string > build_buffer(const Node &node)
Definition text_utils.cpp:422
std::string add_newlines_to_long_string(const std::string &text, size_t max_chars_per_line)
Insert newlines into long strings.
Definition text_utils.cpp:63
const std::string tab
Definition text_utils.hpp:15
const std::string plus
Definition text_utils.hpp:34
std::string surround(const std::string &str, const std::string &left, const std::string &right)
Surround a string with left and right substrings.
Definition text_utils.cpp:306
const std::string greater_than
Definition text_utils.hpp:45
bool contains(const std::string &str, const std::string &substr)
Check if a string contains a substring.
Definition text_utils.cpp:234
const std::string comma
Definition text_utils.hpp:18
const std::string left_brace
Definition text_utils.hpp:42
std::string replace_chars(const std::string &input, const std::unordered_map< char, char > &mapping)
Replace characters in a string according to a mapping.
Definition text_utils.cpp:202
Node parse_block(const std::string &s, size_t &pos)
Definition text_utils.cpp:372
std::string get_substring(const std::string &input, size_t start, size_t end)
Extract a substring from start to end indices.
Definition text_utils.cpp:236
bool is_rational(const std::string &value)
Check if a string represents a rational (floating-point) number.
Definition text_utils.cpp:58
const std::string slash
Definition text_utils.hpp:24
const std::string caret
Definition text_utils.hpp:32
const std::string single_quote
Definition text_utils.hpp:13
const std::string right_paren
Definition text_utils.hpp:39
std::string collapse_whitespace(const std::string &input)
Collapse consecutive whitespace into a single space.
Definition text_utils.cpp:256
const std::string right_brace
Definition text_utils.hpp:43
const std::string right_bracket
Definition text_utils.hpp:41
const std::string dollar
Definition text_utils.hpp:30
const std::string dash
Definition text_utils.hpp:22
std::string trim(const std::string &s)
Trim whitespace from both ends of a string.
Definition text_utils.cpp:125
const std::string colon
Definition text_utils.hpp:20
const std::string less_than
Definition text_utils.hpp:44
const std::string backslash
Definition text_utils.hpp:25
const std::string asterisk
Definition text_utils.hpp:33
Definition text_utils.hpp:335
std::string value
Definition text_utils.hpp:337
bool is_block
Definition text_utils.hpp:339
std::string key
Definition text_utils.hpp:336
std::vector< Node > children
Definition text_utils.hpp:338