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