2016-06-26 11:54:21 +08:00
/* ubytearray.h
Copyright ( c ) 2016 , Nikolaj Schlej . All rights reserved .
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution . The full text of the license may be found at
http : //opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN " AS IS " BASIS ,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND , EITHER EXPRESS OR IMPLIED .
*/
# ifndef UBYTEARRAY_H
# define UBYTEARRAY_H
2016-07-07 13:57:45 +08:00
# if defined(QT_CORE_LIB)
2016-06-26 11:54:21 +08:00
// Use Qt class, if Qt is available
# include <QByteArray>
# define UByteArray QByteArray
# else
// Use own implementation
2016-10-10 14:05:04 +08:00
# include <cstdint>
2016-06-26 11:54:21 +08:00
# include <string>
# include <vector>
2016-07-16 13:42:03 +08:00
# include <algorithm>
2016-06-26 11:54:21 +08:00
class UByteArray
{
public :
UByteArray ( ) : d ( ) { }
UByteArray ( const UByteArray & ba ) : d ( ba . d ) { }
UByteArray ( const std : : basic_string < char > & bs ) : d ( bs ) { }
UByteArray ( const std : : vector < char > & bc ) : d ( bc . data ( ) , bc . size ( ) ) { }
UByteArray ( const char * bytes , int32_t size ) : d ( bytes , size ) { }
2016-10-10 14:05:04 +08:00
UByteArray ( const size_t n , char c ) : d ( n , c ) { }
2016-06-26 11:54:21 +08:00
~ UByteArray ( ) { }
bool isEmpty ( ) const { return d . length ( ) = = 0 ; }
2016-07-16 14:59:29 +08:00
char * data ( ) { return d . length ( ) = = 0 ? NULL : & ( d . at ( 0 ) ) ; /* Feels dirty, but works for all basic_string implementations I know, is fully OK in C++11 and later*/ }
2016-06-26 11:54:21 +08:00
const char * data ( ) const { return d . c_str ( ) ; }
const char * constData ( ) const { return d . c_str ( ) ; }
void clear ( ) { d . clear ( ) ; }
2016-10-10 14:05:04 +08:00
UByteArray toUpper ( ) { std : : basic_string < char > s = d ; std : : transform ( s . begin ( ) , s . end ( ) , s . begin ( ) , : : toupper ) ; return UByteArray ( s ) ; }
uint32_t toUInt ( bool * ok = NULL , const uint8_t base = 10 ) { return ( uint32_t ) std : : strtoul ( d . c_str ( ) , NULL , base ) ; }
2016-06-26 11:54:21 +08:00
int32_t size ( ) const { return d . size ( ) ; }
2016-07-16 14:59:29 +08:00
int32_t count ( char ch ) const { return std : : count ( d . begin ( ) , d . end ( ) , ch ) ; }
2016-06-26 11:54:21 +08:00
char at ( uint32_t i ) const { return d . at ( i ) ; }
char operator [ ] ( uint32_t i ) const { return d [ i ] ; }
char & operator [ ] ( uint32_t i ) { return d [ i ] ; }
bool startsWith ( const UByteArray & ba ) const { return 0 = = d . find ( ba . d , 0 ) ; }
int indexOf ( const UByteArray & ba , int from = 0 ) const { return d . find ( ba . d , from ) ; }
2016-10-10 14:05:04 +08:00
int lastIndexOf ( const UByteArray & ba , int from = 0 ) const {
size_t old_index = d . npos ;
size_t index = d . find ( ba . d , from ) ;
while ( index ! = d . npos ) {
old_index = index ;
index = d . find ( ba . d , index + 1 ) ;
}
return old_index ;
}
2016-06-26 11:54:21 +08:00
UByteArray left ( int32_t len ) const { return d . substr ( 0 , len ) ; }
2016-10-10 14:05:04 +08:00
UByteArray right ( int32_t len ) const { return d . substr ( d . size ( ) - 1 - len , len ) ; }
UByteArray mid ( int32_t pos , int32_t len = - 1 ) const { return d . substr ( pos , len ) ; }
2016-06-26 11:54:21 +08:00
2016-07-05 23:22:03 +08:00
UByteArray & operator = ( const UByteArray & ba ) { d = ba . d ; return * this ; }
UByteArray & operator + = ( const UByteArray & ba ) { d + = ba . d ; return * this ; }
2016-06-26 11:54:21 +08:00
bool operator = = ( const UByteArray & ba ) const { return d = = ba . d ; }
bool operator ! = ( const UByteArray & ba ) const { return d ! = ba . d ; }
inline void swap ( UByteArray & other ) { std : : swap ( d , other . d ) ; }
2016-07-05 23:22:03 +08:00
UByteArray toHex ( ) {
2016-07-16 14:59:29 +08:00
std : : basic_string < char > hex ( size ( ) * 2 , ' \x00 ' ) ;
2016-07-07 13:57:45 +08:00
for ( int32_t i = 0 ; i < size ( ) ; i + + ) {
2016-07-05 23:22:03 +08:00
uint8_t low = d [ i ] & 0x0F ;
uint8_t high = ( d [ i ] & 0xF0 ) > > 4 ;
2016-07-07 13:57:45 +08:00
low + = ( low < 10 ? ' 0 ' : ' a ' ) ;
high + = ( high < 10 ? ' 0 ' : ' a ' ) ;
hex [ 2 * i ] = low ;
hex [ 2 * i + 1 ] = high ;
2016-07-05 23:22:03 +08:00
}
std : : reverse ( hex . begin ( ) , hex . end ( ) ) ;
return UByteArray ( hex ) ;
}
2016-06-26 11:54:21 +08:00
private :
std : : basic_string < char > d ;
} ;
2016-07-05 23:22:03 +08:00
inline const UByteArray operator + ( const UByteArray & a1 , const UByteArray & a2 )
{
return UByteArray ( a1 ) + = a2 ;
}
2016-06-26 11:54:21 +08:00
# endif // QT_CORE_LIB
# endif // UBYTEARRAY_H