28 #include <type_traits>
35 inline bool isPrefix(
const std::string& prefix,
const std::string& element);
46 const std::string&
str, std::size_t* position =
nullptr,
const int base = 10) {
58 std::string binaryNumber;
59 bool parsingBinary =
base == 2;
64 binaryNumber =
"-" +
str.substr(3);
66 binaryNumber =
str.substr(2);
69 const std::string& tmp = parsingBinary ? binaryNumber :
str;
71 #if RAM_DOMAIN_SIZE == 64
72 val = std::stoll(tmp, position,
base);
74 val = std::stoi(tmp, position,
base);
77 if (parsingBinary && position !=
nullptr) {
89 #if RAM_DOMAIN_SIZE == 64
90 val = std::stod(
str, position);
92 val = std::stof(
str, position);
105 const std::string&
str, std::size_t* position =
nullptr,
const int base = 10) {
108 throw std::invalid_argument(
"Unsigned number can't start with minus.");
122 std::string binaryNumber;
123 bool parsingBinary =
false;
125 binaryNumber =
str.substr(2);
126 parsingBinary =
true;
128 const std::string& tmp = parsingBinary ? binaryNumber :
str;
131 #if RAM_DOMAIN_SIZE == 64
132 val = std::stoull(tmp, position,
base);
134 val = std::stoul(tmp, position,
base);
137 if (parsingBinary && position !=
nullptr) {
142 if (val > std::numeric_limits<RamUnsigned>::max()) {
143 throw std::invalid_argument(
"Unsigned number of of bounds");
157 size_t charactersRead = 0;
165 return charactersRead ==
string.size();
174 size_t charactersRead = 0;
180 return charactersRead ==
string.size();
187 size_t charactersRead = 0;
193 return charactersRead ==
string.size();
196 #if RAM_DOMAIN_SIZE == 64
197 inline RamDomain stord(
const std::string&
str, std::size_t* pos =
nullptr,
int base = 10) {
200 #elif RAM_DOMAIN_SIZE == 32
201 inline RamDomain stord(
const std::string&
str, std::size_t* pos =
nullptr,
int base = 10) {
205 #error RAM Domain is neither 32bit nor 64bit
212 if (
str ==
nullptr) {
228 inline const std::string&
toString(
const std::string&
str) {
238 template <
typename T,
typename filter =
void>
239 struct is_printable :
public std::false_type {};
245 template <
typename T>
247 decltype(std::declval<std::ostream&>() << std::declval<T>()), void>::type>
248 : public std::true_type {};
258 template <typename T>
259 typename std::enable_if<detail::is_printable<T>::value, std::string>::type toString(const T& value) {
261 std::stringstream ss;
270 template <typename T>
271 typename std::enable_if<!detail::is_printable<T>::value, std::string>::type toString(const T&) {
272 std::stringstream ss;
273 ss << "(print for type ";
274 ss << typeid(T).name();
275 ss << " not supported)";
286 inline bool isPrefix(const std::string& prefix, const std::string& element) {
287 auto itPrefix = prefix.begin();
288 auto itElement = element.begin();
290 while (itPrefix != prefix.end() && itElement != element.end()) {
291 if (*itPrefix != *itElement) {
298 return itPrefix == prefix.end();
305 inline bool endsWith(const std::string& value, const std::string& ending) {
306 if (value.size() < ending.size()) {
309 return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
315 inline std::vector<std::string> splitString(const std::string& str, char delimiter) {
316 std::vector<std::string> parts;
317 std::stringstream strstr(str);
319 while (std::getline(strstr, token, delimiter)) {
320 parts.push_back(token);
328 inline std::string stringify(const std::string& input) {
329 std::string str(input);
332 size_t start_pos = 0;
333 while ((start_pos = str.find('\\', start_pos)) != std::string::npos) {
334 str.replace(start_pos, 1, "\\\\");
339 while ((start_pos = str.find(';', start_pos)) != std::string::npos) {
340 str.replace(start_pos, 1, "\\;");
345 while ((start_pos = str.find('"', start_pos)) != std::string::npos) {
346 str.replace(start_pos, 1, "\\\"");
351 while ((start_pos = str.find('\n', start_pos)) != std::string::npos) {
352 str.replace(start_pos, 1, "\\n");
357 while ((start_pos = str.find('\t', start_pos)) != std::string::npos) {
358 str.replace(start_pos, 1, "\\t");
367 inline std::string escapeJSONstring(const std::string& JSONstr) {
368 std::ostringstream destination;
371 for (char c : JSONstr) {
377 return destination.str();
381 inline std::string identifier(std::string id) {
382 for (size_t i = 0; i < id.length(); i++) {
383 if (((isalpha(id[i]) == 0) && i == 0) || ((isalnum(id[i]) == 0) && id[i] != '_')) {
392 inline std::string unescape(
393 const std::string& inputString, const std::string& needle, const std::string& replacement) {
394 std::string result = inputString;
396 while ((pos = result.find(needle, pos)) != std::string::npos) {
397 result = result.replace(pos, needle.length(), replacement);
398 pos += replacement.length();
403 inline std::string unescape(const std::string& inputString) {
404 std::string unescaped = unescape(inputString, "\\\"", "\"");
405 unescaped = unescape(unescaped, "\\t", "\t");
406 unescaped = unescape(unescaped, "\\r", "\r");
407 unescaped = unescape(unescaped, "\\n", "\n");
411 inline std::string escape(
412 const std::string& inputString, const std::string& needle, const std::string& replacement) {
413 std::string result = inputString;
415 while ((pos = result.find(needle, pos)) != std::string::npos) {
416 result = result.replace(pos, needle.length(), replacement);
417 pos += replacement.length();
422 inline std::string escape(const std::string& inputString) {
423 std::string escaped = escape(inputString, "\"", "\\\"");
424 escaped = escape(escaped, "\t", "\\t");
425 escaped = escape(escaped, "\r", "\\r");
426 escaped = escape(escaped, "\n", "\\n");