Using Emoji literals in Clojure source

丶灬走出姿态 提交于 2019-11-30 14:33:45

Java represents Unicode characters in UTF-16. The emoji characters are "supplementary characters" and have a codepoint that cannot be represented in 16 bits.

http://www.oracle.com/technetwork/articles/javase/supplementary-142654.html

In essence, supplementary characters are represented not as chars but as ints and there are special apis for dealing with them.

One way is with (Character/toChars 128516) - this returns a char array that you can convert to a string to print: (apply str (Character/toChars 128516)). Or you can create a String from an array of codepoint ints directly with (String. (int-array [128516]) 0 1). Depending on all the various things between Java/Clojure and your eyeballs, that may or may not do what you want.

The format api supports supplementary characters so that may be easiest, however it takes an int so you'll need a cast: (format "Smile! %c" (int 128516)).

Thanks to Clojure’s extensible reader tags, you can create Unicode literals quite easily yourself.

We already know that not all of Unicode can be represented as char literals; that the preferred representation of Unicode characters on the JVM is int; and that a string literal can hold any Unicode character in a way that’s also convenient for humans to read.

So, a tagged literal #u "🍒" that reads as an int would make an excellent Unicode character literal!

Set up a reader function for the new tagged literal in *data-readers*:

(defn read-codepoint
  [^String s]
  {:pre [(= 1 (.codePointCount s 0 (.length s)))]}
  (.codePointAt s 0))

(set! *data-readers* (assoc *data-readers* 'u #'read-codepoint))

With that in place, the reader reads such literals as code point integers:

#u"🍒"  ; => 127826
(Character/getName #u"🍒")  ; => "CHERRIES"

‘Reader tags without namespace qualifiers are reserved for Clojure’, says the documentation … #u is short but perhaps not the most responsible choice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!