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:
|
2022-07-30 03:56:06 +02:00
|
|
|
;; Library for working with strings.
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(require 's)
|
|
|
|
(require 'dash)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
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-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))
|
|
|
|
|
2022-07-30 03:56:06 +02:00
|
|
|
(defun string-to-symbol (string)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Maps `STRING' to a symbol."
|
|
|
|
(intern string))
|
|
|
|
|
2022-07-30 03:56:06 +02:00
|
|
|
(defun string-from-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))
|
|
|
|
|
2022-07-30 03:56:06 +02:00
|
|
|
(defun string-contains? (c x)
|
|
|
|
"Return t if X is in C."
|
|
|
|
(s-contains? c x))
|
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(provide 'string)
|
|
|
|
;;; string.el ends here
|