Skip to content

Instantly share code, notes, and snippets.

@dyoo

dyoo/unicode-name.rkt

Last active Dec 17, 2015
Embed
What would you like to do?
A function for getting the name of unicode code points
#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