#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
pub fn strip_comments(input: &str) -> String {
let mut output = String::with_capacity(input.len());
let bytes = input.as_bytes();
let mut i = 0;
while i < bytes.len() {
if i + 1 < bytes.len() && bytes[i] == b'/' && bytes[i + 1] == b'/' {
i += 2;
while i < bytes.len() && bytes[i] != b'\n' {
i += 1;
}
if i < bytes.len() {
output.push('\n');
i += 1;
}
continue;
}
if i + 1 < bytes.len() && bytes[i] == b'/' && bytes[i + 1] == b'*' {
i += 2;
while i + 1 < bytes.len() {
if bytes[i] == b'*' && bytes[i + 1] == b'/' {
i += 2;
break;
}
if bytes[i] == b'\n' {
output.push('\n');
}
i += 1;
}
continue;
}
if bytes[i] == b'"' {
output.push('"');
i += 1;
while i < bytes.len() {
if bytes[i] == b'\\' && i + 1 < bytes.len() {
output.push(bytes[i] as char);
output.push(bytes[i + 1] as char);
i += 2;
} else if bytes[i] == b'"' {
output.push('"');
i += 1;
break;
} else {
output.push(bytes[i] as char);
i += 1;
}
}
continue;
}
output.push(bytes[i] as char);
i += 1;
}
output
}
pub fn parse_json5(input: &str) -> Result<crate::nodes::node::Node, String> {
let clean = strip_comments(input);
crate::parser::default::from_str(&clean)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_strip_single_line_comments() {
let input = r#"{
"name": "Alice", // This is a comment
"age": 30
}"#;
let output = strip_comments(input);
assert!(!output.contains("// This is a comment"));
assert!(output.contains("\"name\""));
assert!(output.contains("\"age\""));
}
#[test]
fn test_strip_multi_line_comments() {
let input = r#"{
"name": "Alice",
/* This is a
multi-line comment */
"age": 30
}"#;
let output = strip_comments(input);
assert!(!output.contains("/* This is a"));
assert!(!output.contains("multi-line comment */"));
assert!(output.contains("\"name\""));
assert!(output.contains("\"age\""));
}
#[test]
fn test_preserve_strings_with_comment_like_content() {
let input = r#"{
"url": "http://example.com",
"comment": "This // is not a comment"
}"#;
let output = strip_comments(input);
assert!(output.contains("http://example.com"));
assert!(output.contains("This // is not a comment"));
}
#[test]
fn test_multiple_comments() {
let input = r#"{
// First comment
"a": 1,
// Second comment
"b": 2 /* inline comment */
}"#;
let output = strip_comments(input);
assert!(!output.contains("First comment"));
assert!(!output.contains("Second comment"));
assert!(!output.contains("inline comment"));
assert!(output.contains("\"a\""));
assert!(output.contains("\"b\""));
}
#[test]
fn test_preserve_line_numbers() {
let input = "{\n// comment\n\"a\": 1\n}";
let output = strip_comments(input);
assert_eq!(input.matches('\n').count(), output.matches('\n').count());
}
#[test]
fn test_parse_json5() {
let input = r#"{
// Configuration file
"host": "localhost",
"port": 8080 /* default port */
}"#;
let result = parse_json5(input);
assert!(result.is_ok());
let node = result.unwrap();
assert_eq!(node["host"].as_str(), Some("localhost"));
assert_eq!(node["port"].as_i64(), Some(8080));
}
#[test]
fn test_escaped_quotes_in_strings() {
let input = r#"{"message": "He said \"hello\" // not a comment"}"#;
let output = strip_comments(input);
assert!(output.contains(r#"He said \"hello\" // not a comment"#));
}
#[test]
fn test_strip_empty_input() {
assert_eq!(strip_comments(""), "");
}
#[test]
fn test_strip_no_comments() {
let input = r#"{"a": 1, "b": "hello"}"#;
let output = strip_comments(input);
assert_eq!(output, input);
}
#[test]
fn test_single_line_comment_at_start() {
let input = "// leading comment\n{\"a\":1}";
let output = strip_comments(input);
assert!(!output.contains("leading comment"));
assert!(output.contains("{\"a\":1}"));
}
#[test]
fn test_single_line_comment_no_trailing_newline() {
let input = "{\"a\":1} // trailing comment";
let output = strip_comments(input);
assert!(!output.contains("trailing comment"));
assert!(output.contains("{\"a\":1}"));
}
#[test]
fn test_multi_line_comment_inline() {
let input = r#"{"a": /* value */ 1}"#;
let output = strip_comments(input);
assert!(!output.contains("value"));
assert!(output.contains("\"a\""));
assert!(output.contains('1'));
}
#[test]
fn test_multi_line_comment_preserves_newline_count() {
let input = "{\n/* line1\nline2\nline3 */\n\"a\":1\n}";
let output = strip_comments(input);
assert_eq!(input.matches('\n').count(), output.matches('\n').count());
}
#[test]
fn test_slash_star_inside_string() {
let input = r#"{"regex": "a /* b"}"#;
let output = strip_comments(input);
assert!(output.contains("a /* b"));
}
#[test]
fn test_double_slash_inside_string() {
let input = r#"{"url": "https://example.com/path"}"#;
let output = strip_comments(input);
assert!(output.contains("https://example.com/path"));
}
#[test]
fn test_comment_after_string_value() {
let input = r#"{"key": "value"} // end"#;
let output = strip_comments(input);
assert!(!output.contains("end"));
assert!(output.contains("\"value\""));
}
#[test]
fn test_output_capacity_hint() {
let input = "// comment\n{\"a\":1}";
let output = strip_comments(input);
assert!(output.len() <= input.len() + 5); }
#[test]
fn test_strip_only_comments() {
let input = "// just a comment";
let output = strip_comments(input);
assert!(!output.contains("just a comment"));
}
#[test]
fn test_strip_block_comment_only() {
let input = "/* entire file is a comment */";
let output = strip_comments(input);
assert!(!output.contains("entire"));
assert_eq!(output.trim(), "");
}
#[test]
fn test_adjacent_comments() {
let input = "// comment1\n// comment2\n{\"a\":1}";
let output = strip_comments(input);
assert!(!output.contains("comment1"));
assert!(!output.contains("comment2"));
assert!(output.contains("{\"a\":1}"));
}
#[test]
fn test_string_with_escaped_backslash_then_quote() {
let input = r#"{"k": "a\\"}"#;
let output = strip_comments(input);
assert_eq!(output, input);
}
#[test]
fn test_comment_between_keys() {
let input = "{\"a\":1,/* between */\"b\":2}";
let output = strip_comments(input);
assert!(!output.contains("between"));
assert!(output.contains("\"a\""));
assert!(output.contains("\"b\""));
}
#[test]
fn test_parse_json5_empty_object() {
let input = "{ /* nothing */ }";
let result = parse_json5(input);
assert!(result.is_ok());
}
#[test]
fn test_parse_json5_array_with_comments() {
let input = "[\n 1, // first\n 2, // second\n 3\n]";
let result = parse_json5(input);
assert!(result.is_ok());
let node = result.unwrap();
assert_eq!(node.len(), Some(3));
}
#[test]
fn test_parse_json5_nested_with_comments() {
let input = r#"{
"outer": {
// inner comment
"inner": true
}
}"#;
let result = parse_json5(input);
assert!(result.is_ok());
}
#[test]
fn test_parse_json5_all_types() {
let input = r#"{
"s": "hello", // string
"n": 42, /* number */
"f": 3.14,
"b": true,
"nil": null,
"arr": [1,2,3]
}"#;
let result = parse_json5(input);
assert!(result.is_ok());
let node = result.unwrap();
assert_eq!(node["s"].as_str(), Some("hello"));
assert_eq!(node["n"].as_i64(), Some(42));
assert!(node["b"].is_boolean());
assert!(node["nil"].is_null());
}
#[test]
fn test_parse_json5_invalid_json_after_strip() {
let result = parse_json5("{ // comment\n bad }");
assert!(result.is_err());
}
#[test]
fn test_parse_json5_boolean_with_comments() {
assert!(matches!(parse_json5("/* comment */ true"), Ok(n) if n.is_boolean()));
assert!(matches!(parse_json5("false // comment"), Ok(n) if n.is_boolean()));
}
#[test]
fn test_parse_json5_null_with_comment() {
let result = parse_json5("/* before */ null");
assert!(result.is_ok());
assert!(result.unwrap().is_null());
}
#[test]
fn test_parse_json5_url_in_value() {
let input = r#"{"url": "https://example.com"}"#;
let result = parse_json5(input);
assert!(result.is_ok());
assert_eq!(result.unwrap()["url"].as_str(), Some("https://example.com"));
}
}