From 32dd785f881cf3ba6c1c6806cd3af3cbf56437c4 Mon Sep 17 00:00:00 2001 From: Soikk Date: Tue, 9 Aug 2022 02:04:02 +0200 Subject: Improved database name system. --- src/str.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/str.c') diff --git a/src/str.c b/src/str.c index d896868..ce43b8f 100644 --- a/src/str.c +++ b/src/str.c @@ -44,6 +44,36 @@ char *normalizeStrLimit(const char *str, uint32_t *l, uint32_t limit){ 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 -- cgit v1.2.3