Skip to content

Instantly share code, notes, and snippets.

@spacebat
Last active January 13, 2016 22:23
Show Gist options
  • Select an option

  • Save spacebat/2d535fe6c833cdd40da0 to your computer and use it in GitHub Desktop.

Select an option

Save spacebat/2d535fe6c833cdd40da0 to your computer and use it in GitHub Desktop.
String successor like ruby - I guess
(defun char-successor(c)
"values are succesor and carry, same case for letters"
(cond
((char= c #\9) (values #\0 t))
((char= c #\Z) (values #\A t))
((char= c #\z) (values #\a t))
((alphanumericp c) (values (code-char (1+ (char-code c))) nil))
(t (values c t))))
(defun string-successor(s)
(let ((i (1- (length s)))
(s1 (copy-seq s))
(p1 (position-if #'alphanumericp s)))
(when (null p1)
(setf (char s1 i) (code-char (1+ (char-code (char s1 i)))))
(return-from string-successor s1))
(loop (multiple-value-bind (succ carry) (char-successor (char s i))
(setf (char s1 i) succ)
(if (not carry)
(return-from string-successor s1)
(if (= i p1)
(return-from string-successor
(concatenate 'string
(subseq s1 0 i)
(string (if (char= succ #\0) #\1 succ))
(subseq s1 (1+ i))))
(decf i)))))))
@spacebat
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment