Commit 9e46788

kravantokh <kravantokh@noreply.codeberg.org>
2026-09-01 20:29:52
Add splitting by u8 separator of StrSlc. Also fix a mistake.
1 parent 8b7ec38
Changed files (3)
testing/tests/015_array_basic.c
@@ -9,14 +9,18 @@ void PRESERVE_WITH_LTO entry(void) {
 	usizes *a = 0;
 	assert(S_OK( usizes_create( &h, 16, &a ) ));
 
+	for( usize i = 0; i < 8; ++i ) {
+		assert(S_OK( usizes_push( &a, &i, &h ) ));
+	}
+
 	/* Resize allocation to minimum needed size aka 16 elements + metadata */
 	assert(S_OK( usizes_deflate( &a, &h ) ));
 
 	/* Not much to assert here, since the heap may allocate more than asked for */
 
-	assert( a->len == 16 );
+	assert( a->len == 8 );
 	/* Truncate by cutting 16 elements off the end - this preserves allocation size, only changes logical length */
-	usizes_truncate( a, 16 );
+	usizes_truncate( a, 8 );
 	assert( a->len == 0 );
 
 	/* extend - make sure 24 more elemets will fit (0 + 24 = 24) */
notlibc.c
@@ -340,7 +340,7 @@ Status AnyArr_create( usize count, AnyArr **out, Heap *h, usize elem_size ) {
 	usize alloc_size = sizeof(AnyArr) + alloc_count*elem_size;
 
 	TRY( Heap_alloc( h, alloc_size, (void**)out ) );
-	(*out)->len = count;
+	(*out)->len = 0;
 	DEBUG(
 		(*out)->heap = h;
 		_AnyArr_update_checksum( *out )
@@ -667,6 +667,27 @@ u8 StrSlc_equals_Str_by_ref( const StrSlc *sl, const Str *s ) {
 	return 1;
 }
 
+Status StrSlc_split_by_u8( const StrSlc *sl, u8 separator, StrSlcs **out, Heap *h ) {
+	StrSlc new_sl = {
+		.data = sl->data,
+		.len = 0
+	};
+
+	TRY( StrSlcs_create( h, 16, out ) );
+
+	for( usize i = 0; i < sl->len; ++i ) {
+		if( sl->data[i] == separator ) {
+			TRY( StrSlcs_push( out, &new_sl, h ) );
+			new_sl.data = &(sl->data[i+1]);
+			new_sl.len = 0;
+		} else {
+			new_sl.len++;
+		}
+	}
+
+	return S_SUCCESS;
+}
+
 /* String Assembler */
 
 Status StrAsm_create( StrAsm *out, usize max_size ) {
@@ -757,6 +778,8 @@ Status StrAsm_cat_Str( StrAsm *sa, const Str *s ) {
 
 Status StrAsm_cat_StrSlc( StrAsm *sa, StrSlc sl ) {
 	u8 *data = 0;
+	if( sl.len == 0 ) return S_COMPLETION;
+
 	TRY( Arena_take( &sa->a, sl.len, &data ) );
 	mem_copy( sl.data, data, sl.len );
 	return S_SUCCESS;
@@ -764,6 +787,8 @@ Status StrAsm_cat_StrSlc( StrAsm *sa, StrSlc sl ) {
 
 Status StrAsm_cat_StrSlc_by_ref( StrAsm *sa, const StrSlc *sl ) {
 	u8 *data = 0;
+	if( sl->len == 0 ) return S_COMPLETION;
+
 	TRY( Arena_take( &sa->a, sl->len, &data ) );
 	mem_copy( sl->data, data, sl->len );
 	return S_SUCCESS;
notlibc.h
@@ -1326,10 +1326,11 @@ Status StrSlc_from_Str( StrSlc *out, const Str *s, usize start, usize len );
 u8 StrSlc_equals( const StrSlc sl1, const StrSlc sl2 );
 u8 StrSlc_equals_by_ref( const StrSlc *sl1, const StrSlc *sl2 );
 
-
 u8 StrSlc_equals_Str( const StrSlc sl, const Str* s );
 u8 StrSlc_equals_Str_by_ref( const StrSlc *sl, const Str *s );
 
+Status StrSlc_split_by_u8( const StrSlc *sl, u8 separator, StrSlcs **out, Heap *h );
+
 /* String assembler */
 STRUCT( StrAsm,
 	Arena a;