str_trim
This commit is contained in:
@@ -12,10 +12,11 @@ Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p){
|
||||
return e;
|
||||
}
|
||||
|
||||
void Error_destroy(Error* e){
|
||||
void Error_free(Error* e){
|
||||
if(e->is_msg_on_heap)
|
||||
free(e->msg.data);
|
||||
free(e->call_stack.data);
|
||||
free(e);
|
||||
}
|
||||
|
||||
void Error_addCallPos(Error* e, ErrorCallPos p){
|
||||
@@ -47,7 +48,6 @@ void Error_printAndExit(Error* e){
|
||||
str e_str = Error_toStr(e);
|
||||
printfe("%s\n", e_str.data);
|
||||
free(e_str.data);
|
||||
Error_destroy(e);
|
||||
free(e);
|
||||
Error_free(e);
|
||||
exit(111);
|
||||
}
|
||||
|
||||
@@ -132,3 +132,39 @@ str hex_to_str(Array(u8) buf, bool uppercase){
|
||||
StringBuilder_append_memory(&sb, buf, uppercase);
|
||||
return StringBuilder_getStr(&sb);
|
||||
}
|
||||
|
||||
void str_trim(str* line, bool set_zero_at_end){
|
||||
bool stop = false;
|
||||
// loop forward
|
||||
while(line->size > 0 && !stop){
|
||||
char first_char = line->data[line->size - 1];
|
||||
switch(first_char){
|
||||
case '\0': case '\r': case '\n':
|
||||
case '\t': case ' ':
|
||||
line->data++;
|
||||
line->size--;
|
||||
break;
|
||||
default:
|
||||
stop = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// loop backward
|
||||
while(line->size > 0 && !stop)
|
||||
{
|
||||
char last_char = line->data[line->size - 1];
|
||||
switch(last_char){
|
||||
case '\0': case '\r': case '\n':
|
||||
case '\t': case ' ':
|
||||
line->size--;
|
||||
break;
|
||||
default:
|
||||
stop = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(set_zero_at_end){
|
||||
line->data[line->size] = '\0';
|
||||
line->isZeroTerminated = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user