Skip to content

Commit d425923

Browse files
authored
Fix for (str nil) to return the empty string (#707)
Hi, could you please consider compatibility fix with Clojure for `(str nil)` to return the empty string. It fixes #706. I have added a basic test for it and also corrected one that was inserting `nil` while concatenating strings. I've also had to move `nil?` forward to use it in `str`. Thanks --------- Co-authored-by: ikappaki <[email protected]>
1 parent 181ab3c commit d425923

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
* Fix issue with `case` evaluating all of its clauses expressions (#699).
99
* Fix issue with relative paths dropping their first character on MS-Windows (#703).
10+
* Fix incompatibility with `(str nil)` returning "nil" (#706).
1011

1112
## [v0.1.0a2]
1213
### Added

src/basilisp/contrib/sphinx/domain.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ def add_target_and_index(
149149
domain.note_var(fullname, self.objtype, node_id)
150150

151151
if "noindexentry" not in self.options:
152-
indextext = self.get_index_text(modname, name_cls)
152+
indextext = self.get_index_text( # pylint: disable=assignment-from-no-return, useless-suppression
153+
modname, name_cls
154+
)
153155
if indextext:
154156
self.indexnode["entries"].append(
155157
("single", indextext, node_id, "", None)

src/basilisp/core.lpy

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -472,16 +472,25 @@
472472
(recur (rest s))
473473
(first s)))
474474

475+
(defn ^:inline nil?
476+
"Return ``true`` if ``x`` is ``nil``\\, otherwise ``false``\\."
477+
[x]
478+
(operator/is- x nil))
479+
475480
(defn str
476-
"Create a string representation of ``o``."
481+
"Create a string representation of ``o``.
482+
483+
Return the empty string if ``o`` is nil."
477484
([] "")
478-
([o] (basilisp.lang.runtime/lstr o))
485+
([o] (if (nil? o) "" (basilisp.lang.runtime/lstr o)))
479486
([o & args]
480487
(let [coerce (fn [in out]
481-
(if (seq (rest in))
482-
(recur (rest in)
483-
(conj out (basilisp.lang.runtime/lstr (first in))))
484-
(conj out (basilisp.lang.runtime/lstr (first in)))))
488+
(let [item (first in)
489+
repr (if (nil? item) "" (basilisp.lang.runtime/lstr item))]
490+
(if (seq (rest in))
491+
(recur (rest in)
492+
(conj out repr))
493+
(conj out repr))))
485494
strs (coerce (conj args o) [])]
486495
(.join "" strs))))
487496

@@ -4165,13 +4174,13 @@
41654174
:lpy:var:`*print-sep*` (default is an ASCII space)."
41664175
([] (print ""))
41674176
([x]
4168-
(.write *out* (str x))
4177+
(.write *out* (basilisp.lang.runtime/lstr x))
41694178
nil)
41704179
([x & args]
41714180
(let [stdout *out*
41724181
sep *print-sep*
4173-
repr-args (interpose sep (map str args))]
4174-
(.write stdout (str x))
4182+
repr-args (interpose sep (map basilisp.lang.runtime/lstr args))]
4183+
(.write stdout (basilisp.lang.runtime/lstr x))
41754184
(.write stdout sep)
41764185
(.write stdout (apply str repr-args))
41774186
nil)))
@@ -4184,14 +4193,14 @@
41844193
([] (println ""))
41854194
([x]
41864195
(let [stdout *out*]
4187-
(.write stdout (str x))
4196+
(.write stdout (basilisp.lang.runtime/lstr x))
41884197
(.write stdout \newline)
41894198
nil))
41904199
([x & args]
41914200
(let [stdout *out*
41924201
sep *print-sep*
4193-
repr-args (interpose sep (map str args))]
4194-
(.write stdout (str x))
4202+
repr-args (interpose sep (map basilisp.lang.runtime/lstr args))]
4203+
(.write stdout (basilisp.lang.runtime/lstr x))
41954204
(.write stdout sep)
41964205
(.write stdout (apply str repr-args))
41974206
(.write stdout \newline)

tests/basilisp/test_core_fns.lpy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,10 @@
10491049
;; String Functions ;;
10501050
;;;;;;;;;;;;;;;;;;;;;;
10511051

1052+
(deftest str-test
1053+
(is (= "" (str nil)))
1054+
(is (= "ab" (str "a" nil "b"))))
1055+
10521056
(deftest subs-test
10531057
(is (= "" (subs "" 0)))
10541058
(is (thrown? python/IndexError (subs "" 3)))

tests/basilisp/test_core_macros.lpy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@
14341434
(is (= [[{:nested {:a "A"} :a "a" :b "b"}
14351435
{:nested {:a "C"} :a "c" :b "d"}
14361436
{:a "e" :b "e"}]
1437-
"AbCdnile"]
1437+
"AbCde"]
14381438
(loop [items [{:nested {:a "A"} :a "a" :b "b"}
14391439
{:nested {:a "C"} :a "c" :b "d"}
14401440
{:a "e" :b "e"}]

0 commit comments

Comments
 (0)