add 'safe submodule; change internal lists to vectors

main
Matthew Butterick 10 years ago
parent 6e6f89a841
commit f29c44cfd4

@ -4,4 +4,4 @@
; Knuth and Liang's original exception patterns from classic TeX. ; Knuth and Liang's original exception patterns from classic TeX.
; In the public domain. ; In the public domain.
(define default-exceptions (map symbol->string '(as-so-ciate as-so-ciates dec-li-na-tion oblig-a-tory phil-an-thropic present presents project projects reci-procity re-cog-ni-zance ref-or-ma-tion ret-ri-bu-tion ta-ble))) (define default-exceptions (list->vector (map symbol->string '(as-so-ciate as-so-ciates dec-li-na-tion oblig-a-tory phil-an-thropic present presents project projects reci-procity re-cog-ni-zance ref-or-ma-tion ret-ri-bu-tion ta-ble))))

@ -1,6 +1,6 @@
#lang racket/base #lang racket/base
(require (for-syntax racket/base)) (require (for-syntax racket/base))
(require racket/string racket/list racket/contract racket/vector) (require racket/string racket/list racket/vector)
(require "patterns.rkt" "exceptions.rkt" txexpr xml) (require "patterns.rkt" "exceptions.rkt" txexpr xml)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -13,15 +13,19 @@
;;; (also in the public domain) ;;; (also in the public domain)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-syntax (define+provide/contract stx) (module+ safe (require racket/contract))
(define-syntax (define+provide+safe stx)
(syntax-case stx () (syntax-case stx ()
[(_ (proc arg ... . rest-arg) contract body ...) [(_ (proc arg ... . rest-arg) contract body ...)
#'(define+provide/contract proc contract #'(define+provide+safe proc contract
(λ(arg ... . rest-arg) body ...))] (λ(arg ... . rest-arg) body ...))]
[(_ name contract body ...) [(_ name contract body ...)
#'(begin #'(begin
(provide (contract-out [name contract])) (define name body ...)
(define name body ...))])) (provide name)
(module+ safe
(provide (contract-out [name contract]))))]))
;; module data, define now but set! them later (because they're potentially big & slow) ;; module data, define now but set! them later (because they're potentially big & slow)
(define exceptions #f) (define exceptions #f)
@ -32,14 +36,14 @@
;; Convert the hyphenated pattern into a point array for use later. ;; Convert the hyphenated pattern into a point array for use later.
(define (list->exceptions exn-strings) (define (vector->exceptions exn-strings)
(define (make-key x) (define (make-key x)
(string-replace x "-" "")) (string-replace x "-" ""))
(define (make-value x) (define (make-value x)
(list->vector (cons 0 (map (λ(x) (if (equal? x "-") 1 0)) (regexp-split #px"[a-z]" x))))) (list->vector (cons 0 (map (λ(x) (if (equal? x "-") 1 0)) (regexp-split #px"[a-z]" x)))))
(make-hash (map (λ(x) (cons (make-key x) (make-value x))) exn-strings))) (make-hash (vector->list (vector-map (λ(x) (cons (make-key x) (make-value x))) exn-strings))))
;; An exception-word is a string of word characters or hyphens. ;; An exception-word is a string of word characters or hyphens.
(define (exception-word? x) (define (exception-word? x)
@ -68,7 +72,7 @@
(hash-set! tree char (make-hash))) (hash-set! tree char (make-hash)))
(set! tree (hash-ref tree char))) (set! tree (hash-ref tree char)))
(hash-set! tree empty points))) (hash-set! tree empty points)))
(map insert-pattern pattern-data) (vector-map insert-pattern pattern-data)
tree) tree)
@ -78,7 +82,7 @@
(define (make-zeroes points) (define (make-zeroes points)
; controls hyphenation zone from edges of word ; controls hyphenation zone from edges of word
; possible todo: make this user-configurable? ; possible todo: make this user-configurable?
(map (λ(i) (vector-set! points i 0)) (list 1 2 (- (vector-length points) 2) (- (vector-length points) 3))) (vector-map (λ(i) (vector-set! points i 0)) (vector 1 2 (- (vector-length points) 2) (- (vector-length points) 3)))
points) points)
(let* ([word (string-downcase word)] (let* ([word (string-downcase word)]
@ -156,7 +160,7 @@
[else x]))])) [else x]))]))
;; Hyphenate using a filter procedure. ;; Hyphenate using a filter procedure.
(define+provide/contract (hyphenatef x proc [joiner default-joiner] (define+provide+safe (hyphenatef x proc [joiner default-joiner]
#:exceptions [extra-exceptions '()] #:exceptions [extra-exceptions '()]
#:min-length [min-length default-min-length]) #:min-length [min-length default-min-length])
((xexpr? procedure?) ((or/c char? string?) ((xexpr? procedure?) ((or/c char? string?)
@ -165,7 +169,7 @@
;; set up module data ;; set up module data
;; todo?: change set! to parameterize ;; todo?: change set! to parameterize
(set! exceptions (list->exceptions (append default-exceptions extra-exceptions))) (set! exceptions (vector->exceptions (vector-append default-exceptions (list->vector extra-exceptions))))
(when (not pattern-tree) (set! pattern-tree (make-pattern-tree default-patterns))) (when (not pattern-tree) (set! pattern-tree (make-pattern-tree default-patterns)))
(define joiner-string (joiner->string joiner)) (define joiner-string (joiner->string joiner))
@ -178,7 +182,7 @@
;; Default hyphenate is a special case of hyphenatef. ;; Default hyphenate is a special case of hyphenatef.
(define+provide/contract (hyphenate x [joiner default-joiner] (define+provide+safe (hyphenate x [joiner default-joiner]
#:exceptions [extra-exceptions '()] #:exceptions [extra-exceptions '()]
#:min-length [min-length default-min-length]) #:min-length [min-length default-min-length])
((xexpr/c) ((or/c char? string?) ((xexpr/c) ((or/c char? string?)
@ -188,12 +192,10 @@
;; Remove hyphens. ;; Remove hyphens.
(define+provide/contract (unhyphenate x [joiner default-joiner]) (define+provide+safe (unhyphenate x [joiner default-joiner])
((xexpr/c) ((or/c char? string?)) . ->* . xexpr/c) ((xexpr/c) ((or/c char? string?)) . ->* . xexpr/c)
(define (remove-hyphens text) (define (remove-hyphens text)
(string-replace text (joiner->string joiner) "")) (string-replace text (joiner->string joiner) ""))
(apply-xexpr-strings remove-hyphens x)) (apply-xexpr-strings remove-hyphens x))

File diff suppressed because one or more lines are too long

@ -1,5 +1,5 @@
#lang racket/base #lang racket/base
(require "main.rkt" rackunit) (require (submod "main.rkt" safe) rackunit)
(require/expose "main.rkt" (word->hyphenation-points exception-word?)) (require/expose "main.rkt" (word->hyphenation-points exception-word?))

Loading…
Cancel
Save