From 85ca1c6ba90b6b733189e988e9f0e8ed34f21e19 Mon Sep 17 00:00:00 2001 From: Glenn Miao Date: Thu, 1 Jan 2026 21:24:41 +0800 Subject: [PATCH] perf(core): reduce redundant grapheme conversions in common_prefix/suffix --- src/str/core.gleam | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/str/core.gleam b/src/str/core.gleam index 4c395f5..e1ad4f4 100644 --- a/src/str/core.gleam +++ b/src/str/core.gleam @@ -926,7 +926,8 @@ pub fn common_prefix(strings: List(String)) -> String { [single] -> single [first, ..rest] -> { let first_chars = string.to_graphemes(first) - find_common_prefix(first_chars, rest, []) + let others_chars = list.map(rest, string.to_graphemes) + find_common_prefix(first_chars, others_chars, []) |> list.reverse |> string.concat } @@ -935,15 +936,15 @@ pub fn common_prefix(strings: List(String)) -> String { fn find_common_prefix( reference: List(String), - others: List(String), + others: List(List(String)), acc: List(String), ) -> List(String) { case reference { [] -> acc [char, ..rest_ref] -> { let all_have_char = - list.all(others, fn(s) { - case string.to_graphemes(s) { + list.all(others, fn(graphemes) { + case graphemes { [] -> False [first, ..] -> first == char } @@ -952,9 +953,7 @@ fn find_common_prefix( False -> acc True -> { let new_others = - list.map(others, fn(s) { - s |> string.to_graphemes |> list.drop(1) |> string.concat - }) + list.map(others, fn(graphemes) { list.drop(graphemes, 1) }) find_common_prefix(rest_ref, new_others, [char, ..acc]) } } @@ -968,14 +967,17 @@ fn find_common_prefix( /// common_suffix(["hello", "world"]) -> "" /// pub fn common_suffix(strings: List(String)) -> String { - strings - |> list.map(fn(s) { - s |> string.to_graphemes |> list.reverse |> string.concat - }) - |> common_prefix - |> string.to_graphemes - |> list.reverse - |> string.concat + case strings { + [] -> "" + [single] -> single + [first, ..rest] -> { + let first_chars = string.to_graphemes(first) |> list.reverse + let others_chars = + list.map(rest, fn(s) { string.to_graphemes(s) |> list.reverse }) + find_common_prefix(first_chars, others_chars, []) + |> string.concat + } + } } // ============================================================================