• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenLightingProject / ola / 20179851591

12 Dec 2025 09:05PM UTC coverage: 45.048% (-0.7%) from 45.72%
20179851591

Pull #2027

github

web-flow
Bump actions/upload-artifact from 4 to 6

Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 4 to 6.
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](https://github.com/actions/upload-artifact/compare/v4...v6)

---
updated-dependencies:
- dependency-name: actions/upload-artifact
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #2027: Bump actions/upload-artifact from 4 to 6

8554 of 19812 branches covered (43.18%)

22094 of 49046 relevant lines covered (45.05%)

50.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.0
/include/ola/StringUtils.h
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

21
/**
22
 * @file StringUtils.h
23
 * @brief Various string utility functions.
24
 */
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

39
namespace ola {
40

41
/**
42
 * @brief Split a string into pieces.
43
 *
44
 * If two delimiters appear next to each other an empty string is added to the
45
 *   output vector.
46
 * @param[in] input the string to split
47
 * @param[out] tokens pointer to a vector with the parts of the string
48
 * @param delimiters the delimiter to use for splitting. Defaults to ' '
49
 */
50
void StringSplit(const std::string &input,
51
                 std::vector<std::string> *tokens,
52
                 const std::string &delimiters = " ");
53

54
/**
55
 * @brief Split a string into pieces.
56
 *
57
 * If two delimiters appear next to each other an empty string is added to the
58
 *   output vector.
59
 * @param[in] input the string to split
60
 * @param[out] tokens the parts of the string
61
 * @param delimiters the delimiter to use for splitting. Defaults to ' '
62
 * @deprecated Use the version with a vector pointer instead (3 Jan 2015).
63
 */
64
inline void StringSplit(
1✔
65
    const std::string &input,
66
    std::vector<std::string> &tokens,  // NOLINT(runtime/references)
67
    const std::string &delimiters = " ") {
68
  StringSplit(input, &tokens, delimiters);
1✔
69
}
1✔
70

71
/**
72
 * @brief Trim leading and trailing whitespace from a string
73
 * @param input the string to trim.
74
 */
75
void StringTrim(std::string *input);
76

77
/**
78
 * @brief Truncate the string based on the presence of \0 characters.
79
 * @param input the string to shorten
80
 */
81
void ShortenString(std::string *input);
82

83
/**
84
 * @brief Check if one string is a prefix of another.
85
 * @param s the string to check
86
 * @param prefix the prefix to check for
87
 * @returns true if s begins with prefix, false otherwise.
88
 */
89
bool StringBeginsWith(const std::string &s, const std::string &prefix);
90

91
/**
92
 * @brief Check if one string is a suffix of another.
93
 * @param s the string to check
94
 * @param suffix the suffix to check for
95
 * @returns true if s ends with suffix, false otherwise.
96
 */
97
bool StringEndsWith(const std::string &s, const std::string &suffix);
98

99
/**
100
 * @brief Strips a prefix from a string.
101
 * @param s the string to strip
102
 * @param prefix the prefix to remove
103
 * @returns true if we stripped prefix from s, false otherwise.
104
 */
105
bool StripPrefix(std::string *s, const std::string &prefix);
106

107
/**
108
 * @brief Strips a suffix from a string.
109
 * @param s the string to strip
110
 * @param suffix the suffix to remove
111
 * @returns true if we stripped suffix from s, false otherwise.
112
 */
113
bool StripSuffix(std::string *s, const std::string &suffix);
114

115
/**
116
 * @brief Convert an int to a string.
117
 * @param i the int to convert
118
 * @return the string representation of the int
119
 * @deprecated Use ola::strings::IntToString instead (30 Dec 2014).
120
 */
121
inline std::string IntToString(int i) {
101✔
122
  return ola::strings::IntToString(i);
101✔
123
}
124

125
/**
126
 * Convert an unsigned int to a string.
127
 * @param i the unsigned int to convert
128
 * @return The string representation of the unsigned int
129
 * @deprecated Use ola::strings::IntToString instead (30 Dec 2014).
130
 */
131
inline std::string IntToString(unsigned int i) {
59✔
132
  return ola::strings::IntToString(i);
59✔
133
}
134

135
/**
136
 * Convert an unsigned int to a hex string.
137
 * @param i the unsigned int to convert
138
 * @param width the width to zero pad to
139
 * @return The hex string representation of the unsigned int
140
 * @note We don't currently support signed ints due to a lack of requirement
141
 * for it and issues with negative handling and hex in C++
142
 * @deprecated ola::ToHex() instead, unless you really want a string rather
143
 *   than use in an ostream (30 Dec 2014)
144
 */
145
std::string IntToHexString(unsigned int i, unsigned int width);
146

147
/**
148
 * Convert a uint8_t to a hex string.
149
 * @param i the number to convert
150
 * @return The string representation of the number
151
 * @deprecated ola::ToHex() instead, unless you really want a string rather
152
 *   than use in an ostream (30 Dec 2014)
153
 */
154
inline std::string IntToHexString(uint8_t i) {
3✔
155
  std::ostringstream str;
3✔
156
  str << ola::strings::ToHex(i);
3✔
157
  return str.str();
6✔
158
}
3✔
159

160
/**
161
 * Convert a uint16_t to a hex string.
162
 * @param i the number to convert
163
 * @return The string representation of the number
164
 * @deprecated ola::ToHex() instead, unless you really want a string rather
165
 *   than use in an ostream (30 Dec 2014)
166
 */
167
inline std::string IntToHexString(uint16_t i) {
2✔
168
  std::ostringstream str;
2✔
169
  str << ola::strings::ToHex(i);
2✔
170
  return str.str();
4✔
171
}
2✔
172

173
/**
174
 * Convert a uint32_t to a hex string.
175
 * @param i the number to convert
176
 * @return The string representation of the number
177
 * @deprecated ola::ToHex() instead, unless you really want a string rather
178
 *   than use in an ostream (30 Dec 2014)
179
 */
180
inline std::string IntToHexString(uint32_t i) {
2✔
181
  std::ostringstream str;
2✔
182
  str << ola::strings::ToHex(i);
2✔
183
  return str.str();
4✔
184
}
2✔
185

186
// Deliberately no IntToHexString(uint64_t) as its deprecated
187

188
/**
189
 * @brief Escape a string with \\ .
190
 *
191
 * The string is modified in place according to the grammar from json.org
192
 * The following characters are escaped:
193
 *  - \\
194
 *  - "
195
 *  - /
196
 *  - \\b
197
 *  - \\f
198
 *  - \\n
199
 *  - \\r
200
 *  - \\t
201
 * @param original the string to escape
202
 */
203
void Escape(std::string *original);
204

205
/**
206
 * @brief Escape a string, returning a copy.
207
 * @param original the string to escape.
208
 * @returns The escaped string.
209
 * @sa Escape()
210
 */
211
std::string EscapeString(const std::string &original);
212

213
/**
214
 * @brief Replace all instances of the find string with the replace string.
215
 * @param original the string to operate on.
216
 * @param find the string to find
217
 * @param replace what to replace it with
218
 */
219
void ReplaceAll(std::string *original,
220
                const std::string &find,
221
                const std::string &replace);
222

223
/**
224
 * @brief Encode any unprintable characters in a string as hex, returning a
225
 * copy.
226
 *
227
 * "foo\\nbar" becomes "foo\\\\x0abar"
228
 * "foo\\x01test" becomes "foo\\\\x01test"
229
 * @param original the string to encode.
230
 * @returns The encoded string.
231
 */
232
std::string EncodeString(const std::string &original);
233

234
/**
235
 * @brief Convert a string to a bool.
236
 *
237
 * The string can be 'true' or 'false', 't' or 'f', '1' or '0' or case
238
 * insensitive variations of any of the above.
239
 *
240
 * @param[in] value the string to convert
241
 * @param[out] output a pointer where the value will be stored.
242
 * @returns true if the value was converted, false if the string was not a
243
 * bool.
244
 */
245
bool StringToBool(const std::string &value, bool *output);
246

247
/**
248
 * @brief Convert a string to a bool in a tolerant way.
249
 *
250
 * The string can be 'true' or 'false', 't' or 'f', '1' or '0', 'on' or 'off',
251
 * 'enable' or 'disable', 'enabled' or 'disabled' or case insensitive
252
 * variations of any of the above.
253
 *
254
 * @param[in] value the string to convert
255
 * @param[out] output a pointer where the value will be stored.
256
 * @returns true if the value was converted, false if the string was not a
257
 * bool.
258
 * @sa StringToBool
259
 */
260
bool StringToBoolTolerant(const std::string &value, bool *output);
261

262
/**
263
 * @brief Convert a string to a uint64_t.
264
 * @param[in] value the string to convert
265
 * @param[out] output a pointer where the value will be stored.
266
 * @param[in] strict this controls if trailing characters produce an error.
267
 * @returns true if the value was converted, false if the string was not an int
268
 * or the value was too large / small for the type.
269
 */
270
bool StringToInt(const std::string &value,
271
                 uint64_t *output,
272
                 bool strict = false);
273

274
/**
275
 * @brief Convert a string to a unsigned int.
276
 * @param[in] value the string to convert
277
 * @param[out] output a pointer where the value will be stored.
278
 * @param[in] strict this controls if trailing characters produce an error.
279
 * @returns true if the value was converted, false if the string was not an int
280
 * or the value was too large / small for the type.
281
 */
282
bool StringToInt(const std::string &value,
283
                 unsigned int *output,
284
                 bool strict = false);
285

286
/**
287
 * @brief Convert a string to a uint16_t.
288
 * @param[in] value the string to convert
289
 * @param[out] output a pointer where the value will be stored.
290
 * @param[in] strict this controls if trailing characters produce an error.
291
 * @returns true if the value was converted, false if the string was not an int
292
 * or the value was too large / small for the type.
293
 * @sa StringToInt.
294
 */
295
bool StringToInt(const std::string &value,
296
                 uint16_t *output,
297
                 bool strict = false);
298

299
/**
300
 * @brief Convert a string to a uint8_t.
301
 * @param[in] value the string to convert
302
 * @param[out] output a pointer where the value will be stored.
303
 * @param[in] strict this controls if trailing characters produce an error.
304
 * @returns true if the value was converted, false if the string was not an int
305
 * or the value was too large / small for the type.
306
 * @sa StringToInt.
307
 */
308
bool StringToInt(const std::string &value,
309
                 uint8_t *output,
310
                 bool strict = false);
311

312
/**
313
 * @brief Convert a string to a int64_t.
314
 * @param[in] value the string to convert
315
 * @param[out] output a pointer where the value will be stored.
316
 * @param[in] strict this controls if trailing characters produce an error.
317
 * @returns true if the value was converted, false if the string was not an int
318
 * or the value was too large / small for the type.
319
 * @sa StringToInt.
320
 */
321
bool StringToInt(const std::string &value,
322
                 int64_t *output,
323
                 bool strict = false);
324

325
/**
326
 * @brief Convert a string to a int.
327
 * @param[in] value the string to convert
328
 * @param[out] output a pointer where the value will be stored.
329
 * @param[in] strict this controls if trailing characters produce an error.
330
 * @returns true if the value was converted, false if the string was not an int
331
 * or the value was too large / small for the type.
332
 * @sa StringToInt.
333
 */
334
bool StringToInt(const std::string &value, int *output, bool strict = false);
335

336
/**
337
 * @brief Convert a string to a int16_t.
338
 * @param[in] value the string to convert
339
 * @param[out] output a pointer where the value will be stored.
340
 * @param[in] strict this controls if trailing characters produce an error.
341
 * @returns true if the value was converted, false if the string was not an int
342
 * or the value was too large / small for the type.
343
 * @sa StringToInt.
344
 */
345
bool StringToInt(const std::string &value,
346
                 int16_t *output,
347
                 bool strict = false);
348

349
/**
350
 * @brief Convert a string to a int8_t.
351
 * @param[in] value the string to convert
352
 * @param[out] output a pointer where the value will be stored.
353
 * @param[in] strict this controls if trailing characters produce an error.
354
 * @returns true if the value was converted, false if the string was not an int
355
 * or the value was too large / small for the type.
356
 * @sa StringToInt.
357
 */
358
bool StringToInt(const std::string &value, int8_t *output, bool strict = false);
359

360
/**
361
 * @brief Convert a string to an int type or return a default if it failed.
362
 * @tparam int_type the type to convert to
363
 * @param value the string to convert
364
 * @param alternative the default value to return if conversion failed.
365
 * @param[in] strict this controls if trailing characters produce an error.
366
 * @returns the value if it converted successfully or the default if the string
367
 * was not an int or the value was too large / small for the type.
368
 */
369
template <typename int_type>
370
int_type StringToIntOrDefault(const std::string &value,
75✔
371
                              int_type alternative,
372
                              bool strict = false) {
373
  int_type output;
374
  return (StringToInt(value, &output, strict)) ? output : alternative;
75✔
375
}
376

377
/**
378
 * @brief Convert a hex string to a uint8_t.
379
 *
380
 * The string can contain upper or lower case hex characters.
381
 * @param[in] value the string to convert.
382
 * @param[out] output a pointer to the store the converted value in.
383
 * @returns true if the value was converted, false if the string was not an int
384
 * or the value was too large / small for the type.
385
 */
386
bool HexStringToInt(const std::string &value, uint8_t *output);
387

388
/**
389
 * @brief Convert a hex string to a uint16_t.
390
 *
391
 * The string can contain upper or lower case hex characters.
392
 * @param[in] value the string to convert.
393
 * @param[out] output a pointer to the store the converted value in.
394
 * @returns true if the value was converted, false if the string was not an int
395
 * or the value was too large / small for the type.
396
 */
397
bool HexStringToInt(const std::string &value, uint16_t *output);
398

399
/**
400
 * @brief Convert a hex string to a uint32_t.
401
 *
402
 * The string can contain upper or lower case hex characters.
403
 * @param[in] value the string to convert.
404
 * @param[out] output a pointer to the store the converted value in.
405
 * @returns true if the value was converted, false if the string was not an int
406
 * or the value was too large / small for the type.
407
 */
408
bool HexStringToInt(const std::string &value, uint32_t *output);
409

410
/**
411
 * @brief Convert a hex string to a uint64_t.
412
 *
413
 * The string can contain upper or lower case hex characters.
414
 * @param[in] value the string to convert.
415
 * @param[out] output a pointer to the store the converted value in.
416
 * @returns true if the value was converted, false if the string was not an int
417
 * or the value was too large / small for the type.
418
 */
419
bool HexStringToInt(const std::string &value, uint64_t *output);
420

421
/**
422
 * @brief Convert a hex string to a int8_t.
423
 * @param[in] value the string to convert.
424
 * @param[out] output a pointer to the store the converted value in.
425
 * @returns true if the value was converted, false if the string was not an int
426
 * or the value was too large / small for the type.
427
 *
428
 * The string can contain upper or lower case hex characters.
429
 */
430
bool HexStringToInt(const std::string &value, int8_t *output);
431

432
/**
433
 * @brief Convert a hex string to a int16_t.
434
 *
435
 * The string can contain upper or lower case hex characters.
436
 * @param[in] value the string to convert.
437
 * @param[out] output a pointer to the store the converted value in.
438
 * @returns true if the value was converted, false if the string was not an int
439
 * or the value was too large / small for the type.
440
 */
441
bool HexStringToInt(const std::string &value, int16_t *output);
442

443
/**
444
 * @brief Convert a hex string to a int32_t.
445
 *
446
 * The string can contain upper or lower case hex characters.
447
 * @param[in] value the string to convert.
448
 * @param[out] output a pointer to the store the converted value in.
449
 * @returns true if the value was converted, false if the string was not an int
450
 * or the value was too large / small for the type.
451
 */
452
bool HexStringToInt(const std::string &value, int32_t *output);
453

454
/**
455
 * @brief Convert a hex string to a int64_t.
456
 *
457
 * The string can contain upper or lower case hex characters.
458
 * @param[in] value the string to convert.
459
 * @param[out] output a pointer to the store the converted value in.
460
 * @returns true if the value was converted, false if the string was not an int
461
 * or the value was too large / small for the type.
462
 */
463
bool HexStringToInt(const std::string &value, int64_t *output);
464

465
/**
466
 * @brief Convert a string to lower case.
467
 * @param s the string to convert to lower case.
468
 */
469
void ToLower(std::string *s);
470

471
/**
472
 * @brief Convert a string to upper case.
473
 * @param s the string to convert to upper case.
474
 */
475
void ToUpper(std::string *s);
476

477
/**
478
 * @brief Transform a string to a pretty-printed form.
479
 *
480
 * Given a label in the form ([a-zA-Z0-9][-_])?[a-zA-Z0-9], this replaces [-_]
481
 * with [ ] and capitalizes each word. e.g.
482
 * "my_label" becomes "My Label"
483
 * @param s a string to transform.
484
 */
485
void CapitalizeLabel(std::string *s);
486

487
/**
488
 * @brief Similar to CapitalizeLabel() but this also capitalized known
489
 * acronyms.
490
 *
491
 * @param s a string to transform.
492
 * The following are capitalized:
493
 *   - dhcp
494
 *   - dmx
495
 *   - dns
496
 *   - ip
497
 *   - ipv4
498
 *   - ipv6
499
 *   - led
500
 *   - mdmx
501
 *   - rdm
502
 *   - uid
503
 */
504
void CustomCapitalizeLabel(std::string *s);
505

506
/**
507
 * @brief Transform a string by capitalizing the first character.
508
 * @param s a string to transform.
509
 */
510
void CapitalizeFirst(std::string *s);
511

512
/**
513
 * @brief Write binary data to an ostream in a human readable form.
514
 *
515
 * @param out the ostream to write to
516
 * @param data pointer to the data
517
 * @param length length of the data
518
 * @param indent the number of spaces to prefix each line with
519
 * @param byte_per_line the number of bytes to display per line
520
 *
521
 * The data is printed in two columns, hex on the left, ascii on the right.
522
 * Non ascii values are printed as .
523
 * @deprecated Use ola::strings::FormatData instead (30 Dec 2014).
524
 */
525
inline void FormatData(std::ostream *out,
7✔
526
                       const uint8_t *data,
527
                       unsigned int length,
528
                       unsigned int indent = 0,
529
                       unsigned int byte_per_line = 8) {
530
  return ola::strings::FormatData(out, data, length, indent, byte_per_line);
6✔
531
}
532

533
/**
534
 * @brief Convert a hex string, prefixed with 0x or 0X to an int type.
535
 */
536
template <typename int_type>
537
bool PrefixedHexStringToInt(const std::string &input, int_type *output) {
59✔
538
  if ((input.find("0x") != 0) && (input.find("0X") != 0))
59✔
539
    return false;
540
  std::string modified_input = input.substr(2);
×
541
  return HexStringToInt(modified_input, output);
×
542
}
×
543

544
/**
545
 * @brief Join a vector of a type.
546
 * @param delim the delimiter to use between items in the vector
547
 * @param input T can be any type for which the << operator is defined
548
 */
549
template<typename T>
550
std::string StringJoin(const std::string &delim, const T &input) {
×
551
  std::ostringstream str;
×
552
  typename T::const_iterator iter = input.begin();
×
553
  while (iter != input.end()) {
×
554
    str << *iter++;
×
555
    if (iter != input.end())
×
556
      str << delim;
×
557
  }
558
  return str.str();
×
559
}
×
560
}  // namespace ola
561
#endif  // INCLUDE_OLA_STRINGUTILS_H_
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc