diff options
| author | blob1807 <12388588+blob1807@users.noreply.github.com> | 2024-04-13 00:39:32 +1000 |
|---|---|---|
| committer | blob1807 <12388588+blob1807@users.noreply.github.com> | 2024-04-13 00:39:32 +1000 |
| commit | c753711d86fda7392c2556741de0fd9b70d7c2e1 (patch) | |
| tree | bc677e0736f397842999164e5b2b17faa8c64113 /core | |
| parent | 4240e0025e1db18821148210e7c5260faef7d830 (diff) | |
Added support for URL fragments
Added support for a URL's fragment/anchor to `split_url` & `join_url` in `core:net` plus 4 new tests to cover it.
Diffstat (limited to 'core')
| -rw-r--r-- | core/net/url.odin | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/core/net/url.odin b/core/net/url.odin index 7ad88bd1f..5257b757c 100644 --- a/core/net/url.odin +++ b/core/net/url.odin @@ -21,7 +21,7 @@ import "core:strconv" import "core:unicode/utf8" import "core:encoding/hex" -split_url :: proc(url: string, allocator := context.allocator) -> (scheme, host, path: string, queries: map[string]string) { +split_url :: proc(url: string, allocator := context.allocator) -> (scheme, host, path: string, queries: map[string]string, fragment: string) { s := url i := strings.index(s, "://") @@ -30,6 +30,12 @@ split_url :: proc(url: string, allocator := context.allocator) -> (scheme, host, s = s[i+3:] } + i = strings.index_byte(s, '#') + if i != -1 { + fragment = s[i+1:] + s = s[:i] + } + i = strings.index(s, "?") if i != -1 { query_str := s[i+1:] @@ -62,7 +68,7 @@ split_url :: proc(url: string, allocator := context.allocator) -> (scheme, host, return } -join_url :: proc(scheme, host, path: string, queries: map[string]string, allocator := context.allocator) -> string { +join_url :: proc(scheme, host, path: string, queries: map[string]string, fragment: string, allocator := context.allocator) -> string { b := strings.builder_make(allocator) strings.builder_grow(&b, len(scheme) + 3 + len(host) + 1 + len(path)) @@ -95,6 +101,13 @@ join_url :: proc(scheme, host, path: string, queries: map[string]string, allocat i += 1 } + if fragment != "" { + if fragment[0] != '#' { + strings.write_byte(&b, '#') + } + strings.write_string(&b, strings.trim_space(fragment)) + } + return strings.to_string(b) } |