aboutsummaryrefslogtreecommitdiff
path: root/src/stringutils.c
blob: 172d40d8b598e22ec6a193fa7fc5476f04b02a30 (plain)
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
44
45
46
47
#include "stringutils.h"
#include <string.h>

void string_replace(char *str, char old_char, char new_char) {
    for(;;) {
        char c = *str;
        if(c == old_char)
            *str = new_char;
        else if(c == '\0')
            break;
        ++str;
    }
}

char* lstrip(char *str) {
    for(;;) {
        char c = *str;
        if(c != ' ' && c != '\t' && c != '\n')
            break;
        else if(c == '\0')
            break;
        ++str;
    }
    return str;
}

void rstrip(char *str) {
    int len = strlen(str);
    if(len == 0)
        return;

    char *p = str + len - 1;
    while(p != str) {
        char c = *p;
        if(c != ' ' && c != '\t' && c != '\n')
            break;
        --p;
    }

    p[1] = '\0';
}

char* strip(char *str) {
    str = lstrip(str);
    rstrip(str);
    return str;
}