diff options
| author | aura <nw@moneybot.cc> | 2026-03-11 00:41:44 +0100 |
|---|---|---|
| committer | aura <nw@moneybot.cc> | 2026-03-11 00:41:44 +0100 |
| commit | 4c0ab7a739085a32688f34947c0b1812a31d92d7 (patch) | |
| tree | 3b32fa706a61438160433c51585c8d3a70a5ab88 /src/util/string.h | |
| parent | d1233da64ae31a381fd484b446e87c2c55ed02b8 (diff) | |
fix mem leaks, string convenience funcs
Diffstat (limited to 'src/util/string.h')
| -rw-r--r-- | src/util/string.h | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/src/util/string.h b/src/util/string.h index 0781fca..04bd9c6 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -54,6 +54,14 @@ struct ARRSTR { 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; + this->data[this->size] = 0; + } __str( const CT* fmt, ... ) : LIST<CT>() { va_list args; va_start( args, fmt ); @@ -62,6 +70,7 @@ struct __str : public LIST<CT> { 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; @@ -187,7 +196,10 @@ struct __str : public LIST<CT> { return idx_of( str, 0 ); } - U32 idx_of( const CT* str, U32 offset ) { + U32 idx_of( const CT* str, I32 offset ) { + if( offset < 0 ) + offset = this->size + offset; + for( U32 i = offset; i < this->size; ++i ) { U8 found = 1; for( U32 i2 = 0; !!str[i2] && i + i2 < this->size; ++i2 ) { @@ -217,6 +229,47 @@ struct __str : public LIST<CT> { return 0; } + LIST<__str<CT>> split( const char* where ) { + LIST<__str<CT>> ret; + U32 slen = strlen_ct( where ); + + U32 last = 0; + for( U32 i = 0; i < this->size; ++i ) { + for( U32 i2 = 0; i2 < slen; ++i2 ) { + if( this->data[i + i2] != where[i2] ) + break; + + if( i2 == slen - 1 ) { + if( !(last - i) ) + ret.push( "" ); + else + ret.push( __str<CT>( i - last, this->data + last ) ); + i += slen; + last = i; + } + } + } + + if( last < this->size - slen ) { + if( !( this->size - last ) ) + ret.push( "" ); + else + ret.push( __str<CT>( this->size - last, this->data + last ) ); + } + + return ret; + } + + // can take negative input as offset from end + __str<CT> substr( I32 start, I32 end ) { + if( start < 0 ) + start = this->size + start; + if( end < 0 ) + end = this->size + end; + + return __str<CT>( end - start, this->data + start ); + } + LIST_ITERATOR<CT> end() { return LIST_ITERATOR<CT>( this->data + this->size ); } |
