appkit  1.5.1
RoadNarrows Robotics Application Kit
StringTheory.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: StringTheory.h
10 //
11 /*! \file
12  *
13  * \brief Of string spaces and their strangian operators.
14  *
15  * \author Robin Knight (robin.knight@roadnarrows.com)
16  *
17  * \par Copyright
18  * \h_copy 2016-2017. RoadNarrows LLC.\n
19  * http://www.roadnarrows.com\n
20  * All Rights Reserved
21  *
22  * \par License:
23  * MIT
24  */
25 /*
26  * @EulaBegin@
27  *
28  * Permission is hereby granted, without written agreement and without
29  * license or royalty fees, to use, copy, modify, and distribute this
30  * software and its documentation for any purpose, provided that
31  * (1) The above copyright notice and the following two paragraphs
32  * appear in all copies of the source code and (2) redistributions
33  * including binaries reproduces these notices in the supporting
34  * documentation. Substantial modifications to this software may be
35  * copyrighted by their authors and need not follow the licensing terms
36  * described here, provided that the new terms are clearly indicated in
37  * all files where they apply.
38  *
39  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
40  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
41  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
42  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
43  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
44  * THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
47  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
48  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
49  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
50  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
51  *
52  * @EulaEnd@
53  */
54 ////////////////////////////////////////////////////////////////////////////////
55 
56 #ifndef _RNR_STRING_THEORY_H
57 #define _RNR_STRING_THEORY_H
58 
59 #include <stdio.h>
60 #include <unistd.h>
61 #include <stdlib.h>
62 #include <ctype.h>
63 
64 #include <sstream>
65 #include <string>
66 #include <vector>
67 #include <algorithm>
68 #include <functional>
69 
70 /*!
71  * \brief RoadNarrows Robotics
72  */
73 namespace rnr
74 {
75  /*!
76  * \brief String
77  */
78  namespace str
79  {
80  /*!
81  * \brief Useful types.
82  */
83  typedef std::vector<std::string> StringVec; ///< vector of strings type
84 
85  /*!
86  * \brief Falsehood and truthhood strings. Each list termintate with a NULL.
87  */
88  extern const char *FalseHood[]; ///< strings that equate to false
89  extern const char *TruthHood[]; ///< strings that equate to true
90 
91  /*!
92  * \brief Convert boolean to "ok" or "not-ok".
93  *
94  * \param b Boolean value.
95  *
96  * \return String.
97  */
98  inline std::string okstr(bool b)
99  {
100  return b? "ok": "not-ok";
101  }
102 
103  /*!
104  * \brief Create space string.
105  *
106  * \param n Number of spaces.
107  *
108  * \return String.
109  */
110  inline std::string space(unsigned n)
111  {
112  return std::string(n, ' ');
113  }
114 
115  /*!
116  * \brief Split string.
117  *
118  * \param [in] str String to split.
119  * \param [in] delim Character delimiter.
120  * \param [in,out] elems Vector of split strings.
121  *
122  * \return Returns number of element strings appended to elems.
123  */
124  size_t split(const std::string &str, const char delim, StringVec &elems);
125 
126  /*!
127  * \brief Split string.
128  *
129  * \param [in] str String to split.
130  * \param [in] delim Character delimiter.
131  *
132  * \return Vector of split strings.
133  */
134  StringVec split(const std::string &str, const char delim);
135 
136  /*!
137  * \brief Trim string in-place of leading whitespace.
138  *
139  * \param [in,out] str String to trim.
140  *
141  * \return Left trimmed string.
142  */
143  inline std::string &ltrim(std::string &str)
144  {
145  str.erase( str.begin(), std::find_if(str.begin(), str.end(),
146  std::not1(std::ptr_fun<int, int>(std::isspace))) );
147  return str;
148  }
149 
150  /*!
151  * \brief Trim copy of string of leading whitespace.
152  *
153  * \param [in] s Null-terminated character string. Cannot be NULL.
154  *
155  * \return Left trimmed string.
156  */
157  inline std::string ltrim(const char *s)
158  {
159  std::string str(s);
160  return ltrim(str);
161  }
162 
163  /*!
164  * \brief Trim string in-place of trailing whitespace.
165  *
166  * \param [in,out] str String to trim.
167  *
168  * \return Right trimmed string.
169  */
170  inline std::string &rtrim(std::string &str)
171  {
172  str.erase( std::find_if(str.rbegin(), str.rend(),
173  std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end() );
174  return str;
175  }
176 
177  /*!
178  * \brief Trim copy of string of trailing whitespace.
179  *
180  * \param [in] s Null-terminated character string. Cannot be NULL.
181  *
182  * \return Right trimmed string.
183  */
184  inline std::string rtrim(const char *s)
185  {
186  std::string str(s);
187  return rtrim(str);
188  }
189 
190  /*!
191  * \brief Trim string in-place of leading and trailing whitespace.
192  *
193  * \param [in,out] str String to trim.
194  *
195  * \return Trimmed string.
196  */
197  inline std::string &trim(std::string &str)
198  {
199  return ltrim(rtrim(str));
200  }
201 
202  /*!
203  * \brief Trim copy of string of leading and trailing whitespace.
204  *
205  * \param [in] s Null-terminated character string. Cannot be NULL.
206  *
207  * \return Trimmed string.
208  */
209  inline std::string trim(const char *s)
210  {
211  std::string str(s);
212  return trim(str);
213  }
214 
215  /*!
216  * \brief In-place replace all whats in string with with.
217  *
218  * \todo Investigate using c++ function approach.
219  *
220  * \param what What substrings to replace.
221  * \param with With this substring.
222  * \param [in,out] str String before and after.
223  *
224  * \return String with replacements.
225  */
226  extern std::string &replace(const std::string &what,
227  const std::string &with,
228  std::string &str);
229 
230  /*!
231  * \brief Copy replace all whats in string with with.
232  *
233  * \todo Investigate using c++ function approach.
234  *
235  * \param what What substrings to replace.
236  * \param with With this substring.
237  * \param [in] s Null-terminated character string. Cannot be NULL.
238  *
239  * \return String with replacements.
240  */
241  inline std::string replace(const std::string &what,
242  const std::string &with,
243  const char *s)
244  {
245  std::string str(s);
246  return replace(what, with, str);
247  }
248 
249  /*!
250  * \brief Convert in-place string to lower case.
251  *
252  * \param [in,out] str String to convert.
253  *
254  * \return Lower case string.
255  */
256  std::string lowercase(const std::string &str);
257 
258  /*!
259  * \brief Convert copy of string to lower case.
260  *
261  * \param [in] s Null-terminated character string. Cannot be NULL.
262  *
263  * \return Lower case string.
264  */
265  inline std::string lowercase(const char *s)
266  {
267  std::string str(s);
268  return lowercase(str);
269  }
270 
271  /*!
272  * \brief Convert string to upper case.
273  *
274  * \param [in,out] str String to convert.
275  *
276  * \return Upper case string.
277  */
278  std::string uppercase(const std::string &str);
279 
280  /*!
281  * \brief Convert copy of string to upper case.
282  *
283  * \param [in] s Null-terminated character string. Cannot be NULL.
284  *
285  * \return Lower case string.
286  */
287  inline std::string uppercase(const char *s)
288  {
289  std::string str(s);
290  return uppercase(str);
291  }
292 
293  /*!
294  * \brief Find the length of the Greatest Common SubString.
295  *
296  * Examples:
297  * string 1 | string 2 | length
298  * :-------- | :-------- | ------:
299  * "abcd" | "x" | 0
300  * "abcd" | "bc" | 2
301  * "abcd" | "bcx" | 2
302  * "abcd" | "bcdef" | 3
303  *
304  * \param str1 String 1.
305  * \param str2 String 2.
306  * \param pos Starting position in str1.
307  *
308  * \return Returns length of common substring.
309  */
310  size_t gcss(const std::string &str1,
311  const std::string &str2,
312  const size_t pos = 0);
313 
314  /*!
315  * \brief Convert string to boolean.
316  *
317  * false: "0" "false" "f" "off" "low" "disable" "open"
318  * true: "1" "true" "t" "on" "high" "enable" "close"
319  *
320  * \param [in] str String in hex, decimal, or octal format.
321  * \param [out] val Converted boolean value.
322  *
323  * \copydoc doc_return_std
324  */
325  int tobool(const std::string &str, bool &val);
326 
327  /*!
328  * \brief Convert string to a long integer.
329  *
330  * \param [in] str String in hex, decimal, or octal format.
331  * \param [out] val Converted long value.
332  *
333  * \copydoc doc_return_std
334  */
335  int tolong(const std::string &str, long &val);
336 
337  /*!
338  * \brief Convert string to a double-precision floating-point number.
339  *
340  * \param [in] str String in hex, decimal, or octal format.
341  * \param [out] val Converted double value.
342  *
343  * \copydoc doc_return_std
344  */
345  int todouble(const std::string &str, double &val);
346 
347  /*!
348  * \brief Prettify string.
349  *
350  * Binary characters are converted to ascii escape hex sequences. Control
351  * characters are converted to standard ascii escape sequences. If required,
352  * the string is double quoted ('"') to conform to sh(1) strings.
353  *
354  * \param str String to make pretty.
355  *
356  * \return Prettified version of string.
357  */
358  extern std::string prettify(const std::string &str);
359 
360  /*!
361  * \brief Simple canonicalization of a string.
362  *
363  * The string is canonical when:
364  * - leading and trailing whitespace is stripped
365  * - only single space between words
366  *
367  * \note c14n is an cute abbreviation where 14 represents the number of
368  * letters between the 'c' and 'n' in the word "canonicalization".
369  *
370  * \param str String to canonicalize.
371  *
372  * \return Return copy of string holding canonical form.
373  */
374  extern std::string c14n(const std::string &str);
375 
376  /*!
377  * \brief Canonicalization of a list of tokens into a string.
378  *
379  * \note The name c14n is an cute abbreviation where 14 represents the
380  * number of letters between the 'c' and 'n' in the word
381  * "canonicalization".
382  *
383  * \param tokens String tokens to canonicalize.
384  *
385  * \return Return string holding canonical form.
386  */
387  extern std::string c14n(const str::StringVec &tokens);
388 
389  } // namespace str
390 } // namespace rnr
391 
392 #endif // _RNR_STRING_THEORY_H
std::string & ltrim(std::string &str)
Trim string in-place of leading whitespace.
Definition: StringTheory.h:143
int tolong(const std::string &str, long &val)
Convert string to a long integer.
std::vector< std::string > StringVec
Useful types.
Definition: StringTheory.h:83
int tobool(const std::string &str, bool &val)
Convert string to boolean.
std::string lowercase(const std::string &str)
Convert in-place string to lower case.
std::string & rtrim(std::string &str)
Trim string in-place of trailing whitespace.
Definition: StringTheory.h:170
const char * TruthHood[]
strings that equate to true
size_t split(const std::string &str, const char delim, StringVec &elems)
Split string.
int todouble(const std::string &str, double &val)
Convert string to a double-precision floating-point number.
std::string & trim(std::string &str)
Trim string in-place of leading and trailing whitespace.
Definition: StringTheory.h:197
std::string prettify(const std::string &str)
Prettify string.
std::string uppercase(const std::string &str)
Convert string to upper case.
std::string okstr(bool b)
Convert boolean to "ok" or "not-ok".
Definition: StringTheory.h:98
std::string space(unsigned n)
Create space string.
Definition: StringTheory.h:110
std::string & replace(const std::string &what, const std::string &with, std::string &str)
In-place replace all whats in string with with.
std::string c14n(const std::string &str)
Simple canonicalization of a string.
RoadNarrows Robotics.
Definition: Camera.h:74
size_t gcss(const std::string &str1, const std::string &str2, const size_t pos=0)
Find the length of the Greatest Common SubString.
const char * FalseHood[]
Falsehood and truthhood strings. Each list termintate with a NULL.