* lisp/emacs-lisp/helpers.el: Add some string helpers.
[bpt/emacs.git] / lisp / emacs-lisp / helpers.el
1 ;;; helpers.el --- Some non-essential library extensions -*- 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 ;;; Code:
27
28 (defsubst hash-table-keys (hash-table)
29 "Return a list of keys in HASH-TABLE."
30 (let ((keys '()))
31 (maphash (lambda (k _v) (push k keys)) hash-table)
32 keys))
33
34 (defsubst hash-table-values (hash-table)
35 "Return a list of values in HASH-TABLE."
36 (let ((values '()))
37 (maphash (lambda (_k v) (push v values)) hash-table)
38 values))
39
40 (defsubst string-trim-left (string)
41 "Remove leading whitespace from STRING."
42 (if (string-match "\\`[ \t\n\r]+" string)
43 (replace-match "" t t string)
44 string))
45
46 (defsubst string-trim-right (string)
47 "Remove trailing whitespace from STRING."
48 (if (string-match "[ \t\n\r]+\\'" string)
49 (replace-match "" t t string)
50 string))
51
52 (defsubst string-trim (string)
53 "Remove leading and trailing whitespace from STRING."
54 (string-trim-left (string-trim-right string)))
55
56 (provide 'helpers)
57
58 ;;; helpers.el ends here