Function: cl-parse-integer

cl-parse-integer is an autoloaded and byte-compiled function defined in cl-extra.el.gz.

Signature

(cl-parse-integer STRING &key START END RADIX JUNK-ALLOWED)

Documentation

Parse integer from the substring of STRING from START to END.

STRING may be surrounded by whitespace chars (chars with syntax ).
Other non-digit chars are considered junk. RADIX is an integer between 2 and 36, the default is 10. Signal an error if the substring between START and END cannot be parsed as an integer unless JUNK-ALLOWED is non-nil.

View in manual

Probably introduced at or before Emacs version 25.1.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/cl-extra.el.gz
;;;###autoload
(cl-defun cl-parse-integer (string &key start end radix junk-allowed)
  "Parse integer from the substring of STRING from START to END.
STRING may be surrounded by whitespace chars (chars with syntax ` ').
Other non-digit chars are considered junk.
RADIX is an integer between 2 and 36, the default is 10.  Signal
an error if the substring between START and END cannot be parsed
as an integer unless JUNK-ALLOWED is non-nil."
  (declare (side-effect-free t))
  (cl-check-type string string)
  (let* ((start (or start 0))
	 (len	(length string))
	 (end   (or end len))
	 (radix (or radix 10)))
    (or (<= start end len)
	(error "Bad interval: [%d, %d)" start end))
    (cl-flet ((skip-whitespace ()
		(while (and (< start end)
			    (= 32 (char-syntax (aref string start))))
		  (setq start (1+ start)))))
      (skip-whitespace)
      (let ((sign (cl-case (and (< start end) (aref string start))
                    (?+ (incf start) +1)
                    (?- (incf start) -1)
		    (t  +1)))
	    digit sum)
	(while (and (< start end)
		    (setq digit (cl-digit-char-p (aref string start) radix)))
	  (setq sum (+ (* (or sum 0) radix) digit)
		start (1+ start)))
	(skip-whitespace)
	(cond ((and junk-allowed (null sum)) sum)
	      (junk-allowed (* sign sum))
	      ((or (/= start end) (null sum))
	       (error "Not an integer string: `%s'" string))
	      (t (* sign sum)))))))