29#define ASSERT(x) if ( !(x) )\
30 fprintf(stderr,"ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__)
45{
return str ?
static_cast<uint32_t
>(strlen(str)) : 0; }
47inline char *
dstrcpy(
char *dst,
const char *src )
48{
return src ? strcpy(dst, src) :
nullptr; }
50char *
dstrncpy(
char *dst,
const char *src,
size_t len);
53{
return s==
nullptr || *s==
'\0'; }
55inline int dstrcmp(
const char *str1,
const char *str2 )
56{
return (str1 && str2) ? strcmp(str1,str2) :
61inline int dstrncmp(
const char *str1,
const char *str2,
size_t len )
62{
return (str1 && str2) ? strncmp(str1,str2,len) :
68{
return c==
' ' || c==
'\t' || c==
'\n' || c==
'\r'; }
70int dstricmp(
const char *str1,
const char *str2 );
75 return result==0 ?
dstrcmp(str1,str2) : result;
78int dstrnicmp(
const char *str1,
const char *str2,
size_t len );
119#ifndef DISABLE_JAVACC
122 m_rep.resize(s.size());
123 std::memcpy(
m_rep.data(),s.data(),s.size());
127 m_rep.resize(s.size());
128 std::memcpy(
m_rep.data(),s.data(),s.size());
183 static constexpr size_t npos = std::string::npos;
231 size_t copy(
char *dest,
size_t count,
size_t pos=0 )
const {
return m_rep.copy(dest,count,pos); }
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; }
247 size_t find(
const std::string &s,
size_t pos=0 )
const {
return m_rep.find(s,pos); }
252 size_t rfind(
const std::string &s,
size_t pos=
npos )
const {
return m_rep.rfind(s,pos); }
260 size_t find_first_of(
const std::string &s,
size_t pos=0 )
const {
return m_rep.find_first_of(s,pos); }
285 int l = len==-1 ?
static_cast<int>(
m_rep.size()) : len;
286 m_rep = std::string(l,c);
292 int contains(
char c,
bool cs=
true )
const;
293 int contains(
const char *
str,
bool cs=
true )
const;
325 size_t slen =
m_rep.size();
326 if (len==
npos) len = slen-index;
327 return m_rep.empty() || index>slen || len==0 ?
DString() :
344 size_t sl =
m_rep.size();
346 size_t start=0,
end=sl-1;
348 if (start==sl)
return DString();
359 size_t start=0, sl=
m_rep.size(),
end=sl-1;
361 if (start==sl)
return DString();
363 bool needsQuotes=
false;
367 while (i<
end && !needsQuotes)
369 needsQuotes =
m_rep[i] ==
'-';
385 size_t sl =
m_rep.size();
386 if (sl==0)
return *
this;
387 std::string result =
m_rep;
394 if (dst<
m_rep.size()) result.resize(dst);
407 for (offset = 0; offset < n *
size(); offset +=
size())
418 size_t ol =
m_rep.size();
422 std::memset(&
m_rep[ol],
' ',index-ol);
437 size_t ol =
m_rep.size();
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);
446 m_rep.insert(index,s);
454 size_t len = s ?
dstrlen(s) : 0;
457 size_t ol =
m_rep.size();
460 m_rep.resize(index+len);
461 std::memset(&
m_rep[ol],
' ',index-ol);
462 std::memcpy(&
m_rep[index],s,len+1);
466 m_rep.insert(index,s);
474 char s[2] = { c,
'\0' };
516 return insert(0,s.c_str());
526 size_t ol =
m_rep.size();
527 if (index<ol && len>0)
m_rep.erase(index,index+len>=ol ? std::string::npos : len);
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;
543 m_rep = std::to_string(n);
549 m_rep = std::to_string(n);
555 m_rep = std::to_string(n);
561 m_rep = std::to_string(n);
567 m_rep = std::to_string(n);
573 m_rep = std::to_string(n);
579 m_rep = std::to_string(n);
585 m_rep = std::to_string(n);
591 if (
m_rep.empty() || s==
nullptr)
return s==
nullptr;
592 return m_rep.rfind(s,0)==0;
597 return m_rep.rfind(s,0)==0;
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;
615 size_t l = s.length();
625#define HAS_IMPLICIT_CAST_TO_PLAIN_C_STRING 0
626#if HAS_IMPLICIT_CAST_TO_PLAIN_C_STRING
628 operator const char *()
const
634 const std::string &
str()
const
664#define HAS_CHARACTER_APPEND_OPERATOR 1
665#if HAS_CHARACTER_APPEND_OPERATOR
680 const char &
at(
size_t i)
const
705{
return s1.
str() == s2.
str(); }
714{
return s1.
str() != s2.
str(); }
771 if (s)
return s;
else return "";
776 if (!s.
empty())
return s.
data();
else return "";
779inline const char *
qPrint(
const std::string &s)
862 std::string ss = s.
str();
863 std::replace(ss.begin(),ss.end(),srcChar,dstChar);
884 return hash<std::string>{}(s.
str());
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
DString & assign(std::string_view s)
DString & setNum(short n)
DString & operator+=(std::string_view s)
int compare(const char *s) const
DString & assign(const DString &s)
void resize(size_t newlen)
std::string::const_reverse_iterator const_reverse_iterator
std::string::value_type value_type
const_reverse_iterator rbegin() const
char & front()
Returns a reference to the first character.
DString & setNum(unsigned long long n)
DString & operator=(std::string_view sv)
DString(const char *str)
creates a string from a plain C string.
DString & setNum(uint16_t n)
unsigned long toULong(bool *ok=nullptr, int base=10) const
DString & append(std::string_view s)
bool endsWith(const DString &s) const
DString & insert(size_t index, const char *s)
void resize(size_t newlen, char c)
Resizes the string to newlen characters, filling any new characters with c.
std::string::const_reference const_reference
size_t rfind(char c, size_t pos=npos) const
DString(const DString &)=default
const char & back() const
size_t find_first_not_of(char c, size_t pos=0) const
std::string::reverse_iterator reverse_iterator
uint16_t toUShort(bool *ok=nullptr, int base=10) const
DString mid(size_t index, size_t len=npos) const
const_iterator cend() const
DString simplifyWhiteSpace() const
return a copy of this string with leading and trailing whitespace removed and multiple whitespace cha...
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
size_t find_last_not_of(char c, size_t pos=npos) const
DString & replace(size_t index, size_t len, const char *s)
uint32_t toUInt(bool *ok=nullptr, int base=10) const
DString substr(size_t pos=0, size_t count=npos) const
Returns a substring of length count starting at pos.
DString(size_t size, SizeTag t)
DString & operator+=(const char *s)
Appends string str to this string and returns a reference to the result.
bool startsWith(const DString &s) const
uint64_t toUInt64(bool *ok=nullptr, int base=10) const
DString & assign(const char *s)
DString(DString &&)=default
char * rawData()
Returns a writable pointer to the data.
short toShort(bool *ok=nullptr, int base=10) const
size_t find(const char *s, size_t pos=0) const
const char * c_str() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
std::string_view view() const
DString(const JavaCCString &s)
For converting a JavaCC string.
size_t find(const DString &s, size_t pos=0) const
DString & remove(size_t index, size_t len)
size_t find_first_of(const char *s, size_t pos=0) const
DString & operator=(const std::string &s)
static constexpr size_t npos
value used to indicate 'not found' or 'to the end of the string', matching std::string::npos
const_reverse_iterator crbegin() const
DString & append(const std::string &s)
char & at(size_t i)
Returns a reference to the character at index i.
int compare(const std::string &s) const
const char & operator[](size_t i) const
std::string::size_type size_type
DString & prepend(const DString &s)
DString & operator=(const char *str)
replaces the contents by that of C string str.
size_t find_first_of(char c, size_t pos=0) const
size_t find_last_not_of(const std::string &s, size_t pos=npos) const
DString & setNum(uint32_t n)
const_iterator begin() const
const char & at(size_t i) const
DString & append(const char *s)
DString & operator=(DString &&)=default
size_t find_last_not_of(const char *s, size_t pos=npos) const
size_t rfind_insensitive(char c, size_t pos=npos) const
bool endsWith(const std::string &s) const
size_t rfind(const std::string &s, size_t pos=npos) const
const_iterator cbegin() const
DString right(size_t len) const
DString & operator=(const DString &)=default
size_t find_first_not_of(const DString &s, size_t pos=0) const
DString & insert(size_t index, char c)
size_t find_last_of(char c, size_t pos=npos) const
size_t find(const std::string &s, size_t pos=0) const
DString repeat(unsigned int n) const
bool startsWith(const std::string &s) const
size_t size() const
Returns the length of the string, not counting the 0-terminator.
size_t copy(char *dest, size_t count, size_t pos=0) const
Copies (up to) count characters into dest, starting at pos.
DString fill(char c, int len=-1)
Fills a string with a predefined character.
size_t find_first_not_of(const char *s, size_t pos=0) const
DString & prepend(std::string_view s)
bool stripPrefix(const char *prefix)
size_t find_first_of(const DString &s, size_t pos=0) const
size_t rfind(const char *s, size_t pos=npos) const
DString & prepend(const char *s)
DString & insert(size_t index, std::string_view s)
int contains(char c, bool cs=true) const
size_t find_last_not_of(const DString &s, size_t pos=npos) const
size_t find(char c, size_t pos=0) const
DString removeWhiteSpace() const
returns a copy of this string with all whitespace removed
DString & sprintf(const char *format,...)
DString & operator=(std::string &&s)
DString & append(const DString &s)
char & operator[](size_t i)
Indexing operator.
DString(const std::string &s)
const_reverse_iterator crend() const
DString(const char *str, size_t maxlen)
creates a string from str and copies over the first maxlen characters.
std::string::reference reference
void reserve(size_t size)
Reserve space for size bytes without changing the string contents.
DString & setNum(long long n)
size_t find_first_of(const std::string &s, size_t pos=0) const
DString & operator+=(const std::string &s)
DString & erase(size_t index=0, size_t count=npos)
int toInt(bool *ok=nullptr, int base=10) const
char & back()
Returns a reference to the last character.
SizeTag
creates a string with room for size characters
long toLong(bool *ok=nullptr, int base=10) const
DString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
DString(std::string_view sv)
DString & operator+=(const DString &s)
void swap(DString &other)
Swaps the contents of this string with other.
size_t find_last_of(const std::string &s, size_t pos=npos) const
DString & prepend(const std::string &s)
DString left(size_t len) const
const std::string & str() const
bool stripPrefix(const DString &prefix)
int compare(const DString &s) const
const_iterator end() const
size_t find_first_not_of(const std::string &s, size_t pos=0) const
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
DString & setNum(unsigned long n)
DString & operator=(const JavaCCString &s)
bool startsWith(const char *s) const
const_reverse_iterator rend() const
size_t find_last_of(const DString &s, size_t pos=npos) const
std::string::iterator iterator
DString & assign(const std::string &s)
size_t find_last_of(const char *s, size_t pos=npos) const
size_t rfind(const DString &s, size_t pos=npos) const
reverse_iterator rbegin()
std::string::const_iterator const_iterator
bool endsWith(const char *s) const
const char & front() const
DString & insert(size_t index, const DString &s)
size_t length() const
Returns the length of the string, not counting the 0-terminator.
bool operator==(const DirIterator &it1, const DirIterator &it2)
bool operator!=(const DirIterator &it1, const DirIterator &it2)
void dstrfree(const char *str)
Frees the memory allocated using dstrdup().
char * dstrdup(const char *str)
char * dstrncpy(char *dst, const char *src, size_t len)
int dstrnicmp(const char *s1, const char *s2, size_t len)
DString substitute(const DString &s, const DString &src, const DString &dst)
substitute all occurrences of src in s by dst
int dstricmp(const char *s1, const char *s2)
bool operator>(const DString &s1, const char *s2)
std::string toStdString(const DString &s)
char * dstrcpy(char *dst, const char *src)
int dstrncmp(const char *str1, const char *str2, size_t len)
bool operator<=(const DString &s1, const char *s2)
bool disempty(const char *s)
uint32_t dstrlen(const char *str)
Returns the length of string str, or 0 if a null pointer is passed.
std::ostream & operator<<(std::ostream &os, const DString &s)
int dstricmp_sort(const char *str1, const char *str2)
int dstrcmp(const char *str1, const char *str2)
const char * qPrint(const char *s)
bool operator<(const DString &s1, const DString &s2)
void swap(DString &s1, DString &s2)
DString operator+(const DString &s1, const DString &s2)
bool operator>=(const DString &s1, const char *s2)
std::basic_string< JAVACC_CHAR_TYPE > JavaCCString
size_t operator()(const DString &s) const
std::string convertUTF8ToUpper(const std::string &input)
Converts the input string into a upper case version, also taking into account non-ASCII characters th...
std::string convertUTF8ToLower(const std::string &input)
Converts the input string into a lower case version, also taking into account non-ASCII characters th...
Various UTF8 related helper functions.
DString stripLeadingAndTrailingEmptyLines(const DString &s, int &docLine)
Special version of DString::stripWhiteSpace() that only strips completely blank lines.