Skip to content

Commit 0fd2e00

Browse files
GearsDatapackslpil
authored andcommitted
Fix UTF-16 and UTF-32 segments on Erlang
1 parent cba4cd1 commit 0fd2e00

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

compiler-core/src/erlang/pattern.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use std::cell::RefCell;
22

33
use ecow::eco_format;
44

5-
use crate::analyse::Inferred;
5+
use crate::{
6+
analyse::Inferred,
7+
strings::{length_utf16, length_utf32},
8+
};
69

710
use super::*;
811

@@ -215,12 +218,33 @@ fn pattern_segment<'a>(
215218
// the string first, so we can correctly match the bit array segment then compare
216219
// it afterwards.
217220
Pattern::String { value, .. } => {
221+
let escaped = convert_string_escape_chars(value);
222+
let (utf_option, string_length) = if options
223+
.iter()
224+
.any(|option| matches!(option, BitArrayOption::Utf16 { .. }))
225+
{
226+
// Each UTF-16 codepoint is 2 bytes
227+
("utf16", length_utf16(&escaped) * 2)
228+
} else if options
229+
.iter()
230+
.any(|option| matches!(option, BitArrayOption::Utf32 { .. }))
231+
{
232+
// Each UTF-32 codepoint is 4 bytes
233+
("utf32", length_utf32(&escaped) * 4)
234+
} else {
235+
("utf8", escaped.len())
236+
};
237+
218238
guards.borrow_mut().push(docvec![
219239
variable_name.clone(),
220240
" =:= ",
221-
string(value)
241+
"<<\"",
242+
string_inner(value),
243+
"\"/",
244+
utf_option,
245+
">>",
222246
]);
223-
docvec![variable_name, ":", string_length_utf8_bytes(value)]
247+
docvec![variable_name, ":", string_length]
224248
}
225249

226250
// Doing a pattern such as `<<_ as a>>` is the same as just `<<a>>`, so we treat it

0 commit comments

Comments
 (0)