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