Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Clean up

  • Loading branch information...
commit c342ff6eaadf17eb8307fd94148b0e456c202b2c 1 parent d4d9ad4
@alexgartrell authored
Showing with 28 additions and 30 deletions.
  1. +28 −30 src/tnetstrings/core.clj
View
58 src/tnetstrings/core.clj
@@ -1,60 +1,58 @@
(ns tnetstrings.core
(:require [clojure.string :as str]))
-(defn- lstrip [s]
- (.replaceAll s "^\\s*" ""))
+;;;; Provides loads and dumps for conversion to and from typed netstrings
+;;;; More on typed netstrings at http://www.tnetstrings.org
(defn- explode-tnetstring [s]
- (let [[len-str data-plus] (.split (lstrip s) ":" 2)
+ (let [[len-str data-plus] (.split s ":" 2)
len (Integer. len-str)
; data-plus.length >= len + 1
data (.substring data-plus 0 len)
type (.charAt data-plus len)
- rest (.substring data-plus (+ len 1))]
- [data type rest]))
+ remains (.substring data-plus (+ len 1))]
+ [data type remains]))
-(defn- parse-list [data] nil)
-(defn- parse-dict [data] nil)
+;; prototyping for the benefit of load-list and load-map
+(defn- load-item [data type] nil)
-(defn- parse [data type]
- (let [tis (fn [t] (= type t))] ; Abbreviating (= type t) to (tis t)
- (cond (tis \,) data ; String
- (tis \#) (Integer. data) ; Integer
- (tis \!) (= data "true") ; Boolean
- (tis \~) nil ; Null
- (tis \}) (parse-dict data) ; Dictionary
- (tis \]) (parse-list data) ; List
- :else :NOTMATCHED)))
-
-(defn- parse-list [data]
+(defn- load-list [data]
(loop [data data accum []]
(if (str/blank? data)
(reverse accum)
- (let [[data type rest] (explode-tnetstring data)
- item (parse data type)]
+ (let [[data type remains] (explode-tnetstring data)
+ item (load-item data type)]
(if (= item :NOTMATCHED)
:NOTMATCHED
- (recur rest (cons (parse data type) accum)))))))
+ (recur remains (cons (load-item data type) accum)))))))
-(defn- parse-dict [data]
+(defn- load-dict [data]
(loop [data data accum {}]
(if (str/blank? data)
accum
(let [[kdata ktype val-plus] (explode-tnetstring data)
- [vdata vtype rest] (explode-tnetstring val-plus)
- key (parse kdata ktype)
- val (parse vdata vtype)]
+ [vdata vtype remains] (explode-tnetstring val-plus)
+ key (load-item kdata ktype)
+ val (load-item vdata vtype)]
(if (or (= key :NOTMATCHED) (= val :NOTMATCHED))
:NOTMATCHED
- (recur rest (assoc accum key val)))))))
+ (recur remains (assoc accum key val)))))))
-
+(defn- load-item [data type]
+ (let [tis (fn [t] (= type t))] ; Abbreviating (= type t) to (tis t)
+ (cond (tis \,) data ; String
+ (tis \#) (Integer. data) ; Integer
+ (tis \!) (= data "true") ; Boolean
+ (tis \~) nil ; Null
+ (tis \}) (load-dict data) ; Dictionary
+ (tis \]) (load-list data) ; List
+ :else :NOTMATCHED)))
(defn loads [s]
- (let [[data type rest] (explode-tnetstring s)]
- (parse data type)))
+ (let [[data type remains] (explode-tnetstring s)]
+ (load-item data type)))
-;prototype
+;; prototyping for the benefit of dump-list and dump-map
(defn dump-item [item] nil)
(defn- dump-string [s]
Please sign in to comment.
Something went wrong with that request. Please try again.