Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raw string formatter is broken #66

Open
1e1001 opened this issue Oct 30, 2023 · 0 comments
Open

raw string formatter is broken #66

1e1001 opened this issue Oct 30, 2023 · 0 comments

Comments

@1e1001
Copy link

1e1001 commented Oct 30, 2023

use kdl::{KdlDocument, KdlEntry, KdlNode, KdlValue};
use std::str::FromStr;
fn main() {
  let mut node = KdlNode::new("what");
  node.entries_mut().push(KdlEntry::new(KdlValue::RawString(
    "\"#\nkdl-injection \"no way!!\"//".into(),
  )));
  let mut doc = KdlDocument::new();
  doc.nodes_mut().push(node);
  println!("{doc}");
  println!(
    "{} ≠ {} ‽",
    doc.nodes().len(),
    // unparse and parse the document
    KdlDocument::from_str(&doc.to_string())
      .unwrap()
      .nodes()
      .len()
  );
}

outputs

what r#""#
kdl-injection "no way!!"//"#

1 ≠ 2 ‽

this is because the implementation of write_raw_string checks for sequences of /#{n}"/ instead of /"#{n}/

i'd suggest replacing the for loop with something like

for char in raw.chars() {
    if char == '"' {
        consecutive = 1;
    } else if char == '#' && consecutive > 0 {
        consecutive += 1;
    } else {
        consecutive = 0;
    }
    maxhash = maxhash.max(consecutive);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant