Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2002e4642d
Fetching contributors…

Cannot retrieve contributors at this time

file 120 lines (99 sloc) 4.4 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
;;; string-ext.el ---

;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>

;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,

;;; License

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;;; Code:

(require 'cl)

(defun string-ext/cut (string cut from)
  "Cut from STRING fragment CUT from FROM,
FROM must equal :begin or :end. Return result string,
else return nil."
  (cond
    ((and (eq from :begin)
          (string-ext/start-p string cut))
     (substring string (length cut) (length string)))
    ((and (eq from :end)
          (string-ext/end-p string cut))
     (substring string 0 (- (length string) (length cut))))))
;; (t string)))

(defun string-ext/cut-safe (string cut from)
  (let ((str (string-ext/cut string cut from)))
    (if str
        str
      string)))

(defun string-ext/from-symbol (sym)
  "Convert symbol SYM to string."
  (string-ext/cut-safe (format "%s" sym) ":" :begin))

(defun string-ext/start-p (string start)
  "Return t if STRING start with START, else return nil."
  (let ((len (length start))
        (orig-len (length string)))
    (when (<= len orig-len)
      (string= start (substring string 0 len)))))

(defun string-ext/end-p (string end)
  "Return t if STRING end with END, else return nil."
  (let* ((len (length end))
         (orig-len (length string))
         (from (- orig-len len)))
    (when (<= len orig-len)
      (string= end (substring string from orig-len)))))

(defmacro string-ext/string=~ (regex string &rest body)
  "regex matching similar to the =~ operator found in other languages."
  (let ((str (gensym)))
    `(lexical-let ((,str ,string))
       ;; Use lexical-let to make closures (in flet).
       (when (string-match ,regex ,str)
         (symbol-macrolet ,(loop for i to 9 collect
                                 (let ((sym (intern (concat "$" (number-to-string i)))))
                                   `(,sym (match-string ,i ,str))))
           (flet (($ (i) (match-string i ,str))
                  (sub (replacement &optional (i 0) &key fixedcase literal-string)
                       (replace-match replacement fixedcase literal-string ,str i)))
             (symbol-macrolet ( ;;before
                               ($b (substring ,str 0 (match-beginning 0)))
                               ;;match
                               ($m (match-string 0 ,str))
                               ;;after
                               ($a (substring ,str (match-end 0) (length ,str))))
               ,@body)))))))

(defun string-ext/decamelize (string)
  "Convert from camel_case/string to CamelCase::String."
  (let ((case-fold-search nil))
    (replace-regexp-in-string " " ""
      (replace-regexp-in-string " " "::"
        (capitalize
         (replace-regexp-in-string "\_" " "
           (replace-regexp-in-string "\/" " " string)))))))

(defalias 'decamelize-string 'string-ext/decamelize)

(defun string-ext/camelize (string)
  "Convert from CamelCase::String to camel_case/string."
  (let ((case-fold-search nil))
    (downcase
     (replace-regexp-in-string "::" "/"
       (replace-regexp-in-string
        "\\([A-Z]+\\)\\([A-Z][a-z]\\)" "\\1_\\2"
          (replace-regexp-in-string
            "\\([a-z\\d]\\)\\([A-Z]\\)" "\\1_\\2"
            string))))))

(defalias 'camelize-string 'string-ext/camelize)

(defun string-ext/empty-p (str) ;(+)
  "Return t if string STR is not empty."
  (not (and (stringp str)
            (not (string-equal "" str))
            (not (string-match "^ +$" str)))))

(defun string-ext/safe-symbol (str)
  "Return symbol from STR and replace and non word chars to '-'."
  (intern (replace-regexp-in-string "[^a-zA-z0-9]+" "-"
           (downcase str))))

(provide 'string-ext)
Something went wrong with that request. Please try again.