1 ;;; subr-x.el --- extra Lisp functions -*- lexical-binding:t -*-
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
6 ;; Keywords: convenience
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Less commonly used functions that complement basic APIs, often implemented in
27 ;; C code (like hash-tables and strings), and are not eligible for inclusion
32 (defsubst hash-table-keys
(hash-table)
33 "Return a list of keys in HASH-TABLE."
35 (maphash (lambda (k _v
) (push k keys
)) hash-table
)
38 (defsubst hash-table-values
(hash-table)
39 "Return a list of values in HASH-TABLE."
41 (maphash (lambda (_k v
) (push v values
)) hash-table
)
44 (defsubst string-empty-p
(string)
45 "Check whether STRING is empty."
48 (defsubst string-join
(strings &optional separator
)
49 "Join all STRINGS using SEPARATOR."
50 (mapconcat 'identity strings separator
))
52 (defsubst string-reverse
(str)
53 "Reverse the string STR."
54 (apply 'string
(nreverse (string-to-list str
))))
56 (defsubst string-trim-left
(string)
57 "Remove leading whitespace from STRING."
58 (if (string-match "\\`[ \t\n\r]+" string
)
59 (replace-match "" t t string
)
62 (defsubst string-trim-right
(string)
63 "Remove trailing whitespace from STRING."
64 (if (string-match "[ \t\n\r]+\\'" string
)
65 (replace-match "" t t string
)
68 (defsubst string-trim
(string)
69 "Remove leading and trailing whitespace from STRING."
70 (string-trim-left (string-trim-right string
)))
72 (defsubst string-blank-p
(string)
73 "Check whether STRING is either empty or only whitespace."
74 (string-match-p "\\`[ \t\n\r]*\\'" string
))
76 (defsubst string-remove-prefix
(prefix string
)
77 "Remove PREFIX from STRING if present."
78 (if (string-prefix-p prefix string
)
79 (substring string
(length prefix
))
82 (defsubst string-remove-suffix
(suffix string
)
83 "Remove SUFFIX from STRING if present."
84 (if (string-suffix-p suffix string
)
85 (substring string
0 (- (length string
) (length suffix
)))
90 ;;; subr-x.el ends here