Open Lighting Architecture: include/ola/StringUtils.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
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
25
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
39namespace ola {
40
50void 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
75void StringTrim(std::string *input);
76
81void ShortenString(std::string *input);
82
89bool StringBeginsWith(const std::string &s, const std::string &prefix);
90
97bool StringEndsWith(const std::string &s, const std::string &suffix);
98
105bool StripPrefix(std::string *s, const std::string &prefix);
106
113bool 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
145std::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
201void Escape(std::string *original);
202
209std::string EscapeString(const std::string &original);
210
217void ReplaceAll(std::string *original,
218 const std::string &find,
219 const std::string &replace);
220
230std::string EncodeString(const std::string &original);
231
243bool StringToBool(const std::string &value, bool *output);
244
258bool StringToBoolTolerant(const std::string &value, bool *output);
259
268bool StringToInt(const std::string &value,
269 unsigned int *output,
270 bool strict = false);
271
281bool StringToInt(const std::string &value,
282 uint16_t *output,
283 bool strict = false);
284
294bool StringToInt(const std::string &value,
295 uint8_t *output,
296 bool strict = false);
297
307bool StringToInt(const std::string &value, int *output, bool strict = false);
308
318bool StringToInt(const std::string &value,
319 int16_t *output,
320 bool strict = false);
321
331bool StringToInt(const std::string &value, int8_t *output, bool strict = false);
332
342template <typename int_type>
343int_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
359bool HexStringToInt(const std::string &value, uint8_t *output);
360
370bool HexStringToInt(const std::string &value, uint16_t *output);
371
381bool HexStringToInt(const std::string &value, uint32_t *output);
382
392bool HexStringToInt(const std::string &value, int8_t *output);
393
403bool HexStringToInt(const std::string &value, int16_t *output);
404
414bool HexStringToInt(const std::string &value, int32_t *output);
415
420void ToLower(std::string *s);
421
426void ToUpper(std::string *s);
427
436void CapitalizeLabel(std::string *s);
437
455void CustomCapitalizeLabel(std::string *s);
456
461void CapitalizeFirst(std::string *s);
462
476 inline void FormatData(std::ostream *out,
477 const uint8_t *data,
478 unsigned int length,
479 unsigned int indent = 0,
480 unsigned int byte_per_line = 8) {
481 return ola::strings::FormatData(out, data, length, indent, byte_per_line);
482}
483
487template <typename int_type>
488 bool PrefixedHexStringToInt(const std::string &input, int_type *output) {
489 if ((input.find("0x") != 0) && (input.find("0X") != 0))
490 return false;
491 std::string modified_input = input.substr(2);
492 return HexStringToInt(modified_input, output);
493}
494
500template<typename T>
501std::string StringJoin(const std::string &delim, const T &input) {
502 std::ostringstream str;
503 typename T::const_iterator iter = input.begin();
504 while (iter != input.end()) {
505 str << *iter++;
506 if (iter != input.end())
507 str << delim;
508 }
509 return str.str();
510}
511} // namespace ola
512#endif // INCLUDE_OLA_STRINGUTILS_H_
Format.h
Formatting functions for basic types.
ola::strings::ToHex
_ToHex< T > ToHex(T v, bool prefix=true)
Convert a value to a hex string.
Definition Format.h:68
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
ola::CapitalizeLabel
void CapitalizeLabel(string *s)
Transform a string to a pretty-printed form.
Definition StringUtils.cpp:414
ola::StripPrefix
bool StripPrefix(string *s, const string &prefix)
Strips a prefix from a string.
Definition StringUtils.cpp:97
ola::Escape
void Escape(string *original)
Escape a string with \ .
Definition StringUtils.cpp:249
ola::EncodeString
string EncodeString(const string &original)
Encode any unprintable characters in a string as hex, returning a copy.
Definition StringUtils.cpp:302
ola::StringJoin
std::string StringJoin(const std::string &delim, const T &input)
Join a vector of a type.
Definition StringUtils.h:501
ola::FormatData
void FormatData(std::ostream *out, const uint8_t *data, unsigned int length, unsigned int indent=0, unsigned int byte_per_line=8)
Write binary data to an ostream in a human readable form.
Definition StringUtils.h:476
ola::HexStringToInt
bool HexStringToInt(const string &value, uint8_t *output)
Convert a hex string to a uint8_t.
Definition StringUtils.cpp:330
ola::StringTrim
void StringTrim(string *input)
Trim leading and trailing whitespace from a string.
Definition StringUtils.cpp:61
ola::ToUpper
void ToUpper(string *s)
Convert a string to upper case.
Definition StringUtils.cpp:409
ola::StringToBool
bool StringToBool(const string &value, bool *output)
Convert a string to a bool.
Definition StringUtils.cpp:123
ola::StringEndsWith
bool StringEndsWith(const string &s, const string &suffix)
Check if one string is a suffix of another.
Definition StringUtils.cpp:88
ola::StringToBoolTolerant
bool StringToBoolTolerant(const string &value, bool *output)
Convert a string to a bool in a tolerant way.
Definition StringUtils.cpp:136
ola::CapitalizeFirst
void CapitalizeFirst(string *s)
Transform a string by capitalizing the first character.
Definition StringUtils.cpp:481
ola::EscapeString
string EscapeString(const string &original)
Escape a string, returning a copy.
Definition StringUtils.cpp:296
ola::IntToString
std::string IntToString(int i)
Convert an int to a string.
Definition StringUtils.h:121
ola::PrefixedHexStringToInt
bool PrefixedHexStringToInt(const std::string &input, int_type *output)
Convert a hex string, prefixed with 0x or 0X to an int type.
Definition StringUtils.h:488
ola::IntToHexString
string IntToHexString(unsigned int i, unsigned int width)
Definition StringUtils.cpp:115
ola::StringSplit
void StringSplit(const string &input, vector< string > *tokens, const string &delimiters)
Split a string into pieces.
Definition StringUtils.cpp:42
ola::ShortenString
void ShortenString(string *input)
Truncate the string based on the presence of \0 characters.
Definition StringUtils.cpp:73
ola::ToLower
void ToLower(string *s)
Convert a string to lower case.
Definition StringUtils.cpp:404
ola::StringToInt
bool StringToInt(const string &value, unsigned int *output, bool strict)
Convert a string to a unsigned int.
Definition StringUtils.cpp:155
ola::StripSuffix
bool StripSuffix(string *s, const string &suffix)
Strips a suffix from a string.
Definition StringUtils.cpp:106
ola::StringToIntOrDefault
int_type StringToIntOrDefault(const std::string &value, int_type alternative, bool strict=false)
Convert a string to an int type or return a default if it failed.
Definition StringUtils.h:343
ola::ReplaceAll
void ReplaceAll(string *original, const string &find, const string &replace)
Replace all instances of the find string with the replace string.
Definition StringUtils.cpp:317
ola::StringBeginsWith
bool StringBeginsWith(const string &s, const string &prefix)
Check if one string is a prefix of another.
Definition StringUtils.cpp:80
ola::CustomCapitalizeLabel
void CustomCapitalizeLabel(string *s)
Similar to CapitalizeLabel() but this also capitalized known acronyms.
Definition StringUtils.cpp:437