6#include <unordered_map>
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 =
":";
22inline const std::string
dash =
"-";
24inline const std::string
slash =
"/";
26inline const std::string
pipe =
"|";
29inline const std::string
hash =
"#";
30inline const std::string
dollar =
"$";
32inline const std::string
caret =
"^";
34inline const std::string
plus =
"+";
35inline const std::string
equals =
"=";
62 template <
typename... Args>
void add(Args &&...args) {
63 std::ostringstream oss;
72 std::string
str()
const {
return data_; }
75 size_t size()
const {
return data_.size(); }
90 if (indent_level_ > 0) {
100 template <
typename... Args>
void add(Args &&...args) {
101 std::ostringstream oss;
102 oss << std::string(indent_level_ * indent_size_,
' ');
103 (oss << ... << args);
104 lines_.emplace_back(oss.str());
112 std::istringstream iss(multiline_str);
114 while (std::getline(iss, line)) {
115 lines_.emplace_back(std::string(indent_level_ * indent_size_,
' ') + line);
126 if (index > lines_.size()) {
127 throw std::out_of_range(
"insert_line: index out of range");
129 lines_.insert(lines_.begin() + index, std::string(indent_level_ * indent_size_,
' ') + line);
139 if (index > lines_.size()) {
140 throw std::out_of_range(
"insert_lines: index out of range");
142 lines_.insert(lines_.begin() + index, other.lines_.begin(), other.lines_.end());
152 if (index > lines_.size()) {
153 throw std::out_of_range(
"insert_multiline: index out of range");
156 std::istringstream iss(multiline_str);
158 std::vector<std::string> new_lines;
160 while (std::getline(iss, line)) {
161 new_lines.emplace_back(std::string(indent_level_ * indent_size_,
' ') + line);
164 lines_.insert(lines_.begin() + index, new_lines.begin(), new_lines.end());
173 if (index >= lines_.size()) {
174 throw std::out_of_range(
"remove_line: index out of range");
176 lines_.erase(lines_.begin() + index);
181 std::ostringstream oss;
182 for (
size_t i = 0;
i < lines_.size(); ++
i) {
184 if (
i + 1 < lines_.size()) {
198 std::vector<std::string> lines_;
199 size_t indent_level_;
240std::vector<std::string>
split(
const std::string &str,
const std::string &delimiter);
256std::string
join(
const std::vector<std::string> &elements,
const std::string &separator);
259std::string
trim(
const std::string &
s);
268std::string
surround(
const std::string &str,
const std::string &left,
const std::string &right =
"");
290std::string
join_multiline(
const std::string &input,
bool replace_newlines_with_space =
false);
293std::string
replace_char(
const std::string &input,
char from_char,
char to_char);
296std::string
replace_chars(
const std::string &input,
const std::unordered_map<char, char> &mapping);
299std::string
replace_substring(
const std::string &input,
const std::string &from_substr,
const std::string &to_substr);
302bool starts_with(
const std::string &str,
const std::string &prefix);
305bool contains(
const std::string &str,
const std::string &substr);
308std::string
get_substring(
const std::string &input,
size_t start,
size_t end);
326std::string
indent(
const std::string &text,
int indent_level,
int spaces_per_indent = 4);
343std::string
parse_token(
const std::string &
s,
size_t &pos);
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
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