28#define ASSERT(x) if ( !(x) )\
29 fprintf(stderr,"ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__)
44{
return str ?
static_cast<uint32_t
>(strlen(str)) : 0; }
46inline char *
dstrcpy(
char *dst,
const char *src )
47{
return src ? strcpy(dst, src) :
nullptr; }
49char *
dstrncpy(
char *dst,
const char *src,
size_t len);
52{
return s==
nullptr || *s==
'\0'; }
54inline int dstrcmp(
const char *str1,
const char *str2 )
55{
return (str1 && str2) ? strcmp(str1,str2) :
60inline int dstrncmp(
const char *str1,
const char *str2,
size_t len )
61{
return (str1 && str2) ? strncmp(str1,str2,len) :
67{
return c==
' ' || c==
'\t' || c==
'\n' || c==
'\r'; }
69int dstricmp(
const char *str1,
const char *str2 );
74 return result==0 ?
dstrcmp(str1,str2) : result;
77int dstrnicmp(
const char *str1,
const char *str2,
size_t len );
118#ifndef DISABLE_JAVACC
121 m_rep.resize(s.size());
122 std::memcpy(
m_rep.data(),s.data(),s.size());
126 m_rep.resize(s.size());
127 std::memcpy(
m_rep.data(),s.data(),s.size());
182 static constexpr size_t npos = std::string::npos;
230 size_t copy(
char *dest,
size_t count,
size_t pos=0 )
const {
return m_rep.copy(dest,count,pos); }
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; }
246 size_t find(
const std::string &s,
size_t pos=0 )
const {
return m_rep.find(s,pos); }
251 size_t rfind(
const std::string &s,
size_t pos=
npos )
const {
return m_rep.rfind(s,pos); }
259 size_t find_first_of(
const std::string &s,
size_t pos=0 )
const {
return m_rep.find_first_of(s,pos); }
285 m_rep = std::string(len,c);
291 int contains(
char c,
bool cs=
true )
const;
292 int contains(
const char *
str,
bool cs=
true )
const;
324 size_t slen =
m_rep.size();
325 if (len==
npos) len = slen-index;
326 return m_rep.empty() || index>slen || len==0 ?
DString() :
343 size_t sl =
m_rep.size();
345 size_t start=0,
end=sl-1;
347 if (start==sl)
return DString();
358 size_t start=0, sl=
m_rep.size(),
end=sl-1;
360 if (start==sl)
return DString();
362 bool needsQuotes=
false;
366 while (i<
end && !needsQuotes)
368 needsQuotes =
m_rep[i] ==
'-';
384 size_t sl =
m_rep.size();
385 if (sl==0)
return *
this;
386 std::string result =
m_rep;
393 if (dst<
m_rep.size()) result.resize(dst);
422 for (offset = 0; offset < n *
size(); offset +=
size())
433 size_t ol =
m_rep.size();
437 std::memset(&
m_rep[ol],
' ',index-ol);
452 size_t ol =
m_rep.size();
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);
461 m_rep.insert(index,s);
469 size_t len = s ?
dstrlen(s) : 0;
472 size_t ol =
m_rep.size();
475 m_rep.resize(index+len);
476 std::memset(&
m_rep[ol],
' ',index-ol);
477 std::memcpy(&
m_rep[index],s,len+1);
481 m_rep.insert(index,s);
489 char s[2] = { c,
'\0' };
531 return insert(0,s.c_str());
541 size_t ol =
m_rep.size();
542 if (index<ol && len>0)
m_rep.erase(index,index+len>=ol ? std::string::npos : len);
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;
558 m_rep = std::to_string(n);
564 m_rep = std::to_string(n);
570 m_rep = std::to_string(n);
576 m_rep = std::to_string(n);
582 m_rep = std::to_string(n);
588 m_rep = std::to_string(n);
594 m_rep = std::to_string(n);
600 m_rep = std::to_string(n);
606 if (
m_rep.empty() || s==
nullptr)
return s==
nullptr;
607 return m_rep.rfind(s,0)==0;
612 return m_rep.rfind(s,0)==0;
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;
630 size_t l = s.length();
640#define HAS_IMPLICIT_CAST_TO_PLAIN_C_STRING 0
641#if HAS_IMPLICIT_CAST_TO_PLAIN_C_STRING
643 operator const char *()
const
649 const std::string &
str()
const
679#define HAS_CHARACTER_APPEND_OPERATOR 1
680#if HAS_CHARACTER_APPEND_OPERATOR
695 const char &
at(
size_t i)
const
723{
return s1.
str() == s2.
str(); }
732{
return s1.
str() != s2.
str(); }
789 if (s)
return s;
else return "";
794 if (!s.
empty())
return s.
data();
else return "";
797inline const char *
qPrint(
const std::string &s)
880 std::string ss = s.
str();
881 std::replace(ss.begin(),ss.end(),srcChar,dstChar);
901 return c==
'_' || c>=128 || c<0 || isalnum(c) || c==
'$';
910 const int maxMarkerStrLen = 20;
911 char result[maxMarkerStrLen];
912 snprintf(result,maxMarkerStrLen,
"@%d",
id);
922 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)
static DString integerToAlpha(int n, bool upper=true)
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
DString stripLeadingAndTrailingEmptyLines() 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
DString fill(char c, size_t len)
Fills a string with a predefined character.
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 internal white...
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
bool containsWord(const char *word) const
returns true if this string contains word as a whole word, false otherwise.
DString substr(size_t pos=0, size_t count=npos) const
Returns a substring of length count starting at pos.
static DString integerToRoman(int n, bool upper=true)
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
bool findAndRemoveWord(const char *word)
removes occurrences of whole word from this string, while keeping internal spaces and reducing multip...
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.
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 DString &s1, const char *s2)
int dstricmp(const char *str1, const char *str2)
int dstrnicmp(const char *str1, const char *str2, size_t len)
std::string toStdString(const DString &s)
char * dstrncpy(char *dst, const char *src, size_t len)
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)
DString substitute(const DString &str, const DString &find, const DString &replace)
substitute all occurrences of src in s by dst
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.
bool operator!=(const DString &s1, const DString &s2)
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)
bool operator==(const DString &s1, const DString &s2)
void swap(DString &s1, DString &s2)
char * dstrdup(const char *s)
void dstrfree(const char *s)
Frees the memory allocated using dstrdup().
DString operator+(const DString &s1, const DString &s2)
bool isId(int c)
Returns true if c is a valid character for an identifier.
bool operator>=(const DString &s1, const char *s2)
std::basic_string< JAVACC_CHAR_TYPE > JavaCCString
DString generateMarker(int id)
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.