From 25bf3dec42fa05810cb07bcc1e77325700459d60 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Thu, 3 Oct 2013 23:38:30 +0200 Subject: [PATCH] Add a method for casting into byte string or unicode depending on python version --- hy/core/language.hy | 8 +++++++- tests/native_tests/language.hy | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index 10f81e9..45ad5eb 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -177,6 +177,12 @@ "Return second item from `coll`" (get coll 1)) +(defn string [x] + "Cast x as current string implementation" + (if-python2 + (unicode x) + (str x))) + (defn string? [x] "Return True if x is a string" (if-python2 @@ -218,5 +224,5 @@ "even?" "filter" "float?" "inc" "instance?" "integer?" "iterable?" "iterate" "iterator?" "neg?" "none?" "nth" "numeric?" "odd?" "pos?" "remove" "repeat" - "repeatedly" "second" "string?" "take" "take_nth" "take_while" + "repeatedly" "second" "string" "string?" "take" "take_nth" "take_while" "zero?"]) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 6dd2986..6956734 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -779,3 +779,7 @@ "Evaluate an empty list to a []" (assert (= () []))) +(defn test-string [] + (assert (string? (string "a"))) + (assert (string? (string 1))) + (assert (= u"unicode" (string "unicode"))))