1.10
detail/string.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_DETAIL_STRING_H
7#define EGT_DETAIL_STRING_H
8
14#include <egt/detail/meta.h>
15#include <egt/detail/stringhash.h>
16#include <iomanip>
17#include <ostream>
18#include <sstream>
19#include <string>
20#include <vector>
21
22namespace egt
23{
24inline namespace v1
25{
26namespace detail
27{
28
32EGT_API std::string replace_all(std::string str, const std::string& from,
33 const std::string& to);
34
38EGT_API void strip(std::string& str, const std::string& t = " \t\n\r");
39
43EGT_API std::string truncate(const std::string& str, size_t width,
44 bool ellipsis = true);
45
49EGT_API std::string rtrim(const std::string& source, const std::string& t = " \t\n\r");
50
54EGT_API std::string ltrim(const std::string& source, const std::string& t = " \t\n\r");
55
59EGT_API std::string trim(const std::string& source, const std::string& t = " \t\n\r");
60
64template<class T>
65std::string format(T value, int precision = 2)
66{
67 std::ostringstream stream;
68 stream << std::fixed << std::setprecision(precision) << value;
69 return stream.str();
70}
71
75EGT_API void tokenize(const std::string& str, char delimiter, std::vector<std::string>& tokens);
76
80template<class T>
81void join(std::ostream& os, const T& container, const std::string& delimiter = ",")
82{
83 bool first = true;
84 for (auto& item : container)
85 {
86 if (first)
87 first = false;
88 else
89 os << delimiter;
90 os << item;
91 }
92}
93
98EGT_API void tolower(std::string& s);
99
104EGT_API void toupper(std::string& s);
105
109template<class T>
110std::string to_string(const T& x)
111{
112 std::ostringstream ss;
113 ss << x;
114 return ss.str();
115}
116
120template<>
121inline std::string to_string(const bool& x)
122{
123 return x ? "true" : "false";
124}
125
129EGT_API bool from_string(const std::string& x);
130
137constexpr unsigned int hextoul(const char* str, unsigned int base = 16)
138{
139 unsigned int result = 0;
140 auto s = str;
141
142 while (*s == '#' || *s == ' ' || *s == '\t' || *s == '\n' || *s == '\v' || *s == '\f' || *s == '\r')
143 s++;
144
145 while (*s)
146 {
147 unsigned int c = *s++;
148 if (c >= '0' && c <= '9')
149 c -= '0';
150 else if (c >= 'A' && c <= 'F')
151 c -= 'A' - 10;
152 else if (c >= 'a' && c <= 'f')
153 c -= 'a' - 10;
154 else
155 break;
156 result *= base;
157 result += c;
158 }
159
160 return result;
161}
162
163}
164}
165}
166
167#endif
std::string format(T value, int precision=2)
Format a float/double to a fixed precision and return as a string.
Definition detail/string.h:65
EGT_API std::string trim(const std::string &source, const std::string &t=" \t\n\r")
Trim delimiters off both sides of a std::string.
EGT_API void toupper(std::string &s)
Convert a string to lowercase.
EGT_API void tokenize(const std::string &str, char delimiter, std::vector< std::string > &tokens)
Tokenize a std::string.
EGT_API std::string ltrim(const std::string &source, const std::string &t=" \t\n\r")
Trim delimiters off the left side of a std::string.
EGT_API bool from_string(const std::string &x)
Convert a lexical std::string to a bool.
constexpr unsigned int hextoul(const char *str, unsigned int base=16)
Like, but not the same as strtoul().
Definition detail/string.h:137
EGT_API std::string rtrim(const std::string &source, const std::string &t=" \t\n\r")
Trim delimiters off the right side of a std::string.
EGT_API std::string replace_all(std::string str, const std::string &from, const std::string &to)
Replace all in string.
EGT_API void tolower(std::string &s)
Convert a string to lowercase.
EGT_API std::string truncate(const std::string &str, size_t width, bool ellipsis=true)
Truncate a string if applicable and optionally add ellipse to end.
EGT_API void strip(std::string &str, const std::string &t=" \t\n\r")
Strip the specified characters from the string.
std::string to_string(const T &x)
Convert a type to a std::string using std::ostringstream.
Definition detail/string.h:110
void join(std::ostream &os, const T &container, const std::string &delimiter=",")
Join each item of a container with the specified delimiter between each item.
Definition detail/string.h:81
EGT framework namespace.
Definition animation.h:24