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

Reliably handle Unicode string literals #1480

Merged
merged 2 commits into from Jan 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions crates/libs/bindgen/src/helpers.rs
Expand Up @@ -201,6 +201,17 @@ pub fn gen_vtbl(def: &TypeDef, cfg: &Cfg, gen: &Gen) -> TokenStream {
}
}

fn gen_string_literal(value: &str) -> TokenStream {
let mut tokens = "\"".to_string();

for u in value.chars() {
tokens.push_str(&format!("{}", u.escape_default()));
}

tokens.push('\"');
tokens.into()
}

fn gen_type_guid(def: &TypeDef, gen: &Gen, type_name: &TokenStream) -> TokenStream {
if def.generics.is_empty() {
match GUID::from_attributes(def.attributes()) {
Expand Down Expand Up @@ -231,7 +242,10 @@ pub fn gen_constant_type_value(value: &ConstantValue) -> TokenStream {
ConstantValue::I64(value) => quote! { i64 = #value },
ConstantValue::F32(value) => quote! { f32 = #value },
ConstantValue::F64(value) => quote! { f64 = #value },
ConstantValue::String(value) => quote! { &'static str = #value },
ConstantValue::String(value) => {
let value = gen_string_literal(value);
quote! { &'static str = #value }
}
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -275,7 +289,7 @@ pub fn gen_constant_value(value: &ConstantValue) -> TokenStream {
ConstantValue::I64(value) => quote! { #value },
ConstantValue::F32(value) => quote! { #value },
ConstantValue::F64(value) => quote! { #value },
ConstantValue::String(value) => quote! { #value },
ConstantValue::String(value) => gen_string_literal(value),
_ => unimplemented!(),
}
}
Expand Down