Permalink
Browse files

Move chr and ord to text.unicode namespace, where they seem to fit be…

…tter
  • Loading branch information...
1 parent 62faf8a commit b138bf603712f43a6f5adae65dc051f2b738eabb @jafingerhut committed Jan 26, 2012
Showing with 3 additions and 50 deletions.
  1. +1 −26 src/com/fingerhutpress/clj_perls.clj
  2. +2 −24 test/com/fingerhutpress/clj_perls/test.clj
@@ -22,31 +22,6 @@
(System/exit (if (zero? shifted-status) 255 shifted-status)))))
-(defn ^String chr
- "Return a string containing only the one specified Unicode code point,
- although it may contain 1 or 2 UTF-16 code units.
-
- Warning: Will return an invalid UTF-16 string containing only a
- leading or trailing surrogate if you give it a codepoint in the
- surrogate range, 0xD800 through 0xDFFF."
- [codepoint]
- (String. (Character/toChars codepoint)))
-
-
-(defn ord
- "Return the Unicode code point of the first character in string s.
- If the first character is a UTF-16 surrogate pair, the code point
- returned is that of the pair, not of the leading surrogate. Return
- 0 if the string is empty.
-
- The behavior is undefined if the string is not valid UTF-16."
- [^CharSequence s]
- (let [s (.toString s)]
- (if (= s "") ; special case for Perl compatability
- 0
- (.codePointAt s 0))))
-
-
(defmacro while-<>
"Approximately like Perl's:
@@ -373,7 +348,7 @@
(if (.endsWith s c)
(subs s 0 (- (count s) (count c)))
s))
- (instance? Number c) (chomp s (chr c))
+ (instance? Number c) (chomp s (uc/chr c))
:else (throw (IllegalArgumentException. (str "Invalid c arg: " c))))))
@@ -1,4 +1,5 @@
(ns com.fingerhutpress.clj-perls.test
+ (:require [com.fingerhutpress.text.unicode :as uc])
(:use [com.fingerhutpress.clj-perls])
(:use [clojure.test]))
@@ -11,29 +12,6 @@
;; their exit status and *err* output.
-(deftest test-chr
- (is (= "A" (chr 65)))
- (is (= '( "\n" "\r" "\t" ) (map chr [10 13 9])))
- (is (= "The quick brown fox jumped over the lazy dog"
- (apply str (map chr [84 104 101 32 113 117 105 99 107 32 98
- 114 111 119 110 32 102 111 120 32 106
- 117 109 112 101 100 32 111 118 101 114
- 32 116 104 101 32 108 97 122 121 32 100
- 111 103]))))
- (is (= "\ud834\udd1e" (chr 0x1d11e)))
- (is (= "\ud83d\ude03 smiling face"
- (apply str (map chr [0x1f603 32 115 109 105 108 105 110 103 32 102
- 97 99 101]))))
- (is (thrown? IllegalArgumentException (chr -1)))
- (is (thrown? IllegalArgumentException (chr (inc Character/MAX_CODE_POINT))))
- (is (thrown? IllegalArgumentException (chr (Integer/MAX_VALUE))))
- (is (thrown? IllegalArgumentException (chr (Integer/MIN_VALUE)))))
-
-
-;; See com.fingerhutpress.unicode unit tests for more testing of chr
-;; and ord functions.
-
-
;; TBD: Test while-<> here? Should be possible with input from files,
;; but perhaps not from *in*.
@@ -121,7 +99,7 @@
;; TBD: test split
-(def SMILING_FACE_WITH_OPEN_MOUTH_STR (chr 0x1F603))
+(def SMILING_FACE_WITH_OPEN_MOUTH_STR (uc/chr 0x1F603))
(deftest test-chomp
(is (= "" (chomp "" \a)))

0 comments on commit b138bf6

Please sign in to comment.