aboutsummaryrefslogtreecommitdiff
path: root/src/parray.c
diff options
context:
space:
mode:
authorame <[email protected]>2024-02-02 08:37:52 -0600
committerame <[email protected]>2024-02-02 08:37:52 -0600
commit80d7ca8421d758a1a7a2988391f1ee5ae7075ab7 (patch)
tree5c1950e611843ee5ae1a4e33734766c8b233ddfb /src/parray.c
parent582585d17a5d2b55082d9089ca3c8faed9452acf (diff)
wildcard
Diffstat (limited to 'src/parray.c')
-rw-r--r--src/parray.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/parray.c b/src/parray.c
index 077a67f..637bc0b 100644
--- a/src/parray.c
+++ b/src/parray.c
@@ -35,11 +35,51 @@ void* parray_get(parray_t* p, char* key){
return NULL;
}
+void parray_lclear(parray_t* p){
+ free(p->P);
+ free(p);
+}
+
void parray_clear(parray_t* p, int clear_val){
for(int i = 0; i != p->len; i++){
str_free(p[i].P->key);
if(clear_val) free(p[i].P->value);
}
- free(p->P);
- free(p);
+ parray_lclear(p);
+}
+
+int fmatch(char* s, char* p) {
+ int slen = strlen(s);
+ int plen = strlen(p);
+ int sidx = 0, pidx = 0, lastWildcardIdx = -1, sBacktrackIdx = -1, nextToWildcardIdx = -1;
+ for(;sidx < slen;) {
+ if (pidx < plen && (p[pidx] == '?' || p[pidx] == s[sidx])) {
+ sidx++;
+ pidx++;
+ } else if (pidx < plen && p[pidx] == '*') {
+ lastWildcardIdx = pidx;
+ nextToWildcardIdx = ++pidx;
+ sBacktrackIdx = sidx;
+ } else if (lastWildcardIdx == -1) {
+ return 0;
+ } else {
+ pidx = nextToWildcardIdx;
+ sidx = sBacktrackIdx++;
+ }
+ }
+ for(int i = pidx; i < plen; i++) if(p[i] != '*') return 0;
+ return 1;
}
+
+parray_t* parray_find(parray_t* p, char* match){
+ parray_t* ret = malloc(sizeof * ret);
+ ret->P = malloc(sizeof * ret->P * p->len);
+ ret->len = 0;
+ for(int i = 0; i != p->len; i++){
+ if(fmatch(match, p->P[i].key->c)){
+ ret->P[ret->len] = p->P[i];
+ ret->len++;
+ }
+ }
+ return ret;
+} \ No newline at end of file