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-2016 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  { return assign(cstr.ptr); }
149 
151  CString& operator=(const std::string& str)
152  { return assign(str.c_str(), str.size()); }
153 
155  CString& operator=(CString&& cstr) noexcept
156  {
157  delete[] ptr; // delete old
158  ptr = cstr.ptr;
159  cstr.ptr = nullptr;
160  return *this;
161  }
162 
164  CString& operator=(const char* str)
165  { return assign(str); }
166 
168  CString& operator=(char ch)
169  { return assign(1, ch); }
170 
172  CString& operator=(std::initializer_list<char> init)
173  { return assign(init); }
174 
176  CString& assign(const char* str)
177  {
178  if (str==nullptr)
179  {
180  delete[] ptr;
181  ptr = nullptr;
182  return *this;
183  }
184  size_t length = ::strlen(str);
185  return assign(str, length);
186  }
187 
189  CString& assign(const char* str, size_t n)
190  {
191  if (n == 0)
192  { // just clear
193  delete[] ptr;
194  ptr = nullptr;
195  return *this;
196  }
197  char* newPtr = new char[n+1];
198  ::memcpy(newPtr, str, n);
199  newPtr[n] = 0;
200  delete[] ptr;
201  ptr = newPtr;
202  return *this;
203  }
204 
206  CString& assign(const char* str, const char* end)
207  { return assign(str, end-str); }
208 
210  CString& assign (size_t n, char ch)
211  {
212  if (n == 0)
213  { // just clear
214  delete[] ptr;
215  ptr = nullptr;
216  return *this;
217  }
218  char* newPtr = new char[n+1];
219  ::memset(newPtr, ch, n);
220  newPtr[n] = 0; // null-char
221  delete[] ptr;
222  ptr = newPtr;
223  return *this;
224  }
225 
227  CString& assign(std::initializer_list<char> init)
228  {
229  const size_t n = init.size();
230  if (n == 0)
231  { // just clear
232  delete[] ptr;
233  ptr = nullptr;
234  return *this;
235  }
236  char* newPtr = new char[n+1];
237  std::copy(init.begin(), init.end(), newPtr);
238  newPtr[n] = 0; // null char
239  delete[] ptr;
240  ptr = newPtr;
241  return *this;
242  }
243 
245  const char* c_str() const
246  { return ptr!=nullptr ? ptr : ""; }
247 
249  const char* begin() const
250  { return ptr!=nullptr ? ptr : ""; }
251 
253  const char& operator[](size_t i) const
254  { return ptr[i]; }
255 
257  char& operator[](size_t i)
258  { return ptr[i]; }
259 
261  char* begin()
262  { return ptr; }
263 
265  size_t size() const
266  {
267  if (ptr==nullptr) return 0;
268  return ::strlen(ptr);
269  }
271  size_t length() const
272  {
273  if (ptr==nullptr) return 0;
274  return ::strlen(ptr);
275  }
276 
278  void clear()
279  {
280  delete[] ptr;
281  ptr = nullptr;
282  }
283 
285  bool empty() const
286  { return ptr==nullptr; }
287 
289  const char& front() const
290  { return ptr[0]; }
291 
293  char& front()
294  { return ptr[0]; }
295 
297  int compare(const CString& cstr) const
298  { return ::strcmp(c_str(), cstr.c_str()); }
299 
301  int compare(const char* str) const
302  { return ::strcmp(c_str(), str); }
303 
305  int compare(size_t pos, size_t n, const char* str) const
306  { return ::strncmp(c_str()+pos, str, n); }
307 
309  size_type find(char ch, size_t pos = 0) const
310  {
311  const char* th = c_str();
312  const char* p = ::strchr(th+pos, ch);
313  return (p!=nullptr) ? p-th : npos;
314  }
315 
317  size_type find(const CString& str, size_t pos = 0) const
318  {
319  const char* th = c_str();
320  const char* p = ::strstr(th+pos, str.c_str());
321  return (p!=nullptr) ? p-th : npos;
322  }
323 
325  size_type find(const char* str, size_t pos = 0) const
326  {
327  const char* th = c_str();
328  const char* p = ::strstr(th+pos, str);
329  return (p!=nullptr) ? p-th : npos;
330  }
331 
333  CString substr(size_t pos, size_t n) const
334  { return CString(c_str()+pos, n); }
335 
337  void swap(CString& s2) noexcept
338  { std::swap(ptr, s2.ptr); }
339 };
340 
342 inline bool operator==(const CLRX::CString& s1, const CLRX::CString& s2)
343 { return ::strcmp(s1.c_str(), s2.c_str())==0; }
344 
346 inline bool operator!=(const CLRX::CString& s1, const CLRX::CString& s2)
347 { return ::strcmp(s1.c_str(), s2.c_str())!=0; }
348 
350 inline bool operator<(const CLRX::CString& s1, const CLRX::CString& s2)
351 { return ::strcmp(s1.c_str(), s2.c_str())<0; }
352 
354 inline bool operator>(const CLRX::CString& s1, const CLRX::CString& s2)
355 { return ::strcmp(s1.c_str(), s2.c_str())>0; }
356 
358 inline bool operator<=(const CLRX::CString& s1, const CLRX::CString& s2)
359 { return ::strcmp(s1.c_str(), s2.c_str())<=0; }
360 
362 inline bool operator>=(const CLRX::CString& s1, const CLRX::CString& s2)
363 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
364 
365 }
366 
368 inline bool operator==(const CLRX::CString& s1, const std::string& s2)
369 { return ::strcmp(s1.c_str(), s2.c_str())==0; }
370 
372 inline bool operator!=(const CLRX::CString& s1, const std::string& s2)
373 { return ::strcmp(s1.c_str(), s2.c_str())!=0; }
374 
376 inline bool operator<(const CLRX::CString& s1, const std::string& s2)
377 { return ::strcmp(s1.c_str(), s2.c_str())<0; }
378 
380 inline bool operator>(const CLRX::CString& s1, const std::string& s2)
381 { return ::strcmp(s1.c_str(), s2.c_str())>0; }
382 
384 inline bool operator<=(const CLRX::CString& s1, const std::string& s2)
385 { return ::strcmp(s1.c_str(), s2.c_str())<=0; }
386 
388 inline bool operator>=(const CLRX::CString& s1, const std::string& s2)
389 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
390 
392 inline bool operator==(const std::string& s1, const CLRX::CString& s2)
393 { return ::strcmp(s1.c_str(), s2.c_str())==0; }
394 
396 inline bool operator!=(const std::string& s1, const CLRX::CString& s2)
397 { return ::strcmp(s1.c_str(), s2.c_str())!=0; }
398 
400 inline bool operator<(const std::string& s1, const CLRX::CString& s2)
401 { return ::strcmp(s1.c_str(), s2.c_str())<0; }
402 
404 inline bool operator>(const std::string& s1, const CLRX::CString& s2)
405 { return ::strcmp(s1.c_str(), s2.c_str())>0; }
406 
408 inline bool operator<=(const std::string& s1, const CLRX::CString& s2)
409 { return ::strcmp(s1.c_str(), s2.c_str())<=0; }
410 
412 inline bool operator>=(const std::string& s1, const CLRX::CString& s2)
413 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
414 
416 inline bool operator==(const CLRX::CString& s1, const char* s2)
417 { return ::strcmp(s1.c_str(), s2)==0; }
418 
420 inline bool operator!=(const CLRX::CString& s1, const char* s2)
421 { return ::strcmp(s1.c_str(), s2)!=0; }
422 
424 inline bool operator<(const CLRX::CString& s1, const char* s2)
425 { return ::strcmp(s1.c_str(), s2)<0; }
426 
428 inline bool operator>(const CLRX::CString& s1, const char* s2)
429 { return ::strcmp(s1.c_str(), s2)>0; }
430 
432 inline bool operator<=(const CLRX::CString& s1, const char* s2)
433 { return ::strcmp(s1.c_str(), s2)<=0; }
434 
436 inline bool operator>=(const CLRX::CString& s1, const char* s2)
437 { return ::strcmp(s1.c_str(), s2)>=0; }
438 
440 inline bool operator==(const char* s1, const CLRX::CString& s2)
441 { return ::strcmp(s1, s2.c_str())==0; }
442 
444 inline bool operator!=(const char* s1, const CLRX::CString& s2)
445 { return ::strcmp(s1, s2.c_str())!=0; }
446 
448 inline bool operator<(const char* s1, const CLRX::CString& s2)
449 { return ::strcmp(s1, s2.c_str())<0; }
450 
452 inline bool operator>(const char* s1, const CLRX::CString& s2)
453 { return ::strcmp(s1, s2.c_str())>0; }
454 
456 inline bool operator<=(const char* s1, const CLRX::CString& s2)
457 { return ::strcmp(s1, s2.c_str())<=0; }
458 
460 inline bool operator>=(const char* s1, const CLRX::CString& s2)
461 { return ::strcmp(s1, s2.c_str())>=0; }
462 
464 inline std::ostream& operator<<(std::ostream& os, const CLRX::CString& cstr)
465 { return os<<cstr.c_str(); }
466 
467 namespace std
468 {
469 
471 inline void swap(CLRX::CString& s1, CLRX::CString& s2)
472 { s1.swap(s2); }
473 
475 template<>
476 struct hash<CLRX::CString>
477 {
479  typedef std::size_t result_type;
480 
482  size_t operator()(const CLRX::CString& s1) const
483  {
484  size_t hash = 0;
485  for (const char* p = s1.c_str(); *p != 0; p++)
486  hash = ((hash<<8)^(cxbyte)*p)*size_t(0xbf146a3dU);
487  return hash;
488  }
489 };
490 
491 }
492 
493 #endif
char * iterator
type of iterator
Definition: CString.h:41
CString & operator=(CString &&cstr) noexcept
move-assignment
Definition: CString.h:155
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:172
CString & operator=(char ch)
assignment
Definition: CString.h:168
bool operator>(const CLRX::CString &s1, const CLRX::CString &s2)
greater operator
Definition: CString.h:354
CLRX::CString argument_type
argument type
Definition: CString.h:478
CString(const CString &cstr)
copy-constructor
Definition: CString.h:117
const char * begin() const
return C-style string pointer
Definition: CString.h:249
CString & operator=(const char *str)
assignment
Definition: CString.h:164
char & front()
first character (use only if string is not empty)
Definition: CString.h:293
const char * c_str() const
return C-style string pointer
Definition: CString.h:245
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:346
size_type find(char ch, size_t pos=0) const
find character in string
Definition: CString.h:309
char * begin()
return C-style string pointer
Definition: CString.h:261
void swap(CString &s2) noexcept
swap this string with another
Definition: CString.h:337
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:482
const char & operator[](size_t i) const
get ith character (use only if string is not empty)
Definition: CString.h:253
const char & front() const
first character (use only if string is not empty)
Definition: CString.h:289
CString(std::initializer_list< char > init)
constructor
Definition: CString.h:132
bool operator>=(const CLRX::CString &s1, const CLRX::CString &s2)
greater or equal operator
Definition: CString.h:362
CString & assign(const char *str)
assign string
Definition: CString.h:176
int compare(size_t pos, size_t n, const char *str) const
compare with string
Definition: CString.h:305
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:257
std::ostream & operator<<(std::ostream &os, const CLRX::CString &cstr)
push to output stream as string
Definition: CString.h:464
main namespace
Definition: AsmFormats.h:41
bool operator==(const CLRX::CString &s1, const CLRX::CString &s2)
equal operator
Definition: CString.h:342
int compare(const CString &cstr) const
compare with string
Definition: CString.h:297
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:350
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:317
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:358
std::size_t result_type
result type
Definition: CString.h:479
size_t length() const
compute size
Definition: CString.h:271
~CString()
destructor
Definition: CString.h:143
CString & assign(std::initializer_list< char > init)
assign string
Definition: CString.h:227
CString & assign(size_t n, char ch)
assign string
Definition: CString.h:210
CString & operator=(const std::string &str)
assignment
Definition: CString.h:151
CString substr(size_t pos, size_t n) const
make substring from string
Definition: CString.h:333
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:285
size_t size() const
compute size
Definition: CString.h:265
int compare(const char *str) const
compare with string
Definition: CString.h:301
CString & assign(const char *str, const char *end)
assign string
Definition: CString.h:206
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:325
CString(CString &&cstr) noexcept
move-constructor
Definition: CString.h:128
void clear()
clear this string
Definition: CString.h:278
CString & assign(const char *str, size_t n)
assign string
Definition: CString.h:189