add helper functions we will need for ascii art add/search processing
This commit is contained in:
parent
0ad784eb06
commit
8e0b6d05e6
|
@ -2,6 +2,7 @@
|
||||||
(:import (java.net URL)
|
(:import (java.net URL)
|
||||||
(java.io Writer)
|
(java.io Writer)
|
||||||
(java.text SimpleDateFormat)
|
(java.text SimpleDateFormat)
|
||||||
|
(java.security MessageDigest)
|
||||||
(java.util Date))
|
(java.util Date))
|
||||||
(:require [clojure.string :as str]
|
(:require [clojure.string :as str]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
|
@ -91,4 +92,33 @@
|
||||||
(piped-input-stream
|
(piped-input-stream
|
||||||
(fn [output-stream]
|
(fn [output-stream]
|
||||||
(with-open [^Writer w (io/writer output-stream)]
|
(with-open [^Writer w (io/writer output-stream)]
|
||||||
(f w)))))
|
(f w)))))
|
||||||
|
|
||||||
|
(defn convert-line-endings [s]
|
||||||
|
(str/replace s "\r" ""))
|
||||||
|
|
||||||
|
(defn blank-line? [s]
|
||||||
|
(-> s str/trim str/blank?))
|
||||||
|
|
||||||
|
(defn remove-leading-and-trailing-blank-lines [s]
|
||||||
|
(let [lines (str/split s #"\n")
|
||||||
|
num-leading-blank-lines (->> lines (take-while blank-line?) count)
|
||||||
|
num-trailing-blank-lines (->> lines reverse (take-while blank-line?) count)]
|
||||||
|
(as-> lines x
|
||||||
|
(drop num-leading-blank-lines x)
|
||||||
|
(take (- (count x) num-trailing-blank-lines) x)
|
||||||
|
(str/join "\n" x))))
|
||||||
|
|
||||||
|
; shamelessly copied from https://gist.github.com/kisom/1698245
|
||||||
|
(defn hexdigest
|
||||||
|
"returns the hex digest of a string"
|
||||||
|
[^String input hash-algo]
|
||||||
|
(if (string? input)
|
||||||
|
(let [hash (MessageDigest/getInstance hash-algo)]
|
||||||
|
(. hash update (.getBytes input))
|
||||||
|
(let [digest (.digest hash)]
|
||||||
|
(apply str (map #(format "%02x" (bit-and % 0xff)) digest))))
|
||||||
|
(throw (new Exception "input argument must be of type String"))))
|
||||||
|
|
||||||
|
(defn sha256 [s]
|
||||||
|
(hexdigest s "SHA-256"))
|
Reference in a new issue