Merge pull request #1 from mbutterick/dev-fast

Create fast mode (without contracts) + safe mode (with); convert lists to vectors
main
Matthew Butterick 11 years ago
commit 997e6b15ac

@ -4,4 +4,4 @@
; Knuth and Liang's original exception patterns from classic TeX.
; 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
(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)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -13,15 +13,19 @@
;;; (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 ()
[(_ (proc arg ... . rest-arg) contract body ...)
#'(define+provide/contract proc contract
#'(define+provide+safe proc contract
(λ(arg ... . rest-arg) body ...))]
[(_ name contract body ...)
#'(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)
(define exceptions #f)
@ -32,14 +36,14 @@
;; 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)
(string-replace x "-" ""))
(define (make-value 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.
(define (exception-word? x)
@ -68,7 +72,7 @@
(hash-set! tree char (make-hash)))
(set! tree (hash-ref tree char)))
(hash-set! tree empty points)))
(map insert-pattern pattern-data)
(vector-map insert-pattern pattern-data)
tree)
@ -78,7 +82,7 @@
(define (make-zeroes points)
; controls hyphenation zone from edges of word
; 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)
(let* ([word (string-downcase word)]
@ -156,7 +160,7 @@
[else x]))]))
;; 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 '()]
#:min-length [min-length default-min-length])
((xexpr? procedure?) ((or/c char? string?)
@ -165,7 +169,7 @@
;; set up module data
;; 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)))
(define joiner-string (joiner->string joiner))
@ -178,7 +182,7 @@
;; 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 '()]
#:min-length [min-length default-min-length])
((xexpr/c) ((or/c char? string?)
@ -188,12 +192,10 @@
;; 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)
(define (remove-hyphens text)
(string-replace text (joiner->string joiner) ""))
(apply-xexpr-strings remove-hyphens x))

File diff suppressed because one or more lines are too long

@ -22,10 +22,16 @@ At the command line:
After that, you can update the package like so:
@verbatim{raco pkg update hyphenate}
@section{Importing the module}
@section{Interface}
@defmodule[#:multi (hyphenate (submod hyphenate safe))]
The module operates in two modes: fast and safe. Fast mode is the default, which you get by importing the module in the usual way: @code{(require hyphenate)}.
Safe mode enables the function contracts documented below. Use safe mode by importing the module as @code{(require (submod hyphenate safe))}.
@defmodule[hyphenate]
@section{Interface}
@defproc[
(hyphenate

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

Loading…
Cancel
Save