charmap.scm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Character maps, ASCII-only version
  3. ; Enable us to change the semantics afterwards (see the bottom of this file)
  4. (define (char-whitespace? c)
  5. (char-whitespace?-proc c))
  6. (define (char-whitespace?-proc c)
  7. (if (memq (char->ascii c) ascii-whitespaces) #t #f))
  8. (define (char-lower-case? c)
  9. (char-lower-case?-proc c))
  10. (define (char-lower-case?-proc c)
  11. (and (char>=? c #\a)
  12. (char<=? c #\z)))
  13. (define (char-upper-case? c)
  14. (char-upper-case?-proc c))
  15. (define (char-upper-case?-proc c)
  16. (and (char>=? c #\A)
  17. (char<=? c #\Z)))
  18. (define (char-numeric? c)
  19. (char-numeric?-proc c))
  20. (define (char-numeric?-proc c)
  21. (and (char>=? c #\0)
  22. (char<=? c #\9)))
  23. (define (char-alphabetic? c)
  24. (char-alphabetic?-proc c))
  25. (define (char-alphabetic?-proc c)
  26. (or (char-upper-case? c)
  27. (char-lower-case? c)))
  28. (define char-case-delta
  29. (- (char->ascii #\a) (char->ascii #\A)))
  30. (define (make-character-map f)
  31. (let ((s (make-string ascii-limit #\0)))
  32. (do ((i 0 (+ i 1)))
  33. ((>= i ascii-limit))
  34. (string-set! s i (f (ascii->char i))))
  35. s))
  36. (define upcase-map
  37. (make-character-map
  38. (lambda (c)
  39. (if (char-lower-case? c)
  40. (ascii->char (- (char->ascii c) char-case-delta))
  41. c))))
  42. (define (char-upcase c)
  43. (char-upcase-proc c))
  44. (define (char-upcase-proc c)
  45. (string-ref upcase-map (char->ascii c)))
  46. (define downcase-map
  47. (make-character-map
  48. (lambda (c)
  49. (if (char-upper-case? c)
  50. (ascii->char (+ (char->ascii c) char-case-delta))
  51. c))))
  52. (define (char-downcase c)
  53. (char-downcase-proc c))
  54. (define (char-downcase-proc c)
  55. (string-ref downcase-map (char->ascii c)))
  56. ; helper for defining the -ci procedures
  57. ; This is relevant for Unicode, where FOLDCASE != DOWNCASE
  58. (define (char-foldcase c)
  59. (char-foldcase-proc c))
  60. (define char-foldcase-proc char-downcase-proc)
  61. (define (char-ci-compare pred)
  62. (lambda (c1 c2) (pred (char-foldcase c1) (char-foldcase c2))))
  63. (define char-ci=? (char-ci-compare char=?))
  64. (define char-ci<? (char-ci-compare char<?))
  65. (define char-ci<=? (char-ci-compare char<=?))
  66. (define char-ci>? (char-ci-compare char>?))
  67. (define char-ci>=? (char-ci-compare char>=?))
  68. ; Later, we replace these by the Unicode versions. We don't want them
  69. ; in the initial image because they use a lot more memory.
  70. (define (set-char-map-procedures! alphabetic?
  71. numeric?
  72. whitespace?
  73. upper-case?
  74. lower-case?
  75. upcase
  76. downcase
  77. foldcase)
  78. (set! char-alphabetic?-proc alphabetic?)
  79. (set! char-numeric?-proc numeric?)
  80. (set! char-whitespace?-proc whitespace?)
  81. (set! char-upper-case?-proc upper-case?)
  82. (set! char-lower-case?-proc lower-case?)
  83. (set! char-upcase-proc upcase)
  84. (set! char-downcase-proc downcase)
  85. (set! char-foldcase-proc foldcase))