From 4c0ab7a739085a32688f34947c0b1812a31d92d7 Mon Sep 17 00:00:00 2001 From: aura Date: Wed, 11 Mar 2026 00:41:44 +0100 Subject: fix mem leaks, string convenience funcs --- src/util/string.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'src/util/string.h') 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 struct __str : public LIST { __str() : LIST() {} + __str( U32 count, const CT* str ) : LIST() { + 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() { va_list args; va_start( args, fmt ); @@ -62,6 +70,7 @@ struct __str : public LIST { 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 { 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 { return 0; } + LIST<__str> split( const char* where ) { + LIST<__str> 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( i - last, this->data + last ) ); + i += slen; + last = i; + } + } + } + + if( last < this->size - slen ) { + if( !( this->size - last ) ) + ret.push( "" ); + else + ret.push( __str( this->size - last, this->data + last ) ); + } + + return ret; + } + + // can take negative input as offset from end + __str substr( I32 start, I32 end ) { + if( start < 0 ) + start = this->size + start; + if( end < 0 ) + end = this->size + end; + + return __str( end - start, this->data + start ); + } + LIST_ITERATOR end() { return LIST_ITERATOR( this->data + this->size ); } -- cgit v1.2.3