SPH
String.cpp
Go to the documentation of this file.
2 #include <iomanip>
3 
5 
7 
8 Size String::find(const String& s, const Size pos) const {
9  SPH_ASSERT(pos < this->size() && !s.empty(), pos, this->size());
10  if (s.size() > this->size()) {
11  return npos;
12  }
13  for (Size i = pos; i <= this->size() - s.size(); ++i) {
14  if (data[i] == s[0]) {
15  bool matching = true;
16  for (Size j = 1; j < s.size(); ++j) {
17  if (data[i + j] != s[j]) {
18  matching = false;
19  break;
20  }
21  }
22  if (matching) {
23  return i;
24  }
25  }
26  }
27  return npos;
28 }
29 
31  Size n = npos;
32  for (String& s : ss) {
33  n = min(n, this->find(s, pos));
34  }
35  return n;
36 }
37 
38 Size String::findLast(const String& s) const {
39  SPH_ASSERT(!s.empty());
40  if (s.size() > this->size()) {
41  return npos;
42  }
43  for (Size i = this->size() - s.size() + 1; i > 0; --i) {
44  if (data[i - 1] == s[0]) {
45  bool matching = true;
46  for (Size j = 1; j < s.size(); ++j) {
47  if (data[i + j - 1] != s[j]) {
48  matching = false;
49  break;
50  }
51  }
52  if (matching) {
53  return i - 1;
54  }
55  }
56  }
57  return npos;
58 }
59 
60 void String::replace(const Size pos, const Size n, const String& s) {
61  SPH_ASSERT(pos + n <= this->size());
62  Array<char> replaced;
63  replaced.reserve(data.size() + s.size() - n);
64  for (Size i = 0; i < pos; ++i) {
65  replaced.push(data[i]);
66  }
67  for (Size i = 0; i < s.size(); ++i) {
68  replaced.push(s[i]);
69  }
70  for (Size i = pos + n; i < data.size(); ++i) {
71  replaced.push(data[i]);
72  }
73  *this = String(std::move(replaced));
74 }
75 
76 void String::replace(const String& old, const String& s) {
77  const Size n = this->find(old);
78  if (n == npos) {
79  return;
80  }
81  this->replace(n, old.size(), s);
82 }
83 
84 String String::substr(const Size pos, const Size n) const {
85  SPH_ASSERT(pos < this->size());
86  Array<char> ss;
87  const Size m = min(n, this->size() - pos);
88  ss.reserve(m + 1);
89  for (Size i = pos; i < pos + m; ++i) {
90  ss.push(data[i]);
91  }
92  ss.push('\0');
93  return ss;
94 }
95 
97  Size i1 = 0;
98  for (; i1 < data.size(); ++i1) {
99  if (data[i1] != ' ') {
100  break;
101  }
102  }
103  Size i2 = data.size() - 1;
104  for (; i2 > 0; --i2) {
105  if (data[i2 - 1] != ' ') {
106  break;
107  }
108  }
109  Array<char> trimmed;
110  for (Size i = i1; i < i2; ++i) {
111  trimmed.push(data[i]);
112  }
113  trimmed.push('\0');
114  return trimmed;
115 }
116 
118  String s = *this;
119  for (char& c : s) {
120  if (c >= 'A' && c <= 'Z') {
121  c = c - 'A' + 'a';
122  }
123  }
124  return s;
125 }
126 
127 
129  std::time_t t = std::time(nullptr);
130  char buffer[256];
131  auto retval = strftime(buffer, sizeof(buffer), format.cStr(), std::localtime(&t));
132  String s(buffer);
133  SPH_ASSERT(retval == s.size());
134  return s;
135 }
136 
137 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Definition: MathBasic.h:15
#define NAMESPACE_SPH_END
Definition: Object.h:12
String getFormattedTime(const String &format)
Utility functions.
Definition: String.cpp:128
Object representing a sequence of characters.
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
void reserve(const TCounter newMaxSize)
Allocates enough memory to store the given number of elements.
Definition: Array.h:279
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
INLINE TCounter size() const noexcept
Definition: Array.h:193
Definition: String.h:13
void replace(const Size pos, const Size n, const String &s)
Definition: String.cpp:60
const char * cStr() const
Definition: String.h:81
String lower() const
Definition: String.cpp:117
INLINE Size size() const
Definition: String.h:90
static Size npos
Definition: String.h:123
String trim() const
Definition: String.cpp:96
Size findAny(ArrayView< String > ss, const Size pos) const
Definition: String.cpp:30
String substr(const Size pos, const Size n=String::npos) const
Definition: String.cpp:84
String()=default
Size findLast(const String &s) const
Definition: String.cpp:38
INLINE bool empty() const
Definition: String.h:94
Size find(const String &s, const Size pos=0) const
Definition: String.cpp:8