source: trunk/j/src/org/armedbear/lisp/parse-integer.lisp @ 2787

Last change on this file since 2787 was 2787, checked in by piso, 20 years ago

(in-package "SYSTEM")

File size: 2.4 KB
Line 
1;;; parse-integer.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: parse-integer.lisp,v 1.2 2003-07-05 02:32:16 piso Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package "SYSTEM")
21
22;;; From OpenMCL.
23
24(defun parse-integer (string &key (start 0) end
25                             (radix 10) junk-allowed)
26  (when (null end)
27    (setq end (length string)))
28  (let ((index (do ((i start (1+ i)))
29                   ((= i end)
30                    (if junk-allowed
31                        (return-from parse-integer (values nil end))
32                        (error "not an integer string: ~S" string)))
33                 (unless (whitespacep (char string i)) (return i))))
34        (minusp nil)
35        (found-digit nil)
36        (result 0))
37    (let ((char (char string index)))
38      (cond ((char= char #\-)
39             (setq minusp t)
40             (setq index (1+ index)))
41            ((char= char #\+)
42             (setq index (1+ index)))))
43    (loop
44      (when (= index end) (return nil))
45      (let* ((char (char string index))
46             (weight (digit-char-p char radix)))
47        (cond (weight
48               (setq result (+ weight (* result radix))
49                     found-digit t))
50              (junk-allowed (return nil))
51              ((whitespacep char)
52               (until (eq (setq index (1+ index)) end)
53                      (unless (whitespacep (char string index))
54                        (error "not an integer string: ~S" string)))
55               (return nil))
56              (t
57               (error "not an integer string: ~S" string))))
58      (setq index (1+ index)))
59    (values
60     (if found-digit
61         (if minusp (- result) result)
62         (if junk-allowed
63             nil
64             (error "not an integer string: ~S" string)))
65     index)))
Note: See TracBrowser for help on using the repository browser.