#numbers #extract #scan #text #simd

nightly no-std numscan

Extract numbers from text

1 unstable release

0.1.0 Sep 1, 2022

#88 in #scan

MIT/Apache

13KB
161 lines

numscan

Experimental utility to extract numbers from strings. Requires nightly.


lib.rs:

numscan

A library to scan for numbers in text.

  • A number must have a digit in it.
  • A number may have a minus (-) prefix.
  • A number may contain digits, dots, and comma.
  • A number ends on the first non-numeric character it finds or second dot, and never on comma or dot.
  • The library only looks for ASCII digits (in contrast to Arabic numerals etc.)
  • Comma separators can be used liberally, they're mostly ignored.

Examples

use numscan::NumberScanner;

let input = "1.9 - (-1.7) + 1,000 is 1,000.2.";
let output = NumberScanner::from(input).collect::<Vec<_>>();

assert_eq!(
  output,
  [
    0..3,   // "1.9"
    7..11,  // "-1.7"
    15..20, // "1,000"
    24..31, // "1,000.2"
  ]
);

Tip: Parsing

The library returns ranges which you can use for indexing the numbers. In the likely scenario you want to parse them as numbers, keep in mind:

  • You might want to use a decimal library, if precision matters at all.
  • The strings might contain commas which your number type's parsing function is likely to not accept, so you might want to remove them before attempting to parse.

Dependencies

~29KB