Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

escape special chars in strings #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/venia/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@
[arg]
(str "[" (apply str (interpose "," (map arg->str arg))) "]"))

(defn encode-string
"Handles escaping special characters."
[arg]
(str "\"" (str/escape arg {\" "\\\""
\\ "\\\\"
\newline "\\n"
\return "\\r"
\tab "\\t"
\formfeed "\\f"
\backspace "\\b"}) "\""))

#?(:clj (extend-protocol ArgumentFormatter
nil
(arg->str [arg] "null")
String
(arg->str [arg] (str "\"" arg "\""))
(arg->str [arg] (encode-string arg))
IPersistentMap
(arg->str [arg] (str "{" (arguments->str arg) "}"))
IPersistentCollection
Expand All @@ -43,7 +54,7 @@
nil
(arg->str [arg] "null")
string
(arg->str [arg] (str "\"" arg "\""))
(arg->str [arg] (encode-string arg))
PersistentArrayMap
(arg->str [arg] (str "{" (arguments->str arg) "}"))
PersistentHashMap
Expand Down
7 changes: 7 additions & 0 deletions test/venia/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
(deftest ArgumentFormatter-test
(is (= "null" (v/arg->str nil)))
(is (= "\"human\"" (v/arg->str "human")))
(is (= "\"\\\"human\\\"\"" (v/arg->str "\"human\"")))
(is (= "\"What's a \\\"human\\\"?\"" (v/arg->str "What's a \"human\"?")))
(is (= "\"hu\\nman\"" (v/arg->str (str "hu" \newline "man"))))
(is (= "\"hu\\rman\"" (v/arg->str (str "hu" \return "man"))))
(is (= "\"hu\\tman\"" (v/arg->str (str "hu" \tab "man"))))
(is (= "\"hu\\fman\"" (v/arg->str (str "hu" \formfeed "man"))))
(is (= "\"hu\\bman\"" (v/arg->str (str "hu" \backspace "man"))))
(is (= "{id:1}" (v/arg->str {:id 1})))
(is (= "{id:null}" (v/arg->str {:id nil})))
(is (= "[1,2,3]" (v/arg->str [1 2 3])))
Expand Down