Skip to content

Instantly share code, notes, and snippets.

@dyoo
Last active December 17, 2015 09:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyoo/5586470 to your computer and use it in GitHub Desktop.
Save dyoo/5586470 to your computer and use it in GitHub Desktop.
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