]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/subr-x.el
41c3ff63ae44c43c9138d9b16fb7eea33323a0a7
[gnu-emacs] / lisp / emacs-lisp / subr-x.el
1 ;;; subr-x.el --- extra Lisp functions -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: convenience
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
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
28 ;; in subr.el.
29
30 ;;; Code:
31
32 (defsubst hash-table-keys (hash-table)
33 "Return a list of keys in HASH-TABLE."
34 (let ((keys '()))
35 (maphash (lambda (k _v) (push k keys)) hash-table)
36 keys))
37
38 (defsubst hash-table-values (hash-table)
39 "Return a list of values in HASH-TABLE."
40 (let ((values '()))
41 (maphash (lambda (_k v) (push v values)) hash-table)
42 values))
43
44 (defsubst string-empty-p (string)
45 "Check whether STRING is empty."
46 (string= string ""))
47
48 (defsubst string-join (strings &optional separator)
49 "Join all STRINGS using SEPARATOR."
50 (mapconcat 'identity strings separator))
51
52 (defsubst string-reverse (str)
53 "Reverse the string STR."
54 (apply 'string (nreverse (string-to-list str))))
55
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)
60 string))
61
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)
66 string))
67
68 (defsubst string-trim (string)
69 "Remove leading and trailing whitespace from STRING."
70 (string-trim-left (string-trim-right string)))
71
72 (defsubst string-blank-p (string)
73 "Check whether STRING is either empty or only whitespace."
74 (string-match-p "\\`[ \t\n\r]*\\'" string))
75
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))
80 string))
81
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)))
86 string))
87
88 (provide 'subr-x)
89
90 ;;; subr-x.el ends here