Refactor: use efficient char list building instead of string concat in loop
Co-authored-by: Kinneyzhang <38454496+Kinneyzhang@users.noreply.github.com>
This commit is contained in:
parent
5b8a328855
commit
836ff75c89
27
ekp.el
27
ekp.el
@ -178,14 +178,27 @@ Each takes ideal, stretch (+), and shrink (-) values."
|
|||||||
(defun ekp--split-hyphenated-word (word)
|
(defun ekp--split-hyphenated-word (word)
|
||||||
"Split WORD at existing hyphens into parts with hyphens attached.
|
"Split WORD at existing hyphens into parts with hyphens attached.
|
||||||
E.g., \"help-echo\" -> (\"help-\" \"echo\").
|
E.g., \"help-echo\" -> (\"help-\" \"echo\").
|
||||||
|
Handles edge cases: trailing hyphens, leading hyphens, consecutive hyphens.
|
||||||
Does NOT apply dictionary hyphenation - just uses existing hyphens."
|
Does NOT apply dictionary hyphenation - just uses existing hyphens."
|
||||||
(let* ((segments (split-string word "-" t)) ; t = omit empty/null strings
|
(let ((result nil)
|
||||||
(num-segs (length segments)))
|
(current nil) ; list of chars (built in reverse)
|
||||||
(cl-loop for seg in segments
|
(i 0)
|
||||||
for i from 0
|
(len (length word)))
|
||||||
collect (if (< i (1- num-segs))
|
(while (< i len)
|
||||||
(concat seg "-")
|
(let ((char (aref word i)))
|
||||||
seg))))
|
(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)
|
(defun ekp--split-with-hyphen (string)
|
||||||
"Split STRING into boxes with hyphenation points marked.
|
"Split STRING into boxes with hyphenation points marked.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user