1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2025-01-18 18:05:07 +00:00

Move string-trim functions to subr.el

* doc/lispref/strings.texi (Creating Strings): Document them.

* lisp/faces.el: Don't require subr-x, because that leads to build
errors.

* lisp/subr.el (string-trim, string-trim-right)
(string-trim-left): Move here from subr-x.el.

* lisp/emacs-lisp/shortdoc.el (string): Adjust.
This commit is contained in:
Lars Ingebrigtsen 2021-03-24 09:22:40 +01:00
parent c0d24d5316
commit a4ececf004
5 changed files with 38 additions and 27 deletions

View File

@ -390,6 +390,22 @@ whitespace to a single space character, as well as removing all
whitespace from the start and the end of @var{string}.
@end defun
@defun string-trim-left string &optional regexp
Remove the leading text that matches @var{regexp} from @var{string}.
@var{regexp} defaults to @samp{[ \t\n\r]+}.
@end defun
@defun string-trim-right string &optional regexp
Remove the trailing text that matches @var{regexp} from @var{string}.
@var{regexp} defaults to @samp{[ \t\n\r]+}.
@end defun
@defun string-trim string &optional trim-left trim-right
Remove the leading text that matches @var{trim-left} and trailing text
that matches @var{trim-right} from from @var{string}. Both regexps
default to @samp{[ \t\n\r]+}.
@end defun
@defun string-fill string length
Attempt to Word-wrap @var{string} so that no lines are longer than
@var{length}. Filling is done on whitespace boundaries only. If

View File

@ -168,15 +168,12 @@ There can be any number of :example/:result elements."
(replace-regexp-in-string
:eval (replace-regexp-in-string "[a-z]+" "_" "*foo*"))
(string-trim
:no-manual t
:args (string)
:doc "Trim STRING of leading and trailing white space."
:eval (string-trim " foo "))
(string-trim-left
:no-manual t
:eval (string-trim-left "oofoo" "o+"))
(string-trim-right
:no-manual t
:eval (string-trim-right "barkss" "s+"))
(string-truncate-left
:no-manual t

View File

@ -215,28 +215,6 @@ The variable list SPEC is the same as in `if-let'."
(define-obsolete-function-alias 'string-reverse 'reverse "25.1")
(defsubst string-trim-left (string &optional regexp)
"Trim STRING of leading string matching REGEXP.
REGEXP defaults to \"[ \\t\\n\\r]+\"."
(if (string-match (concat "\\`\\(?:" (or regexp "[ \t\n\r]+") "\\)") string)
(substring string (match-end 0))
string))
(defsubst string-trim-right (string &optional regexp)
"Trim STRING of trailing string matching REGEXP.
REGEXP defaults to \"[ \\t\\n\\r]+\"."
(let ((i (string-match-p (concat "\\(?:" (or regexp "[ \t\n\r]+") "\\)\\'")
string)))
(if i (substring string 0 i) string)))
(defsubst string-trim (string &optional trim-left trim-right)
"Trim STRING of leading and trailing strings matching TRIM-LEFT and TRIM-RIGHT.
TRIM-LEFT and TRIM-RIGHT default to \"[ \\t\\n\\r]+\"."
(string-trim-left (string-trim-right string trim-right) trim-left))
;;;###autoload
(defun string-truncate-left (string length)
"Truncate STRING to LENGTH, replacing initial surplus with \"...\"."

View File

@ -25,8 +25,6 @@
;;; Code:
(eval-when-compile (require 'subr-x))
(defcustom term-file-prefix (purecopy "term/")
"If non-nil, Emacs startup performs terminal-specific initialization.
It does this by: (load (concat term-file-prefix (getenv \"TERM\")))

View File

@ -6200,6 +6200,28 @@ returned list are in the same order as in TREE.
;; for discoverability:
(defalias 'flatten-list #'flatten-tree)
(defun string-trim-left (string &optional regexp)
"Trim STRING of leading string matching REGEXP.
REGEXP defaults to \"[ \\t\\n\\r]+\"."
(if (string-match (concat "\\`\\(?:" (or regexp "[ \t\n\r]+") "\\)") string)
(substring string (match-end 0))
string))
(defun string-trim-right (string &optional regexp)
"Trim STRING of trailing string matching REGEXP.
REGEXP defaults to \"[ \\t\\n\\r]+\"."
(let ((i (string-match-p (concat "\\(?:" (or regexp "[ \t\n\r]+") "\\)\\'")
string)))
(if i (substring string 0 i) string)))
(defun string-trim (string &optional trim-left trim-right)
"Trim STRING of leading and trailing strings matching TRIM-LEFT and TRIM-RIGHT.
TRIM-LEFT and TRIM-RIGHT default to \"[ \\t\\n\\r]+\"."
(string-trim-left (string-trim-right string trim-right) trim-left))
;; The initial anchoring is for better performance in searching matches.
(defconst regexp-unmatchable "\\`a\\`"
"Standard regexp guaranteed not to match any string at all.")