aboutsummaryrefslogtreecommitdiff
path: root/src/str.c
blob: ce43b8fa08e9609f414e07bbed6b81050f77eaa5 (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "db.h"


uint32_t len(const char *s){
	uint32_t l = -1;
	while(s[++l]);
	return l;
}

bool sameStr(const char *s1, const char *s2){
	uint32_t i1 = 0, i2 = 0;
	while(s1[i1] && s1[i1] == s2[i2])
		++i1, ++i2;
	return !s1[i1] && !s2[i2];
}

// Lowercases the whole string and removes trailing spaces
char *normalizeStr(const char *str, uint32_t *l){
	*l = len(str);
	uint32_t trw = 0;
	while(isspace(str[--(*l)]))
		++trw;
	char *nstr = calloc(++(*l)+1, sizeof(char));
	for(int i = 0; i < *l; ++i)
		nstr[i] = tolower(str[i]);
	return nstr;
}

// Same as normalizeStr but with a limit (str[limit] will be equal to '\0')
// If limit is 0, it will return NULL
// WARNING: It allocates limit+1 characters
char *normalizeStrLimit(const char *str, uint32_t *l, uint32_t limit){
	if(limit == 0){
		return NULL;
	}
	*l = len(str);
	*l = (*l > limit) ? limit : *l;
	uint32_t trw = 0;
	while(isspace(str[--(*l)]))
		++trw;
	char *nstr = calloc(++(*l)+1, sizeof(char));
	for(int i = 0; i < *l; ++i)
		nstr[i] = tolower(str[i]);
	return nstr;
}

// Trims trailing spaces
char *trimStr(const char *str, uint32_t *l){
	*l = len(str);
	uint32_t trw = 0;
	while(isspace(str[--(*l)]))
		++trw;
	char *nstr = calloc(++(*l)+1, sizeof(char));
	for(int i = 0; i < *l; ++i)
		nstr[i] = str[i];
	return nstr;
}

// Same as trimStr but with a limit (str[limit] will be equal to '\0')
// If limit is 0, it will return NULL
// WARNING: It allocates limit+1 characters
char *trimStrLimit(const char *str, uint32_t *l, uint32_t limit){
	if(limit == 0){
		return NULL;
	}
	*l = len(str);
	*l = (*l > limit) ? limit : *l;
	uint32_t trw = 0;
	while(isspace(str[--(*l)]))
		++trw;
	char *nstr = calloc(++(*l)+1, sizeof(char));
	for(int i = 0; i < *l; ++i)
		nstr[i] = str[i];
	return nstr;
}

// Auxiliary function for creating a lookup table of the haystack
// table[i] will be the number of shifts right until the next
// separator when checking position i
// Only really useful for this implementation of tags
static int *toTable(const char *y, int n, char sep){
	int *tb = calloc(n, sizeof(int));
	if(tb == NULL){
		fprintf(stderr, "Error callocating array (table)");
		exit(EXIT_FAILURE);
	}

	int lSep = n-1;
	for(int i = n-1; i >= 0; --i){
		if(y[i] == sep){
			tb[i] = 1;
			lSep = i;
		}else if(y[i] != '\0'){
			tb[i] = lSep-i;
		}
	}
	return tb;
}

// Returns the position of ndl in tags, -1 if its not found
// A return of 0 means ndl occurs in tags starting in position 0
// Use 'if(strInTags(...) != -1)' when using this function
ssize_t strInTags(const char *tags, int n, const char *ndl, int m, char sep){
	int *tb = toTable(tags, n, sep);

	for(int i = 0; i < n; ){
		int j = 0;
		while(j < m && tags[i+j] == ndl[j]){
			++j;
		}
		if(j == m){
			return i;
		}
		if(tags[i+j] != ndl[j]){
			i += tb[i];
		}
	}
	return -1;
}