simple-cursor

Crates.iosimple-cursor
lib.rssimple-cursor
version0.1.1
sourcesrc
created_at2023-07-13 21:03:15.542368
updated_at2023-07-13 22:04:54.340666
descriptionA super simple character cursor implementation geared towards lexers/tokenizers.
homepage
repositoryhttps://github.com/LouisGariepy/simple-cursor
max_upload_size
id915637
size21,370
__dil__ (LouisGariepy)

documentation

README

simple-cursor

Crates.io version docs.rs Crates.io version no_std compatible License

A super simple #[no_std]-compatible character cursor implementation geared towards lexers/tokenizers. The implementation is inspired by the one used in rustc and should be performant enough to handle pretty much anything you could throw at it.

Basic use

The following examples showcases the basic features of simple_cursor. Please refer to the Cursor docs for more info.

use simple_cursor::Cursor;

// Create the input string and the cursor.
let input = "123 foobar竜<!>";
let mut cursor = Cursor::new(input);

// "123"
let number_start = cursor.byte_pos();
cursor.skip_while(|c| c.is_ascii_digit());
let number_end = cursor.byte_pos();

// Some(' ')
let whitespace = cursor.bump();

// "foobar"
let ident_start = cursor.byte_pos();
cursor.skip_while(|c| c.is_ascii_alphabetic());
let ident_end = cursor.byte_pos();

// "竜<!>"
let rest_start = ident_end;
let rest_end = input.len();

assert_eq!("123", &input[number_start..number_end]);
assert_eq!(Some(' '), whitespace);
assert_eq!("foobar", &input[ident_start..ident_end]);
assert_eq!("竜<!>", &input[rest_start..rest_end]);
Commit count: 7

cargo fmt