Theoretica
Mathematical Library
Loading...
Searching...
No Matches
strings.h
1
2#ifndef THEORETICA_IO_STRINGS_H
3#define THEORETICA_IO_STRINGS_H
4
5#include <string>
6
7
8namespace theoretica {
9namespace io {
10
11
16 inline bool is_number(const std::string& str) {
17
18 if (str.empty())
19 return false;
20
21 const std::string allowed = "1234567890.,Ee+-Nnaif";
22
23 for (char c : str)
24 if (allowed.find(c) == std::string::npos)
25 return false;
26
27 return true;
28 }
29
30
36 inline std::string trim(const std::string& str) {
37
38 size_t start = 0;
39 while (start < str.length() && std::isspace(str[start]))
40 ++start;
41
42 size_t end = str.length();
43 while (end > start && std::isspace(str[end - 1]))
44 --end;
45
46 return str.substr(start, end - start);
47 }
48
49
54 inline std::string unquote(const std::string& str) {
55
56 if (str.length() >= 2 && str.front() == '"' && str.back() == '"')
57 return str.substr(1, str.length() - 2);
58
59 return str;
60 }
61
62}}
63
64
65#endif
bool is_number(const std::string &str)
Check if a given string could be correctly interpreted as a number.
Definition strings.h:16
std::string trim(const std::string &str)
Remove all leading and trailing whitespace from a string, returning the resulting string.
Definition strings.h:36
std::string unquote(const std::string &str)
Remove leading and trailing double quotes from a string, if both are present.
Definition strings.h:54
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
TH_CONSTEXPR Type make_error()
Create a number representing an error state, constructed from a NaN value.
Definition real_analysis.h:1322