1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
//! Replace json comments and trailing commas in place.
//!
//! A fork of a fork:
//!
//! * <https://github.com/tmccombs/json-comments-rs>
//! * <https://github.com/parcel-bundler/parcel/pull/9032>
//!
//! `json-strip-comments` is a library to strip out comments from JSON. By processing text
//! through a [`StripComments`] adapter first, it is possible to use a standard JSON parser (such
//! as [serde_json](https://crates.io/crates/serde_json) with quasi-json input that contains
//! comments.
//!
//! In fact, this code makes few assumptions about the input and could probably be used to strip
//! comments out of other types of code as well, provided that strings use double quotes and
//! backslashes are used for escapes in strings.
//!
//! The following types of comments are supported:
//! - C style block comments (`/* ... */`)
//! - C style line comments (`// ...`)
//! - Shell style line comments (`# ...`)
//!
//! ## Example
//!
//! ```rust
#![doc = include_str!("../examples/example.rs")]
//! ```
use std::io::{ErrorKind, Read, Result};
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
enum State {
Top,
InString,
StringEscape,
InComment,
InBlockComment,
MaybeCommentEnd,
InLineComment,
}
use State::{
InBlockComment, InComment, InLineComment, InString, MaybeCommentEnd, StringEscape, Top,
};
/// A [`Read`] that transforms another [`Read`] so that it changes all comments to spaces so that a downstream json parser
/// (such as json-serde) doesn't choke on them.
///
/// The supported comments are:
/// - C style block comments (`/* ... */`)
/// - C style line comments (`// ...`)
/// - Shell style line comments (`# ...`)
///
/// ## Example
/// ```
/// use json_strip_comments::StripComments;
/// use std::io::Read;
///
/// let input = r#"{
/// // c line comment
/// "a": "comment in string /* a */",
/// ## shell line comment
/// } /** end */"#;
///
/// let mut stripped = String::new();
/// StripComments::new(input.as_bytes()).read_to_string(&mut stripped).unwrap();
///
/// assert_eq!(stripped, "{
/// \n\"a\": \"comment in string /* a */\",
/// \n} ");
///
/// ```
///
pub struct StripComments<T: Read> {
inner: T,
state: State,
settings: CommentSettings,
}
impl<T> StripComments<T>
where
T: Read,
{
pub fn new(input: T) -> Self {
Self {
inner: input,
state: Top,
settings: CommentSettings::default(),
}
}
/// Create a new `StripComments` with settings which may be different from the default.
///
/// This is useful if you wish to disable allowing certain kinds of comments.
#[inline]
pub fn with_settings(settings: CommentSettings, input: T) -> Self {
Self {
inner: input,
state: Top,
settings,
}
}
}
macro_rules! invalid_data {
() => {
return Err(ErrorKind::InvalidData.into())
};
}
impl<T> Read for StripComments<T>
where
T: Read,
{
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let count = self.inner.read(buf)?;
if count > 0 {
strip_buf(&mut self.state, &mut buf[..count], self.settings, false)?;
} else if self.state != Top && self.state != InLineComment {
invalid_data!();
}
Ok(count)
}
}
fn consume_comment_whitespace_until_maybe_bracket(
state: &mut State,
buf: &mut [u8],
i: &mut usize,
settings: CommentSettings,
) -> Result<bool> {
*i += 1;
while *i < buf.len() {
let c = &mut buf[*i];
*state = match state {
Top => {
*state = top(c, settings);
if c.is_ascii_whitespace() {
*i += 1;
continue;
}
return Ok(*c == b'}' || *c == b']');
}
InString => in_string(*c),
StringEscape => InString,
InComment => in_comment(c, settings)?,
InBlockComment => consume_block_comments(buf, i),
MaybeCommentEnd => maybe_comment_end(c),
InLineComment => consume_line_comments(buf, i),
};
*i += 1;
}
Ok(false)
}
fn strip_buf(
state: &mut State,
buf: &mut [u8],
settings: CommentSettings,
remove_trailing_commas: bool,
) -> Result<()> {
let mut i = 0;
let len = buf.len();
while i < len {
let c = &mut buf[i];
if matches!(state, Top) {
let cur = i;
*state = top(c, settings);
if remove_trailing_commas
&& *c == b','
&& consume_comment_whitespace_until_maybe_bracket(state, buf, &mut i, settings)?
{
buf[cur] = b' ';
}
} else {
*state = match state {
Top => unreachable!(),
InString => in_string(*c),
StringEscape => InString,
InComment => in_comment(c, settings)?,
InBlockComment => consume_block_comments(buf, &mut i),
MaybeCommentEnd => maybe_comment_end(c),
InLineComment => consume_line_comments(buf, &mut i),
}
}
i += 1;
}
Ok(())
}
#[inline]
fn consume_line_comments(buf: &mut [u8], i: &mut usize) -> State {
let cur = *i;
match memchr::memchr(b'\n', &buf[*i..]) {
Some(offset) => {
*i += offset;
buf[cur..*i].fill(b' ');
Top
}
None => {
*i = buf.len() - 1;
buf[cur..].fill(b' ');
InLineComment
}
}
}
#[inline]
fn consume_block_comments(buf: &mut [u8], i: &mut usize) -> State {
let cur = *i;
match memchr::memchr(b'*', &buf[*i..]) {
Some(offset) => {
*i += offset;
buf[cur..=*i].fill(b' ');
MaybeCommentEnd
}
None => {
*i = buf.len() - 1;
buf[cur..].fill(b' ');
InBlockComment
}
}
}
/// Strips comments from a string in place, replacing it with whitespaces.
///
/// /// ## Example
/// ```
/// use json_strip_comments::{strip_comments_in_place, CommentSettings};
///
/// let mut string = String::from(r#"{
/// // c line comment
/// "a": "comment in string /* a */",
/// ## shell line comment
/// } /** end */"#);
///
/// strip_comments_in_place(&mut string, CommentSettings::default(), false).unwrap();
///
/// assert_eq!(string, "{
/// \n\"a\": \"comment in string /* a */\",
/// \n} ");
///
/// ```
pub fn strip_comments_in_place(
s: &mut str,
settings: CommentSettings,
remove_trailing_commas: bool,
) -> Result<()> {
// Safety: we have made sure the text is UTF-8
strip_buf(
&mut Top,
unsafe { s.as_bytes_mut() },
settings,
remove_trailing_commas,
)
}
pub fn strip(s: &mut str) -> Result<()> {
strip_comments_in_place(s, CommentSettings::all(), true)
}
/// Settings for `StripComments`
///
/// The default is for all comment types to be enabled.
#[derive(Copy, Clone, Debug)]
pub struct CommentSettings {
/// True if c-style block comments (`/* ... */`) are allowed
block_comments: bool,
/// True if c-style `//` line comments are allowed
slash_line_comments: bool,
/// True if shell-style `#` line comments are allowed
hash_line_comments: bool,
}
impl Default for CommentSettings {
fn default() -> Self {
Self::all()
}
}
impl CommentSettings {
/// Enable all comment Styles
pub const fn all() -> Self {
Self {
block_comments: true,
slash_line_comments: true,
hash_line_comments: true,
}
}
/// Only allow line comments starting with `#`
pub const fn hash_only() -> Self {
Self {
hash_line_comments: true,
block_comments: false,
slash_line_comments: false,
}
}
/// Only allow "c-style" comments.
///
/// Specifically, line comments beginning with `//` and
/// block comment like `/* ... */`.
pub const fn c_style() -> Self {
Self {
block_comments: true,
slash_line_comments: true,
hash_line_comments: false,
}
}
/// Create a new `StripComments` for `input`, using these settings.
///
/// Transform `input` into a [`Read`] that strips out comments.
/// The types of comments to support are determined by the configuration of
/// `self`.
///
/// ## Examples
///
/// ```
/// use json_strip_comments::CommentSettings;
/// use std::io::Read;
///
/// let input = r#"{
/// // c line comment
/// "a": "b"
/// /** multi line
/// comment
/// */ }"#;
///
/// let mut stripped = String::new();
/// CommentSettings::c_style().strip_comments(input.as_bytes()).read_to_string(&mut stripped).unwrap();
///
/// assert_eq!(stripped, "{
/// \n\"a\": \"b\"
/// }");
/// ```
///
/// ```
/// use json_strip_comments::CommentSettings;
/// use std::io::Read;
///
/// let input = r#"{
/// ## shell line comment
/// "a": "b"
/// }"#;
///
/// let mut stripped = String::new();
/// CommentSettings::hash_only().strip_comments(input.as_bytes()).read_to_string(&mut stripped).unwrap();
///
/// assert_eq!(stripped, "{
/// \n\"a\": \"b\"\n}");
/// ```
#[inline]
pub fn strip_comments<I: Read>(self, input: I) -> StripComments<I> {
StripComments::with_settings(self, input)
}
}
#[inline]
fn top(c: &mut u8, settings: CommentSettings) -> State {
match *c {
b'"' => InString,
b'/' => {
*c = b' ';
InComment
}
b'#' if settings.hash_line_comments => {
*c = b' ';
InLineComment
}
_ => Top,
}
}
#[inline]
fn in_string(c: u8) -> State {
match c {
b'"' => Top,
b'\\' => StringEscape,
_ => InString,
}
}
fn in_comment(c: &mut u8, settings: CommentSettings) -> Result<State> {
let new_state = match c {
b'*' if settings.block_comments => InBlockComment,
b'/' if settings.slash_line_comments => InLineComment,
_ => {
invalid_data!()
}
};
*c = b' ';
Ok(new_state)
}
fn maybe_comment_end(c: &mut u8) -> State {
let old = *c;
*c = b' ';
match old {
b'/' => Top,
b'*' => MaybeCommentEnd,
_ => InBlockComment,
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::{ErrorKind, Read};
fn strip_string(input: &str) -> String {
let mut out = String::new();
let count = StripComments::new(input.as_bytes())
.read_to_string(&mut out)
.unwrap();
assert_eq!(count, input.len());
out
}
#[test]
fn block_comments() {
let json = r#"{/* Comment */"hi": /** abc */ "bye"}"#;
let stripped = strip_string(json);
assert_eq!(stripped, r#"{ "hi": "bye"}"#);
}
#[test]
fn block_comments_with_possible_end() {
let json = r#"{/* Comment*PossibleEnd */"hi": /** abc */ "bye"}"#;
let stripped = strip_string(json);
assert_eq!(
stripped,
r#"{ "hi": "bye"}"#
);
}
// See https://github.com/tmccombs/json-comments-rs/issues/12
// Make sure we can parse a block comment that ends with more than one "*"
#[test]
fn doc_comment() {
let json = r##"/** C **/ { "foo": 123 }"##;
let stripped = strip_string(json);
assert_eq!(stripped, r##" { "foo": 123 }"##);
}
#[test]
fn line_comments() {
let json = r#"{
// line comment
"a": 4,
# another
}"#;
let expected = "{
\n \"a\": 4,
\n }";
assert_eq!(strip_string(json), expected);
}
#[test]
fn incomplete_string() {
let json = r#""foo"#;
let mut stripped = String::new();
let err = StripComments::new(json.as_bytes())
.read_to_string(&mut stripped)
.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidData);
}
#[test]
fn incomplete_comment() {
let json = "/* foo ";
let mut stripped = String::new();
let err = StripComments::new(json.as_bytes())
.read_to_string(&mut stripped)
.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidData);
}
#[test]
fn incomplete_comment2() {
let json = "/* foo *";
let mut stripped = String::new();
let err = StripComments::new(json.as_bytes())
.read_to_string(&mut stripped)
.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidData);
}
#[test]
fn no_hash_comments() {
let json = r#"# bad comment
{"a": "b"}"#;
let mut stripped = String::new();
CommentSettings::c_style()
.strip_comments(json.as_bytes())
.read_to_string(&mut stripped)
.unwrap();
assert_eq!(stripped, json);
}
#[test]
fn no_slash_line_comments() {
let json = r#"// bad comment
{"a": "b"}"#;
let mut stripped = String::new();
let err = CommentSettings::hash_only()
.strip_comments(json.as_bytes())
.read_to_string(&mut stripped)
.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidData);
}
#[test]
fn no_block_comments() {
let json = r#"/* bad comment */ {"a": "b"}"#;
let mut stripped = String::new();
let err = CommentSettings::hash_only()
.strip_comments(json.as_bytes())
.read_to_string(&mut stripped)
.unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidData);
}
#[test]
fn strip_in_place() {
let mut json = String::from(r#"{/* Comment */"hi": /** abc */ "bye"}"#);
strip_comments_in_place(&mut json, CommentSettings::default(), false).unwrap();
assert_eq!(json, r#"{ "hi": "bye"}"#);
}
#[test]
fn trailing_comma() {
let mut json = String::from(
r#"{
"a1": [1,],
"a2": [1,/* x */],
"a3": [
1, // x
],
"o1": {v:1,},
"o2": {v:1,/* x */},
"o3": {
"v":1, // x
},
# another
}"#,
);
strip_comments_in_place(&mut json, CommentSettings::default(), true).unwrap();
let expected = r#"{
"a1": [1 ],
"a2": [1 ],
"a3": [
1
],
"o1": {v:1 },
"o2": {v:1 },
"o3": {
"v":1
}
}"#;
assert_eq!(
json.replace(|s: char| s.is_ascii_whitespace(), ""),
expected.replace(|s: char| s.is_ascii_whitespace(), "")
);
}
}