aboutsummaryrefslogtreecommitdiff
path: root/src/str.c
diff options
context:
space:
mode:
authorSoikk2022-08-09 02:04:02 +0200
committerSoikk2022-08-09 02:04:02 +0200
commit32dd785f881cf3ba6c1c6806cd3af3cbf56437c4 (patch)
treeb8ce805c52ae07b8b57819644f4c3b137d361fa3 /src/str.c
parentae0921866fb49aadea65b8057ab000d42e75f24c (diff)
downloadsoikk-DB-32dd785f881cf3ba6c1c6806cd3af3cbf56437c4.tar.xz
soikk-DB-32dd785f881cf3ba6c1c6806cd3af3cbf56437c4.tar.zst
Improved database name system.
Diffstat (limited to 'src/str.c')
-rw-r--r--src/str.c30
1 files changed, 30 insertions, 0 deletions
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