| 1 |
;;;; Implementation of SRFI 129 titlecase functions |
| 2 |
|
| 3 |
;; Returns #t if argument is a titlecase character, #f if not |
| 4 |
(define (char-title-case? ch) |
| 5 |
(let* ((codepoint (char->integer ch)) |
| 6 |
(result (assq codepoint titlecase-chars))) |
| 7 |
(if result #t #f))) |
| 8 |
|
| 9 |
;; Returns the single-character titlecase mapping of argument |
| 10 |
(define (char-titlecase ch) |
| 11 |
(let* ((codepoint (char->integer ch)) |
| 12 |
(result (assq codepoint title-single-map))) |
| 13 |
(if result |
| 14 |
(integer->char (cadr result)) |
| 15 |
(char-upcase ch)))) |
| 16 |
|
| 17 |
;; Returns #t if a character is caseless, otherwise #f |
| 18 |
(define (char-caseless? ch) |
| 19 |
(not (or (char-lower-case? ch) (char-upper-case? ch) (char-title-case? ch)))) |
| 20 |
|
| 21 |
;; Push a list onto another list in reverse order |
| 22 |
(define (reverse-push new old) |
| 23 |
(if (null? new) |
| 24 |
old |
| 25 |
(reverse-push (cdr new) (cons (car new) old)))) |
| 26 |
|
| 27 |
;; Returns the string titlecase mapping of argument |
| 28 |
(define (string-titlecase str) |
| 29 |
(let loop ((n 0) (result '())) |
| 30 |
(if (= n (string-length str)) |
| 31 |
(apply string (map integer->char (reverse result))) |
| 32 |
(let* ((ch (string-ref str n)) (codepoint (char->integer ch))) |
| 33 |
(if (or (= n 0) (char-caseless? (string-ref str (- n 1)))) |
| 34 |
; ch must be titlecased |
| 35 |
(let ((multi-title (assq codepoint title-multiple-map))) |
| 36 |
(if multi-title |
| 37 |
; ch has multiple- or single-character titlecase mapping |
| 38 |
(loop (+ n 1) (reverse-push (cdr multi-title) result)) |
| 39 |
; ch has single-character uppercase mapping |
| 40 |
(loop (+ n 1) (reverse-push (list (char->integer (char-upcase ch))) result)))) |
| 41 |
; ch must be lowercased |
| 42 |
(let ((multi-downcase (assq codepoint lower-multiple-map))) |
| 43 |
(if multi-downcase |
| 44 |
; ch has multiple-character lowercase mapping |
| 45 |
(loop (+ n 1) (reverse-push (cdr multi-downcase) result)) |
| 46 |
; ch has single-character lowercase mapping |
| 47 |
(loop (+ n 1) (reverse-push (list (char->integer (char-downcase ch))) result))))))))) |
| 48 |
|