parse-float

2020-02-18

Parse floating point values in strings.

Upstream URL

github.com/soemraws/parse-float

Author

Sumant Oemrawsingh

License

Public Domain
README

This package exports the following function to parse floating-point values from a string in common lisp.

The information below is purposefully as close as possible to the text and format of parse-integer in the Common Lisp HyperSpec, but this package is not related to LispWorks in any way.

Syntax:

parse-float string &key start end radix junk-allowed decimal-character exponent-character type => float, pos

Arguments and Values:

string---a string.

start, end---bounding index designators of string. The defaults for start and end are 0 and nil, respectively.

radix---a radix. The default is 10.

junk-allowed---a generalized boolean. The default is false.

decimal-character---a character separating the integer and decimal parts. The default is #\..

exponent-character---the exponentiation character (case insensitive). The default is #\e.

type---a number type specifier. The default is *READ-DEFAULT-FLOAT-FORMAT*.

float---a float, depending on type, or false.

pos---a bounding index of string.

Description:

parse-float parses a float in the specified radix from the substring of string delimited by start and end into a number of the given type.

parse-float expects an optional sign (+ or -) followed by a a non-empty sequence of digits to be interpreted in the specified radix, optionally followed by decimal-character, a sequence of digits, exponent-character, a sign (+ or -) and a sequence of digits. Optional leading and trailing whitespace is ignored.

parse-float does not recognize the syntactic radix-specifier prefixes #O, #B, #X, and #nR, nor does it recognize the exponent if radix is not 10.

If junk-allowed is false, an error of type parse-error is signaled if substring does not consist entirely of the representation of a signed float, possibly surrounded on either side by whitespace characters.

The first value returned is either the float that was parsed, or else nil if no syntactically correct float was seen but junk-allowed was true.

The second value is either the index into the string of the delimiter that terminated the parse, or the upper bounding index of the substring if the parse terminated at the end of the substring (as is always the case if junk-allowed is false).

Examples:

Load the package parse-float using Quicklisp:

(ql:quickload "parse-float")

Import the parse-float function:

(use-package :parse-float)

Use the imported parse-float function:

(parse-float "123") => 123.0, 3

(parse-float "123.1" :start 1 :radix 5 :type 'double-float) => 13.2d0, 5

(parse-float "123,0D2" :decimal-character #\, :exponent-character #\d :type 'single-float) => 12300.0, 7

(parse-float "no-integer" :junk-allowed t) => NIL, 0

(parse-float "1.2e-3" :type 'number) => 3/2500, 6

Side Effects:

None.

Affected By:

None.

Exceptional Situations:

If junk-allowed is false, an error is signaled if substring does not consist entirely of the representation of a float, possibly surrounded on either side by whitespace characters.

See Also:

parse-integer, [parse-number] (https://github.com/sharplispers/parse-number "parse-number on Github")

Dependencies (2)

  • alexandria
  • lisp-unit
  • GitHub
  • Quicklisp