CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
CString.h
Go to the documentation of this file.
1 /*
2  * CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3  * Copyright (C) 2014-2017 Mateusz Szpakowski
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
23 #ifndef __CLRX_CSTRING_H__
24 #define __CLRX_CSTRING_H__
25 
26 #include <CLRX/Config.h>
27 #include <algorithm>
28 #include <cstddef>
29 #include <string>
30 #include <ostream>
31 #include <cstring>
32 #include <initializer_list>
33 
34 namespace CLRX
35 {
36 
38 class CString
39 {
40 public:
41  typedef char* iterator;
42  typedef const char* const_iterator;
43  typedef char element_type;
44  typedef std::string::size_type size_type;
45  static const size_type npos = -1;
46 private:
47  char* ptr;
48 public:
50  CString(): ptr(nullptr)
51  { }
52 
54  explicit CString(size_t n) : ptr(nullptr)
55  {
56  if (n == 0) return;
57  ptr = new char[n+1];
58  ptr[n] = 0;
59  }
60 
62  CString(const char* str) : ptr(nullptr)
63  {
64  if (str == nullptr)
65  return;
66  const size_t n = ::strlen(str);
67  if (n == 0)
68  return;
69  ptr = new char[n+1];
70  ::memcpy(ptr, str, n);
71  ptr[n] = 0;
72  }
73 
75  CString(const std::string& str) : ptr(nullptr)
76  {
77  const size_t n = str.size();
78  if (n == 0)
79  return;
80  ptr = new char[n+1];
81  ::memcpy(ptr, str.c_str(), n);
82  ptr[n] = 0;
83  }
84 
86  CString(const char* str, size_t n) : ptr(nullptr)
87  {
88  if (n == 0)
89  return;
90  ptr = new char[n+1];
91  ::memcpy(ptr, str, n);
92  ptr[n] = 0;
93  }
94 
96  CString(const char* str, const char* end) : ptr(nullptr)
97  {
98  const size_t n = end-str;
99  if (n == 0)
100  return;
101  ptr = new char[n+1];
102  ::memcpy(ptr, str, n);
103  ptr[n] = 0;
104  }
105 
107  CString(size_t n, char ch) : ptr(nullptr)
108  {
109  if (n == 0)
110  return;
111  ptr = new char[n+1];
112  ::memset(ptr, ch, n);
113  ptr[n] = 0;
114  }
115 
117  CString(const CString& cstr) : ptr(nullptr)
118  {
119  const size_t n = cstr.size();
120  if (n == 0)
121  return;
122  ptr = new char[n+1];
123  ::memcpy(ptr, cstr.c_str(), n);
124  ptr[n] = 0;
125  }
126 
128  CString(CString&& cstr) noexcept : ptr(cstr.ptr)
129  { cstr.ptr = nullptr; }
130 
132  CString(std::initializer_list<char> init) : ptr(nullptr)
133  {
134  const size_t n = init.size();
135  if (n == 0)
136  return;
137  ptr = new char[n+1];
138  std::copy(init.begin(), init.end(), ptr);
139  ptr[n] = 0; // null char
140  }
141 
144  { delete[] ptr; }
145 
147  CString& operator=(const CString& cstr)
148  {
149  if (this==&cstr)
150  return *this;
151  return assign(cstr.ptr);
152  }
153 
155  CString& operator=(const std::string& str)
156  { return assign(str.c_str(), str.size()); }
157 
159  CString& operator=(CString&& cstr) noexcept
160  {
161  delete[] ptr; // delete old
162  ptr = cstr.ptr;
163  cstr.ptr = nullptr;
164  return *this;
165  }
166 
168  CString& operator=(const char* str)
169  { return assign(str); }
170 
172  CString& operator=(char ch)
173  { return assign(1, ch); }
174 
176  CString& operator=(std::initializer_list<char> init)
177  { return assign(init); }
178 
180  CString& assign(const char* str)
181  {
182  if (str==nullptr)
183  {
184  delete[] ptr;
185  ptr = nullptr;
186  return *this;
187  }
188  size_t length = ::strlen(str);
189  return assign(str, length);
190  }
191 
193  CString& assign(const char* str, size_t n)
194  {
195  if (n == 0)
196  {
197  // just clear
198  delete[] ptr;
199  ptr = nullptr;
200  return *this;
201  }
202  char* newPtr = new char[n+1];
203  ::memcpy(newPtr, str, n);
204  newPtr[n] = 0;
205  delete[] ptr;
206  ptr = newPtr;
207  return *this;
208  }
209 
211  CString& assign(const char* str, const char* end)
212  { return assign(str, end-str); }
213 
215  CString& assign (size_t n, char ch)
216  {
217  if (n == 0)
218  {
219  // just clear
220  delete[] ptr;
221  ptr = nullptr;
222  return *this;
223  }
224  char* newPtr = new char[n+1];
225  ::memset(newPtr, ch, n);
226  newPtr[n] = 0; // null-char
227  delete[] ptr;
228  ptr = newPtr;
229  return *this;
230  }
231 
233  CString& assign(std::initializer_list<char> init)
234  {
235  const size_t n = init.size();
236  if (n == 0)
237  {
238  // just clear
239  delete[] ptr;
240  ptr = nullptr;
241  return *this;
242  }
243  char* newPtr = new char[n+1];
244  std::copy(init.begin(), init.end(), newPtr);
245  newPtr[n] = 0; // null char
246  delete[] ptr;
247  ptr = newPtr;
248  return *this;
249  }
250 
252  const char* c_str() const
253  { return ptr!=nullptr ? ptr : ""; }
254 
256  const char* begin() const
257  { return ptr!=nullptr ? ptr : ""; }
258 
260  const char& operator[](size_t i) const
261  { return ptr[i]; }
262 
264  char& operator[](size_t i)
265  { return ptr[i]; }
266 
268  char* begin()
269  { return ptr; }
270 
272  size_t size() const
273  {
274  if (ptr==nullptr) return 0;
275  return ::strlen(ptr);
276  }
278  size_t length() const
279  {
280  if (ptr==nullptr) return 0;
281  return ::strlen(ptr);
282  }
283 
285  void clear()
286  {
287  delete[] ptr;
288  ptr = nullptr;
289  }
290 
292  bool empty() const
293  { return ptr==nullptr; }
294 
296  const char& front() const
297  { return ptr[0]; }
298 
300  char& front()
301  { return ptr[0]; }
302 
304  int compare(const CString& cstr) const
305  { return ::strcmp(c_str(), cstr.c_str()); }
306 
308  int compare(const char* str) const
309  { return ::strcmp(c_str(), str); }
310 
312  int compare(size_t pos, size_t n, const char* str) const
313  { return ::strncmp(c_str()+pos, str, n); }
314 
316  size_type find(char ch, size_t pos = 0) const
317  {
318  const char* th = c_str();
319  const char* p = ::strchr(th+pos, ch);
320  return (p!=nullptr) ? p-th : npos;
321  }
322 
324  size_type find(const CString& str, size_t pos = 0) const
325  {
326  const char* th = c_str();
327  const char* p = ::strstr(th+pos, str.c_str());
328  return (p!=nullptr) ? p-th : npos;
329  }
330 
332  size_type find(const char* str, size_t pos = 0) const
333  {
334  const char* th = c_str();
335  const char* p = ::strstr(th+pos, str);
336  return (p!=nullptr) ? p-th : npos;
337  }
338 
340  CString substr(size_t pos, size_t n) const
341  { return CString(c_str()+pos, n); }
342 
344  void swap(CString& s2) noexcept
345  { std::swap(ptr, s2.ptr); }
346 };
347 
349 inline bool operator==(const CLRX::CString& s1, const CLRX::CString& s2)
350 { return ::strcmp(s1.c_str(), s2.c_str())==0; }
351 
353 inline bool operator!=(const CLRX::CString& s1, const CLRX::CString& s2)
354 { return ::strcmp(s1.c_str(), s2.c_str())!=0; }
355 
357 inline bool operator<(const CLRX::CString& s1, const CLRX::CString& s2)
358 { return ::strcmp(s1.c_str(), s2.c_str())<0; }
359 
361 inline bool operator>(const CLRX::CString& s1, const CLRX::CString& s2)
362 { return ::strcmp(s1.c_str(), s2.c_str())>0; }
363 
365 inline bool operator<=(const CLRX::CString& s1, const CLRX::CString& s2)
366 { return ::strcmp(s1.c_str(), s2.c_str())<=0; }
367 
369 inline bool operator>=(const CLRX::CString& s1, const CLRX::CString& s2)
370 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
371 
372 }
373 
375 inline bool operator==(const CLRX::CString& s1, const std::string& s2)
376 { return ::strcmp(s1.c_str(), s2.c_str())==0; }
377 
379 inline bool operator!=(const CLRX::CString& s1, const std::string& s2)
380 { return ::strcmp(s1.c_str(), s2.c_str())!=0; }
381 
383 inline bool operator<(const CLRX::CString& s1, const std::string& s2)
384 { return ::strcmp(s1.c_str(), s2.c_str())<0; }
385 
387 inline bool operator>(const CLRX::CString& s1, const std::string& s2)
388 { return ::strcmp(s1.c_str(), s2.c_str())>0; }
389 
391 inline bool operator<=(const CLRX::CString& s1, const std::string& s2)
392 { return ::strcmp(s1.c_str(), s2.c_str())<=0; }
393 
395 inline bool operator>=(const CLRX::CString& s1, const std::string& s2)
396 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
397 
399 inline bool operator==(const std::string& s1, const CLRX::CString& s2)
400 { return ::strcmp(s1.c_str(), s2.c_str())==0; }
401 
403 inline bool operator!=(const std::string& s1, const CLRX::CString& s2)
404 { return ::strcmp(s1.c_str(), s2.c_str())!=0; }
405 
407 inline bool operator<(const std::string& s1, const CLRX::CString& s2)
408 { return ::strcmp(s1.c_str(), s2.c_str())<0; }
409 
411 inline bool operator>(const std::string& s1, const CLRX::CString& s2)
412 { return ::strcmp(s1.c_str(), s2.c_str())>0; }
413 
415 inline bool operator<=(const std::string& s1, const CLRX::CString& s2)
416 { return ::strcmp(s1.c_str(), s2.c_str())<=0; }
417 
419 inline bool operator>=(const std::string& s1, const CLRX::CString& s2)
420 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
421 
423 inline bool operator==(const CLRX::CString& s1, const char* s2)
424 { return ::strcmp(s1.c_str(), s2)==0; }
425 
427 inline bool operator!=(const CLRX::CString& s1, const char* s2)
428 { return ::strcmp(s1.c_str(), s2)!=0; }
429 
431 inline bool operator<(const CLRX::CString& s1, const char* s2)
432 { return ::strcmp(s1.c_str(), s2)<0; }
433 
435 inline bool operator>(const CLRX::CString& s1, const char* s2)
436 { return ::strcmp(s1.c_str(), s2)>0; }
437 
439 inline bool operator<=(const CLRX::CString& s1, const char* s2)
440 { return ::strcmp(s1.c_str(), s2)<=0; }
441 
443 inline bool operator>=(const CLRX::CString& s1, const char* s2)
444 { return ::strcmp(s1.c_str(), s2)>=0; }
445 
447 inline bool operator==(const char* s1, const CLRX::CString& s2)
448 { return ::strcmp(s1, s2.c_str())==0; }
449 
451 inline bool operator!=(const char* s1, const CLRX::CString& s2)
452 { return ::strcmp(s1, s2.c_str())!=0; }
453 
455 inline bool operator<(const char* s1, const CLRX::CString& s2)
456 { return ::strcmp(s1, s2.c_str())<0; }
457 
459 inline bool operator>(const char* s1, const CLRX::CString& s2)
460 { return ::strcmp(s1, s2.c_str())>0; }
461 
463 inline bool operator<=(const char* s1, const CLRX::CString& s2)
464 { return ::strcmp(s1, s2.c_str())<=0; }
465 
467 inline bool operator>=(const char* s1, const CLRX::CString& s2)
468 { return ::strcmp(s1, s2.c_str())>=0; }
469 
471 inline std::ostream& operator<<(std::ostream& os, const CLRX::CString& cstr)
472 { return os<<cstr.c_str(); }
473 
474 namespace std
475 {
476 
478 inline void swap(CLRX::CString& s1, CLRX::CString& s2)
479 { s1.swap(s2); }
480 
482 template<>
483 struct hash<CLRX::CString>
484 {
486  typedef std::size_t result_type;
487 
489  size_t operator()(const CLRX::CString& s1) const
490  {
491  size_t hash = 0;
492  for (const char* p = s1.c_str(); *p != 0; p++)
493  hash = ((hash<<8)^(cxbyte)*p)*size_t(0xbf146a3dU);
494  return hash;
495  }
496 };
497 
498 }
499 
500 #endif
char * iterator
type of iterator
Definition: CString.h:41
CString & operator=(CString &&cstr) noexcept
move-assignment
Definition: CString.h:159
CString(size_t n)
constructor from C-style string pointer
Definition: CString.h:54
CString & operator=(std::initializer_list< char > init)
assignment
Definition: CString.h:176
CString & operator=(char ch)
assignment
Definition: CString.h:172
bool operator>(const CLRX::CString &s1, const CLRX::CString &s2)
greater operator
Definition: CString.h:361
CLRX::CString argument_type
argument type
Definition: CString.h:485
CString(const CString &cstr)
copy-constructor
Definition: CString.h:117
const char * begin() const
return C-style string pointer
Definition: CString.h:256
CString & operator=(const char *str)
assignment
Definition: CString.h:168
char & front()
first character (use only if string is not empty)
Definition: CString.h:300
const char * c_str() const
return C-style string pointer
Definition: CString.h:252
CString(size_t n, char ch)
constructor
Definition: CString.h:107
bool operator!=(const CLRX::CString &s1, const CLRX::CString &s2)
not-equal operator
Definition: CString.h:353
size_type find(char ch, size_t pos=0) const
find character in string
Definition: CString.h:316
char * begin()
return C-style string pointer
Definition: CString.h:268
void swap(CString &s2) noexcept
swap this string with another
Definition: CString.h:344
CString(const char *str, const char *end)
constructor
Definition: CString.h:96
std::string::size_type size_type
size type
Definition: CString.h:44
STL namespace.
size_t operator()(const CLRX::CString &s1) const
a calling operator
Definition: CString.h:489
const char & operator[](size_t i) const
get ith character (use only if string is not empty)
Definition: CString.h:260
const char & front() const
first character (use only if string is not empty)
Definition: CString.h:296
CString(std::initializer_list< char > init)
constructor
Definition: CString.h:132
Configuration header.
bool operator>=(const CLRX::CString &s1, const CLRX::CString &s2)
greater or equal operator
Definition: CString.h:369
CString & assign(const char *str)
assign string
Definition: CString.h:180
int compare(size_t pos, size_t n, const char *str) const
compare with string
Definition: CString.h:312
const char * const_iterator
type of constant iterator
Definition: CString.h:42
CString & operator=(const CString &cstr)
copy-assignment
Definition: CString.h:147
CString(const std::string &str)
constructor from C++ std::string
Definition: CString.h:75
char & operator[](size_t i)
get ith character (use only if string is not empty)
Definition: CString.h:264
unsigned char cxbyte
unsigned byte
Definition: Config.h:215
std::ostream & operator<<(std::ostream &os, const CLRX::CString &cstr)
push to output stream as string
Definition: CString.h:471
main namespace
Definition: AsmDefs.h:38
bool operator==(const CLRX::CString &s1, const CLRX::CString &s2)
equal operator
Definition: CString.h:349
int compare(const CString &cstr) const
compare with string
Definition: CString.h:304
static const size_type npos
value to indicate no position
Definition: CString.h:45
bool operator<(const CLRX::CString &s1, const CLRX::CString &s2)
less operator
Definition: CString.h:357
CString(const char *str)
constructor from C-style string pointer
Definition: CString.h:62
size_type find(const CString &str, size_t pos=0) const
find string in string
Definition: CString.h:324
CString(const char *str, size_t n)
constructor
Definition: CString.h:86
bool operator<=(const CLRX::CString &s1, const CLRX::CString &s2)
less or equal operator
Definition: CString.h:365
std::size_t result_type
result type
Definition: CString.h:486
size_t length() const
compute size
Definition: CString.h:278
~CString()
destructor
Definition: CString.h:143
CString & assign(std::initializer_list< char > init)
assign string
Definition: CString.h:233
CString & assign(size_t n, char ch)
assign string
Definition: CString.h:215
CString & operator=(const std::string &str)
assignment
Definition: CString.h:155
CString substr(size_t pos, size_t n) const
make substring from string
Definition: CString.h:340
char element_type
element type
Definition: CString.h:43
CString()
constructor
Definition: CString.h:50
bool empty() const
return true if string is empty
Definition: CString.h:292
size_t size() const
compute size
Definition: CString.h:272
int compare(const char *str) const
compare with string
Definition: CString.h:308
CString & assign(const char *str, const char *end)
assign string
Definition: CString.h:211
simple C-string container
Definition: CString.h:38
size_type find(const char *str, size_t pos=0) const
find string in string
Definition: CString.h:332
CString(CString &&cstr) noexcept
move-constructor
Definition: CString.h:128
void clear()
clear this string
Definition: CString.h:285
CString & assign(const char *str, size_t n)
assign string
Definition: CString.h:193