[plt-scheme] Checking if strings are only whitespace

From: Hans Oesterholt-Dijkema (hdnews at gawab.com)
Date: Sun Mar 19 06:26:28 EST 2006

Dear All,

I'm looking fot the fastest way of checking if a string only consists
of whitespace. I've tried these solutions:

=== First solution
;; THIS IS WAY TO SLOW!
; (define reg-ws (pregexp "^[ \t\r\n]+$"))
;
; (define (is-space? str)
;   (not (eq? (pregexp-match reg-ws str) #f)))
;;;;;;;

=== Second solution
;; THIS IS ORDERS OF MAGNITUDE FASTER
(define (is-space-char? L)
  (if (null? L)
      #t
      (if (char-whitespace? (car L))
      (is-space-char? (cdr L))
      #f)))

(define (is-space? str)      
  (is-space-char? (string->list str)))

=== Third solution
(define (is-space? str)
  (do
      ((i 0 (+ i 1))
       (n (string-length str)))
      ((or (= i n)
       (not (char-whitespace? (string-ref str i))))
       (= i n))
    ()))

The last two solutions are comparable in speed. However, I want a still
faster solution.

Does anyone have a clue (last resort would be to use C).

--Hans




Posted on the users mailing list.