Skip to content

Commit

Permalink
chore: improve json fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Nov 27, 2023
1 parent 2a06607 commit 25e396e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jarvis"
version = "1.1.5"
version = "1.1.6"
edition = "2021"
rust-version = "1.64"
description = ""
Expand Down
61 changes: 60 additions & 1 deletion src/json_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ impl RawJSONArray {
}
}

fn skip_space_v(&self) -> usize {
let mut offset = self.offset;
while offset < self.chars.len() {
if self.chars[offset].is_whitespace() {
offset += 1;
} else {
break;
}
}
return offset;

Check warning on line 70 in src/json_util.rs

View workflow job for this annotation

GitHub Actions / test

unneeded `return` statement
}

// return error message if failed
fn array(&mut self) -> Option<String> {
self.result.push('[');
Expand Down Expand Up @@ -110,9 +122,15 @@ impl RawJSONArray {

match self.chars[self.offset] {
',' => {
self.result.push(',');
self.offset += 1;
self.skip_space();
if self.offset < self.chars.len() && self.chars[self.offset] == ']' {
self.result.push(']');
self.offset += 1;
return None;
} else {
self.result.push(',');
}
}
']' => {
self.result.push(']');
Expand Down Expand Up @@ -201,6 +219,20 @@ impl RawJSONArray {
}
}
}
']' => {
self.offset += 1;
let offset = self.skip_space_v();
if offset >= self.chars.len()
|| (offset <= self.chars.len() - 1

Check warning on line 226 in src/json_util.rs

View workflow job for this annotation

GitHub Actions / test

unnecessary `>= y + 1` or `x - 1 >=`
&& (self.chars[offset] == ',' || self.chars[offset] == ']'))
{
self.result.push('"');
self.offset -= 1;
return None;
}

self.result.push(']');
}
_ => {
self.result.push(self.chars[self.offset]);
self.offset += 1;
Expand Down Expand Up @@ -329,6 +361,33 @@ mod tests {
output: r#"[[],["] Stream: ["],["Internet Engineering Task Force \\(IETF)"]]"#.to_string(),
err: None,
},
Case {
input: r#"[
[],
[
""] Stream: ["
],
[
"Internet Engineering Task Force \(IETF)"
],
]"#
.to_string(),
output: r#"[[],["] Stream: ["],["Internet Engineering Task Force \\(IETF)"]]"#.to_string(),
err: None,
},
Case {
input: r#"[
[],
[
""] Stream: ["
],
[
"Internet Engineering Task Force \(IETF)1]
]"#
.to_string(),
output: r#"[[],["] Stream: ["],["Internet Engineering Task Force \\(IETF)1"]]"#.to_string(),
err: None,
},
Case {
input: r#"[["作为UTF-8 [","RFC3629",""]编码的文本字符串(","第2节", ")。字符串中的字节数等于参数。包含无效UTF-8序列的字符串是格式良好但无效的(","第1.2节", ")。此类型适用于需要解释或显示人类可读文本的系统,并允许区分结构化字节和具有指定曲目(Unicode)和编码(UTF-8)的文本。与JSON等格式不同,此类型中的Unicode字符永远不会被转义。因此,换行符(U+000A)始终表示为字符串中的字节0x0a,而不是字符0x5c6e(字符“\”和“n”)或0x5c7530303061(字符“\”,“u”,“0”,“0”,“0”和“a”)。","¶"]]"#
.to_string(),
Expand Down

0 comments on commit 25e396e

Please sign in to comment.