I understand your question but my problem is that I cannot read AutoLisp very well anymore (I'm not very good at it anymore because of lack of practice). But from what little I understand of your code I think you're making it too complicated -i.e. why are you sorting a list of chars? Operating on a smallest as possible list (a list of numbers for example) will make your operation faster. I would tackle this in a few steps like so:
1. Read the contents of the file into a list of lists.
2. Iterate through that list and convert to a list of ASCII numbers and search for a value that falls out of range (then replace if necessary).
3. OPTIONAL: do the search and replace here as separate process.
3./4. Convert list back to text and write back to file.
The logic being that searching for number falling outside a specific range is faster then comparing characters or strings.
I have *some* code but you'd have to be the judge of its value (Any autolisp code I have is very old; I wrote it years ago).
I hope that makes sense and what little I offered up helps you in some way. Sorry if it doesn't.
(defun file
-get
-list
-from
( name
/ fp lst read
-file
-into
-list ) ;;/*{{{*/ ;; file-get-list-from
;;
;; GENERAL FILE READER
;; given a file name this procedure will read the contents
;; into a list
;;
;; EX:
;; (mapcar
;; 'read (get-list-from-file
;; (findfile layers-to-load-from-file)))
;;
;; By: John K
;;
(defun read
-file
-into
-list ( str file
) ;;/*{{{*/ lst ) ;;/*}}}*/
(defun al
-string
-to
-list (str
/ alst cntr
) ;;/*{{{*/ ;; al-string-to-list
;;
;; Convert a string to a ascii number list of intigers
;;
;; Ex: (al-string-to-list "test")
;; > (116 101 115 116)
;;
;; By: John K
;;
(if (= cntr '
()) (setq cntr
1))
(defun al
-list
-to
-string
(strlist
/ strlst
) ;;/*{{{*/ ;; al-list-to-string
;;
;; Convert a list of ascii numbers to a string
;;
;; Ex: (al-list-to-string '(116 101 115 116))
;; > ("test")
;;
;; By: John K
;;
strlist)
)
)
strlst
) ;;/*}}}*/