2020-09-01 14:44:18 +02:00
|
|
|
;;; string.el --- Library for working with strings -*- lexical-binding: t -*-
|
2020-09-01 00:28:47 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-09-01 00:28:47 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; Package-Requires: ((emacs "24"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Library for working with string.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(require 's)
|
|
|
|
(require 'dash)
|
2019-12-22 22:33:48 +01:00
|
|
|
;; TODO: Resolve the circular dependency that this introduces.
|
|
|
|
;; (require 'prelude)
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-contains? (c x)
|
2020-01-18 23:37:27 +01:00
|
|
|
"Return t if X is in C."
|
|
|
|
(s-contains? c x))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-hookify (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Append \"-hook\" to X."
|
|
|
|
(s-append "-hook" x))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-split (y x)
|
2019-12-23 18:31:42 +01:00
|
|
|
"Map string X into a list of strings that were separated by Y."
|
|
|
|
(s-split y x))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-ensure-hookified (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Ensure that X has \"-hook\" appended to it."
|
|
|
|
(if (s-ends-with? "-hook" x)
|
|
|
|
x
|
2020-09-01 00:28:47 +02:00
|
|
|
(string-hookify x)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-format (x &rest args)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Format template string X with ARGS."
|
|
|
|
(apply #'format (cons x args)))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-concat (&rest strings)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Joins `STRINGS' into onto string."
|
|
|
|
(apply #'s-concat strings))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-->symbol (string)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Maps `STRING' to a symbol."
|
|
|
|
(intern string))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-<-symbol (symbol)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Maps `SYMBOL' into a string."
|
|
|
|
(symbol-name symbol))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-prepend (prefix x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Prepend `PREFIX' onto `X'."
|
|
|
|
(s-concat prefix x))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-append (postfix x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Appen `POSTFIX' onto `X'."
|
|
|
|
(s-concat x postfix))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-surround (s x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Surrounds `X' one each side with `S'."
|
|
|
|
(->> x
|
2020-09-01 00:28:47 +02:00
|
|
|
(string-prepend s)
|
|
|
|
(string-append s)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;; TODO: Define a macro for defining a function and a test.
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Casing
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-caps->kebab (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Change the casing of `X' from CAP_CASE to kebab-case."
|
|
|
|
(->> x
|
|
|
|
s-downcase
|
|
|
|
(s-replace "_" "-")))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-kebab->caps (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Change the casing of X from CAP_CASE to kebab-case."
|
|
|
|
(->> x
|
|
|
|
s-upcase
|
|
|
|
(s-replace "-" "_")))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-lower->caps (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Change the casing of X from lowercase to CAPS_CASE."
|
|
|
|
(->> x
|
|
|
|
s-upcase
|
|
|
|
(s-replace " " "_")))
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-lower->kebab (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Change the casing of `X' from lowercase to kebab-case."
|
|
|
|
(s-replace " " "-" x))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Predicates
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 00:28:47 +02:00
|
|
|
(defun string-instance? (x)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if X is a string."
|
|
|
|
(stringp x))
|
|
|
|
|
|
|
|
(provide 'string)
|
|
|
|
;;; string.el ends here
|