aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util.c29
-rw-r--r--util.h4
2 files changed, 29 insertions, 4 deletions
diff --git a/util.c b/util.c
index f2c464e..eae228e 100644
--- a/util.c
+++ b/util.c
@@ -7,6 +7,10 @@
#include <errno.h>
#include <regex.h>
+/*
+ * Networking
+ */
+
uint16_t atop(const char *port) {
return htons(atoi(port));
}
@@ -17,6 +21,10 @@ struct in_addr aton(const char* cp, int* output) {
return inp;
}
+/*
+ * Error handling
+ */
+
void herrc(int output, const char* funcName) {
if (output < 0 && errno != EINTR) {
perror(funcName);
@@ -30,6 +38,10 @@ void herr(int output, const char* funcName) {
}
}
+/*
+ * sds string substitution
+ */
+
sds getMatch(const sds str, const regmatch_t match) {
return sdsnewlen(str + match.rm_so, match.rm_eo - match.rm_so);
}
@@ -73,18 +85,23 @@ sds gsub_getm(sds str, const regex_t *regex, const char* repl, int* *matches, in
size_t replLen = strlen(repl);
sds ret = sdsempty();
+ /*
+ * Substitute all occurences of regex with repl in str
+ */
// sdslen is in O(1) time
while (strInd < sdslen(str)) {
- /* If no matches were found, cat the entire string and stop trying */
+ /* Run regex */
if (regexec(regex, MATCHSTART, 10, pmatch, 0) != 0) {
+ /* If there are no matches, return the rest of the string as-is */
ret = sdscat(ret, MATCHSTART);
break;
}
- /* Cat everything before the match */
+ /* Store everything before the match as-is */
ret = sdscatlen(ret, MATCHSTART, pmatch[0].rm_so);
- /* Cat repl in place of the match */
+ /* Replace match with repl
+ * repl can include matched subexpressions */
for(size_t i = 0; i < replLen; i++) {
if (repl[i] <= '\10') {
if (pmatch[repl[i] % 10].rm_so > -1)
@@ -94,7 +111,7 @@ sds gsub_getm(sds str, const regex_t *regex, const char* repl, int* *matches, in
ret = sdscatlen(ret, &repl[i], 1);
}
- /* Add current match to matches array */
+ /* Add index of current match to matches */
if (matchesCount != NULL) {
pushBackMatch(matches, matchesCount, &matchesSize, strInd + pmatch[0].rm_so);
}
@@ -111,6 +128,10 @@ sds gsub(sds str, const regex_t* regex, const char* repl) {
return gsub_getm(str, regex, repl, NULL, NULL);
}
+/*
+ * other
+ */
+
int digits(int num) {
if (num < 0) num *= -1;
// This is the fastest way to get the number of digits
diff --git a/util.h b/util.h
index bfa95f7..3bea3c2 100644
--- a/util.h
+++ b/util.h
@@ -6,15 +6,19 @@
#include <arpa/inet.h>
#include <regex.h>
+/* Networking */
uint16_t atop(const char *port);
struct in_addr aton(const char* cp, int* output);
+/* Error handling */
void herr(int output, const char* funcName);
void herrc(int output, const char* funcName);
+/* sds string substition */
sds gsub(sds str, const regex_t* regex, const char* repl);
sds gsub_getm(sds str, const regex_t *regex, const char* repl, int* *matches, int *matchesCount);
+/* Other */
int digits(int num);
#define clear_arr(arr) memset(arr, 0, sizeof(arr)/sizeof(*arr))