Expand description
§Spanley
This is a generic string span library,
it is meant for use with tokenizing
applications or the likes.
Serde support and API changes
are coming soon.
Please do look into string interners
before deciding to use this crate
to make sure this actually fits your
use case.
String intering features may also
get added on later to this project, but that
is a much later spot on the roadmap.
§Example
§Input
use spanley::Span;
let message = "\
This is my generic string span, his name is Spanley.\n\
Say hi Spanley!\
";
let spanley = Span::new(message, 57, 11).unwrap();
println!("{}", spanley);§Output
hi Spanley!§Features
§location
Adds SpanLocation.
A SpanLocation can be obtained directly
from a Span through its related methods.
SpanLocation gets the line and offset
for possible logging applications.
§Example
§Input
use spanley::Span;
let message = "\
This is my generic string span, his name is Spanley.\n\
Say hi Spanley!\
";
let spanley = Span::new(message, 57, 11).unwrap();
let location = spanley.get_start_location();
println!("{}", location);§Output
1:4§location-column
This can only be compiled in conjecture with
the location feature flag set to true.
Adds a relatively large extra dependencies;
unicode-width
This adds additional fields on
SpanLocation
(column_offset,
column_cjk_offset)
which are calculated by the
unicode-width crate.
column_offset
is then used instead of
char_offset
in the Display
implementation for SpanLocation.
§Example
§Input
use spanley::Span;
let source = "👩👩👦👦👩👩👦👦👩👩👦👦";
// Additional context:
//
// '👩👩👦👦' spans 7 characters behind
// the scenes and 2 columns on a TUI.
// Hence, inputs divisible by 7 and
// Outputs divisible by 2.
let spanley = Span::new(source, 14, 7).unwrap();
let location = spanley.get_start_location();
println!("{}", location.column_offset());§Output
4§container
Adds SpanContainer.
SpanContainer simplay associates a
Span to an instance of type T.
This is best used as a way to construct
or otherwise handle tokens or other
types where it is contextually relevant
to associate it with a Span.
§Example
§Input
use spanley::{Span, SpanContainer};
enum TokenKind {
Hello,
World
}
const source: &str = "Hello World!";
let hello_span = Span::new(source, 0, 5).unwrap(); // Hello
let world_span = Span::new(source, 6, 5).unwrap(); // World
let hello = SpanContainer::new(hello_span, TokenKind::Hello);
let world = SpanContainer::new(world_span, TokenKind::World);Modules§
Structs§
- Span
- A struct that spans over a
&str, written asSpan<'src>. - Span
Container container - A generic container to bundle a
Span<'src>to an instance of typeTit is representing. Written asSpanContainer<'src, T> - Span
Location location - A struct used to represent
Span’sbyte_indexfield as line and character offset indices. Meant for logging purposes.