SPH
String.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 #include <string.h>
10 
12 
13 class String {
14 private:
15  Array<char> data = { '\0' };
16 
17 public:
18  String() = default;
19 
20  String(const String& other)
21  : data(other.data.clone()) {}
22 
23  String(String&& other) {
24  // we need to put '\0' before swapping to leave other in consistent state
25  data = std::move(other.data);
26  }
27 
28  String(Array<char>&& buffer)
29  : data(std::move(buffer)) {
30  SPH_ASSERT(sanityCheck());
31  }
32 
33  String(const char* s) {
34  data.pop(); // pop terminating zero
35  const Size length = strlen(s);
36  data.reserve(length + 1);
37  for (Size i = 0; i < length; ++i) {
38  data.push(s[i]);
39  }
40  data.push('\0');
41  }
42 
43  String& operator=(const String& other) {
44  data = copyable(other.data);
45  return *this;
46  }
47 
48  String& operator=(String&& other) {
49  data = std::move(other.data);
50  return *this;
51  }
52 
53  String& operator+=(const String& other) {
54  data.pop(); // pop terminating zero
55  data.pushAll(other.data); // push all characters including terminating zero
56  SPH_ASSERT(sanityCheck());
57  return *this;
58  }
59 
60  friend String operator+(const String& s1, const String& s2) {
61  String s = s1;
62  s += s2;
63  return s;
64  }
65 
66  String& operator+=(const char c) {
67  data.pop();
68  data.push(c);
69  data.push('\0');
70  return *this;
71  }
72 
73  bool operator==(const String& other) const {
74  return data == other.data;
75  }
76 
77  bool operator!=(const String& other) const {
78  return data != other.data;
79  }
80 
81  const char* cStr() const {
82  return &data[0];
83  }
84 
85  INLINE char operator[](const Size idx) const {
86  SPH_ASSERT(idx < this->size());
87  return data[idx];
88  }
89 
90  INLINE Size size() const {
91  return data.size() - 1;
92  }
93 
94  INLINE bool empty() const {
95  return data.size() == 1;
96  }
97 
99 
101  return data.begin();
102  }
103 
105  return data.begin();
106  }
107 
109  return data.end() - 1;
110  }
111 
113  return data.end() - 1;
114  }
115 
116  friend std::ostream& operator<<(std::ostream& stream, const String& str) {
117  stream << str.cStr();
118  return stream;
119  }
120 
122 
123  static Size npos;
124 
125  Size find(const String& s, const Size pos = 0) const;
126 
127  Size findAny(ArrayView<String> ss, const Size pos) const;
128 
129  Size findLast(const String& s) const;
130 
131  void replace(const Size pos, const Size n, const String& s);
132 
133  void replace(const String& old, const String& s);
134 
135  template <typename... TArgs>
136  void replace(const String& old, const String& s, TArgs&&... args) {
137  replace(old, s);
138  replace(std::forward<TArgs>(args)...);
139  }
140 
141  String substr(const Size pos, const Size n = String::npos) const;
142 
143  String trim() const;
144 
145  String lower() const;
146 
147 private:
148  bool sanityCheck() {
149  return !data.empty() && data.size() < npos / 2 && data[data.size() - 1] == '\0';
150  }
151 };
152 
153 
154 INLINE bool operator<(const String& s1, const String& s2) {
155  return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end());
156 }
157 
158 INLINE String operator"" _s(const char* s, std::size_t UNUSED(len)) {
159  return String(s);
160 }
161 
162 template <typename T>
163 INLINE String toString(const T& value) {
164  std::stringstream ss;
165  ss << value;
166  return ss.str().c_str();
167 }
168 
169 
171 
173 String getFormattedTime(const String& format);
174 
175 
Generic dynamically allocated resizable storage.
INLINE CopyableArray< T, TAllocator, TCounter > copyable(const Array< T, TAllocator, TCounter > &array)
Definition: Array.h:558
#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
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE String toString(const T &value)
Definition: String.h:163
String getFormattedTime(const String &format)
Utility functions.
Definition: String.cpp:128
INLINE bool operator<(const String &s1, const String &s2)
Definition: String.h:154
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 Iterator< StorageType > end() noexcept
Definition: Array.h:462
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
INLINE T pop()
Removes the last element from the array and return its value.
Definition: Array.h:375
INLINE TCounter size() const noexcept
Definition: Array.h:193
INLINE bool empty() const noexcept
Definition: Array.h:201
INLINE Iterator< StorageType > begin() noexcept
Definition: Array.h:450
void pushAll(const TIter first, const TIter last)
Definition: Array.h:312
Simple (forward) iterator over continuous array of objects of type T.
Definition: Iterator.h:18
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
Iterator< char > begin()
Definition: String.h:100
String lower() const
Definition: String.cpp:117
INLINE Size size() const
Definition: String.h:90
bool operator==(const String &other) const
Definition: String.h:73
Iterator< char > end()
Definition: String.h:108
static Size npos
Definition: String.h:123
String(String &&other)
Definition: String.h:23
String trim() const
Definition: String.cpp:96
friend std::ostream & operator<<(std::ostream &stream, const String &str)
Definition: String.h:116
String(Array< char > &&buffer)
Definition: String.h:28
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
INLINE char operator[](const Size idx) const
Definition: String.h:85
friend String operator+(const String &s1, const String &s2)
Definition: String.h:60
String & operator+=(const char c)
Definition: String.h:66
String & operator=(String &&other)
Definition: String.h:48
String()=default
void replace(const String &old, const String &s, TArgs &&... args)
Definition: String.h:136
String & operator+=(const String &other)
Definition: String.h:53
Size findLast(const String &s) const
Definition: String.cpp:38
String(const char *s)
Definition: String.h:33
String & operator=(const String &other)
Definition: String.h:43
INLINE bool empty() const
Definition: String.h:94
Size find(const String &s, const Size pos=0) const
Definition: String.cpp:8
bool operator!=(const String &other) const
Definition: String.h:77
Iterator< const char > begin() const
Definition: String.h:104
String(const String &other)
Definition: String.h:20
Iterator< const char > end() const
Definition: String.h:112
Overload of std::swap for Sph::Array.
Definition: Array.h:578