-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unicode escaping in resolver (#84)
* Add unicode escaping in resolver * Switch to Cow
- Loading branch information
1 parent
8067752
commit ff5b216
Showing
9 changed files
with
126 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
face-with-tears-of-joy = 😂 | ||
tetragram-for-centre = 𝌆 | ||
surrogates-in-text = \uD83D\uDE02 | ||
surrogates-in-string = {"\uD83D\uDE02"} | ||
surrogates-in-adjacent-strings = {"\uD83D"}{"\uDE02"} | ||
emoji-in-text = A face 😂 with tears of joy. | ||
emoji-in-string = {"A face 😂 with tears of joy."} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod ast; | ||
pub mod parser; | ||
pub mod unicode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::borrow::Cow; | ||
use std::char; | ||
|
||
fn encode_unicode(s: &str) -> char { | ||
u32::from_str_radix(s, 16) | ||
.ok() | ||
.and_then(char::from_u32) | ||
.unwrap_or('�') | ||
} | ||
|
||
pub fn unescape_unicode<'u>(input: &'u str) -> Cow<'u, str> { | ||
let bytes = input.as_bytes(); | ||
let mut result = Cow::from(input); | ||
|
||
let mut ptr = 0; | ||
|
||
while let Some(b) = bytes.get(ptr) { | ||
if b != &b'\\' { | ||
if let Cow::Owned(ref mut s) = result { | ||
s.push(*b as char); | ||
} | ||
ptr += 1; | ||
continue; | ||
} | ||
|
||
if let Cow::Borrowed(_) = result { | ||
result = Cow::from(&input[0..ptr]); | ||
} | ||
|
||
ptr += 1; | ||
|
||
let new_char = match bytes.get(ptr) { | ||
Some(b'\\') => '\\', | ||
Some(b'"') => '"', | ||
Some(u @ b'u') | Some(u @ b'U') => { | ||
let start = ptr + 1; | ||
let len = if u == &b'u' { 4 } else { 6 }; | ||
ptr += len; | ||
input | ||
.get(start..(start + len)) | ||
.map(|slice| encode_unicode(slice)) | ||
.unwrap_or('�') | ||
} | ||
_ => '�', | ||
}; | ||
result.to_mut().push(new_char); | ||
ptr += 1; | ||
} | ||
result | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use fluent_syntax::unicode::unescape_unicode; | ||
use std::borrow::Cow; | ||
|
||
fn is_cow_borrowed<'a>(input: Cow<'a, str>) -> bool { | ||
if let Cow::Borrowed(_) = input { | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
#[test] | ||
fn unescape_unicode_test() { | ||
assert!(is_cow_borrowed(unescape_unicode("foo"))); | ||
|
||
assert_eq!(unescape_unicode("foo"), "foo"); | ||
assert_eq!(unescape_unicode("foo \\\\"), "foo \\"); | ||
assert_eq!(unescape_unicode("foo \\\""), "foo \""); | ||
assert_eq!(unescape_unicode("foo \\\\ faa"), "foo \\ faa"); | ||
assert_eq!( | ||
unescape_unicode("foo \\\\ faa \\\\ fii"), | ||
"foo \\ faa \\ fii" | ||
); | ||
assert_eq!( | ||
unescape_unicode("foo \\\\\\\" faa \\\"\\\\ fii"), | ||
"foo \\\" faa \"\\ fii" | ||
); | ||
assert_eq!(unescape_unicode("\\u0041\\u004F"), "AO"); | ||
assert_eq!(unescape_unicode("\\uA"), "�"); | ||
assert_eq!(unescape_unicode("\\uA0Pl"), "�"); | ||
assert_eq!(unescape_unicode("\\d Foo"), "� Foo"); | ||
} |