Doxygen
Loading...
Searching...
No Matches
dstring.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** Copyright (C) 1997-2015 by Dimitri van Heesch.
4**
5** Permission to use, copy, modify, and distribute this software and its
6** documentation under the terms of the GNU General Public License is hereby
7** granted. No representations are made about the suitability of this software
8** for any purpose. It is provided "as is" without express or implied warranty.
9** See the GNU General Public License for more details.
10**
11**********************************************************************/
12
13#ifndef DSTRING_H
14#define DSTRING_H
15
16#include <string>
17#include <string_view>
18#include <algorithm>
19
20#include <cctype>
21#include <cstring>
22#include <cstdio>
23#include <cstdlib>
24#include <cstdint>
25#include <ostream>
26
27#include "utf8.h"
28
29#define ASSERT(x) if ( !(x) )\
30 fprintf(stderr,"ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__)
31
32
33/*****************************************************************************
34 Safe and portable C string functions; extensions to standard string.h
35 *****************************************************************************/
36
37//! Returns a copy of a string \a s.
38//! Note that memory is passed to the caller, use dstrfree() to release.
39char *dstrdup( const char *s );
40//! Frees the memory allocated using dstrdup().
41void dstrfree( const char *s );
42
43//! Returns the length of string \a str, or 0 if a null pointer is passed.
44inline uint32_t dstrlen( const char *str )
45{ return str ? static_cast<uint32_t>(strlen(str)) : 0; }
46
47inline char *dstrcpy( char *dst, const char *src )
48{ return src ? strcpy(dst, src) : nullptr; }
49
50char *dstrncpy(char *dst,const char *src, size_t len);
51
52inline bool disempty( const char *s)
53{ return s==nullptr || *s=='\0'; }
54
55inline int dstrcmp( const char *str1, const char *str2 )
56{ return (str1 && str2) ? strcmp(str1,str2) : // both non-empty
57 (disempty(str1) && disempty(str2)) ? 0 : // both empty
58 disempty(str1) ? -1 : 1; // one empty, other non-empty
59}
60
61inline int dstrncmp( const char *str1, const char *str2, size_t len )
62{ return (str1 && str2) ? strncmp(str1,str2,len) : // both non-empty
63 (disempty(str1) && disempty(str2)) ? 0 : // both empty
64 disempty(str1) ? -1 : 1; // one empty other non-empty
65}
66
67inline bool disspace(char c)
68{ return c==' ' || c=='\t' || c=='\n' || c=='\r'; }
69
70int dstricmp( const char *str1, const char *str2 );
71
72inline int dstricmp_sort( const char *str1, const char *str2 )
73{
74 int result = dstricmp(str1,str2);
75 return result==0 ? dstrcmp(str1,str2) : result;
76}
77
78int dstrnicmp( const char *str1, const char *str2, size_t len );
79
80#ifndef DISABLE_JAVACC
81using JavaCCString = std::basic_string<JAVACC_CHAR_TYPE>;
82#endif
83
84/** A String class for use with Doxygen wrapping std::string and adding some additional
85 * functionality offered by QCString.
86 * QCString is part of qtools which was used as a portability layer in the past.
87 */
89{
90 public:
91 DString() = default;
92 DString(const DString &) = default;
93 DString &operator=(const DString &) = default;
94 DString(DString &&) = default;
95 DString &operator=(DString &&) = default;
96 ~DString() = default;
97
98 DString( const std::string &s ) : m_rep(s) {}
99
100 DString( std::string &&s) : m_rep(std::move(s)) {}
101
102 DString &operator=( std::string &&s)
103 {
104 m_rep=std::move(s);
105 return *this;
106 }
107
108 DString( std::string_view sv) : m_rep(sv) {}
109
110 DString &operator=(std::string_view sv)
111 {
112 m_rep=sv;
113 return *this;
114 }
115
116 DString( int ) = delete;
117
118 /** For converting a JavaCC string */
119#ifndef DISABLE_JAVACC
121 {
122 m_rep.resize(s.size());
123 std::memcpy(m_rep.data(),s.data(),s.size());
124 }
126 {
127 m_rep.resize(s.size());
128 std::memcpy(m_rep.data(),s.data(),s.size());
129 return *this;
130 }
131#endif
132
133 /** creates a string with room for size characters
134 * @param[in] size the number of character to allocate (also counting the 0-terminator!)
135 */
137 explicit DString( size_t size, SizeTag t) { m_rep.resize(size); }
138
139 /** creates a string from a plain C string.
140 * @param[in] str A zero terminated C string. When 0 an empty string is created.
141 */
142 DString( const char *str ) : m_rep(str?str:"") {}
143
144 /** creates a string from \a str and copies over the first \a maxlen characters. */
145 DString( const char *str, size_t maxlen ) : m_rep(str?str:"") { m_rep.resize(maxlen); }
146
147 /** replaces the contents by that of C string \a str. */
148 DString &operator=( const char *str) { m_rep = str?str:""; return *this; }
149
150 DString &operator=( const std::string &s) { m_rep = s; return *this; }
151
152 /** Returns true iff the string is empty (std::string compatible alias for isEmpty()) */
153 bool empty() const { return m_rep.empty(); }
154
155 /** Returns the length of the string, not counting the 0-terminator. Equivalent to size(). */
156 size_t length() const { return m_rep.size(); }
157
158 /** Returns the length of the string, not counting the 0-terminator. */
159 size_t size() const { return m_rep.size(); }
160
161 /** Returns a pointer to the contents of the string in the form of a 0-terminated C string */
162 const char *data() const { return m_rep.c_str(); }
163
164 /** Returns a pointer to the contents of the string in the form of a 0-terminated C string */
165 const char *c_str() const { return m_rep.c_str(); }
166
167 std::string_view view() const { return m_rep; }
168
169 /** Returns a writable pointer to the data.
170 */
171 char *rawData() { return &m_rep[0]; }
172
173 using value_type = std::string::value_type;
174 using size_type = std::string::size_type;
175 using reference = std::string::reference;
176 using const_reference = std::string::const_reference;
177 using iterator = std::string::iterator;
178 using const_iterator = std::string::const_iterator;
179 using reverse_iterator = std::string::reverse_iterator;
180 using const_reverse_iterator = std::string::const_reverse_iterator;
181
182 /** value used to indicate 'not found' or 'to the end of the string', matching std::string::npos */
183 static constexpr size_t npos = std::string::npos;
184
185 iterator begin() { return m_rep.begin(); }
186 const_iterator begin() const { return m_rep.begin(); }
187 const_iterator cbegin() const { return m_rep.cbegin(); }
188 iterator end() { return m_rep.end(); }
189 const_iterator end() const { return m_rep.end(); }
190 const_iterator cend() const { return m_rep.cend(); }
191
192 reverse_iterator rbegin() { return m_rep.rbegin(); }
193 const_reverse_iterator rbegin() const { return m_rep.rbegin(); }
194 const_reverse_iterator crbegin() const { return m_rep.crbegin(); }
195 reverse_iterator rend() { return m_rep.rend(); }
196 const_reverse_iterator rend() const { return m_rep.rend(); }
197 const_reverse_iterator crend() const { return m_rep.crend(); }
198
199 /** Returns a reference to the first character. Undefined behavior if the string is empty. */
200 char &front() { return m_rep.front(); }
201 const char &front() const { return m_rep.front(); }
202
203 /** Returns a reference to the last character. Undefined behavior if the string is empty. */
204 char &back() { return m_rep.back(); }
205 const char &back() const { return m_rep.back(); }
206
207 void push_back( char c ) { m_rep.push_back(c); }
208 void pop_back() { m_rep.pop_back(); }
209
210 size_t capacity() const { return m_rep.capacity(); }
211 size_t max_size() const { return m_rep.max_size(); }
212 void shrink_to_fit() { m_rep.shrink_to_fit(); }
213
214 void resize( size_t newlen) { m_rep.resize(newlen); }
215
216 /** Resizes the string to \a newlen characters, filling any new characters with \a c */
217 void resize( size_t newlen, char c ) { m_rep.resize(newlen,c); }
218
219 void clear() { m_rep.clear(); }
220
221 /** Reserve space for \a size bytes without changing the string contents */
222 void reserve( size_t size ) { m_rep.reserve(size); }
223
224 /** Swaps the contents of this string with \a other */
225 void swap( DString &other ) { m_rep.swap(other.m_rep); }
226
227 /** Returns a substring of length \a count starting at \a pos */
228 DString substr( size_t pos=0, size_t count=npos ) const { return DString(m_rep.substr(pos,count)); }
229
230 /** Copies (up to) \a count characters into \a dest, starting at \a pos. Returns the number of characters copied. */
231 size_t copy( char *dest, size_t count, size_t pos=0 ) const { return m_rep.copy(dest,count,pos); }
232
233 int compare( const DString &s ) const { return m_rep.compare(s.str()); }
234 int compare( const char *s ) const { return m_rep.compare(s?s:""); }
235 int compare( const std::string &s ) const { return m_rep.compare(s); }
236
237 DString &erase( size_t index=0, size_t count=npos ) { m_rep.erase(index,count); return *this; }
238
239 DString &assign( const char *s ) { return operator=(s); }
240 DString &assign( const DString &s ) { m_rep = s.str(); return *this; }
241 DString &assign( const std::string &s ) { return operator=(s); }
242 DString &assign( std::string_view s ) { return operator=(s); }
243
244 size_t find( char c, size_t pos=0 ) const { return m_rep.find(c,pos); }
245 size_t find( const char *s, size_t pos=0 ) const { return s?m_rep.find(s,pos):npos; }
246 size_t find( const DString &s, size_t pos=0 ) const { return m_rep.find(s.str(),pos); }
247 size_t find( const std::string &s, size_t pos=0 ) const { return m_rep.find(s,pos); }
248
249 size_t rfind( char c, size_t pos=npos ) const { return m_rep.rfind(c,pos); }
250 size_t rfind( const char *s, size_t pos=npos ) const { return s?m_rep.rfind(s,pos):npos; }
251 size_t rfind( const DString &s, size_t pos=npos ) const { return m_rep.rfind(s.str(),pos); }
252 size_t rfind( const std::string &s, size_t pos=npos ) const { return m_rep.rfind(s,pos); }
253
254 size_t rfind_insensitive( char c, size_t pos=npos) const;
255 size_t rfind_insensitive( const char *str, size_t pos=npos) const;
256
257 size_t find_first_of( char c, size_t pos=0 ) const { return m_rep.find_first_of(c,pos); }
258 size_t find_first_of( const char *s, size_t pos=0 ) const { return s?m_rep.find_first_of(s,pos):npos; }
259 size_t find_first_of( const DString &s, size_t pos=0 ) const { return m_rep.find_first_of(s.str(),pos); }
260 size_t find_first_of( const std::string &s, size_t pos=0 ) const { return m_rep.find_first_of(s,pos); }
261
262 size_t find_last_of( char c, size_t pos=npos ) const { return m_rep.find_last_of(c,pos); }
263 size_t find_last_of( const char *s, size_t pos=npos ) const { return s?m_rep.find_last_of(s,pos):npos; }
264 size_t find_last_of( const DString &s, size_t pos=npos ) const { return m_rep.find_last_of(s.str(),pos); }
265 size_t find_last_of( const std::string &s, size_t pos=npos ) const { return m_rep.find_last_of(s,pos); }
266
267 size_t find_first_not_of( char c, size_t pos=0 ) const { return m_rep.find_first_not_of(c,pos); }
268 size_t find_first_not_of( const char *s, size_t pos=0 ) const { return s?m_rep.find_first_not_of(s,pos):npos; }
269 size_t find_first_not_of( const DString &s, size_t pos=0 ) const { return m_rep.find_first_not_of(s.str(),pos); }
270 size_t find_first_not_of( const std::string &s, size_t pos=0 ) const { return m_rep.find_first_not_of(s,pos); }
271
272 size_t find_last_not_of( char c, size_t pos=npos ) const { return m_rep.find_last_not_of(c,pos); }
273 size_t find_last_not_of( const char *s, size_t pos=npos ) const { return s?m_rep.find_last_not_of(s,pos):npos; }
274 size_t find_last_not_of( const DString &s, size_t pos=npos ) const { return m_rep.find_last_not_of(s.str(),pos); }
275 size_t find_last_not_of( const std::string &s, size_t pos=npos ) const { return m_rep.find_last_not_of(s,pos); }
276
277 /** Fills a string with a predefined character
278 * @param[in] c the character used to fill the string with.
279 * @param[in] len the number of character to fill. Use -1 to fill the whole string.
280 * @note the string will be resized to contain \a len characters. The contents of the
281 * string will be lost.
282 */
283 DString fill( char c, int len = -1 )
284 {
285 int l = len==-1 ? static_cast<int>(m_rep.size()) : len;
286 m_rep = std::string(l,c);
287 return *this;
288 }
289
290 DString &sprintf( const char *format, ... );
291
292 int contains( char c, bool cs=true ) const;
293 int contains( const char *str, bool cs=true ) const;
294
296 {
297 if (prefix.empty() || m_rep.empty()) return false;
298 if (m_rep.rfind(prefix.data(),0)==0) // string starts with prefix
299 {
300 m_rep.erase(0,prefix.length());
301 return true;
302 }
303 return false;
304 }
305
306 bool stripPrefix(const char *prefix)
307 {
308 return stripPrefix(DString(prefix));
309 }
310
311 DString left( size_t len ) const
312 {
313 return m_rep.empty() ? DString() : DString(m_rep.substr(0,len));
314 }
315
316 DString right( size_t len ) const
317 {
318 return m_rep.empty() ? DString() :
319 len<m_rep.size() ? DString(m_rep.substr(m_rep.size()-len,len)) :
320 *this;
321 }
322
323 DString mid( size_t index, size_t len=npos ) const
324 {
325 size_t slen = m_rep.size();
326 if (len==npos) len = slen-index;
327 return m_rep.empty() || index>slen || len==0 ? DString() :
328 DString(m_rep.substr(index,len));
329 }
330
332 {
334 }
335
337 {
339 }
340
341 /// returns a copy of this string with leading and trailing whitespace removed
343 {
344 size_t sl = m_rep.size();
345 if (sl==0 || (!disspace(m_rep[0]) && !disspace(m_rep[sl-1]))) return *this;
346 size_t start=0,end=sl-1;
347 while (start<sl && disspace(m_rep[start])) start++;
348 if (start==sl) return DString(); // only whitespace
349 while (end>start && disspace(m_rep[end])) end--;
350 return DString(m_rep.substr(start,1+end-start));
351 }
352
354
355 // Returns a quoted copy of this string, unless it is already quoted.
356 // Note that trailing and leading whitespace is removed.
358 {
359 size_t start=0, sl=m_rep.size(), end=sl-1;
360 while (start<sl && disspace(m_rep[start])) start++; // skip over leading whitespace
361 if (start==sl) return DString(); // only whitespace
362 while (end>start && disspace(m_rep[end])) end--; // skip over trailing whitespace
363 bool needsQuotes=false;
364 size_t i=start;
365 if (i<end && m_rep[i]!='"') // stripped string has at least non-whitespace unquoted character
366 {
367 while (i<end && !needsQuotes) // check if the to be quoted part has at least one whitespace character
368 {
369 needsQuotes = m_rep[i] =='-';
370 needsQuotes |= disspace(m_rep[i++]);
371 }
372 }
373 DString result(m_rep.substr(start,1+end-start));
374 if (needsQuotes)
375 {
376 result.prepend("\"");
377 result.append("\"");
378 }
379 return result;
380 }
381
382 /// returns a copy of this string with all whitespace removed
384 {
385 size_t sl = m_rep.size();
386 if (sl==0) return *this;
387 std::string result = m_rep;
388 size_t src=0,dst=0;
389 while (src<sl)
390 {
391 if (!disspace(m_rep[src])) result[dst++]=m_rep[src];
392 src++;
393 }
394 if (dst<m_rep.size()) result.resize(dst);
395 return DString(result);
396 }
397
398 /// return a copy of this string with leading and trailing whitespace removed and multiple
399 /// whitespace characters replaced by a single space
401
402 // Returns a copy of this string repeated n times
403 DString repeat(unsigned int n) const
404 {
405 DString result(n * size(), ExplicitSize);
406 size_t offset = 0;
407 for (offset = 0; offset < n * size(); offset += size())
408 {
409 memcpy(result.rawData() + offset, data(), size());
410 }
411 return result;
412 }
413
414 DString &insert( size_t index, const DString &s )
415 {
416 if (s.length()>0)
417 {
418 size_t ol = m_rep.size();
419 if (index>ol) // insert beyond end of string and fill gap with spaces
420 {
421 m_rep.resize(index+s.length());
422 std::memset(&m_rep[ol],' ',index-ol);
423 std::memcpy(&m_rep[index],s.data(),s.length()+1);
424 }
425 else // insert inside the string
426 {
427 m_rep.insert(index,s.str());
428 }
429 }
430 return *this;
431 }
432
433 DString &insert( size_t index, std::string_view s)
434 {
435 if (s.length()>0)
436 {
437 size_t ol = m_rep.size();
438 if (index>ol) // insert beyond end of string and fill gap with spaces
439 {
440 m_rep.resize(index+s.length());
441 std::memset(&m_rep[ol],' ',index-ol);
442 std::memcpy(&m_rep[index],s.data(),s.length()+1);
443 }
444 else // insert inside the string
445 {
446 m_rep.insert(index,s);
447 }
448 }
449 return *this;
450 }
451
452 DString &insert( size_t index, const char *s )
453 {
454 size_t len = s ? dstrlen(s) : 0;
455 if (len>0)
456 {
457 size_t ol = m_rep.size();
458 if (index>ol) // insert beyond end of string and fill gap with spaces
459 {
460 m_rep.resize(index+len);
461 std::memset(&m_rep[ol],' ',index-ol);
462 std::memcpy(&m_rep[index],s,len+1);
463 }
464 else // insert inside the string
465 {
466 m_rep.insert(index,s);
467 }
468 }
469 return *this;
470 }
471
472 DString &insert( size_t index, char c)
473 {
474 char s[2] = { c, '\0' };
475 return insert(index,s);
476 }
477
478 DString &append( char c)
479 {
480 m_rep+=c;
481 return *this;
482 }
483
484 DString &append( const char *s )
485 {
486 return operator+=(s);
487 }
488
489 DString &append( const DString &s )
490 {
491 return operator+=(s);
492 }
493
494 DString &append( const std::string &s )
495 {
496 return operator+=(s);
497 }
498
499 DString &append( std::string_view s)
500 {
501 return operator+=(s);
502 }
503
504 DString &prepend( const char *s )
505 {
506 return insert(0,s);
507 }
508
510 {
511 return insert(0,s.data());
512 }
513
514 DString &prepend( const std::string &s )
515 {
516 return insert(0,s.c_str());
517 }
518
519 DString &prepend( std::string_view s)
520 {
521 return insert(0,s);
522 }
523
524 DString &remove( size_t index, size_t len )
525 {
526 size_t ol = m_rep.size();
527 if (index<ol && len>0) m_rep.erase(index,index+len>=ol ? std::string::npos : len);
528 return *this;
529 }
530
531 DString &replace( size_t index, size_t len, const char *s);
532
533 short toShort( bool *ok=nullptr, int base=10 ) const;
534 uint16_t toUShort( bool *ok=nullptr, int base=10 ) const;
535 int toInt( bool *ok=nullptr, int base=10 ) const;
536 uint32_t toUInt( bool *ok=nullptr, int base=10 ) const;
537 long toLong( bool *ok=nullptr, int base=10 ) const;
538 unsigned long toULong( bool *ok=nullptr, int base=10 ) const;
539 uint64_t toUInt64( bool *ok=nullptr, int base=10 ) const;
540
541 DString &setNum(short n)
542 {
543 m_rep = std::to_string(n);
544 return *this;
545 }
546
547 DString &setNum(uint16_t n)
548 {
549 m_rep = std::to_string(n);
550 return *this;
551 }
552
554 {
555 m_rep = std::to_string(n);
556 return *this;
557 }
558
559 DString &setNum(uint32_t n)
560 {
561 m_rep = std::to_string(n);
562 return *this;
563 }
564
566 {
567 m_rep = std::to_string(n);
568 return *this;
569 }
570
571 DString &setNum(long long n)
572 {
573 m_rep = std::to_string(n);
574 return *this;
575 }
576
577 DString &setNum(unsigned long long n)
578 {
579 m_rep = std::to_string(n);
580 return *this;
581 }
582
583 DString &setNum(unsigned long n)
584 {
585 m_rep = std::to_string(n);
586 return *this;
587 }
588
589 bool startsWith( const char *s ) const
590 {
591 if (m_rep.empty() || s==nullptr) return s==nullptr;
592 return m_rep.rfind(s,0)==0; // looking "backward" starting and ending at index 0
593 }
594
595 bool startsWith( const std::string &s) const
596 {
597 return m_rep.rfind(s,0)==0; // looking "backward" starting and ending at index 0
598 }
599
600 bool startsWith( const DString &s ) const
601 {
602 if (m_rep.empty() || s.empty()) return s.empty();
603 return m_rep.rfind(s.str(),0)==0; // looking "backward" starting and ending at index 0
604 }
605
606 bool endsWith(const char *s) const
607 {
608 if (m_rep.empty() || s==nullptr) return s==nullptr;
609 size_t l = strlen(s);
610 return m_rep.length()>=l && m_rep.compare(m_rep.length()-l, l, s, l)==0;
611 }
612
613 bool endsWith(const std::string &s) const
614 {
615 size_t l = s.length();
616 return m_rep.length()>=l && m_rep.compare(m_rep.length()-l, l, s)==0;
617 }
618
619 bool endsWith(const DString &s) const
620 {
621 size_t l = s.length();
622 return m_rep.length()>=l && m_rep.compare(m_rep.length()-l, l, s.str())==0;
623 }
624
625#define HAS_IMPLICIT_CAST_TO_PLAIN_C_STRING 0
626#if HAS_IMPLICIT_CAST_TO_PLAIN_C_STRING
627 /** Converts the string to a plain C string */
628 operator const char *() const
629 {
630 return data();
631 }
632#endif
633
634 const std::string &str() const
635 {
636 return m_rep;
637 }
638
640 {
641 m_rep+=s.str();
642 return *this;
643 }
644
645 DString &operator+=( const std::string &s)
646 {
647 m_rep+=s;
648 return *this;
649 }
650
651 DString &operator+=(std::string_view s)
652 {
653 m_rep+=s;
654 return *this;
655 }
656
657 /** Appends string \a str to this string and returns a reference to the result. */
658 DString &operator+=( const char *s )
659 {
660 if (s) m_rep+=s;
661 return *this;
662 }
663
664#define HAS_CHARACTER_APPEND_OPERATOR 1
665#if HAS_CHARACTER_APPEND_OPERATOR
666 /** Appends character \a c to this string and returns a reference to the result. */
667 DString &operator+=( char c )
668 {
669 m_rep+=c;
670 return *this;
671 }
672#endif
673
674 /** Returns a reference to the character at index \a i. */
675 char &at( size_t i)
676 {
677 return m_rep[i];
678 }
679
680 const char &at( size_t i) const
681 {
682 return m_rep[i];
683 }
684
685 /** Indexing operator. Equivalent to at(). */
686 char &operator[]( size_t i )
687 {
688 return m_rep[i];
689 }
690
691 const char &operator[]( size_t i ) const
692 {
693 return m_rep[i];
694 }
695
696 private:
697 std::string m_rep;
698};
699
700/*****************************************************************************
701 DString non-member operators
702 *****************************************************************************/
703
704inline bool operator==( const DString &s1, const DString &s2 )
705{ return s1.str() == s2.str(); }
706
707inline bool operator==( const DString &s1, const char *s2 )
708{ return dstrcmp(s1.data(),s2) == 0; }
709
710inline bool operator==( const char *s1, const DString &s2 )
711{ return dstrcmp(s1,s2.data()) == 0; }
712
713inline bool operator!=( const DString &s1, const DString &s2 )
714{ return s1.str() != s2.str(); }
715
716inline bool operator!=( const DString &s1, const char *s2 )
717{ return dstrcmp(s1.data(),s2) != 0; }
718
719inline bool operator!=( const char *s1, const DString &s2 )
720{ return dstrcmp(s1,s2.data()) != 0; }
721
722inline bool operator<( const DString &s1, const DString& s2 )
723{ return dstrcmp(s1.data(),s2.data()) < 0; }
724
725inline bool operator<( const DString &s1, const char *s2 )
726{ return dstrcmp(s1.data(),s2) < 0; }
727
728inline bool operator<( const char *s1, const DString &s2 )
729{ return dstrcmp(s1,s2.data()) < 0; }
730
731inline bool operator<=( const DString &s1, const char *s2 )
732{ return dstrcmp(s1.data(),s2) <= 0; }
733
734inline bool operator<=( const char *s1, const DString &s2 )
735{ return dstrcmp(s1,s2.data()) <= 0; }
736
737inline bool operator>( const DString &s1, const char *s2 )
738{ return dstrcmp(s1.data(),s2) > 0; }
739
740inline bool operator>( const char *s1, const DString &s2 )
741{ return dstrcmp(s1,s2.data()) > 0; }
742
743inline bool operator>=( const DString &s1, const char *s2 )
744{ return dstrcmp(s1.data(),s2) >= 0; }
745
746inline bool operator>=( const char *s1, const DString &s2 )
747{ return dstrcmp(s1,s2.data()) >= 0; }
748
749inline DString operator+( const DString &s1, const DString &s2 )
750{
751 return DString(s1.str()+s2.str());
752}
753
754
755inline DString operator+( const DString &s1, const char *s2 )
756{
757 DString tmp(s1);
758 tmp.append(s2);
759 return tmp;
760}
761
762inline DString operator+( const char *s1, const DString &s2 )
763{
764 DString tmp(s1);
765 tmp.append(s2);
766 return tmp;
767}
768
769inline const char *qPrint(const char *s)
770{
771 if (s) return s; else return "";
772}
773
774inline const char *qPrint(const DString &s)
775{
776 if (!s.empty()) return s.data(); else return "";
777}
778
779inline const char *qPrint(const std::string &s)
780{
781 return s.c_str();
782}
783
784inline std::string toStdString(const DString &s)
785{
786 return s.str();
787}
788
789//---- overloads
790
791inline int dstrcmp( const DString &str1, const char *str2 )
792{
793 return dstrcmp(str1.data(),str2);
794}
795
796inline int dstrcmp( const char *str1, const DString &str2 )
797{
798 return dstrcmp(str1,str2.data());
799}
800
801inline int dstrcmp( const DString &str1, const DString &str2 )
802{
803 return dstrcmp(str1.data(),str2.data());
804}
805
806inline int dstricmp( const DString &str1, const char *str2 )
807{
808 return dstricmp(str1.data(),str2);
809}
810
811inline int dstricmp( const char *str1, const DString &str2 )
812{
813 return dstricmp(str1,str2.data());
814}
815
816inline int dstricmp( const DString &str1, const DString &str2 )
817{
818 return dstricmp(str1.data(),str2.data());
819}
820
821inline int dstricmp_sort( const DString &str1, const char *str2 )
822{
823 return dstricmp_sort(str1.data(),str2);
824}
825
826inline int dstricmp_sort( const char *str1, const DString &str2 )
827{
828 return dstricmp_sort(str1,str2.data());
829}
830
831inline int dstricmp_sort( const DString &str1, const DString &str2 )
832{
833 return dstricmp_sort(str1.data(),str2.data());
834}
835
836
837inline int dstrnicmp( const DString &str1, const char *str2, size_t len )
838{
839 return dstrnicmp(str1.data(),str2,len);
840}
841
842inline int dstrnicmp( const char *str1, const DString &str2, size_t len )
843{
844 return dstrnicmp(str1,str2.data(),len);
845}
846
847inline int dstrnicmp( const DString &str1, const DString &str2, size_t len )
848{
849 return dstrnicmp(str1.data(),str2.data(),len);
850}
851
852// helper functions
853DString substitute(const DString &str,const DString &find,const DString &replace);
854inline DString substitute(const DString &str,const char *find,const char *replace)
855{
856 return substitute(str,DString(find),DString(replace));
857}
858DString substitute(const DString &s,const DString &src,const DString &dst,int skip_seq);
859
860inline DString substitute(const DString &s,char srcChar,char dstChar)
861{
862 std::string ss = s.str();
863 std::replace(ss.begin(),ss.end(),srcChar,dstChar);
864 return DString(ss);
865}
866
867inline std::ostream& operator<<(std::ostream& os, const DString& s)
868{
869 os << s.str();
870 return os;
871}
872
873inline void swap(DString &s1, DString &s2)
874{
875 s1.swap(s2);
876}
877
878namespace std
879{
880 template<> struct hash<DString>
881 {
882 size_t operator()(const DString &s) const
883 {
884 return hash<std::string>{}(s.str());
885 }
886 };
887}
888
889#endif // QCSTRING_H
constexpr auto prefix
Definition anchor.cpp:44
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:89
DString & assign(std::string_view s)
Definition dstring.h:242
void clear()
Definition dstring.h:219
DString & setNum(short n)
Definition dstring.h:541
DString & operator+=(std::string_view s)
Definition dstring.h:651
int compare(const char *s) const
Definition dstring.h:234
DString & assign(const DString &s)
Definition dstring.h:240
void resize(size_t newlen)
Definition dstring.h:214
std::string::const_reverse_iterator const_reverse_iterator
Definition dstring.h:180
std::string::value_type value_type
Definition dstring.h:173
const_reverse_iterator rbegin() const
Definition dstring.h:193
char & front()
Returns a reference to the first character.
Definition dstring.h:200
DString()=default
DString & setNum(unsigned long long n)
Definition dstring.h:577
DString & operator=(std::string_view sv)
Definition dstring.h:110
DString(const char *str)
creates a string from a plain C string.
Definition dstring.h:142
DString & setNum(uint16_t n)
Definition dstring.h:547
void shrink_to_fit()
Definition dstring.h:212
unsigned long toULong(bool *ok=nullptr, int base=10) const
Definition dstring.cpp:271
void push_back(char c)
Definition dstring.h:207
DString & append(std::string_view s)
Definition dstring.h:499
bool endsWith(const DString &s) const
Definition dstring.h:619
DString & insert(size_t index, const char *s)
Definition dstring.h:452
~DString()=default
void resize(size_t newlen, char c)
Resizes the string to newlen characters, filling any new characters with c.
Definition dstring.h:217
std::string::const_reference const_reference
Definition dstring.h:176
DString upper() const
Definition dstring.h:336
DString(std::string &&s)
Definition dstring.h:100
size_t rfind(char c, size_t pos=npos) const
Definition dstring.h:249
DString(const DString &)=default
const char & back() const
Definition dstring.h:205
size_t find_first_not_of(char c, size_t pos=0) const
Definition dstring.h:267
std::string::reverse_iterator reverse_iterator
Definition dstring.h:179
uint16_t toUShort(bool *ok=nullptr, int base=10) const
Definition dstring.cpp:172
DString mid(size_t index, size_t len=npos) const
Definition dstring.h:323
const_iterator cend() const
Definition dstring.h:190
DString lower() const
Definition dstring.h:331
DString simplifyWhiteSpace() const
return a copy of this string with leading and trailing whitespace removed and multiple whitespace cha...
Definition dstring.cpp:118
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:153
size_t find_last_not_of(char c, size_t pos=npos) const
Definition dstring.h:272
iterator end()
Definition dstring.h:188
DString & replace(size_t index, size_t len, const char *s)
Definition dstring.cpp:145
DString & setNum(long n)
Definition dstring.h:565
DString(int)=delete
uint32_t toUInt(bool *ok=nullptr, int base=10) const
Definition dstring.cpp:187
DString substr(size_t pos=0, size_t count=npos) const
Returns a substring of length count starting at pos.
Definition dstring.h:228
DString(size_t size, SizeTag t)
Definition dstring.h:137
DString & operator+=(const char *s)
Appends string str to this string and returns a reference to the result.
Definition dstring.h:658
bool startsWith(const DString &s) const
Definition dstring.h:600
uint64_t toUInt64(bool *ok=nullptr, int base=10) const
Definition dstring.cpp:342
DString & assign(const char *s)
Definition dstring.h:239
DString(DString &&)=default
char * rawData()
Returns a writable pointer to the data.
Definition dstring.h:171
short toShort(bool *ok=nullptr, int base=10) const
Definition dstring.cpp:162
size_t find(const char *s, size_t pos=0) const
Definition dstring.h:245
const char * c_str() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:165
std::string_view view() const
Definition dstring.h:167
size_t capacity() const
Definition dstring.h:210
DString(const JavaCCString &s)
For converting a JavaCC string.
Definition dstring.h:120
size_t find(const DString &s, size_t pos=0) const
Definition dstring.h:246
DString & remove(size_t index, size_t len)
Definition dstring.h:524
size_t find_first_of(const char *s, size_t pos=0) const
Definition dstring.h:258
DString & operator=(const std::string &s)
Definition dstring.h:150
static constexpr size_t npos
value used to indicate 'not found' or 'to the end of the string', matching std::string::npos
Definition dstring.h:183
const_reverse_iterator crbegin() const
Definition dstring.h:194
reverse_iterator rend()
Definition dstring.h:195
DString & append(const std::string &s)
Definition dstring.h:494
char & at(size_t i)
Returns a reference to the character at index i.
Definition dstring.h:675
int compare(const std::string &s) const
Definition dstring.h:235
const char & operator[](size_t i) const
Definition dstring.h:691
std::string::size_type size_type
Definition dstring.h:174
DString & prepend(const DString &s)
Definition dstring.h:509
DString & operator=(const char *str)
replaces the contents by that of C string str.
Definition dstring.h:148
void pop_back()
Definition dstring.h:208
DString & append(char c)
Definition dstring.h:478
size_t find_first_of(char c, size_t pos=0) const
Definition dstring.h:257
DString quoted() const
Definition dstring.h:357
size_t find_last_not_of(const std::string &s, size_t pos=npos) const
Definition dstring.h:275
DString & setNum(uint32_t n)
Definition dstring.h:559
const_iterator begin() const
Definition dstring.h:186
const char & at(size_t i) const
Definition dstring.h:680
DString & append(const char *s)
Definition dstring.h:484
DString & operator=(DString &&)=default
DString & setNum(int n)
Definition dstring.h:553
size_t find_last_not_of(const char *s, size_t pos=npos) const
Definition dstring.h:273
size_t rfind_insensitive(char c, size_t pos=npos) const
Definition dstring.cpp:43
bool endsWith(const std::string &s) const
Definition dstring.h:613
size_t rfind(const std::string &s, size_t pos=npos) const
Definition dstring.h:252
iterator begin()
Definition dstring.h:185
std::string m_rep
Definition dstring.h:697
const_iterator cbegin() const
Definition dstring.h:187
DString right(size_t len) const
Definition dstring.h:316
DString & operator=(const DString &)=default
size_t find_first_not_of(const DString &s, size_t pos=0) const
Definition dstring.h:269
DString & insert(size_t index, char c)
Definition dstring.h:472
size_t find_last_of(char c, size_t pos=npos) const
Definition dstring.h:262
size_t find(const std::string &s, size_t pos=0) const
Definition dstring.h:247
size_t max_size() const
Definition dstring.h:211
DString repeat(unsigned int n) const
Definition dstring.h:403
bool startsWith(const std::string &s) const
Definition dstring.h:595
size_t size() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:159
size_t copy(char *dest, size_t count, size_t pos=0) const
Copies (up to) count characters into dest, starting at pos.
Definition dstring.h:231
DString fill(char c, int len=-1)
Fills a string with a predefined character.
Definition dstring.h:283
size_t find_first_not_of(const char *s, size_t pos=0) const
Definition dstring.h:268
DString & prepend(std::string_view s)
Definition dstring.h:519
bool stripPrefix(const char *prefix)
Definition dstring.h:306
size_t find_first_of(const DString &s, size_t pos=0) const
Definition dstring.h:259
size_t rfind(const char *s, size_t pos=npos) const
Definition dstring.h:250
DString & prepend(const char *s)
Definition dstring.h:504
DString & insert(size_t index, std::string_view s)
Definition dstring.h:433
int contains(char c, bool cs=true) const
Definition dstring.cpp:76
size_t find_last_not_of(const DString &s, size_t pos=npos) const
Definition dstring.h:274
size_t find(char c, size_t pos=0) const
Definition dstring.h:244
DString removeWhiteSpace() const
returns a copy of this string with all whitespace removed
Definition dstring.h:383
DString & sprintf(const char *format,...)
Definition dstring.cpp:29
DString & operator=(std::string &&s)
Definition dstring.h:102
DString & append(const DString &s)
Definition dstring.h:489
char & operator[](size_t i)
Indexing operator.
Definition dstring.h:686
DString(const std::string &s)
Definition dstring.h:98
const_reverse_iterator crend() const
Definition dstring.h:197
DString(const char *str, size_t maxlen)
creates a string from str and copies over the first maxlen characters.
Definition dstring.h:145
std::string::reference reference
Definition dstring.h:175
void reserve(size_t size)
Reserve space for size bytes without changing the string contents.
Definition dstring.h:222
DString & setNum(long long n)
Definition dstring.h:571
size_t find_first_of(const std::string &s, size_t pos=0) const
Definition dstring.h:260
DString & operator+=(const std::string &s)
Definition dstring.h:645
DString & erase(size_t index=0, size_t count=npos)
Definition dstring.h:237
int toInt(bool *ok=nullptr, int base=10) const
Definition dstring.cpp:182
char & back()
Returns a reference to the last character.
Definition dstring.h:204
SizeTag
creates a string with room for size characters
Definition dstring.h:136
@ ExplicitSize
Definition dstring.h:136
long toLong(bool *ok=nullptr, int base=10) const
Definition dstring.cpp:193
DString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition dstring.h:342
DString(std::string_view sv)
Definition dstring.h:108
DString & operator+=(const DString &s)
Definition dstring.h:639
void swap(DString &other)
Swaps the contents of this string with other.
Definition dstring.h:225
size_t find_last_of(const std::string &s, size_t pos=npos) const
Definition dstring.h:265
DString & prepend(const std::string &s)
Definition dstring.h:514
DString left(size_t len) const
Definition dstring.h:311
const std::string & str() const
Definition dstring.h:634
bool stripPrefix(const DString &prefix)
Definition dstring.h:295
int compare(const DString &s) const
Definition dstring.h:233
const_iterator end() const
Definition dstring.h:189
size_t find_first_not_of(const std::string &s, size_t pos=0) const
Definition dstring.h:270
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:162
DString & setNum(unsigned long n)
Definition dstring.h:583
DString & operator=(const JavaCCString &s)
Definition dstring.h:125
bool startsWith(const char *s) const
Definition dstring.h:589
const_reverse_iterator rend() const
Definition dstring.h:196
size_t find_last_of(const DString &s, size_t pos=npos) const
Definition dstring.h:264
std::string::iterator iterator
Definition dstring.h:177
DString & assign(const std::string &s)
Definition dstring.h:241
size_t find_last_of(const char *s, size_t pos=npos) const
Definition dstring.h:263
size_t rfind(const DString &s, size_t pos=npos) const
Definition dstring.h:251
reverse_iterator rbegin()
Definition dstring.h:192
std::string::const_iterator const_iterator
Definition dstring.h:178
bool endsWith(const char *s) const
Definition dstring.h:606
const char & front() const
Definition dstring.h:201
DString & insert(size_t index, const DString &s)
Definition dstring.h:414
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:156
bool operator==(const DirIterator &it1, const DirIterator &it2)
Definition dir.cpp:160
bool operator!=(const DirIterator &it1, const DirIterator &it2)
Definition dir.cpp:165
void dstrfree(const char *str)
Frees the memory allocated using dstrdup().
Definition dstring.cpp:422
char * dstrdup(const char *str)
Definition dstring.cpp:415
char * dstrncpy(char *dst, const char *src, size_t len)
Definition dstring.cpp:427
int dstrnicmp(const char *s1, const char *s2, size_t len)
Definition dstring.cpp:453
DString substitute(const DString &s, const DString &src, const DString &dst)
substitute all occurrences of src in s by dst
Definition dstring.cpp:476
int dstricmp(const char *s1, const char *s2)
Definition dstring.cpp:435
bool operator>(const DString &s1, const char *s2)
Definition dstring.h:737
std::string toStdString(const DString &s)
Definition dstring.h:784
char * dstrcpy(char *dst, const char *src)
Definition dstring.h:47
int dstrncmp(const char *str1, const char *str2, size_t len)
Definition dstring.h:61
bool operator<=(const DString &s1, const char *s2)
Definition dstring.h:731
bool disempty(const char *s)
Definition dstring.h:52
uint32_t dstrlen(const char *str)
Returns the length of string str, or 0 if a null pointer is passed.
Definition dstring.h:44
std::ostream & operator<<(std::ostream &os, const DString &s)
Definition dstring.h:867
int dstricmp_sort(const char *str1, const char *str2)
Definition dstring.h:72
int dstrcmp(const char *str1, const char *str2)
Definition dstring.h:55
const char * qPrint(const char *s)
Definition dstring.h:769
bool operator<(const DString &s1, const DString &s2)
Definition dstring.h:722
void swap(DString &s1, DString &s2)
Definition dstring.h:873
DString operator+(const DString &s1, const DString &s2)
Definition dstring.h:749
bool operator>=(const DString &s1, const char *s2)
Definition dstring.h:743
bool disspace(char c)
Definition dstring.h:67
std::basic_string< JAVACC_CHAR_TYPE > JavaCCString
Definition dstring.h:81
Definition dstring.h:879
size_t operator()(const DString &s) const
Definition dstring.h:882
std::string convertUTF8ToUpper(const std::string &input)
Converts the input string into a upper case version, also taking into account non-ASCII characters th...
Definition utf8.cpp:192
std::string convertUTF8ToLower(const std::string &input)
Converts the input string into a lower case version, also taking into account non-ASCII characters th...
Definition utf8.cpp:187
Various UTF8 related helper functions.
DString stripLeadingAndTrailingEmptyLines(const DString &s, int &docLine)
Special version of DString::stripWhiteSpace() that only strips completely blank lines.
Definition util.cpp:5071