diff options
| author | Soikk | 2022-08-09 02:04:02 +0200 |
|---|---|---|
| committer | Soikk | 2022-08-09 02:04:02 +0200 |
| commit | 32dd785f881cf3ba6c1c6806cd3af3cbf56437c4 (patch) | |
| tree | b8ce805c52ae07b8b57819644f4c3b137d361fa3 /src/str.c | |
| parent | ae0921866fb49aadea65b8057ab000d42e75f24c (diff) | |
| download | soikk-DB-32dd785f881cf3ba6c1c6806cd3af3cbf56437c4.tar.xz soikk-DB-32dd785f881cf3ba6c1c6806cd3af3cbf56437c4.tar.zst | |
Improved database name system.
Diffstat (limited to 'src/str.c')
| -rw-r--r-- | src/str.c | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -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 |
