Skip to content

Instantly share code, notes, and snippets.

@peterwang
Created February 7, 2017 09:33
Show Gist options
  • Save peterwang/c8b044e706c48cbd18f1275fbfc30692 to your computer and use it in GitHub Desktop.
Save peterwang/c8b044e706c48cbd18f1275fbfc30692 to your computer and use it in GitHub Desktop.
;; πŸ˜€ https://codepoints.net/U+1f600
;; 😊 https://codepoints.net/U+1F60A
;; πͺš₯ https://codepoints.net/U+2A6A5
(defn codepoint-seq [s]
(loop [i 0 e (.length s) acc []]
(if (>= i e)
acc
(let [cp (.codePointAt s i)
cc (Character/charCount cp)]
(recur (+ i cc) e (conj acc cp))))))
(def utf8mb4-str "hello δ½ ε₯½ こんにけは μ•ˆλ…•ν•˜μ„Έμš” πŸ˜€πŸ˜Šπͺš₯")
(->> utf8mb4-str
codepoint-seq
(map #(String. (Character/toChars %))))
;; => ("h" "e" "l" "l" "o" " " "δ½ " "ε₯½" " " "こ" "γ‚“" "に" "け" "は" " " "μ•ˆ" "λ…•" "ν•˜" "μ„Έ" "μš”" " " "πŸ˜€" "😊" "πͺš₯")
(->> utf8mb4-str
seq
(map #(String. (Character/toChars (int %)))))
;; => ("h" "e" "l" "l" "o" " " "δ½ " "ε₯½" " " "こ" "γ‚“" "に" "け" "は" " " "μ•ˆ" "λ…•" "ν•˜" "μ„Έ" "μš”" " " "?" "?" "?" "?" "?" "?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment