Debian Codegraph @

src/unicode.rs

raw symbols SCIP
1 //! A set of helper functions for unescaping Fluent unicode escape sequences.
2 //!
3 //! # Unicode
4 //!
5 //! Fluent supports UTF-8 in all FTL resources, but it also allows
6 //! unicode sequences to be escaped in [`String
7 //! Literals`](super::ast::InlineExpression::StringLiteral).
8 //!
9 //! Four byte sequences are encoded with `\u` and six byte
10 //! sequences using `\U`.
11 //! ## Example
12 //!
13 //! ```
14 //! use fluent_syntax::unicode::unescape_unicode_to_string;
15 //!
16 //! assert_eq!(
17 //! unescape_unicode_to_string("Foo \\u5bd2 Bar"),
18 //! "Foo 寒 Bar"
19 //! );
20 //!
21 //! assert_eq!(
22 //! unescape_unicode_to_string("Foo \\U01F68A Bar"),
23 //! "Foo 🚊 Bar"
24 //! );
25 //! ```
26 //!
27 //! # Other unescapes
28 //!
29 //! This also allows for a char `"` to be present inside an FTL string literal,
30 //! and for `\` itself to be escaped.
31 //!
32 //! ## Example
33 //!
34 //! ```
35 //! use fluent_syntax::unicode::unescape_unicode_to_string;
36 //!
37 //! assert_eq!(
38 //! unescape_unicode_to_string("Foo \\\" Bar"),
39 //! "Foo \" Bar"
40 //! );
41 //! assert_eq!(
42 //! unescape_unicode_to_string("Foo \\\\ Bar"),
43 //! "Foo \\ Bar"
44 //! );
45 //! ```
46 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
47 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/;
48 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
49
50 const UNKNOWN_CHARrust-analyzer cargo fluent-syntax 0.12.0 unicode/UNKNOWN_CHAR.: char = '�';
51
52 fn encode_unicoderust-analyzer cargo fluent-syntax 0.12.0 unicode/encode_unicode().(s: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>) -> char {
53 s.and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|s| u32::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_str_radix().(s, 16).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/::from_u32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/from_u32().))
54 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(UNKNOWN_CHARrust-analyzer cargo fluent-syntax 0.12.0 unicode/UNKNOWN_CHAR.)
55 }
56
57 /// Unescapes to a writer without allocating.
58 ///
59 /// ## Example
60 ///
61 /// ```
62 /// use fluent_syntax::unicode::unescape_unicode;
63 ///
64 /// let mut s = String::new();
65 /// unescape_unicode(&mut s, "Foo \\U01F60A Bar");
66 /// assert_eq!(s, "Foo 😊 Bar");
67 /// ```
68 pub fn unescape_unicoderust-analyzer cargo fluent-syntax 0.12.0 unicode/unescape_unicode().<W>(w: &mut W, input: &str) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result#
69 where
70 W: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#,
71 {
72 if unescaperust-analyzer cargo fluent-syntax 0.12.0 unicode/unescape().(w, input)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
73 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(());
74 }
75 w.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#write_str().(input)
76 }
77
78 fn unescaperust-analyzer cargo fluent-syntax 0.12.0 unicode/unescape().<W>(w: &mut W, input: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#>
79 where
80 W: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#,
81 {
82 let bytes = input.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().();
83
84 let mut start = 0;
85 let mut ptr = 0;
86
87 while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b) = bytes.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(ptr) {
88 if b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().&b'\\' {
89 ptr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
90 continue;
91 }
92 if start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().ptr {
93 w.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#write_str().(&input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#ptr]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
94 }
95
96 ptr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
97
98 let new_char = match bytes.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(ptr) {
99 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'\\') => '\\',
100 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'"') => '"',
101 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(u @ b'u') | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(u @ b'U') => {
102 let seq_start = ptr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1;
103 let len = if u rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().&b'u' { 4 } else { 6 };
104 ptr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().len;
105 encode_unicoderust-analyzer cargo fluent-syntax 0.12.0 unicode/encode_unicode().(input.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]get().(seq_start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#seq_start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().len))
106 }
107 _ => UNKNOWN_CHARrust-analyzer cargo fluent-syntax 0.12.0 unicode/UNKNOWN_CHAR.,
108 };
109 ptr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
110 w.write_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#write_char().(new_char)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
111 start = ptr;
112 }
113
114 if start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
115 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false);
116 }
117
118 if start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().ptr {
119 w.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#write_str().(&input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#ptr]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
120 }
121 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(true)
122 }
123
124 /// Unescapes to a `Cow<str>` optionally allocating.
125 ///
126 /// ## Example
127 ///
128 /// ```
129 /// use fluent_syntax::unicode::unescape_unicode_to_string;
130 ///
131 /// assert_eq!(
132 /// unescape_unicode_to_string("Foo \\U01F60A Bar"),
133 /// "Foo 😊 Bar"
134 /// );
135 /// ```
136 pub fn unescape_unicode_to_stringrust-analyzer cargo fluent-syntax 0.12.0 unicode/unescape_unicode_to_string().(input: &str) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str> {
137 let mut result = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
138 let owned = unescaperust-analyzer cargo fluent-syntax 0.12.0 unicode/unescape().(&mut result, input).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("String write methods don't Err");
139 if owned {
140 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(result)
141 } else {
142 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(input)
143 }
144 }