Question

I need to test a string to see if it contains any characters that have codes above decimal 127 (extended ASCII codes) or are below 32. Is there any really nice way to do this or will I just have to iterate through the whole string and compare char-codes of the characters? I am using the common lisp implementation CCL.

Was it helpful?

Solution

The portable way is, as you suggested yourself,

(defun string-standard-p (string &key (min 32) (max 127))
  (every (lambda (c) (<= min (char-code c) max)) string))

There may be an implementation-specific way, e.g., in CLISP, you can do

(defun string-encodable-p (string encoding)
  (every (lambda (c) (typep c encoding)) string))
(string-encodable-p "foo" charset:ascii)
==> T

although it will actually accept all ASCII characters, not just 32:127.

(I am sorry, I am not familiar with CCL).

However, I am pretty sure that you will not find a nicer solution than the one you suggested in your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top