diff options
| author | day <day@national.shitposting.agency> | 2026-03-16 16:25:49 +0100 |
|---|---|---|
| committer | day <day@national.shitposting.agency> | 2026-03-16 16:25:49 +0100 |
| commit | 7f85c9fc75bd62ac09ea4457d3b17f85988fca66 (patch) | |
| tree | 15248e42bfafc6bd19e50c9010b701057958ff3a /src/util/string.h | |
| parent | 872c39b24ecf4063f785ff3e8b2f940acd8c2d59 (diff) | |
| parent | 991352b0d2767e6bd1a46f554db4ac9d208c13ad (diff) | |
Merge remote-tracking branch 'origin/master' into obj
Diffstat (limited to 'src/util/string.h')
| -rw-r--r-- | src/util/string.h | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/src/util/string.h b/src/util/string.h index 2268a36..672fb5d 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -55,8 +55,6 @@ template <typename CT> struct __str : public LIST<CT> { __str() : LIST<CT>() {} __str( U32 count, const CT* str ) : LIST<CT>() { - this->data = 0; - this->capacity = 0; this->reserve( count * 2 ); memcpy( this->data, str, count ); this->size = count; @@ -69,8 +67,6 @@ struct __str : public LIST<CT> { va_copy( args2, args ); U32 c = vsnprintf( 0, 0, fmt, args ); va_end( args ); - this->data = 0; - this->capacity = 0; this->reserve( c * 2 ); vsnprintf( this->data, c + 1, fmt, args2 ); this->data[c] = 0; @@ -89,8 +85,6 @@ struct __str : public LIST<CT> { if( this->data && this->data != other.data ) free( this->data ); - this->data = 0; - if( !other.capacity || !other.size ) { this->capacity = 1; this->size = 0; @@ -116,8 +110,9 @@ struct __str : public LIST<CT> { __str<CT> operator+( const CT rhs ) { __str<CT> ret = *this; ret.push( rhs ); return ret; } __str<CT>& operator+=( const __str<CT>& rhs ) { return this->append( rhs ); } __str<CT>& operator+=( const CT* rhs ) { return this->append( rhs ); } - __str<CT>& operator+=( const CT rhs ) { this->push( rhs ); return this; } + __str<CT>& operator+=( const CT rhs ) { this->push( rhs ); return *this; } + operator const CT*() { return this->data; } operator CT*() { return this->data; } CT operator[]( U32 i ) { @@ -128,10 +123,8 @@ struct __str : public LIST<CT> { if( rhs.size != this->size ) return 0; - for( U32 i = 0; i < this->size; ++i ) { - if( this->data[i] != rhs.data[i] ) - return 0; - } + if( !!memcmp( this->data, rhs.data, this->size ) ) + return 0; return 1; } @@ -165,7 +158,7 @@ struct __str : public LIST<CT> { __str<CT>& append( const CT* str ) { U32 len; for( len = 0; !!str[len]; ++len ); - if( this->size + len > this->capacity ) + if( this->size + len + 1 >= this->capacity ) this->reserve( this->size + len + 1 ); memcpy( this->data + this->size, str, len * sizeof(CT) ); @@ -179,7 +172,7 @@ struct __str : public LIST<CT> { if( this->size + len + 1 >= this->capacity ) this->reserve( this->size + len + 1 ); - memcpy( this->data + this->size, str, len * sizeof(CT) ); + memcpy( this->data + this->size, str.data, len * sizeof(CT) ); this->size += len; this->data[this->size] = 0; return *this; @@ -281,3 +274,11 @@ struct __str : public LIST<CT> { using STR = __str<char>; using WSTR = __str<wchar_t>; + +inline STR to_str( F32 f ) { return STR( "%.02f", f ); } +inline STR to_str( F64 f ) { return STR( "%.02g", f ); } +inline STR to_str( I32 i ) { return STR( "%d", i ); } +inline STR to_str( I64 i ) { return STR( "%lld", i ); } +inline STR to_str( U32 u ) { return STR( "%u", u ); } +inline STR to_str( U64 u ) { return STR( "%llu", u ); } +inline STR to_str( void* p ) { return STR( "%llx", (U64)p ); } |
