charmap.scm 2.8 KB

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