From 836ff75c8939d3d1f0925ccb28cdc840acf699d8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 25 Jan 2026 12:30:08 +0000 Subject: [PATCH] Refactor: use efficient char list building instead of string concat in loop Co-authored-by: Kinneyzhang <38454496+Kinneyzhang@users.noreply.github.com> --- ekp.el | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/ekp.el b/ekp.el index 1586b37..24c92d2 100644 --- a/ekp.el +++ b/ekp.el @@ -178,14 +178,27 @@ Each takes ideal, stretch (+), and shrink (-) values." (defun ekp--split-hyphenated-word (word) "Split WORD at existing hyphens into parts with hyphens attached. E.g., \"help-echo\" -> (\"help-\" \"echo\"). +Handles edge cases: trailing hyphens, leading hyphens, consecutive hyphens. Does NOT apply dictionary hyphenation - just uses existing hyphens." - (let* ((segments (split-string word "-" t)) ; t = omit empty/null strings - (num-segs (length segments))) - (cl-loop for seg in segments - for i from 0 - collect (if (< i (1- num-segs)) - (concat seg "-") - seg)))) + (let ((result nil) + (current nil) ; list of chars (built in reverse) + (i 0) + (len (length word))) + (while (< i len) + (let ((char (aref word i))) + (if (= char ?-) + ;; Hyphen: attach to current segment and push + (progn + (push char current) + (push (apply #'string (nreverse current)) result) + (setq current nil)) + ;; Non-hyphen: accumulate + (push char current))) + (cl-incf i)) + ;; Push remaining content (last segment after final hyphen, or entire word if no hyphen) + (unless (null current) + (push (apply #'string (nreverse current)) result)) + (nreverse result))) (defun ekp--split-with-hyphen (string) "Split STRING into boxes with hyphenation points marked.