summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2021-06-23 16:33:29 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2021-06-23 16:33:29 -0400
commit1b252ddbadf7e67e25ba407d3a2483cab452340f (patch)
tree145cdafe93aa1a051f69e149fc7957572b0b16ee /misc.c
parent2d5d1fdc84aeb19e1ebbf5169c496befea592206 (diff)
Added `sanitize_strlen()`, and `sanitize_str()`.
`sanitize_str()` creates a string with each starting space replaced by a hyphen, and with all trailing spaces, and periods removed. `sanitize_strlen()` gets the length of the sanatized version of the supplied string.
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/misc.c b/misc.c
index 9cc72c9..3e0b304 100644
--- a/misc.c
+++ b/misc.c
@@ -97,3 +97,31 @@ int delm_span(char *str, const char delm) {
for (i = 0; str[i] == delm; i++);
return i;
}
+
+int sanitize_strlen(char *str) {
+ int len = 0;
+ while (*str != '\0') {
+ const int tok_len = strcspn(str, " .");
+ const char delm = str[tok_len];
+ const int span_len = (delm != '\0') ? delm_span(&str[tok_len+1], delm) : 0;
+ str += (tok_len + span_len);
+ len += tok_len;
+ }
+ return len;
+}
+
+char *sanitize_str(char *str) {
+ const int len = sanitize_strlen(str);
+ char *san_str = calloc(len+1, sizeof(char));
+ char *tmp = san_str;
+ while (*str != '\0' && tmp < &san_str[len]) {
+ const int tok_len = strcspn(str, " .");
+ const char delm = str[tok_len];
+ const int span_len = (delm != '\0') ? delm_span(&str[tok_len+1], delm) : 0;
+ memcpy(tmp, str, tok_len);
+ tmp += tok_len;
+ str += (tok_len + span_len);
+ *tmp = (delm == ' ') ? '-' : delm;
+ }
+ return san_str;
+}