Finishing the text family the previous lane started. All three are over [u8] and none of them allocates, which is what decides their shapes. trim answers a slice of its input. That is the only shape available without an allocator, and it is also the better one: there is no new storage, only a narrower view of the caller's, so the result dies with its owner and trimming modifies nothing. Both loops test (< lo hi), because an all-whitespace input otherwise walks lo past hi and (slice s lo hi) traps on a reversed range - the same trap the bounds table already asserts on. That input is in the case list. index-of-bytes is naive and stays naive. Boyer-Moore wants a skip table sized by the needle, which is an array, which is an allocation. The empty needle answers Some 0 so that index-of-bytes and starts-with? agree on every needle, and the length test returns before the loop so a needle longer than the haystack cannot build a window off the end. parse-f64 splits the work where the two halves actually differ: the grammar is Flan's and the rounding is libc's. parse-i64 is entirely Flan because strtoll's answers are wrong for a caller - 0 for "", 0 for "abc", 12 for "12x" - and not because decimal-to-binary conversion is suspect. Reimplementing correctly rounded conversion is a different and much larger problem than rejecting junk, and IEEE-754 already guarantees strtod gives the same bits everywhere. So this validates the whole slice and only a slice that is entirely a number reaches bytes->f64. Every refusal in the table - "", "abc", "1x", ".", "1e", " 1", "1 ", "0x10", "nan" - is a plausible number out of strtod. Two caveats, both written into the source rather than discovered later. The locale worry that keeps parse-i64 in Flan does apply to strtod's decimal point, and is moot only because nothing in the runtime calls setlocale; if that stops being true this is what breaks. And the length is capped at 511 because flan_bytes_to_f64 truncates there - a validator that approved 600 digits would be approving a different number than the one strtod reads. digit? and space? exist because parse-f64 and trim need them, and calc-me loses its own byte-identical digit?. One top-level namespace makes the second definition an error rather than a shadow, which is the rule doing its job: two copies that later drift apart is exactly what it prevents.
96 lines
5.1 KiB
Plaintext
96 lines
5.1 KiB
Plaintext
;;;; index-of-bytes, trim, the two byte classes, and parse-f64.
|
|
;;;;
|
|
;;;; The search cases are the ones a naive loop gets wrong rather than the
|
|
;;;; ones it gets right: a needle that matches only at the very end, one that
|
|
;;;; matches only at index 0, one whose first byte occurs repeatedly before
|
|
;;;; the real match ("aab" in "aaab"), a needle longer than the haystack
|
|
;;;; (which must answer None and must not trap building the window), the
|
|
;;;; empty needle, and a near-miss that shares every byte but the last.
|
|
;;;;
|
|
;;;; The parse-f64 cases are every shape strtod answers a plausible number
|
|
;;;; for and a caller cannot tell from a real one: "", "abc", "1x", ".",
|
|
;;;; "1e", " 1", "0x10" and "nan". Each must be None.
|
|
|
|
(defn show-idx [o (Option i32)]
|
|
(print-i64 (i64 (match o (Some i) i None -1)))
|
|
(print-str " "))
|
|
|
|
(defn show-bool [b bool]
|
|
(print-str (if b "t" "f")))
|
|
|
|
;; Brackets around the result so an empty trim is visible as [] rather than
|
|
;; as nothing at all — the all-whitespace case is otherwise indistinguishable
|
|
;; from a trim that printed the wrong slice of length zero.
|
|
(defn show-trim [s string]
|
|
(print-str "[")
|
|
(print-bytes (trim (bytes s)))
|
|
(print-str "]"))
|
|
|
|
(defn main [] i32
|
|
(show-idx (index-of-bytes (bytes "hello world") (bytes "world"))) ; 6, at the end
|
|
(show-idx (index-of-bytes (bytes "hello world") (bytes "hello"))) ; 0, at the start
|
|
(show-idx (index-of-bytes (bytes "hello world") (bytes "o w"))) ; 4, in the middle
|
|
(show-idx (index-of-bytes (bytes "banana") (bytes "na"))) ; 2, first of two
|
|
(show-idx (index-of-bytes (bytes "aaab") (bytes "aab"))) ; 1, after false starts
|
|
(newline)
|
|
(show-idx (index-of-bytes (bytes "hello") (bytes "hellp"))) ; -1, last byte differs
|
|
(show-idx (index-of-bytes (bytes "hi") (bytes "hiya"))) ; -1, longer, no trap
|
|
(show-idx (index-of-bytes (bytes "") (bytes "a"))) ; -1, empty haystack
|
|
(show-idx (index-of-bytes (bytes "hello") (bytes ""))) ; 0, empty needle
|
|
(show-idx (index-of-bytes (bytes "") (bytes ""))) ; 0, both empty
|
|
(show-idx (index-of-bytes (bytes "hello") (bytes "hello"))) ; 0, whole string
|
|
(newline)
|
|
|
|
(show-trim " hi ") ; [hi]
|
|
(show-trim "hi") ; [hi] nothing to remove
|
|
(show-trim "\thi\n") ; [hi] tab and newline count
|
|
(show-trim " ") ; [] all whitespace, must not run backwards
|
|
(show-trim "") ; []
|
|
(show-trim " a b ") ; [a b] the inner space survives
|
|
(show-trim " x") ; [x] one-sided
|
|
(show-trim "x ") ; [x]
|
|
(newline)
|
|
|
|
(show-bool (digit? \0)) (show-bool (digit? \9)) (show-bool (digit? \/))
|
|
(show-bool (digit? \:)) (show-bool (digit? \a))
|
|
(newline)
|
|
(show-bool (space? \space)) (show-bool (space? \tab))
|
|
(show-bool (space? \newline)) (show-bool (space? \return))
|
|
(show-bool (space? \a)) (show-bool (space? \0))
|
|
(newline)
|
|
|
|
;; Accepted. The last is the round trip through %g that proves the value and
|
|
;; not merely the acceptance is right.
|
|
(print-f64 (match (parse-f64 (bytes "0")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "3.5")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "-3.5")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "+0.25")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "1e3")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "1.5E-2")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "12")) (Some v) v None -999.0))
|
|
(newline)
|
|
;; Refused. Every one of these is a number out of strtod, which is the point.
|
|
(print-f64 (match (parse-f64 (bytes "")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "abc")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "1x")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes ".")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "1e")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "1e+")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes " 1")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "1 ")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "0x10")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "nan")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes "+")) (Some v) v None -999.0))
|
|
(newline)
|
|
;; A trailing dot with no fraction is a C float literal and is accepted; a
|
|
;; leading one is too. Both are here because they are the boundary the
|
|
;; digit counter, not the position, decides.
|
|
(print-f64 (match (parse-f64 (bytes "1.")) (Some v) v None -999.0)) (print-str " ")
|
|
(print-f64 (match (parse-f64 (bytes ".5")) (Some v) v None -999.0))
|
|
(newline)
|
|
|
|
;; Parsing a trimmed field, which is why both exist.
|
|
(print-f64 (match (parse-f64 (trim (bytes " 2.25 "))) (Some v) v None -999.0))
|
|
(newline)
|
|
0)
|