-
Notifications
You must be signed in to change notification settings - Fork 3
/
string_utils.c
43 lines (37 loc) · 910 Bytes
/
string_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string_utils.h>
char* short_string_to_string(const short_string_t* short_string) {
char* result = calloc(short_string->length + 1, sizeof(char));
if (result == NULL) {
return NULL;
}
memcpy(result, short_string->data, short_string->length);
return result;
}
void replace_char_in_place(char* src, char from, char to) {
for (int i = 0; i < strlen(src); ++i) {
if (src[i] == from) {
src[i] = to;
}
}
}
void print_frame(int length) {
for (int i = 0; i < length; ++i) {
printf("-");
}
printf("\n");
}
const char* trim_string(const char* src, char symbol) {
int i = 0;
for (; i < strlen(src) && src[i] == symbol; ++i)
;
return &(src[i]);
}
const char* rtrim_string(const char* src, char symbol) {
int i = strlen(src);
for (; i > 0 && src[i] == symbol; --i)
;
return &(src[i]);
}