[racket] newbie string handling problem

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Thu Mar 31 20:26:03 EDT 2011

Eli Barzilay wrote at 03/31/2011 07:46 PM:
> Two minutes ago, Charles Hixson wrote:
>   
>> Hi, I'm trying to determine whether the last non-whitespace character of a string is a colon,

>   (define (last-non-ws str)
>     (cond [(regexp-match #px"(\\S)\\s*$" " \t") => cadr]
>           [else #f]))
>
> then see the docs about regexps and this use of `cond'.
>   

Or, more specific:

(define (last-non-whitespace-character-of-string-is-colon? str)
  (regexp-match? #px":\\s*$" str))

>> The best I've come up with is to explode the string into a list of characters, and handle it that way, but that's clearly quite wasteful of both processing and RAM.
>>     

Your intuition about exploding the string seems pretty good to me, 
mainly because I like to think of allocations as expensive.

Another way to do this is in Racket (or in old Scheme, for that matter) 
is to do a "string-length" and then one or more "string-ref", starting 
at the end or the beginning of the string.  Or, if you had slow regexps 
but faster (constant-time?) "string-length" and "string-ref", I suppose 
it might be beneficial to first check the last character of the string, 
and if it's colon, return true; if it's whitespace, do the regexp; if 
it's neither colon nor whitespace, return false.

If you were micro-optimizing, you could try a few approaches and compare 
the performance of each in representative contexts.  In practice, I'd 
bet that just using a regexp is fine.

-- 
http://www.neilvandyke.org/


Posted on the users mailing list.