Subject: Re: bug#17814: 24.3.91; better string manipulation in subr-x

From: Shigeru Fukaya <[email-masked]>
Date: Sat, 21 Jun 2014 02:14:19 +0900

>> I change the string-trim defined using defun from defsubst, as its
>> string literal is somewhat big (Actually I suspect most of other
>> functions would also be better if defined by defun).
>
>The use of `defsubst' is so that subr-x.el is not needed at run-time.

I see.  I didn't know that, and it's very good.
Then, the code is

(defsubst string-trim (string)
  "Remove leading and trailing whitespace from STRING."
  (string-trim-right (string-trim-left string)))

or

(defsubst string-trim (string)
  "Remove leading and trailing whitespace from STRING."
  (string-match "\\`[\s\t\n\r]*\\(.*?\\)[\s\t\n\r]*\\'" string)
  (if (or (< 0 (match-beginning 1)) (< (match-end 1) (match-end 0)))
      (match-string 1 string)
    string))

The latter is shorter in byte-compiled code, and call string-match
only once.  Literal string is seemingly larger, but the overhead of
a string object will cover it, I think.

Regards,
Shigeru