Skip to content

Commit 6502b59

Browse files
committed
(feat): reduced allocations
1 parent af11b1a commit 6502b59

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/query_string.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@ use urlencoding::decode;
66
#[inline]
77
pub fn parse_query_string(qs: &[u8], separator: char) -> Vec<(String, String)> {
88
String::from_utf8(qs.to_vec())
9-
.unwrap()
9+
.unwrap_or_default()
1010
.replace('+', " ")
1111
.split(separator)
12-
.filter(|value| !value.is_empty())
13-
.map(|value| {
14-
let decoded = decode(value).unwrap();
15-
let (x, y) = decoded.split_once('=').unwrap_or((value, ""));
16-
(x.to_owned(), y.to_owned())
12+
.filter_map(|value| {
13+
if !value.is_empty() {
14+
return match decode(value).unwrap_or_default().split_once('=') {
15+
Some(value) => Some((value.0.to_owned(), value.1.to_owned())),
16+
None => Some((value.to_owned(), String::from(""))),
17+
};
18+
}
19+
None
1720
})
1821
.collect::<Vec<(String, String)>>()
1922
}
2023

2124
#[inline]
2225
fn decode_value(json_str: String) -> Value {
2326
if json_str.starts_with('{') && json_str.ends_with('}') {
24-
let values_map: Map<String, Value> = serde_json::from_str(json_str.as_str()).unwrap();
27+
let values_map: Map<String, Value> =
28+
serde_json::from_str(json_str.as_str()).unwrap_or_default();
2529
let mut result: Map<String, Value> = Map::new();
2630

2731
for (k, v) in values_map {
@@ -31,7 +35,7 @@ fn decode_value(json_str: String) -> Value {
3135
return Value::from(result);
3236
}
3337
if json_str.starts_with('[') && json_str.ends_with(']') {
34-
let values_array: Value = serde_json::from_str(json_str.as_str()).unwrap();
38+
let values_array: Value = serde_json::from_str(json_str.as_str()).unwrap_or_default();
3539
let vector_values = values_array
3640
.as_array()
3741
.unwrap()
@@ -82,7 +86,7 @@ mod tests {
8286
use serde_json::{json, to_string, Value};
8387

8488
fn eq_str(value: Value, string: &str) {
85-
assert_eq!(&to_string(&value).unwrap(), string)
89+
assert_eq!(&to_string(&value).unwrap_or_default(), string)
8690
}
8791

8892
#[test]

0 commit comments

Comments
 (0)