From 8e0b6d05e6fee47e913a920f0c093abfa71a7b32 Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 6 Apr 2014 14:26:13 -0400 Subject: [PATCH] add helper functions we will need for ascii art add/search processing --- src/toascii/util.clj | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/toascii/util.clj b/src/toascii/util.clj index a8c7080..04b7647 100644 --- a/src/toascii/util.clj +++ b/src/toascii/util.clj @@ -2,6 +2,7 @@ (:import (java.net URL) (java.io Writer) (java.text SimpleDateFormat) + (java.security MessageDigest) (java.util Date)) (:require [clojure.string :as str] [clojure.java.io :as io] @@ -91,4 +92,33 @@ (piped-input-stream (fn [output-stream] (with-open [^Writer w (io/writer output-stream)] - (f w))))) \ No newline at end of file + (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")) \ No newline at end of file