Theoretica
Scientific Computing
Loading...
Searching...
No Matches
strings.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_IO_STRINGS_H
7#define THEORETICA_IO_STRINGS_H
8
9#include <string>
10
11
12namespace theoretica {
13namespace io {
14
15
20 inline bool is_number(const std::string& str) {
21
22 if (str.empty())
23 return false;
24
25 const std::string allowed = "1234567890.,Ee+-Nnaif";
26
27 for (char c : str)
28 if (allowed.find(c) == std::string::npos)
29 return false;
30
31 return true;
32 }
33
34
40 inline std::string trim(const std::string& str) {
41
42 size_t start = 0;
43 while (start < str.length() && std::isspace(str[start]))
44 ++start;
45
46 size_t end = str.length();
47 while (end > start && std::isspace(str[end - 1]))
48 --end;
49
50 return str.substr(start, end - start);
51 }
52
53
58 inline std::string unquote(const std::string& str) {
59
60 if (str.length() >= 2 && str.front() == '"' && str.back() == '"')
61 return str.substr(1, str.length() - 2);
62
63 return str;
64 }
65
66}}
67
68
69#endif
bool is_number(const std::string &str)
Check if a given string could be correctly interpreted as a number.
Definition strings.h:20
std::string trim(const std::string &str)
Remove all leading and trailing whitespace from a string, returning the resulting string.
Definition strings.h:40
std::string unquote(const std::string &str)
Remove leading and trailing double quotes from a string, if both are present.
Definition strings.h:58
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103