Biorbd
Release_1.13.0
Toggle main menu visibility
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
11
using namespace
BIORBD_NAMESPACE;
12
13
utils::String::String
() : std::string(
""
) {}
14
15
utils::String::String
(
const
char
*text) : std::string(text) {}
16
17
utils::String::String
(
const
String
&text) : std::string(text) {}
18
19
utils::String::String
(
const
std::basic_string<char> &text)
20
: std::string(text) {}
21
22
utils::String
&
utils::String::operator=
(
const
utils::String
&other) {
23
if
(
this
== &other) {
// check for self-assigment
24
return
*
this
;
25
}
26
27
this->std::string::operator=(other);
28
return
*
this
;
29
}
30
31
utils::String
utils::String::operator+
(
const
char
*text) {
32
String
tp = *
this
;
33
tp.append(text);
34
return
tp;
35
}
36
37
utils::String
utils::String::operator+
(
double
val) {
38
return
*
this
+
to_string
(val);
39
}
40
41
utils::String
utils::String::operator+
(
size_t
val) {
42
return
*
this
+ std::to_string(val);
43
}
44
45
utils::String
utils::String::operator+
(
int
val) {
46
return
*
this
+ std::to_string(val);
47
}
48
49
utils::String
utils::String::operator()
(
size_t
idx)
const
{
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
57
utils::String
utils::String::operator()
(
size_t
startIdx,
size_t
lastIdx)
const
{
58
utils::Error::check
(
59
(startIdx < this->length() || lastIdx < this->length()),
60
"Index for string out of range"
);
61
utils::Error::check
(
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
74
utils::String::~String
() {}
75
76
utils::String
utils::String::tolower
(
const
utils::String
&str) {
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
82
utils::String
utils::String::tolower
()
const
{
return
tolower
(*
this
); }
83
84
utils::String
utils::String::toupper
(
const
utils::String
&str) {
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
90
utils::String
utils::String::toupper
()
const
{
return
toupper
(*
this
); }
91
92
utils::String
utils::String::to_string
(
double
val) {
93
std::ostringstream out;
94
out.precision(20);
95
out << std::fixed << val;
96
return
removeTrailing
(out.str(),
"0"
);
97
}
98
99
utils::String
utils::String::to_string
(
float
val) {
100
std::ostringstream out;
101
out.precision(20);
102
out << std::fixed << val;
103
return
removeTrailing
(out.str(),
"0"
);
104
}
105
106
utils::String
utils::String::removeTrailing
(
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
118
std::ostream &operator<<(std::ostream &os,
const
utils::String
&a) {
119
os << a.c_str();
120
return
os;
121
}
BIORBD_NAMESPACE::utils::Error::check
static void check(bool cond, const String &message)
Assert that raises the error message if false.
Definition
Error.cpp:10
BIORBD_NAMESPACE::utils::String
Wrapper around the std::string class with augmented functionality.
Definition
String.h:18
BIORBD_NAMESPACE::utils::String::removeTrailing
static String removeTrailing(const String &origin, const String &trailTag)
The trailing tags of a string.
Definition
String.cpp:106
BIORBD_NAMESPACE::utils::String::tolower
String tolower() const
Return a lower case string.
Definition
String.cpp:82
BIORBD_NAMESPACE::utils::String::tolower
static String tolower(const String &str)
Convert a string to a lower case string.
Definition
String.cpp:76
BIORBD_NAMESPACE::utils::String::~String
virtual ~String()
Destroy class properly.
Definition
String.cpp:74
BIORBD_NAMESPACE::utils::String::toupper
String toupper() const
Return an upper case string.
Definition
String.cpp:90
BIORBD_NAMESPACE::utils::String::toupper
static String toupper(const String &str)
Convert a string to a upper case string.
Definition
String.cpp:84
BIORBD_NAMESPACE::utils::String::operator=
String & operator=(const String &other)
Allow to use operator=.
Definition
String.cpp:22
BIORBD_NAMESPACE::utils::String::String
String()
Construct string.
Definition
String.cpp:13
BIORBD_NAMESPACE::utils::String::operator+
String operator+(size_t val)
Append an unsigned int to the string.
Definition
String.cpp:41
BIORBD_NAMESPACE::utils::String::to_string
static String to_string(double val)
Overload of the to_string C++11 function to allow for X-digits precision.
Definition
String.cpp:92
BIORBD_NAMESPACE::utils::String::operator()
String operator()(size_t idx) const
Get a specific character in the string.
Definition
String.cpp:49
src
Utils
String.cpp
Generated by
1.18.0