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