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-2018 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  if (this==&cstr)
162  return *this;
163  delete[] ptr; // delete old
164  ptr = cstr.ptr;
165  cstr.ptr = nullptr;
166  return *this;
167  }
168 
170  CString& operator=(const char* str)
171  { return assign(str); }
172 
174  CString& operator=(char ch)
175  { return assign(1, ch); }
176 
178  CString& operator=(std::initializer_list<char> init)
179  { return assign(init); }
180 
182  CString& assign(const char* str)
183  {
184  if (str==nullptr)
185  {
186  delete[] ptr;
187  ptr = nullptr;
188  return *this;
189  }
190  size_t length = ::strlen(str);
191  return assign(str, length);
192  }
193 
195  CString& assign(const char* str, size_t n)
196  {
197  if (n == 0)
198  {
199  // just clear
200  delete[] ptr;
201  ptr = nullptr;
202  return *this;
203  }
204  char* newPtr = new char[n+1];
205  ::memcpy(newPtr, str, n);
206  newPtr[n] = 0;
207  delete[] ptr;
208  ptr = newPtr;
209  return *this;
210  }
211 
213  CString& assign(const char* str, const char* end)
214  { return assign(str, end-str); }
215 
217  CString& assign (size_t n, char ch)
218  {
219  if (n == 0)
220  {
221  // just clear
222  delete[] ptr;
223  ptr = nullptr;
224  return *this;
225  }
226  char* newPtr = new char[n+1];
227  ::memset(newPtr, ch, n);
228  newPtr[n] = 0; // null-char
229  delete[] ptr;
230  ptr = newPtr;
231  return *this;
232  }
233 
235  CString& assign(std::initializer_list<char> init)
236  {
237  const size_t n = init.size();
238  if (n == 0)
239  {
240  // just clear
241  delete[] ptr;
242  ptr = nullptr;
243  return *this;
244  }
245  char* newPtr = new char[n+1];
246  std::copy(init.begin(), init.end(), newPtr);
247  newPtr[n] = 0; // null char
248  delete[] ptr;
249  ptr = newPtr;
250  return *this;
251  }
252 
254  const char* c_str() const
255  { return ptr!=nullptr ? ptr : ""; }
256 
258  const char* begin() const
259  { return ptr!=nullptr ? ptr : ""; }
260 
262  const char& operator[](size_t i) const
263  { return ptr[i]; }
264 
266  char& operator[](size_t i)
267  { return ptr[i]; }
268 
270  char* begin()
271  { return ptr; }
272 
274  size_t size() const
275  {
276  if (ptr==nullptr) return 0;
277  return ::strlen(ptr);
278  }
280  size_t length() const
281  {
282  if (ptr==nullptr) return 0;
283  return ::strlen(ptr);
284  }
285 
287  void clear()
288  {
289  delete[] ptr;
290  ptr = nullptr;
291  }
292 
294  bool empty() const
295  { return ptr==nullptr; }
296 
298  const char& front() const
299  { return ptr[0]; }
300 
302  char& front()
303  { return ptr[0]; }
304 
306  int compare(const CString& cstr) const
307  { return ::strcmp(c_str(), cstr.c_str()); }
308 
310  int compare(const char* str) const
311  { return ::strcmp(c_str(), str); }
312 
314  int compare(size_t pos, size_t n, const char* str) const
315  { return ::strncmp(c_str()+pos, str, n); }
316 
318  size_type find(char ch, size_t pos = 0) const
319  {
320  const char* th = c_str();
321  const char* p = ::strchr(th+pos, ch);
322  return (p!=nullptr) ? p-th : npos;
323  }
324 
326  size_type find(const CString& str, size_t pos = 0) const
327  {
328  const char* th = c_str();
329  const char* p = ::strstr(th+pos, str.c_str());
330  return (p!=nullptr) ? p-th : npos;
331  }
332 
334  size_type find(const char* str, size_t pos = 0) const
335  {
336  const char* th = c_str();
337  const char* p = ::strstr(th+pos, str);
338  return (p!=nullptr) ? p-th : npos;
339  }
340 
342  CString substr(size_t pos, size_t n) const
343  { return CString(c_str()+pos, n); }
344 
346  void swap(CString& s2) noexcept
347  { std::swap(ptr, s2.ptr); }
348 };
349 
351 inline bool operator==(const CLRX::CString& s1, const CLRX::CString& s2)
352 { return ::strcmp(s1.c_str(), s2.c_str())==0; }
353 
355 inline bool operator!=(const CLRX::CString& s1, const CLRX::CString& s2)
356 { return ::strcmp(s1.c_str(), s2.c_str())!=0; }
357 
359 inline bool operator<(const CLRX::CString& s1, const CLRX::CString& s2)
360 { return ::strcmp(s1.c_str(), s2.c_str())<0; }
361 
363 inline bool operator>(const CLRX::CString& s1, const CLRX::CString& s2)
364 { return ::strcmp(s1.c_str(), s2.c_str())>0; }
365 
367 inline bool operator<=(const CLRX::CString& s1, const CLRX::CString& s2)
368 { return ::strcmp(s1.c_str(), s2.c_str())<=0; }
369 
371 inline bool operator>=(const CLRX::CString& s1, const CLRX::CString& s2)
372 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
373 
374 }
375 
377 inline bool operator==(const CLRX::CString& s1, const std::string& s2)
378 { return ::strcmp(s1.c_str(), s2.c_str())==0; }
379 
381 inline bool operator!=(const CLRX::CString& s1, const std::string& s2)
382 { return ::strcmp(s1.c_str(), s2.c_str())!=0; }
383 
385 inline bool operator<(const CLRX::CString& s1, const std::string& s2)
386 { return ::strcmp(s1.c_str(), s2.c_str())<0; }
387 
389 inline bool operator>(const CLRX::CString& s1, const std::string& s2)
390 { return ::strcmp(s1.c_str(), s2.c_str())>0; }
391 
393 inline bool operator<=(const CLRX::CString& s1, const std::string& s2)
394 { return ::strcmp(s1.c_str(), s2.c_str())<=0; }
395 
397 inline bool operator>=(const CLRX::CString& s1, const std::string& s2)
398 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
399 
401 inline bool operator==(const std::string& s1, const CLRX::CString& s2)
402 { return ::strcmp(s1.c_str(), s2.c_str())==0; }
403 
405 inline bool operator!=(const std::string& s1, const CLRX::CString& s2)
406 { return ::strcmp(s1.c_str(), s2.c_str())!=0; }
407 
409 inline bool operator<(const std::string& s1, const CLRX::CString& s2)
410 { return ::strcmp(s1.c_str(), s2.c_str())<0; }
411 
413 inline bool operator>(const std::string& s1, const CLRX::CString& s2)
414 { return ::strcmp(s1.c_str(), s2.c_str())>0; }
415 
417 inline bool operator<=(const std::string& s1, const CLRX::CString& s2)
418 { return ::strcmp(s1.c_str(), s2.c_str())<=0; }
419 
421 inline bool operator>=(const std::string& s1, const CLRX::CString& s2)
422 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
423 
425 inline bool operator==(const CLRX::CString& s1, const char* s2)
426 { return ::strcmp(s1.c_str(), s2)==0; }
427 
429 inline bool operator!=(const CLRX::CString& s1, const char* s2)
430 { return ::strcmp(s1.c_str(), s2)!=0; }
431 
433 inline bool operator<(const CLRX::CString& s1, const char* s2)
434 { return ::strcmp(s1.c_str(), s2)<0; }
435 
437 inline bool operator>(const CLRX::CString& s1, const char* s2)
438 { return ::strcmp(s1.c_str(), s2)>0; }
439 
441 inline bool operator<=(const CLRX::CString& s1, const char* s2)
442 { return ::strcmp(s1.c_str(), s2)<=0; }
443 
445 inline bool operator>=(const CLRX::CString& s1, const char* s2)
446 { return ::strcmp(s1.c_str(), s2)>=0; }
447 
449 inline bool operator==(const char* s1, const CLRX::CString& s2)
450 { return ::strcmp(s1, s2.c_str())==0; }
451 
453 inline bool operator!=(const char* s1, const CLRX::CString& s2)
454 { return ::strcmp(s1, s2.c_str())!=0; }
455 
457 inline bool operator<(const char* s1, const CLRX::CString& s2)
458 { return ::strcmp(s1, s2.c_str())<0; }
459 
461 inline bool operator>(const char* s1, const CLRX::CString& s2)
462 { return ::strcmp(s1, s2.c_str())>0; }
463 
465 inline bool operator<=(const char* s1, const CLRX::CString& s2)
466 { return ::strcmp(s1, s2.c_str())<=0; }
467 
469 inline bool operator>=(const char* s1, const CLRX::CString& s2)
470 { return ::strcmp(s1, s2.c_str())>=0; }
471 
473 inline std::ostream& operator<<(std::ostream& os, const CLRX::CString& cstr)
474 { return os<<cstr.c_str(); }
475 
476 namespace std
477 {
478 
480 inline void swap(CLRX::CString& s1, CLRX::CString& s2)
481 { s1.swap(s2); }
482 
484 template<>
485 struct hash<CLRX::CString>
486 {
488  typedef std::size_t result_type;
489 
491  size_t operator()(const CLRX::CString& s1) const
492  {
493  size_t hash = 0;
494  for (const char* p = s1.c_str(); *p != 0; p++)
495  hash = ((hash<<8)^(cxbyte)*p)*size_t(0xbf146a3dU);
496  return hash;
497  }
498 };
499 
500 }
501 
502 #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:178
CString & operator=(char ch)
assignment
Definition: CString.h:174
bool operator>(const CLRX::CString &s1, const CLRX::CString &s2)
greater operator
Definition: CString.h:363
CLRX::CString argument_type
argument type
Definition: CString.h:487
CString(const CString &cstr)
copy-constructor
Definition: CString.h:117
const char * begin() const
return C-style string pointer
Definition: CString.h:258
CString & operator=(const char *str)
assignment
Definition: CString.h:170
char & front()
first character (use only if string is not empty)
Definition: CString.h:302
const char * c_str() const
return C-style string pointer
Definition: CString.h:254
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:355
size_type find(char ch, size_t pos=0) const
find character in string
Definition: CString.h:318
char * begin()
return C-style string pointer
Definition: CString.h:270
void swap(CString &s2) noexcept
swap this string with another
Definition: CString.h:346
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:491
const char & operator[](size_t i) const
get ith character (use only if string is not empty)
Definition: CString.h:262
const char & front() const
first character (use only if string is not empty)
Definition: CString.h:298
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:371
CString & assign(const char *str)
assign string
Definition: CString.h:182
int compare(size_t pos, size_t n, const char *str) const
compare with string
Definition: CString.h:314
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:266
unsigned char cxbyte
unsigned byte
Definition: Config.h:229
std::ostream & operator<<(std::ostream &os, const CLRX::CString &cstr)
push to output stream as string
Definition: CString.h:473
main namespace
Definition: AsmDefs.h:38
bool operator==(const CLRX::CString &s1, const CLRX::CString &s2)
equal operator
Definition: CString.h:351
int compare(const CString &cstr) const
compare with string
Definition: CString.h:306
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:359
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:326
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:367
std::size_t result_type
result type
Definition: CString.h:488
size_t length() const
compute size
Definition: CString.h:280
~CString()
destructor
Definition: CString.h:143
CString & assign(std::initializer_list< char > init)
assign string
Definition: CString.h:235
CString & assign(size_t n, char ch)
assign string
Definition: CString.h:217
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:342
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:294
size_t size() const
compute size
Definition: CString.h:274
int compare(const char *str) const
compare with string
Definition: CString.h:310
CString & assign(const char *str, const char *end)
assign string
Definition: CString.h:213
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:334
CString(CString &&cstr) noexcept
move-constructor
Definition: CString.h:128
void clear()
clear this string
Definition: CString.h:287
CString & assign(const char *str, size_t n)
assign string
Definition: CString.h:195