Euphoria
stringutils.h
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include <map>
8 
9 
10 namespace eu
11 {
17  bool
18  is_number(char b);
19 
20 
24  constexpr std::string_view space_characters = " \n\r\t";
25 
26 
28  std::string
29  strip_last_string(const std::string& str, char sep);
30 
31 
33  std::pair<std::string, std::string>
34  get_last_string(const std::string& str, char sep);
35 
36 
37  std::string
38  get_first_chars(const std::string& str, std::size_t count);
39 
40  std::string
41  get_first_chars_with_ellipsis(const std::string& str, unsigned int count = 10);
42 
43 
49  std::string
50  trim_right(const std::string& string_to_trim, std::string_view trim_characters = space_characters);
51 
52 
58  std::string
59  trim_left(const std::string& string_to_trim, std::string_view trim_characters = space_characters);
60 
61 
67  std::string
68  trim(const std::string& string_to_trim, std::string_view trim_characters = space_characters);
69 
70 
76  bool
77  begins_with(const std::string& string_to_test, const std::string& start);
78 
79 
85  bool
86  ends_with(const std::string& string_to_test, const std::string& end);
87 
88 
89  char
90  to_lower_char(char b);
91 
92  char
93  to_upper_char(char b);
94 
95 
100  std::string
101  to_lower(const std::string& str);
102 
103 
104  std::vector<std::string>
105  to_lower(const std::vector<std::string>& str);
106 
107 
108  std::string
109  to_upper(const std::string& str);
110 
111 
112  enum class CharToStringStyle
113  {
115  };
116 
117 
118  std::string
120 
121 
122  std::string::size_type
123  find_first_index_of_mismatch(const std::string& lhs, const std::string& rhs);
124 
125 
131  void
132  replace_all(std::string* string, const std::string& to_find, const std::string& to_replace);
133 
134 
141  std::string
142  replace_all(const std::string& string, const std::string& to_find, const std::string& to_replace);
143 
144 
145  std::string
146  replace_with_character(const std::string& string, const std::string& to_find, char to_replace);
147 
148 
155  void
156  copy(char* dst, const std::string& src, const std::string::size_type& count);
157 
158  std::string
159  remove_from_end(const std::string& str, const std::string& end);
160 
161 
162  template <typename TKey, typename TValue>
163  std::vector<std::string>
164  to_string_vector(const std::map<TKey, TValue>& map)
165  {
166  std::vector<std::string> ret;
167  for(const auto& m: map)
168  {
169  const std::string str = fmt::format("({}:{})", m.first, m.second);
170  ret.emplace_back(str);
171  }
172  return ret;
173  }
174 
175 
176  template <typename TKey, typename TValue>
177  std::vector<std::string>
178  from_key_to_string_vector(const std::map<TKey, TValue>& map)
179  {
180  std::vector<std::string> ret;
181  for(const auto& m: map)
182  {
183  const std::string str = fmt::format("{}", m.first);
184  ret.emplace_back(str);
185  }
186  return ret;
187  }
188 
189 
190  template <typename T>
191  std::vector<std::string>
192  to_string_vector(const std::vector<T>& data)
193  {
194  std::vector<std::string> ret;
195  ret.reserve(data.size());
196  for(const auto& d: data)
197  {
198  const std::string str = fmt::format("{}", d);
199  ret.emplace_back(str);
200  }
201  return ret;
202  }
203 
204 
205  template <typename T, typename C>
206  std::vector<std::string>
207  to_string_vector(const std::vector<T>& data, C callback)
208  {
209  std::vector<std::string> ret;
210  ret.reserve(data.size());
211  for(const auto& d: data)
212  {
213  ret.emplace_back(callback(d));
214  }
215  return ret;
216  }
217 
218 
219  // remove all characters in ch from str
220  std::string
221  strip(const std::string& str, const std::string& ch);
222 
223 
224  // remove all characters in ch except the first one in a chain from str
225  std::string
226  remove_consecutive(const std::string& str, const std::string& ch);
227 
228 
229  std::vector<std::string>
230  split(const std::string& string, char delim);
231 
232 
233  std::vector<std::string>
234  split_on_spaces(const std::string& string);
235 
236 
237  std::string
238  get_string_or_empty(bool b, const std::string& str);
239 
240 
241  // http://stereopsis.com/strcmp4humans.html
242  int
243  compare_string(const std::string& lhs, const std::string& rhs);
246 }
247 
std::vector< std::string > from_key_to_string_vector(const std::map< TKey, TValue > &map)
Definition: stringutils.h:178
CharToStringStyle
Definition: stringutils.h:113
std::string get_first_chars(const std::string &str, std::size_t count)
Definition: stringutils.cc:32
std::string remove_from_end(const std::string &str, const std::string &end)
Definition: stringutils.cc:313
std::string trim_left(const std::string &string_to_trim, std::string_view trim_characters)
Remove characters from the left, stops at invalid character.
Definition: stringutils.cc:72
std::string remove_consecutive(const std::string &str, const std::string &ch)
Definition: stringutils.cc:347
std::string to_lower(const std::string &str)
Generate a string containing only lower characters.
Definition: stringutils.cc:143
void copy(char *dst, const std::string &src, const std::string::size_type &count)
Copy a string to a character buffer, adding null terminator at the end.
Definition: stringutils.cc:293
std::vector< std::string > split_on_spaces(const std::string &string)
Definition: stringutils.cc:444
std::string get_first_chars_with_ellipsis(const std::string &str, unsigned int count)
Definition: stringutils.cc:40
constexpr std::string_view space_characters
Space characters.
Definition: stringutils.h:24
std::string from_char_to_string(char c, CharToStringStyle style)
Definition: stringutils.cc:168
std::vector< std::string > to_string_vector(const std::map< TKey, TValue > &map)
Definition: stringutils.h:164
std::vector< std::string > split(const std::string &s, char delim)
Definition: stringutils.cc:431
std::string replace_with_character(const std::string &string, const std::string &to_find, char to_replace)
Definition: stringutils.cc:301
void replace_all(std::string *string, const std::string &to_find, const std::string &to_replace)
Replace all occurrences in a string.
Definition: stringutils.cc:269
char to_upper_char(char b)
Definition: stringutils.cc:129
std::string trim_right(const std::string &string_to_trim, std::string_view trim_characters)
Remove characters from the right, stops at a invalid character.
Definition: stringutils.cc:65
int compare_string(const std::string &lhs, const std::string &rhs)
Definition: stringutils.cc:498
std::string to_upper(const std::string &str)
Definition: stringutils.cc:159
bool is_number(char b)
Definition: stringutils.cc:471
std::string get_string_or_empty(bool b, const std::string &str)
Definition: stringutils.cc:457
std::string::size_type find_first_index_of_mismatch(const std::string &lhs, const std::string &rhs)
Definition: stringutils.cc:244
std::pair< std::string, std::string > get_last_string(const std::string &str, char sep)
for hello.dog gets .dog
Definition: stringutils.cc:17
std::string strip(const std::string &str, const std::string &ch)
Definition: stringutils.cc:331
std::string trim(const std::string &string_to_trim, std::string_view trim_characters)
Remove characters from both the start and the end.
Definition: stringutils.cc:79
char to_lower_char(char b)
Definition: stringutils.cc:116
std::string strip_last_string(const std::string &str, char sep)
for hello.dog and . gets hello
Definition: stringutils.cc:52
bool begins_with(const std::string &string_to_test, const std::string &start)
Tests if a string starts with another string.
Definition: stringutils.cc:87
Definition: assert.h:90
bool ends_with(const std::string &str, char c)
Definition: os.cc:139
std::vector< T > map(const std::vector< F > &fs, C convert)
Definition: functional.h:56
int ch
Definition: nlp_sentence.cc:92