Skip to content

Commit 091ce14

Browse files
authored
feat: add strip_slice api (#116)
1 parent ba2b2c0 commit 091ce14

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,22 @@ where
119119
/// \n} ");
120120
///
121121
/// ```
122+
#[inline]
122123
pub fn strip_comments_in_place(s: &mut str) -> Result<()> {
123124
// Safety: we have made sure the text is UTF-8
124125
strip_buf(&mut Top, unsafe { s.as_bytes_mut() })
125126
}
126127

128+
#[inline]
127129
pub fn strip(s: &mut str) -> Result<()> {
128130
strip_comments_in_place(s)
129131
}
130132

133+
#[inline]
134+
pub fn strip_slice(s: &mut [u8]) -> Result<()> {
135+
strip_buf(&mut Top, s)
136+
}
137+
131138
fn consume_comment_whitespace_until_maybe_bracket(
132139
state: &mut State,
133140
buf: &mut [u8],

tests/main.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use json_strip_comments::{StripComments, strip, strip_comments_in_place};
1+
use json_strip_comments::{StripComments, strip, strip_comments_in_place, strip_slice};
22

33
use std::io::{ErrorKind, Read};
44

@@ -404,3 +404,18 @@ fn special_characters_in_comments() {
404404
assert!(stripped.contains(r#""key": "value""#));
405405
assert!(stripped.contains(r#""key2": "value2""#));
406406
}
407+
408+
#[test]
409+
fn slice_api() {
410+
let mut json = String::from(
411+
r#"{
412+
"a": 1, // comment after comma
413+
"b": 2
414+
}"#,
415+
)
416+
.into_bytes();
417+
strip_slice(&mut json).unwrap();
418+
let json = String::from_utf8(json).unwrap();
419+
assert!(json.contains(r#""a": 1,"#));
420+
assert!(json.contains(r#""b": 2"#));
421+
}

0 commit comments

Comments
 (0)