Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Updated for RcStr
Browse files Browse the repository at this point in the history
  • Loading branch information
hdamron17 authored and Joshua Nelson committed Aug 3, 2020
1 parent 8ef29cf commit 3d2d038
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/lex/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,6 @@ h",
assert_same_exact("#define hash #a\nhash", "\n#a");
}
#[test]
#[ignore = "Depends on #384"]
fn stringify_string() {
assert_same_stringified(r#""\'""#, r#""\"\\'\"""#);
assert_same_stringified(
Expand Down
33 changes: 17 additions & 16 deletions src/lex/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
use super::{cpp::CppResult, files::FileProcessor};
use crate::{
error::CppError, CompileError, CompileResult, InternedStr, Literal, Locatable, Location, Token,
error::CppError, CompileError, CompileResult, InternedStr, LiteralToken, Locatable, Location,
Token,
};
use std::collections::{HashMap, HashSet, VecDeque};

use shared_str::RcStr;

/// All known macro definitions.
///
/// Note that this is a simple HashMap and not a `Scope`, because
Expand Down Expand Up @@ -375,24 +378,22 @@ fn stringify(args: Vec<Token>) -> Token {
.map(|arg| {
match arg {
Token::Whitespace(_) => String::from(" "), // Single space in replacement
Token::Literal(Literal::Str(s)) => {
// TODO can we unwrap safely?
let new_s = std::str::from_utf8(&s)
.unwrap()
.trim_end_matches('\0') // remove null terminator
.escape_default() // Escape whitespace, etc
.flat_map(char::escape_default); // Escape a second time
"\\\"" // Wrap with escaped quotation marks
.chars()
.chain(new_s)
.chain("\\\"".chars())
.collect::<String>()
Token::Literal(LiteralToken::Str(rcstrs)) => {
rcstrs
.iter()
.map(|rcstr| {
rcstr
.as_str()
.escape_default()
.collect::<String>()
.replace(r#"\'"#, "'") // Because Rust escapes ' but C does not
})
.collect::<Vec<_>>()
.join(" ")
}
other => other.to_string(),
}
})
.collect();
let mut ret: Vec<_> = ret.into();
ret.push(0); // null terminate
Token::Literal(Literal::Str(ret))
Token::Literal(LiteralToken::Str(vec![RcStr::from(format!("\"{}\"", ret))]))
}

0 comments on commit 3d2d038

Please sign in to comment.