Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the form data variant of percent_encode/decode #2930

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions core/net/url.odin
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,78 @@ percent_decode :: proc(encoded_string: string, allocator := context.allocator) -
return
}

// For data with content type application/x-www-form-urlencoded
form_data_encode :: proc(s: string, allocator := context.allocator) -> string {
b := strings.builder_make(allocator)
strings.builder_grow(&b, len(s) + 16) // NOTE(tetra): A reasonable number to allow for the number of things we need to escape.

for ch in s {
switch ch {
case 'A'..='Z', 'a'..='z', '0'..='9', '-', '_', '.', '~':
strings.write_rune(&b, ch)
case ' ':
strings.write_rune(&b, '+')
case:
bytes, n := utf8.encode_rune(ch)
for byte in bytes[:n] {
buf: [2]u8 = ---
t := strconv.append_int(buf[:], i64(byte), 16)
strings.write_rune(&b, '%')
strings.write_string(&b, t)
}
}
}

return strings.to_string(b)
}

form_data_decode :: proc(encoded_string: string, allocator := context.allocator) -> (decoded_string: string, ok: bool) {
b := strings.builder_make(allocator)
strings.builder_grow(&b, len(encoded_string))
defer if !ok do strings.builder_destroy(&b)

s := encoded_string

for len(s) > 0 {
i := strings.index_byte_any(s, {'%', '+'})
if i == -1 {
strings.write_string(&b, s) // no '%'s; the string is already decoded
break
}

strings.write_string(&b, s[:i])

if s[i] == '+' {
strings.write_byte(&b, ' ')
s = s[i+1:]
continue
}

s = s[i:]

if len(s) == 0 do return // percent without anything after it
s = s[1:]

if s[0] == '%' {
strings.write_byte(&b, '%')
s = s[1:]
continue
}

if len(s) < 2 {
return // percent without encoded value
}

val := hex.decode_sequence(s[:2]) or_return
strings.write_byte(&b, val)
s = s[2:]
}

ok = true
decoded_string = strings.to_string(b)
return
}

//
// TODO: encoding/base64 is broken...
//
Expand Down
47 changes: 47 additions & 0 deletions core/strings/strings.odin
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,53 @@ index_byte :: proc(s: string, c: byte) -> (res: int) {
return -1
}
/*
Returns the byte offset of the first byte in the string s which is in `cs` that it finds, -1 when not found.
NOTE: Can't find UTF-8 based runes.

Inputs:
- s: The input string to search in.
- cs: The bytes to search for.

Returns:
- res: The byte offset of the first occurrence of any of `cs` in `s`, or -1 if not found.

Example:

import "core:fmt"
import "core:strings"

index_byte_any_example :: proc() {
fmt.println(strings.index_byte_any("test", {'t'}))
fmt.println(strings.index_byte_any("test", {'e'}))
fmt.println(strings.index_byte_any("test", {'e', 't'}))
fmt.println(strings.index_byte_any("test", {'e', 's'}))
fmt.println(strings.index_byte_any("test", {'x'}))
fmt.println(strings.index_byte_any("test", {'s', 'x'}))
fmt.println(strings.index_byte_any("teäst", {'ä'}))
}

Output:

0
1
0
1
-1
2
-1

*/
index_byte_any :: proc(s: string, cs: []byte) -> (res: int) {
for i := 0; i < len(s); i += 1 {
for c in cs {
if s[i] == c {
return i
}
}
}
return -1
}
/*
Returns the byte offset of the last byte `c` in the string `s`, -1 when not found.

Inputs:
Expand Down