Last active
December 17, 2015 09:19
-
-
Save dyoo/5586470 to your computer and use it in GitHub Desktop.
A function for getting the name of unicode code points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(provide unicode-name) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; We want to compute the table at compile-time, so that the result can be saved | |
;; by using raco make. | |
(require (for-syntax net/url | |
racket/base | |
racket/match)) | |
(define-syntax (define-unicode-lookup-table stx) | |
(syntax-case stx () | |
[(_ id) | |
(let () | |
(define ht (make-hash)) | |
(define data | |
(get-pure-port (string->url | |
"http://www.unicode.org/Public/UNIDATA/UnicodeData.txt"))) | |
(for ([line (in-lines data)]) | |
(match (regexp-split #rx";" line) | |
[(list code name _ ...) | |
(hash-set! ht (string->number code 16) name)])) | |
(close-input-port data) | |
#`(define id '#,ht))])) | |
(define-unicode-lookup-table unicode-lookup-table) | |
;; unicode-name: number -> string | |
(define (unicode-name n) | |
(hash-ref unicode-lookup-table n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment