From b47910107442c8ba810959317e45a1f7bbbbafe2 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Sat, 25 May 2024 21:50:53 -0300 Subject: [PATCH] use common string escape chars function Adapt https://github.com/gleam-lang/gleam/pull/3139 to the Nix target --- compiler-core/src/nix/pattern.rs | 58 ++------------------------------ 1 file changed, 3 insertions(+), 55 deletions(-) diff --git a/compiler-core/src/nix/pattern.rs b/compiler-core/src/nix/pattern.rs index fd748119f..a205ff486 100644 --- a/compiler-core/src/nix/pattern.rs +++ b/compiler-core/src/nix/pattern.rs @@ -1,8 +1,8 @@ -use crate::analyse::Inferred; use ecow::EcoString; use itertools::Itertools; use std::sync::OnceLock; +use crate::analyse::Inferred; use crate::ast::{AssignName, ClauseGuard, Pattern, TypedClauseGuard, TypedExpr, TypedPattern}; use crate::docvec; use crate::nix::{ @@ -10,6 +10,7 @@ use crate::nix::{ UsageTracker, }; use crate::pretty::{nil, Document, Documentable}; +use crate::strings::convert_string_escape_chars; use crate::type_::{FieldMap, PatternConstructor}; pub static ASSIGNMENT_VAR: &str = "_pat'"; @@ -1060,58 +1061,5 @@ pub(crate) fn assign_subjects<'a>( /// Calculates the length of str as UTF-8 bytes without escape characters. fn no_escape_bytes_len(str: &EcoString) -> usize { - let mut filtered_str = String::new(); - let mut str_iter = str.chars().peekable(); - loop { - match str_iter.next() { - Some('\\') => match str_iter.next() { - // Check for Unicode escape sequence, e.g. \u{00012FF} - Some('u') => { - if str_iter.peek() != Some(&'{') { - // Invalid Unicode escape sequence - filtered_str.push('u'); - continue; - } - - // Consume the left brace after peeking - let _ = str_iter.next(); - - let codepoint_str = str_iter - .peeking_take_while(char::is_ascii_hexdigit) - .collect::(); - - if codepoint_str.is_empty() || str_iter.peek() != Some(&'}') { - // Invalid Unicode escape sequence - filtered_str.push_str("u{"); - filtered_str.push_str(&codepoint_str); - continue; - } - - let codepoint = u32::from_str_radix(&codepoint_str, 16) - .ok() - .and_then(char::from_u32); - - if let Some(codepoint) = codepoint { - // Consume the right brace after peeking - let _ = str_iter.next(); - - // Consider this codepoint's length instead of - // that of the Unicode escape sequence itself - filtered_str.push(codepoint); - } else { - // Invalid Unicode escape sequence - // (codepoint value not in base 16 or too large) - filtered_str.push_str("u{"); - filtered_str.push_str(&codepoint_str); - } - } - Some(c) => filtered_str.push(c), - None => break, - }, - Some(c) => filtered_str.push(c), - None => break, - } - } - - filtered_str.len() + convert_string_escape_chars(str).len() }