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  { // just clear
197  delete[] ptr;
198  ptr = nullptr;
199  return *this;
200  }
201  char* newPtr = new char[n+1];
202  ::memcpy(newPtr, str, n);
203  newPtr[n] = 0;
204  delete[] ptr;
205  ptr = newPtr;
206  return *this;
207  }
208 
210  CString& assign(const char* str, const char* end)
211  { return assign(str, end-str); }
212 
214  CString& assign (size_t n, char ch)
215  {
216  if (n == 0)
217  { // just clear
218  delete[] ptr;
219  ptr = nullptr;
220  return *this;
221  }
222  char* newPtr = new char[n+1];
223  ::memset(newPtr, ch, n);
224  newPtr[n] = 0; // null-char
225  delete[] ptr;
226  ptr = newPtr;
227  return *this;
228  }
229 
231  CString& assign(std::initializer_list<char> init)
232  {
233  const size_t n = init.size();
234  if (n == 0)
235  { // just clear
236  delete[] ptr;
237  ptr = nullptr;
238  return *this;
239  }
240  char* newPtr = new char[n+1];
241  std::copy(init.begin(), init.end(), newPtr);
242  newPtr[n] = 0; // null char
243  delete[] ptr;
244  ptr = newPtr;
245  return *this;
246  }
247 
249  const char* c_str() const
250  { return ptr!=nullptr ? ptr : ""; }
251 
253  const char* begin() const
254  { return ptr!=nullptr ? ptr : ""; }
255 
257  const char& operator[](size_t i) const
258  { return ptr[i]; }
259 
261  char& operator[](size_t i)
262  { return ptr[i]; }
263 
265  char* begin()
266  { return ptr; }
267 
269  size_t size() const
270  {
271  if (ptr==nullptr) return 0;
272  return ::strlen(ptr);
273  }
275  size_t length() const
276  {
277  if (ptr==nullptr) return 0;
278  return ::strlen(ptr);
279  }
280 
282  void clear()
283  {
284  delete[] ptr;
285  ptr = nullptr;
286  }
287 
289  bool empty() const
290  { return ptr==nullptr; }
291 
293  const char& front() const
294  { return ptr[0]; }
295 
297  char& front()
298  { return ptr[0]; }
299 
301  int compare(const CString& cstr) const
302  { return ::strcmp(c_str(), cstr.c_str()); }
303 
305  int compare(const char* str) const
306  { return ::strcmp(c_str(), str); }
307 
309  int compare(size_t pos, size_t n, const char* str) const
310  { return ::strncmp(c_str()+pos, str, n); }
311 
313  size_type find(char ch, size_t pos = 0) const
314  {
315  const char* th = c_str();
316  const char* p = ::strchr(th+pos, ch);
317  return (p!=nullptr) ? p-th : npos;
318  }
319 
321  size_type find(const CString& str, size_t pos = 0) const
322  {
323  const char* th = c_str();
324  const char* p = ::strstr(th+pos, str.c_str());
325  return (p!=nullptr) ? p-th : npos;
326  }
327 
329  size_type find(const char* str, size_t pos = 0) const
330  {
331  const char* th = c_str();
332  const char* p = ::strstr(th+pos, str);
333  return (p!=nullptr) ? p-th : npos;
334  }
335 
337  CString substr(size_t pos, size_t n) const
338  { return CString(c_str()+pos, n); }
339 
341  void swap(CString& s2) noexcept
342  { std::swap(ptr, s2.ptr); }
343 };
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 
366 inline bool operator>=(const CLRX::CString& s1, const CLRX::CString& s2)
367 { return ::strcmp(s1.c_str(), s2.c_str())>=0; }
368 
369 }
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 CLRX::CString& s1, const std::string& 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 std::string& s1, const CLRX::CString& s2)
417 { return ::strcmp(s1.c_str(), s2.c_str())>=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 CLRX::CString& s1, const char* s2)
441 { return ::strcmp(s1.c_str(), s2)>=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 bool operator>=(const char* s1, const CLRX::CString& s2)
465 { return ::strcmp(s1, s2.c_str())>=0; }
466 
468 inline std::ostream& operator<<(std::ostream& os, const CLRX::CString& cstr)
469 { return os<<cstr.c_str(); }
470 
471 namespace std
472 {
473 
475 inline void swap(CLRX::CString& s1, CLRX::CString& s2)
476 { s1.swap(s2); }
477 
479 template<>
480 struct hash<CLRX::CString>
481 {
483  typedef std::size_t result_type;
484 
486  size_t operator()(const CLRX::CString& s1) const
487  {
488  size_t hash = 0;
489  for (const char* p = s1.c_str(); *p != 0; p++)
490  hash = ((hash<<8)^(cxbyte)*p)*size_t(0xbf146a3dU);
491  return hash;
492  }
493 };
494 
495 }
496 
497 #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:358
CLRX::CString argument_type
argument type
Definition: CString.h:482
CString(const CString &cstr)
copy-constructor
Definition: CString.h:117
const char * begin() const
return C-style string pointer
Definition: CString.h:253
CString & operator=(const char *str)
assignment
Definition: CString.h:168
char & front()
first character (use only if string is not empty)
Definition: CString.h:297
const char * c_str() const
return C-style string pointer
Definition: CString.h:249
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:350
size_type find(char ch, size_t pos=0) const
find character in string
Definition: CString.h:313
char * begin()
return C-style string pointer
Definition: CString.h:265
void swap(CString &s2) noexcept
swap this string with another
Definition: CString.h:341
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:486
const char & operator[](size_t i) const
get ith character (use only if string is not empty)
Definition: CString.h:257
const char & front() const
first character (use only if string is not empty)
Definition: CString.h:293
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:366
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:309
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:261
unsigned char cxbyte
unsigned byte
Definition: Config.h:213
std::ostream & operator<<(std::ostream &os, const CLRX::CString &cstr)
push to output stream as string
Definition: CString.h:468
main namespace
Definition: AsmDefs.h:38
bool operator==(const CLRX::CString &s1, const CLRX::CString &s2)
equal operator
Definition: CString.h:346
int compare(const CString &cstr) const
compare with string
Definition: CString.h:301
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:354
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:321
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:362
std::size_t result_type
result type
Definition: CString.h:483
size_t length() const
compute size
Definition: CString.h:275
~CString()
destructor
Definition: CString.h:143
CString & assign(std::initializer_list< char > init)
assign string
Definition: CString.h:231
CString & assign(size_t n, char ch)
assign string
Definition: CString.h:214
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:337
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:289
size_t size() const
compute size
Definition: CString.h:269
int compare(const char *str) const
compare with string
Definition: CString.h:305
CString & assign(const char *str, const char *end)
assign string
Definition: CString.h:210
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:329
CString(CString &&cstr) noexcept
move-constructor
Definition: CString.h:128
void clear()
clear this string
Definition: CString.h:282
CString & assign(const char *str, size_t n)
assign string
Definition: CString.h:193