Biorbd Release_1.13.0
Loading...
Searching...
No Matches
String.cpp
1#define BIORBD_API_EXPORTS
2#include "Utils/String.h"
3
4#include <algorithm>
5#include <map>
6#include <sstream>
7#include <vector>
8
9#include "Utils/Error.h"
10
11using namespace BIORBD_NAMESPACE;
12
13utils::String::String() : std::string("") {}
14
15utils::String::String(const char *text) : std::string(text) {}
16
17utils::String::String(const String &text) : std::string(text) {}
18
19utils::String::String(const std::basic_string<char> &text)
20 : std::string(text) {}
21
23 if (this == &other) { // check for self-assigment
24 return *this;
25 }
26
27 this->std::string::operator=(other);
28 return *this;
29}
30
32 String tp = *this;
33 tp.append(text);
34 return tp;
35}
36
38 return *this + to_string(val);
39}
40
42 return *this + std::to_string(val);
43}
44
46 return *this + std::to_string(val);
47}
48
50 utils::Error::check(idx < this->length(), "Index for string out of range");
51 char out[2];
52 out[0] = (*this)[idx];
53 out[1] = '\0';
54 return out;
55}
56
57utils::String utils::String::operator()(size_t startIdx, size_t lastIdx) const {
59 (startIdx < this->length() || lastIdx < this->length()),
60 "Index for string out of range");
62 lastIdx > startIdx, "Second argument should be higher than first!");
63 char *out =
64 static_cast<char *>(malloc(lastIdx - startIdx + 2 * sizeof(char)));
65 for (size_t k = 0; k < lastIdx - startIdx + 1; ++k) {
66 out[k] = (*this)[startIdx + k];
67 }
68 out[lastIdx - startIdx + 1] = '\0';
69 utils::String Out(out);
70 free(out);
71 return Out;
72}
73
75
77 utils::String new_str = str;
78 std::transform(new_str.begin(), new_str.end(), new_str.begin(), ::tolower);
79 return new_str;
80}
81
83
85 utils::String new_str = str;
86 std::transform(new_str.begin(), new_str.end(), new_str.begin(), ::toupper);
87 return new_str;
88}
89
91
93 std::ostringstream out;
94 out.precision(20);
95 out << std::fixed << val;
96 return removeTrailing(out.str(), "0");
97}
98
100 std::ostringstream out;
101 out.precision(20);
102 out << std::fixed << val;
103 return removeTrailing(out.str(), "0");
104}
105
107 const utils::String &origin,
108 const utils::String &trailTag) {
109 utils::Error::check(trailTag.length() == 1, "Tag should be of length 1");
110 utils::String out(origin);
111
112 while (out.length() > 0 && out.back() == trailTag[0]) {
113 out.pop_back();
114 }
115 return out;
116}
117
118std::ostream &operator<<(std::ostream &os, const utils::String &a) {
119 os << a.c_str();
120 return os;
121}
static void check(bool cond, const String &message)
Assert that raises the error message if false.
Definition Error.cpp:10
Wrapper around the std::string class with augmented functionality.
Definition String.h:18
static String removeTrailing(const String &origin, const String &trailTag)
The trailing tags of a string.
Definition String.cpp:106
String tolower() const
Return a lower case string.
Definition String.cpp:82
static String tolower(const String &str)
Convert a string to a lower case string.
Definition String.cpp:76
virtual ~String()
Destroy class properly.
Definition String.cpp:74
String toupper() const
Return an upper case string.
Definition String.cpp:90
static String toupper(const String &str)
Convert a string to a upper case string.
Definition String.cpp:84
String & operator=(const String &other)
Allow to use operator=.
Definition String.cpp:22
String()
Construct string.
Definition String.cpp:13
String operator+(size_t val)
Append an unsigned int to the string.
Definition String.cpp:41
static String to_string(double val)
Overload of the to_string C++11 function to allow for X-digits precision.
Definition String.cpp:92
String operator()(size_t idx) const
Get a specific character in the string.
Definition String.cpp:49