Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringUtils.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * StringUtils.h
17  * Random String functions.
18  * Copyright (C) 2005 Simon Newton
19  */
20 
26 #ifndef INCLUDE_OLA_STRINGUTILS_H_
27 #define INCLUDE_OLA_STRINGUTILS_H_
28 
29 #include <ola/strings/Format.h>
30 #include <stdint.h>
31 #include <iomanip>
32 #include <iostream>
33 #include <limits>
34 #include <ostream>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 
39 namespace ola {
40 
50 void StringSplit(const std::string &input,
51  std::vector<std::string> *tokens,
52  const std::string &delimiters = " ");
53 
64 inline void StringSplit(
65  const std::string &input,
66  std::vector<std::string> &tokens, // NOLINT(runtime/references)
67  const std::string &delimiters = " ") {
68  StringSplit(input, &tokens, delimiters);
69 }
70 
75 void StringTrim(std::string *input);
76 
81 void ShortenString(std::string *input);
82 
89 bool StringBeginsWith(const std::string &s, const std::string &prefix);
90 
97 bool StringEndsWith(const std::string &s, const std::string &suffix);
98 
105 bool StripPrefix(std::string *s, const std::string &prefix);
106 
113 bool StripSuffix(std::string *s, const std::string &suffix);
114 
121 inline std::string IntToString(int i) {
122  return ola::strings::IntToString(i);
123 }
124 
131 inline std::string IntToString(unsigned int i) {
132  return ola::strings::IntToString(i);
133 }
134 
145 std::string IntToHexString(unsigned int i, unsigned int width);
146 
154 inline std::string IntToHexString(uint8_t i) {
155  std::ostringstream str;
156  str << ola::strings::ToHex(i);
157  return str.str();
158 }
159 
167 inline std::string IntToHexString(uint16_t i) {
168  std::ostringstream str;
169  str << ola::strings::ToHex(i);
170  return str.str();
171 }
172 
180 inline std::string IntToHexString(uint32_t i) {
181  std::ostringstream str;
182  str << ola::strings::ToHex(i);
183  return str.str();
184 }
185 
201 void Escape(std::string *original);
202 
209 std::string EscapeString(const std::string &original);
210 
217 void ReplaceAll(std::string *original,
218  const std::string &find,
219  const std::string &replace);
220 
230 std::string EncodeString(const std::string &original);
231 
243 bool StringToBool(const std::string &value, bool *output);
244 
258 bool StringToBoolTolerant(const std::string &value, bool *output);
259 
268 bool StringToInt(const std::string &value,
269  unsigned int *output,
270  bool strict = false);
271 
281 bool StringToInt(const std::string &value,
282  uint16_t *output,
283  bool strict = false);
284 
294 bool StringToInt(const std::string &value,
295  uint8_t *output,
296  bool strict = false);
297 
307 bool StringToInt(const std::string &value, int *output, bool strict = false);
308 
318 bool StringToInt(const std::string &value,
319  int16_t *output,
320  bool strict = false);
321 
331 bool StringToInt(const std::string &value, int8_t *output, bool strict = false);
332 
342 template <typename int_type>
343 int_type StringToIntOrDefault(const std::string &value,
344  int_type alternative,
345  bool strict = false) {
346  int_type output;
347  return (StringToInt(value, &output, strict)) ? output : alternative;
348 }
349 
359 bool HexStringToInt(const std::string &value, uint8_t *output);
360 
370 bool HexStringToInt(const std::string &value, uint16_t *output);
371 
381 bool HexStringToInt(const std::string &value, uint32_t *output);
382 
392 bool HexStringToInt(const std::string &value, int8_t *output);
393 
403 bool HexStringToInt(const std::string &value, int16_t *output);
404 
414 bool HexStringToInt(const std::string &value, int32_t *output);
415 
420 void ToLower(std::string *s);
421 
426 void ToUpper(std::string *s);
427 
436 void CapitalizeLabel(std::string *s);
437 
447 void CustomCapitalizeLabel(std::string *s);
448 
462 inline void FormatData(std::ostream *out,
463  const uint8_t *data,
464  unsigned int length,
465  unsigned int indent = 0,
466  unsigned int byte_per_line = 8) {
467  return ola::strings::FormatData(out, data, length, indent, byte_per_line);
468 }
469 
473 template <typename int_type>
474 bool PrefixedHexStringToInt(const std::string &input, int_type *output) {
475  if ((input.find("0x") != 0) && (input.find("0X") != 0))
476  return false;
477  std::string modified_input = input.substr(2);
478  return HexStringToInt(modified_input, output);
479 }
480 
486 template<typename T>
487 std::string StringJoin(const std::string &delim, const T &input) {
488  std::ostringstream str;
489  typename T::const_iterator iter = input.begin();
490  while (iter != input.end()) {
491  str << *iter++;
492  if (iter != input.end())
493  str << delim;
494  }
495  return str.str();
496 }
497 } // namespace ola
498 #endif // INCLUDE_OLA_STRINGUTILS_H_