1- #include <sys/types.h>
2- #include <stdlib.h>
3- #include <stdio.h>
4- #include <string.h>
5- #include <ctype.h>
6- #define KEEPALIVE __attribute__((used))
7-
8- typedef struct {
9- char * buffer ;
10- size_t size ;
11- size_t capacity ;
12- } ss_string ;
1+ #include "ss_string.h"
132
143size_t KEEPALIVE ss_string_get_capacity_with_size (size_t size ) {
154 if (size == 0 ) {
@@ -23,8 +12,8 @@ size_t KEEPALIVE ss_string_get_capacity_with_size(size_t size) {
2312 }
2413}
2514
26- ss_string * KEEPALIVE ss_string_create_with_capacity (size_t capacity ) {
27- ss_string * str = (ss_string * ) malloc (sizeof (ss_string ));
15+ ss_string * KEEPALIVE ss_string_create_with_capacity (size_t capacity ) {
16+ ss_string * str = (ss_string * ) malloc (sizeof (ss_string ));
2817 if (!str ) {
2918 return NULL ;
3019 }
@@ -35,10 +24,10 @@ ss_string* KEEPALIVE ss_string_create_with_capacity(size_t capacity) {
3524 return str ;
3625}
3726
38- ss_string * KEEPALIVE ss_string_create (const char * literal ) {
27+ ss_string * KEEPALIVE ss_string_create (const char * literal ) {
3928 size_t size = strlen (literal );
4029 size_t capacity = ss_string_get_capacity_with_size (size );
41- ss_string * str = ss_string_create_with_capacity (capacity );
30+ ss_string * str = ss_string_create_with_capacity (capacity );
4231 if (!str ) {
4332 return NULL ;
4433 }
@@ -47,16 +36,16 @@ ss_string* KEEPALIVE ss_string_create(const char *literal) {
4736 return str ;
4837}
4938
50- void KEEPALIVE ss_string_delete (ss_string * str ) {
39+ void KEEPALIVE ss_string_delete (ss_string * str ) {
5140 free (str -> buffer );
5241 free (str );
5342}
5443
55- size_t KEEPALIVE ss_string_get_size (ss_string * str ) {
44+ size_t KEEPALIVE ss_string_get_size (ss_string * str ) {
5645 return str -> size ;
5746}
5847
59- int KEEPALIVE ss_string_grow_with_capacity (ss_string * str , size_t new_capacity ) {
48+ long KEEPALIVE ss_string_grow_with_capacity (ss_string * str , size_t new_capacity ) {
6049 char * new_buffer = (char * ) calloc (new_capacity , 1 );
6150 if (!new_buffer ) {
6251 return -1 ;
@@ -69,7 +58,7 @@ int KEEPALIVE ss_string_grow_with_capacity(ss_string* str, size_t new_capacity)
6958 return 0 ;
7059}
7160
72- int KEEPALIVE ss_string_grow (ss_string * str ) {
61+ long KEEPALIVE ss_string_grow (ss_string * str ) {
7362 if (str -> capacity < 32 ) {
7463 str -> capacity += 16 ;
7564 } else {
@@ -78,44 +67,46 @@ int KEEPALIVE ss_string_grow(ss_string* str) {
7867 return ss_string_grow_with_capacity (str , str -> capacity );
7968}
8069
81- int KEEPALIVE ss_string_append_with_literal (ss_string * str , const char * literal ) {
82- size_t literal_size = strlen (literal );
83- size_t needed_size = str -> size + literal_size ;
70+ long KEEPALIVE ss_string_append (ss_string * dest , ss_string * src ) {
71+ size_t src_size = strlen (src -> buffer );
72+ size_t needed_size = dest -> size + src_size ;
8473 size_t needed_capacity = ss_string_get_capacity_with_size (needed_size );
85- if (str -> capacity < needed_capacity ) {
86- if (ss_string_grow_with_capacity (str , needed_capacity ) == -1 ) {
74+ if (dest -> capacity < needed_capacity ) {
75+ if (ss_string_grow_with_capacity (dest , needed_capacity ) == -1 ) {
8776 return -1 ;
8877 }
8978 }
90- strncat (str -> buffer , literal , literal_size );
91- str -> size = needed_size ;
79+ strncat (dest -> buffer , src -> buffer , src_size );
80+ dest -> size = needed_size ;
9281 return 0 ;
9382}
9483
95- int KEEPALIVE ss_string_append (ss_string * dest , ss_string * src ) {
96- return ss_string_append_with_literal (dest , src -> buffer );
97- }
98-
99- int KEEPALIVE ss_string_prepend_with_literal (ss_string * str , const char * literal ) {
100- size_t literal_size = strlen (literal );
101- size_t needed_size = str -> size + literal_size ;
84+ long KEEPALIVE ss_string_prepend (ss_string * dest , ss_string * src ) {
85+ size_t src_size = strlen (src -> buffer );
86+ size_t needed_size = dest -> size + src_size ;
10287 size_t needed_capacity = ss_string_get_capacity_with_size (needed_size );
103- if (str -> capacity < needed_capacity ) {
104- if (ss_string_grow_with_capacity (str , needed_capacity ) == -1 ) {
88+ if (dest -> capacity < needed_capacity ) {
89+ if (ss_string_grow_with_capacity (dest , needed_capacity ) == -1 ) {
10590 return -1 ;
10691 }
10792 }
108- memmove (str -> buffer + literal_size , str -> buffer , literal_size + 1 );
109- memcpy (str -> buffer , literal , literal_size );
110- str -> size = needed_size ;
93+ memmove (dest -> buffer + src_size , dest -> buffer , src_size + 1 );
94+ memcpy (dest -> buffer , src -> buffer , src_size );
95+ dest -> size = needed_size ;
11196 return 0 ;
11297}
11398
114- int KEEPALIVE ss_string_prepend (ss_string * dest , ss_string * src ) {
115- return ss_string_prepend_with_literal (dest , src -> buffer );
99+ ss_string * ss_string_concat (ss_string * str1 , ss_string * str2 ) {
100+ size_t size = str1 -> size + str2 -> size ;
101+ size_t capacity = ss_string_get_capacity_with_size (size );
102+ ss_string * str = ss_string_create_with_capacity (capacity );
103+ memcpy (str -> buffer , str1 -> buffer , str1 -> size );
104+ memcpy (str -> buffer + str1 -> size , str2 -> buffer , str2 -> size );
105+ str -> size = size ;
106+ return str ;
116107}
117108
118- ss_string * KEEPALIVE ss_string_slice (ss_string * str , ssize_t from , ssize_t to ) {
109+ ss_string * KEEPALIVE ss_string_slice (ss_string * str , ssize_t from , ssize_t to ) {
119110 if (from < 0 ) {
120111 from = str -> size + from ;
121112 }
@@ -127,30 +118,30 @@ ss_string* KEEPALIVE ss_string_slice(ss_string* str, ssize_t from, ssize_t to) {
127118 }
128119 size_t slice_size = to - from ;
129120 size_t new_capacity = ss_string_get_capacity_with_size (slice_size );
130- ss_string * new_str = ss_string_create_with_capacity (new_capacity );
121+ ss_string * new_str = ss_string_create_with_capacity (new_capacity );
131122 memcpy (new_str -> buffer , str -> buffer + from , slice_size );
132123 new_str -> size = slice_size ;
133124 return new_str ;
134125}
135126
136- int KEEPALIVE ss_string_equals (ss_string * str1 , ss_string * str2 ) {
127+ long KEEPALIVE ss_string_equals (ss_string * str1 , ss_string * str2 ) {
137128 size_t max_size = str1 -> size > str2 -> size ? str1 -> size : str2 -> size ;
138129 return strncmp (str1 -> buffer , str2 -> buffer , max_size ) == 0 ;
139130}
140131
141- ssize_t KEEPALIVE ss_string_index_of_with_literal (ss_string * str , const char * literal ) {
132+ ssize_t KEEPALIVE ss_string_index_of_with_literal (ss_string * str , const char * literal ) {
142133 char * sub = strstr (str -> buffer , literal );
143134 if (!sub ) {
144135 return -1 ;
145136 }
146137 return sub - str -> buffer ;
147138}
148139
149- ssize_t KEEPALIVE ss_string_index_of (ss_string * str , ss_string * substr ) {
140+ ssize_t KEEPALIVE ss_string_index_of (ss_string * str , ss_string * substr ) {
150141 return ss_string_index_of_with_literal (str , substr -> buffer );
151142}
152143
153- int KEEPALIVE ss_string_trim_left (ss_string * str ) {
144+ long KEEPALIVE ss_string_trim_left (ss_string * str ) {
154145 size_t i = 0 ;
155146 while (isspace (str -> buffer [i ])) {
156147 i += 1 ;
@@ -167,7 +158,7 @@ int KEEPALIVE ss_string_trim_left(ss_string* str) {
167158 return 0 ;
168159}
169160
170- void KEEPALIVE ss_string_trim_right (ss_string * str ) {
161+ void KEEPALIVE ss_string_trim_right (ss_string * str ) {
171162 ssize_t i = str -> size - 1 ;
172163 while (i >= 0 && isspace (str -> buffer [i ])) {
173164 str -> buffer [i ] = 0 ;
@@ -176,11 +167,7 @@ void KEEPALIVE ss_string_trim_right(ss_string* str) {
176167 str -> size = i + 1 ;
177168}
178169
179- int KEEPALIVE ss_string_trim (ss_string * str ) {
170+ long KEEPALIVE ss_string_trim (ss_string * str ) {
180171 ss_string_trim_right (str );
181172 return ss_string_trim_left (str );
182173}
183-
184- void KEEPALIVE ss_console_log (ss_string * str ) {
185- printf ("%s\n" , str -> buffer );
186- }
0 commit comments