Source

gambit-libs / tcv-char.scm

Full commit
;;; Copyright (c) 2009, Taylor Venable
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions are met:
;;;
;;;     * Redistributions of source code must retain the above copyright
;;;       notice, this list of conditions and the following disclaimer.
;;;
;;;     * Redistributions in binary form must reproduce the above copyright
;;;       notice, this list of conditions and the following disclaimer in the
;;;       documentation and/or other materials provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
;;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;;; POSSIBILITY OF SUCH DAMAGE.

;; Indicates whether or not the specified character is a digit.

(namespace ("char#" digit? hex? upper? lower? space? kbd))

(define digit?
  (lambda (c)
    (let ((bottom (char->integer #\0))
          (top (char->integer #\9)))
      (and (>= (char->integer c) bottom)
           (<= (char->integer c) top)))))

;; Indicates whether the given char is a valid hexadecimal character.

(define hex?
  (lambda (c)
    (let ((bottom (char->integer #\A))
          (top    (char->integer #\F)))
      (or (digit? c)
          (and (>= (char->integer (char-upcase c)) bottom)
               (<= (char->integer (char-upcase c)) top))))))

;; Indicates if a character is upper-case.

(define upper?
  (lambda (c)
    (char=? c (char-upcase c))))

;; Indicates that a character is lower-case.

(define lower?
  (lambda (c)
    (char=? c (char-downcase c))))

;; Indicates that a character is a space character.

(define space?
  (lambda (c)
    (or (char=? c #\space)
        (char=? c #\tab)
        (char=? c #\newline)
        (char=? c #\return))))

(define (kbd . args)
  (case (car args)
    ((c ctl ctrl control)
     (modulo (- (apply kbd (cdr args)) 64) 128))
    (else
     (char->integer (car args)))))

(namespace (""))

;; Local Variables:
;; scheme-dialect: gambit
;; End: