From e1115a28535663cfe404791ede5bb7ca70399053 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 24 Apr 2023 09:16:32 -0700 Subject: Additions to blob/utfstring for json. --- src/unstable/blob.cpp | 47 +++++++++++++++++++++++++++++--- src/unstable/blob.h | 10 ++++--- src/unstable/settingsdrivertaf.cpp | 6 +++-- src/unstable/utfstring.cpp | 55 +++++++++++++++++++++++--------------- src/unstable/utfstring.h | 29 +++++++++++++------- 5 files changed, 107 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/unstable/blob.cpp b/src/unstable/blob.cpp index e72e265..23987f3 100644 --- a/src/unstable/blob.cpp +++ b/src/unstable/blob.cpp @@ -499,6 +499,26 @@ Bu::Blob::iterator &Bu::Blob::iterator::operator--() return *this; } +Bu::Blob::iterator &Bu::Blob::iterator::operator+=( int32_t iDelta ) +{ + if( bForward ) + iIndex += iDelta; + else + iIndex -= iDelta; + + return *this; +} + +Bu::Blob::iterator &Bu::Blob::iterator::operator-=( int32_t iDelta ) +{ + if( bForward ) + iIndex -= iDelta; + else + iIndex += iDelta; + + return *this; +} + Bu::Blob::iterator Bu::Blob::iterator::operator+( int32_t iDelta ) const { return iterator( pBlob, iIndex + iDelta, bForward ); @@ -541,7 +561,8 @@ bool Bu::Blob::iterator::operator!=( const Bu::Blob::const_iterator &rRhs ) return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; } -Bu::Blob::iterator &Bu::Blob::iterator::operator=( Bu::Blob::iterator &rRhs ) +Bu::Blob::iterator &Bu::Blob::iterator::operator=( + const Bu::Blob::iterator &rRhs ) { pBlob = rRhs.pBlob; iIndex = rRhs.iIndex; @@ -651,6 +672,26 @@ Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator--() return *this; } +Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator+=( int32_t iDelta ) +{ + if( bForward ) + iIndex += iDelta; + else + iIndex -= iDelta; + + return *this; +} + +Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator-=( int32_t iDelta ) +{ + if( bForward ) + iIndex -= iDelta; + else + iIndex += iDelta; + + return *this; +} + Bu::Blob::const_iterator Bu::Blob::const_iterator::operator+( int32_t iDelta ) const { @@ -698,7 +739,7 @@ bool Bu::Blob::const_iterator::operator!=( } Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( - Bu::Blob::iterator &rRhs ) + const Bu::Blob::iterator &rRhs ) { pBlob = rRhs.pBlob; iIndex = rRhs.iIndex; @@ -708,7 +749,7 @@ Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( } Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( - Bu::Blob::const_iterator &rRhs ) + const Bu::Blob::const_iterator &rRhs ) { pBlob = rRhs.pBlob; iIndex = rRhs.iIndex; diff --git a/src/unstable/blob.h b/src/unstable/blob.h index d6c40e3..bda29d6 100644 --- a/src/unstable/blob.h +++ b/src/unstable/blob.h @@ -100,6 +100,8 @@ namespace Bu iterator &operator++(); iterator &operator--( int32_t ); iterator &operator--(); + iterator &operator+=( int32_t iDelta ); + iterator &operator-=( int32_t iDelta ); iterator operator+( int32_t iDelta ) const; iterator operator-( int32_t iDelta ) const; bool operator==( const iterator &rRhs ); @@ -107,7 +109,7 @@ namespace Bu bool operator!=( const iterator &rRhs ); bool operator!=( const const_iterator &rRhs ); - iterator &operator=( iterator &rRhs ); + iterator &operator=( const iterator &rRhs ); private: Blob *pBlob; @@ -136,6 +138,8 @@ namespace Bu const_iterator &operator++(); const_iterator &operator--( int32_t ); const_iterator &operator--(); + const_iterator &operator+=( int32_t iDelta ); + const_iterator &operator-=( int32_t iDelta ); const_iterator operator+( int32_t iDelta ) const; const_iterator operator-( int32_t iDelta ) const; bool operator==( const iterator &rRhs ); @@ -143,8 +147,8 @@ namespace Bu bool operator!=( const iterator &rRhs ); bool operator!=( const const_iterator &rRhs ); - const_iterator &operator=( iterator &rRhs ); - const_iterator &operator=( const_iterator &rRhs ); + const_iterator &operator=( const iterator &rRhs ); + const_iterator &operator=( const const_iterator &rRhs ); private: const Blob *pBlob; diff --git a/src/unstable/settingsdrivertaf.cpp b/src/unstable/settingsdrivertaf.cpp index 3fd61b3..d99d0ba 100644 --- a/src/unstable/settingsdrivertaf.cpp +++ b/src/unstable/settingsdrivertaf.cpp @@ -48,7 +48,7 @@ void Bu::SettingsDriverTaf::init( const Bu::UtfString &sCompany, void Bu::SettingsDriverTaf::set( const Bu::UtfString &sKey, const Bu::UtfString &sValue ) { - Bu::StringList lPath = sKey.get().split('/'); + Bu::StringList lPath = Bu::String(sKey.get()).split('/'); Bu::StringList::iterator i = lPath.begin(); Bu::StringList::iterator in; Bu::TafGroup *pGrp = pRoot; @@ -74,7 +74,7 @@ void Bu::SettingsDriverTaf::set( const Bu::UtfString &sKey, Bu::UtfString Bu::SettingsDriverTaf::get( const Bu::UtfString &sKey, const Bu::UtfString &sValue ) { - Bu::StringList lPath = sKey.get().split('/'); + Bu::StringList lPath = Bu::String(sKey.get()).split('/'); Bu::StringList::iterator i = lPath.begin(); Bu::StringList::iterator in; Bu::TafGroup *pGrp = pRoot; @@ -98,5 +98,7 @@ Bu::UtfString Bu::SettingsDriverTaf::get( const Bu::UtfString &sKey, } i = in; } + + return Bu::UtfString(); } diff --git a/src/unstable/utfstring.cpp b/src/unstable/utfstring.cpp index 21b3f82..6217f80 100644 --- a/src/unstable/utfstring.cpp +++ b/src/unstable/utfstring.cpp @@ -8,6 +8,7 @@ #include "bu/utfstring.h" #include "bu/string.h" +#include "bu/blob.h" #include "bu/stream.h" #include "bu/config.h" #include "bu/sio.h" @@ -36,11 +37,16 @@ Bu::UtfString::UtfString( const Bu::String &sInput, Encoding eEnc ) set( sInput, eEnc ); } -Bu::UtfString::UtfString( const char *sInput, Encoding eEnc ) +Bu::UtfString::UtfString( const Bu::Blob &sInput, Encoding eEnc ) { set( sInput, eEnc ); } +Bu::UtfString::UtfString( const char *sInput, Encoding eEnc ) +{ + set( Bu::Blob(sInput), eEnc ); +} + Bu::UtfString::~UtfString() { } @@ -56,6 +62,11 @@ Bu::UtfString::const_iterator Bu::UtfString::begin() const } void Bu::UtfString::set( const Bu::String &sInput, Encoding eEnc ) +{ + set( sInput, eEnc ); +} + +void Bu::UtfString::set( const Bu::Blob &sInput, Encoding eEnc ) { switch( eEnc ) { @@ -122,9 +133,9 @@ void Bu::UtfString::append( const UtfString &rSrc ) iCharLen += rSrc.iCharLen; } -void Bu::UtfString::setUtf8( const Bu::String &sInput ) +void Bu::UtfString::setUtf8( const Bu::Blob &sInput ) { - for( Bu::String::const_iterator i = sInput.begin(); i; i++ ) + for( Bu::Blob::const_iterator i = sInput.begin(); i; i++ ) { if( ((int)(uint8_t)*i)&0x80 ) { @@ -145,9 +156,9 @@ void Bu::UtfString::setUtf8( const Bu::String &sInput ) } } -void Bu::UtfString::setUtf16( const Bu::String &sInput ) +void Bu::UtfString::setUtf16( const Bu::Blob &sInput ) { -// Bu::String::const_iterator i = sInput.begin(); +// Bu::Blob::const_iterator i = sInput.begin(); if( (uint8_t)*sInput.begin() == 0xFF && (uint8_t)*(sInput.begin()+1) == 0xFE ) { @@ -157,9 +168,9 @@ void Bu::UtfString::setUtf16( const Bu::String &sInput ) setUtf16be( sInput ); } -void Bu::UtfString::setUtf16be( const Bu::String &sInput ) +void Bu::UtfString::setUtf16be( const Bu::Blob &sInput ) { - Bu::String::const_iterator i = sInput.begin(); + Bu::Blob::const_iterator i = sInput.begin(); if( (uint8_t)*sInput.begin() == 0xFE && (uint8_t)*(sInput.begin()+1) == 0xFF ) @@ -184,9 +195,9 @@ void Bu::UtfString::setUtf16be( const Bu::String &sInput ) } } -void Bu::UtfString::setUtf16le( const Bu::String &sInput ) +void Bu::UtfString::setUtf16le( const Bu::Blob &sInput ) { - Bu::String::const_iterator i = sInput.begin(); + Bu::Blob::const_iterator i = sInput.begin(); if( (uint8_t)*sInput.begin() == 0xFF && (uint8_t)*(sInput.begin()+1) == 0xFE ) { @@ -210,9 +221,9 @@ void Bu::UtfString::setUtf16le( const Bu::String &sInput ) } } -void Bu::UtfString::setUtf32( const Bu::String &sInput ) +void Bu::UtfString::setUtf32( const Bu::Blob &sInput ) { - Bu::String::const_iterator i = sInput.begin(); + Bu::Blob::const_iterator i = sInput.begin(); if( (uint8_t)*i == 0x00 && (uint8_t)*(++i) == 0x00 && (uint8_t)*(++i) == 0xFF && @@ -224,9 +235,9 @@ void Bu::UtfString::setUtf32( const Bu::String &sInput ) setUtf32be( sInput ); } -void Bu::UtfString::setUtf32be( const Bu::String &sInput ) +void Bu::UtfString::setUtf32be( const Bu::Blob &sInput ) { - Bu::String::const_iterator i = sInput.begin(); + Bu::Blob::const_iterator i = sInput.begin(); if( (uint8_t)*i == 0x00 && (uint8_t)*(++i) == 0x00 && (uint8_t)*(++i) == 0xFE && @@ -250,9 +261,9 @@ void Bu::UtfString::setUtf32be( const Bu::String &sInput ) } } -void Bu::UtfString::setUtf32le( const Bu::String &sInput ) +void Bu::UtfString::setUtf32le( const Bu::Blob &sInput ) { - Bu::String::const_iterator i = sInput.begin(); + Bu::Blob::const_iterator i = sInput.begin(); if( (uint8_t)*i == 0x00 && (uint8_t)*(++i) == 0x00 && (uint8_t)*(++i) == 0xFF && @@ -443,12 +454,12 @@ int Bu::UtfString::writePoint( Bu::Stream &sOut, const Bu::UtfChar &c, int32_t Bu::UtfString::toInt32( int iRadix ) const { - return strtol( get().getStr(), NULL, iRadix ); + return strtol( get().getData(), NULL, iRadix ); } int64_t Bu::UtfString::toInt64( int iRadix ) const { - return strtoll( get().getStr(), NULL, iRadix ); + return strtoll( get().getData(), NULL, iRadix ); } void Bu::UtfString::writeUtf8( Bu::Stream &sOut ) const @@ -626,7 +637,7 @@ bool Bu::UtfString::operator==( const Bu::UtfString &rhs ) const return aData == rhs.aData; } -bool Bu::UtfString::operator==( const Bu::String &rhs ) const +bool Bu::UtfString::operator==( const Bu::Blob &rhs ) const { // Nieve comparison if( aData.getSize() != rhs.getSize() ) @@ -718,11 +729,11 @@ bool Bu::UtfString::operator>=( const Bu::UtfString &rhs ) const return false; } -Bu::String Bu::UtfString::get( Encoding eEnc ) const +Bu::Blob Bu::UtfString::get( Encoding eEnc ) const { Bu::MemBuf mb; write( mb, eEnc ); - return mb.getString(); + return Bu::Blob( mb.getString().getStr(), mb.getString().getSize() ); } void Bu::UtfString::debug() const @@ -745,9 +756,9 @@ void Bu::UtfString::debug() const sio << sio.nl; } /* -void Bu::UtfString::debugUtf8( const Bu::String &sUtf8 ) +void Bu::UtfString::debugUtf8( const Bu::Blob &sUtf8 ) { - for( Bu::String::const_iterator i = sUtf8.begin(); i; i++ ) + for( Bu::Blob::const_iterator i = sUtf8.begin(); i; i++ ) { if( i != sUtf8.begin() ) sio << ", "; diff --git a/src/unstable/utfstring.h b/src/unstable/utfstring.h index 5a72948..3c64a75 100644 --- a/src/unstable/utfstring.h +++ b/src/unstable/utfstring.h @@ -72,6 +72,7 @@ namespace Bu UtfString(); UtfString( const Bu::String &sInput, Encoding eEnc=Utf8 ); + UtfString( const Bu::Blob &sInput, Encoding eEnc=Utf8 ); UtfString( const char *sInput, Encoding eEnc=Utf8 ); virtual ~UtfString(); @@ -182,6 +183,14 @@ namespace Bu * string literals in code. */ void set( const Bu::String &sInput, Encoding eEnc=Utf8 ); + + /** + * Set the value of the entire string based on the given input and + * encoding. The default encoding is Utf8, which is compatible with + * 7-bit ascii, so it's a great choice for setting UtfStrings from + * string literals in code. + */ + void set( const Bu::Blob &bInput, Encoding eEnc=Utf8 ); /** * This encodes the UtfString in the given encoding and outputs it to @@ -204,10 +213,10 @@ namespace Bu /** * This encodes the UtfString in the given encoding and returns it as - * a binary Bu::String. Like write, this also includes the proper BOM + * a binary Bu::Blob. Like write, this also includes the proper BOM * at the begining. */ - Bu::String get( Encoding eEnc=Utf8 ) const; + Bu::Blob get( Encoding eEnc=Utf8 ) const; void debug() const; @@ -228,7 +237,7 @@ namespace Bu UtfChar nextChar( int &iIndex ) const; bool operator==( const Bu::UtfString &rhs ) const; - bool operator==( const Bu::String &rhs ) const; + bool operator==( const Bu::Blob &rhs ) const; bool operator==( const char *rhs ) const; UtfString &operator+=( const Bu::UtfString &rhs ); UtfString &operator+=( const UtfChar &rhs ); @@ -241,13 +250,13 @@ namespace Bu private: void append16( uint16_t i ) { aData.append( i ); } - void setUtf8( const Bu::String &sInput ); - void setUtf16( const Bu::String &sInput ); - void setUtf16be( const Bu::String &sInput ); - void setUtf16le( const Bu::String &sInput ); - void setUtf32( const Bu::String &sInput ); - void setUtf32be( const Bu::String &sInput ); - void setUtf32le( const Bu::String &sInput ); + void setUtf8( const Bu::Blob &sInput ); + void setUtf16( const Bu::Blob &sInput ); + void setUtf16be( const Bu::Blob &sInput ); + void setUtf16le( const Bu::Blob &sInput ); + void setUtf32( const Bu::Blob &sInput ); + void setUtf32be( const Bu::Blob &sInput ); + void setUtf32le( const Bu::Blob &sInput ); void writeUtf8( Bu::Stream &sOut ) const; void writeUtf16be( Bu::Stream &sOut ) const; -- cgit v1.2.3