diff options
| author | ame <[email protected]> | 2024-09-01 20:18:34 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2024-09-01 20:18:34 -0500 |
| commit | dfb108f6c7abbdeba832d3568d77153fc8c5a3a6 (patch) | |
| tree | 2507e28872f7cd6c5d583054d6ea26aaf931bbc8 | |
| parent | f605f194eb45235989fc5ec91505201498834f24 (diff) | |
clean
| -rw-r--r-- | src/net/util.c | 110 | ||||
| -rw-r--r-- | tests/match.lua | 21 |
2 files changed, 66 insertions, 65 deletions
diff --git a/src/net/util.c b/src/net/util.c index c25ac39..41069fb 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -245,68 +245,66 @@ int match_param(char* path, char* match, parray_t* arr){ mi = pi = imatch = start = 0; index = -1; - int step = 0; - //0 increment both - //1 move path to find '}' - //2 move match to find '/' or '\0' + enum steps { + NORMAL, + GET_KEY, + GET_VALUE + }; + + enum steps step = NORMAL; char* name; for(; /*(path[pi] != '\0' || step == 2) && */(match[mi] != '\0' || step == 1);){ - if(step == 0){ - if(path[pi] == '{'){ - - step = 1; - start = pi; - } else if(path[pi] == '*'){ - //printf("found\n"); - index = pi; - imatch = mi; - - } else { - - if(path[pi] != match[mi]){ - //printf("whaa\n"); - if(index == -1) return 0; - //printf("here\n"); - pi = index + 1; - imatch++; - mi = imatch; - + switch(step){ + case NORMAL: + if(path[pi] == '{'){ + + step = GET_KEY; + start = pi; + } else if(path[pi] == '*'){ + index = pi; + imatch = mi; + + } else { + + if(path[pi] != match[mi]){ + if(index == -1) return 0; + + pi = index + 1; + imatch++; + mi = imatch; + continue; + } + mi++; + } + pi++; + break; + case GET_KEY: + if(path[pi] == '}'){ + step = GET_VALUE; + name = calloc(pi - start, sizeof * name); + memcpy(name, path + start + 1, pi - start - 1); + start = mi; } - mi++; - } - pi++; - - } else if (step == 1){ - if(path[pi] == '}'){ - step = 2; - name = calloc(pi - start, sizeof * name); - memcpy(name, path + start + 1, pi - start - 1); - start = mi; - //printf(": "); - } else { - //printf("%c", path[pi]); - } - pi++; - - } else if (step == 2){ - //change this to maybe match the next path char? - if(match[mi] == '/'){ - step = 0; - - char* out = calloc(mi - start, sizeof * out); - memcpy(out, match + start, mi - start); - parray_set(arr, name, out); - free(name); - //printf("\n"); - } else { - //printf("%c", match[mi]); - mi++; - } + pi++; + break; + case GET_VALUE: + //change this to maybe match the next path char? + if(match[mi] == '/'){ + step = NORMAL; + + char* out = calloc(mi - start, sizeof * out); + memcpy(out, match + start, mi - start); + parray_set(arr, name, out); + free(name); + } else { + mi++; + } + break; } } - if(step == 2){ + if(step == GET_VALUE){ char* out = calloc(mi - start, sizeof * out); memcpy(out, match + start, mi - start); parray_set(arr, name, out); @@ -315,8 +313,6 @@ int match_param(char* path, char* match, parray_t* arr){ if(path[pi] != 0) for(; path[pi] == '*'; pi++); - //printf("path: '%s':'%s'\nmatch: '%s':'%s'\nend: %i,%i\n",path, &path[pi], match,&match[mi],path[pi],match[mi]); - return path[pi] == 0 && match[mi] == 0; } diff --git a/tests/match.lua b/tests/match.lua index d9c1533..000b1e7 100644 --- a/tests/match.lua +++ b/tests/match.lua @@ -2,13 +2,17 @@ llb = require "lullaby" function test(a, b, match, expect) res, out = llb.test._match(a, b) + + if res == 0 and res == match then return end - if res ~= match then - return llb.io.error("res != match") + if res ~= match then + return llb.io.error(a..":"..b.." res("..res..") != expected") end - if res == 0 then return end - + if res == 0 then + return llb.io.error(a..":"..b.." res == match") + end + if llb.array.len(out) ~= llb.array.len(expect) then return llb.io.error("out != expect") end @@ -18,10 +22,10 @@ function test(a, b, match, expect) return llb.io.error("out != expect") end end - llb.io.pprint(out) end test("/{test}/","/name/", 1, {test="name"}) +test("/", "/wawawawawawaw", 0) test("*","/wdejowe/wde", 1, {}) test("/*/{hello}/{meow}/end","/blah/blah/hii/end", 1, {hello="blah", meow="hii"}) test("/*/*/{test}/*/*/end/*/real","/a/b/testing/d/e/end/f/real", 1, {test="testing"}) @@ -31,7 +35,8 @@ test("{meow}", "owo", 1, {meow="owo"}) test("/{meow}", "/owo", 1, {meow="owo"}) test("{meow}", "/", 0) test("/{meow}", "/", 0) - - - +test("/*/", "/test", 0) +test("/{meow}/", "/aw", 0) +--i dont know if this should be valid, but idc +test("/{meow}/", "//", 1, {meow=""}) |
