Skip to content

Commit

Permalink
use common string escape chars function
Browse files Browse the repository at this point in the history
Adapt gleam-lang/gleam#3139 to the Nix target
  • Loading branch information
PgBiel committed Jun 8, 2024
1 parent 938ba47 commit b479101
Showing 1 changed file with 3 additions and 55 deletions.
58 changes: 3 additions & 55 deletions compiler-core/src/nix/pattern.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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::{
expression, maybe_escape_identifier_doc, module_var_name_doc, syntax, Error, Output,
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'";
Expand Down Expand Up @@ -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::<String>();

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()
}

0 comments on commit b479101

Please sign in to comment.