URL Locator
This library provides a streaming parser for locating URLs.
Instead of returning the URL itself, this library will only return the length of the URL and the offset from the current parsing position.
The length and offset counts follow the example of Rust's standard library's char
type and are
based on unicode scalar values instead of graphemes.
Usage
This crate is available on crates.io and can be used by
adding urlocator
to your dependencies in your project's Cargo.toml:
[]
= "0.1.4"
Example: URL boundaries
By keeping track of the current parser position, it is possible to locate the boundaries of a URL in a character stream:
use ;
// Boundaries: 10-v v-28
let input = "[example](https://example.org)";
let mut locator = new;
let = ;
for in input.chars.enumerate
assert_eq!;
assert_eq!;
Examlpe: Counting URLs
By checking for the return state of the parser, it is possible to determine exactly when a URL has been broken. Using this, you can count the number of URLs in a stream:
use ;
let input = "https://example.org/1 https://rust-lang.org/二 https://example.com/Ⅲ";
let mut locator = new;
let mut url_count = 0;
let mut reset = true;
for c in input.chars
assert_eq!;