diff options
Diffstat (limited to 'src/qcstring.h')
-rw-r--r-- | src/qcstring.h | 127 |
1 files changed, 67 insertions, 60 deletions
diff --git a/src/qcstring.h b/src/qcstring.h index c62c8a6..272c7fd 100644 --- a/src/qcstring.h +++ b/src/qcstring.h @@ -35,12 +35,6 @@ #ifndef TRUE #define TRUE true #endif -typedef unsigned char uchar; -typedef unsigned short ushort; -typedef unsigned uint; -typedef unsigned long ulong; -typedef int64_t int64; -typedef uint64_t uint64; #define ASSERT(x) if ( !(x) )\ fprintf(stderr,"ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__) @@ -51,33 +45,25 @@ typedef uint64_t uint64; void *qmemmove( void *dst, const void *src, size_t len ); -#if defined(_OS_WIN32_) -#define qsnprintf _snprintf -#else #define qsnprintf snprintf -#endif - -char *qstrdup( const char * ); - -inline uint cstrlen( const char *str ) -{ return static_cast<uint>(strlen(str)); } -inline uint qstrlen( const char *str ) -{ return str ? static_cast<uint>(strlen(str)) : 0; } +//! Returns a copy of a string \a s. +//! Note that memory is passed to the caller, use qstrfree() to release. +char *qstrdup( const char *s ); +//! Frees the memory allocated using qstrdup(). +void qstrfree( const char *s ); -inline char *cstrcpy( char *dst, const char *src ) -{ return strcpy(dst,src); } +//! Returns the length of string \a str, or 0 if a null pointer is passed. +inline uint32_t qstrlen( const char *str ) +{ return str ? static_cast<uint32_t>(strlen(str)) : 0; } inline char *qstrcpy( char *dst, const char *src ) -{ return src ? strcpy(dst, src) : 0; } +{ return src ? strcpy(dst, src) : nullptr; } -char * qstrncpy(char *dst,const char *src, size_t len); - -inline int cstrcmp( const char *str1, const char *str2 ) -{ return strcmp(str1,str2); } +char *qstrncpy(char *dst,const char *src, size_t len); inline bool qisempty( const char *s) -{ return s==0 || *s==0; } +{ return s==nullptr || *s=='\0'; } inline int qstrcmp( const char *str1, const char *str2 ) { return (str1 && str2) ? strcmp(str1,str2) : // both non-empty @@ -85,9 +71,6 @@ inline int qstrcmp( const char *str1, const char *str2 ) qisempty(str1) ? -1 : 1; // one empty, other non-empty } -inline int cstrncmp( const char *str1, const char *str2, size_t len ) -{ return strncmp(str1,str2,len); } - inline int qstrncmp( const char *str1, const char *str2, size_t len ) { return (str1 && str2) ? strncmp(str1,str2,len) : // both non-empty (qisempty(str1) && qisempty(str2)) ? 0 : // both empty @@ -101,6 +84,7 @@ int qstricmp( const char *str1, const char *str2 ); int qstrnicmp( const char *str1, const char *str2, size_t len ); +using JavaCCString = std::basic_string<unsigned char>; /** This is an alternative implementation of QCString. It provides basically * the same functions but uses std::string as the underlying string type @@ -119,6 +103,19 @@ class QCString QCString( std::string &&s) : m_rep(std::move(s)) {} + /** For converting a JavaCC string */ + QCString( const JavaCCString &s) + { + m_rep.resize(s.size()); + memcpy(m_rep.data(),s.data(),s.size()); + } + QCString &operator=( const JavaCCString &s) + { + m_rep.resize(s.size()); + memcpy(m_rep.data(),s.data(),s.size()); + return *this; + } + /** creates a string with room for size characters * @param[in] size the number of character to allocate (also counting the 0-terminator!) */ @@ -144,10 +141,10 @@ class QCString bool isEmpty() const { return m_rep.empty(); } /** Returns the length of the string, not counting the 0-terminator. Equivalent to size(). */ - uint length() const { return static_cast<uint>(m_rep.size()); } + uint32_t length() const { return static_cast<uint32_t>(m_rep.size()); } /** Returns the length of the string, not counting the 0-terminator. */ - uint size() const { return static_cast<uint>(m_rep.size()); } + uint32_t size() const { return static_cast<uint32_t>(m_rep.size()); } /** Returns a pointer to the contents of the string in the form of a 0-terminated C string */ const char *data() const { return m_rep.c_str(); } @@ -166,6 +163,9 @@ class QCString /** Truncates the string at position \a pos. */ bool truncate( size_t pos ) { return resize( pos + 1 ); } + /** Reserve space for \a size bytes without changing the string contents */ + void reserve( size_t size ) { m_rep.reserve(size); } + /** Fills a string with a predefined character * @param[in] c the character used to fill the string with. * @param[in] len the number of character to fill. Use -1 to fill the whole string. @@ -224,7 +224,7 @@ class QCString QCString mid( size_t index, size_t len=static_cast<size_t>(-1)) const { size_t slen = m_rep.size(); - if (len==static_cast<uint>(-1)) len = slen-index; + if (len==static_cast<uint32_t>(-1)) len = slen-index; return m_rep.empty() || index>slen || len==0 ? QCString() : QCString(m_rep.substr(index,len)); } @@ -265,7 +265,8 @@ class QCString { while (i<end && !needsQuotes) // check if the to be quoted part has at least one whitespace character { - needsQuotes = qisspace(m_rep[i++]); + needsQuotes = m_rep[i] =='-'; + needsQuotes |= qisspace(m_rep[i++]); } } QCString result(m_rep.substr(start,1+end-start)); @@ -297,6 +298,18 @@ class QCString /// whitespace characters replaced by a single space QCString simplifyWhiteSpace() const; + // Returns a copy of this string repeated n times + QCString repeat(unsigned int n) const + { + QCString result(n * size() + 1); + size_t offset = 0; + for (offset = 0; offset < n * size(); offset += size()) + { + memcpy(result.rawData() + offset, data(), size()); + } + return result; + } + QCString &insert( size_t index, const QCString &s ) { if (s.length()>0) @@ -387,13 +400,13 @@ class QCString QCString &replace( size_t index, size_t len, const char *s); //QCString &replace( const QRegExp &rx, const char *str ); - short toShort( bool *ok=0, int base=10 ) const; - ushort toUShort( bool *ok=0, int base=10 ) const; - int toInt( bool *ok=0, int base=10 ) const; - uint toUInt( bool *ok=0, int base=10 ) const; - long toLong( bool *ok=0, int base=10 ) const; - ulong toULong( bool *ok=0, int base=10 ) const; - uint64 toUInt64( bool *ok=0, int base=10 ) const; + short toShort( bool *ok=0, int base=10 ) const; + uint16_t toUShort( bool *ok=0, int base=10 ) const; + int toInt( bool *ok=0, int base=10 ) const; + uint32_t toUInt( bool *ok=0, int base=10 ) const; + long toLong( bool *ok=0, int base=10 ) const; + unsigned long toULong( bool *ok=0, int base=10 ) const; + uint64_t toUInt64( bool *ok=0, int base=10 ) const; QCString &setNum(short n) { @@ -401,7 +414,7 @@ class QCString return *this; } - QCString &setNum(ushort n) + QCString &setNum(uint16_t n) { m_rep = std::to_string(n); return *this; @@ -413,7 +426,7 @@ class QCString return *this; } - QCString &setNum(uint n) + QCString &setNum(uint32_t n) { m_rep = std::to_string(n); return *this; @@ -425,7 +438,19 @@ class QCString return *this; } - QCString &setNum(ulong n) + QCString &setNum(long long n) + { + m_rep = std::to_string(n); + return *this; + } + + QCString &setNum(unsigned long long n) + { + m_rep = std::to_string(n); + return *this; + } + + QCString &setNum(unsigned long n) { m_rep = std::to_string(n); return *this; @@ -594,24 +619,6 @@ inline QCString operator+( const char *s1, const QCString &s2 ) return tmp; } -#define HAD_PLUS_OPERATOR_FOR_CHAR 0 -#if HAS_PLUS_OPERATOR_FOR_CHAR -inline QCString operator+( const QCString &s1, char c2 ) -{ - QCString tmp( s1.data() ); - tmp.append(c2); - return tmp; -} - -inline QCString operator+( char c1, const QCString &s2 ) -{ - QCString tmp; - tmp.append(c1); - tmp.append(s2); - return tmp; -} -#endif - inline const char *qPrint(const char *s) { if (s) return s; else return ""; |