From 4a8a102b3b4f8483af5b688e7ea480ff6b8dc2ed Mon Sep 17 00:00:00 2001 From: Rahguzar Date: Fri, 5 Jan 2024 22:09:27 +0100 Subject: Count characters directly from string --- lisp/mastodon-toot.el | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'lisp/mastodon-toot.el') diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index f60f314..51990fd 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -1669,20 +1669,13 @@ REPLY-REGION is a string to be injected into the buffer." URLs always = 23, and domain names of handles are not counted. This is how mastodon does it. CW is the content warning, which contributes to the character count." - (with-temp-buffer - (switch-to-buffer (current-buffer)) - (insert toot-string) - (goto-char (point-min)) - ;; handle URLs - (while (search-forward-regexp mastodon-toot-url-regex nil t) - ; "\\w+://[^ \n]*" old regex - (replace-match "xxxxxxxxxxxxxxxxxxxxxxx")) ; 23 x's - ;; handle @handles - (goto-char (point-min)) - (while (search-forward-regexp mastodon-toot-handle-regex nil t) - (replace-match (match-string 2))) ; replace with handle only + (let* ((url-replacement (make-string 23 ?x)) + (count-str (replace-regexp-in-string ; handle @handles + mastodon-toot-handle-regex "\2" + (replace-regexp-in-string ; handle URLs + mastodon-toot-url-regex url-replacement toot-string)))) (+ (length cw) - (length (buffer-substring (point-min) (point-max)))))) + (length count-str)))) ;;; DRAFTS -- cgit v1.2.3