string_scanner 1.0.4

  • Installing
  • Versions
  • 96

This package exposes a StringScanner type that makes it easy to parse a string using a series of Patterns. For example:

import 'dart:math';

import 'package:string_scanner/string_scanner.dart';

num parseNumber(String source) {
  // Scan a number ("1", "1.5", "-3").
  var scanner = new StringScanner(source);

  // [Scanner.scan] tries to consume a [Pattern] and returns whether or not it
  // succeeded. It will move the scan pointer past the end of the pattern.
  var negative = scanner.scan("-");

  // [Scanner.expect] consumes a [Pattern] and throws a [FormatError] if it
  // fails. Like [Scanner.scan], it will move the scan pointer forward.
  scanner.expect(new RegExp(r"\d+"));

  // [Scanner.lastMatch] holds the [MatchData] for the most recent call to
  // [Scanner.scan], [Scanner.expect], or [Scanner.matches].
  var number = int.parse(scanner.lastMatch[0]);

  if (scanner.scan(".")) {
    scanner.expect(new RegExp(r"\d+"));
    var decimal = scanner.lastMatch[0];
    number += int.parse(decimal) / math.pow(10, decimal.length);

  // [Scanner.expectDone] will throw a [FormatError] if there's any input that
  // hasn't yet been consumed.

  return (negative ? -1 : 1) * number;

1.0.4 #

  • Add @alwaysThrows annotation to error method.

1.0.3 #

  • Set max SDK version to <3.0.0, and adjust other dependencies.

1.0.2 #

  • SpanScanner no longer crashes when creating a span that contains a UTF-16 surrogate pair.

1.0.1 #

  • Fix the error text emitted by StringScanner.expectChar().

1.0.0 #

  • Breaking change: StringScanner.error()'s length argument now defaults to 0 rather than 1 when no match data is available.

  • Breaking change: StringScanner.lastMatch and related methods are now reset when the scanner's position changes without producing a new match.

Note: While the changes in 1.0.0 are user-visible, they're unlikely to actually break any code in practice. Unless you know that your package is incompatible with 0.1.x, consider using 0.1.5 as your lower bound rather than 1.0.0. For example, string_scanner: ">=0.1.5 <2.0.0".

0.1.5 #

  • Add new SpanScanner.within(), which scans within a existing FileSpan.

  • Add StringScanner.scanChar() and StringScanner.expectChar().

0.1.4+1 #

  • Remove the dependency on path, since we don't actually import it.

0.1.4 #

  • Add new SpanScanner.eager() for creating a SpanScanner that eagerly computes its current line and column numbers.

0.1.3+2 #

  • Fix LineScanner's handling of carriage returns to match that of SpanScanner.

0.1.3+1 #

  • Fixed the homepage URL.

0.1.3 #

  • Add an optional endState argument to SpanScanner.spanFrom.

0.1.2 #

  • Add StringScanner.substring, which returns a substring of the source string.

0.1.1 #

  • Declare SpanScanner's exposed SourceSpans and SourceLocations to be FileSpans and FileLocations. They always were underneath, but callers may now rely on it.

  • Add SpanScanner.location, which returns the scanner's current SourceLocation.

0.1.0 #

  • Switch from source_maps' Span class to source_span's SourceSpan class.

  • new StringScanner()'s sourceUrl parameter is now named to make it clear that it can be safely null.

  • new StringScannerException() takes different arguments in a different order to match SpanFormatException.

  • StringScannerException.string has been renamed to StringScannerException.source to match the FormatException interface.

0.0.3 #

  • Make StringScannerException inherit from source_map's [SpanFormatException][].

0.0.2 #

  • new StringScanner() now takes an optional sourceUrl argument that provides the URL of the source file. This is used for error reporting.

  • Add StringScanner.readChar() and StringScanner.peekChar() methods for doing character-by-character scanning.

  • Scanners now throw StringScannerExceptions which provide more detailed access to information about the errors that were thrown and can provide terminal-colored messages.

  • Add a LineScanner subclass of StringScanner that automatically tracks line and column information of the text being scanned.

  • Add a SpanScanner subclass of LineScanner that exposes matched ranges as [source map][] Span objects.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  string_scanner: ^1.0.4

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:string_scanner/string_scanner.dart';
Version Uploaded Documentation Archive
1.0.4 Aug 13, 2018 Go to the documentation of string_scanner 1.0.4 Download string_scanner 1.0.4 archive
1.0.3 Jul 18, 2018 Go to the documentation of string_scanner 1.0.3 Download string_scanner 1.0.3 archive
1.0.2 May 22, 2017 Go to the documentation of string_scanner 1.0.2 Download string_scanner 1.0.2 archive
1.0.1 Nov 2, 2016 Go to the documentation of string_scanner 1.0.1 Download string_scanner 1.0.1 archive
1.0.0 Jun 14, 2016 Go to the documentation of string_scanner 1.0.0 Download string_scanner 1.0.0 archive
0.1.5 Jun 6, 2016 Go to the documentation of string_scanner 0.1.5 Download string_scanner 0.1.5 archive
0.1.4+1 Jan 15, 2016 Go to the documentation of string_scanner 0.1.4+1 Download string_scanner 0.1.4+1 archive
0.1.4 Sep 2, 2015 Go to the documentation of string_scanner 0.1.4 Download string_scanner 0.1.4 archive
0.1.3+2 Sep 2, 2015 Go to the documentation of string_scanner 0.1.3+2 Download string_scanner 0.1.3+2 archive
0.1.3+1 Mar 30, 2015 Go to the documentation of string_scanner 0.1.3+1 Download string_scanner 0.1.3+1 archive

All 17 versions...

Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jun 19, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:string_scanner/string_scanner.dart.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and string_scanner.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.

The package description is too short. (-3 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
charcode ^1.1.0 1.1.2
meta ^1.1.0 1.1.7
source_span ^1.4.0 1.5.5
Transitive dependencies
path 1.6.2
term_glyph 1.1.0
Dev dependencies
test >=0.12.0 <2.0.0