320 lines
8.0 KiB
Rust

// Copyright 2024 Jeremy Wall <jeremy@marzhillstudios.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! A set of zero copy utility classes for managing cursor like access through slices.
use std::fmt::Debug;
use std::iter::Iterator;
use std::ops::Index;
#[cfg(test)]
mod test;
/// A trait for types that can have an offset as a count of processed items.
pub trait Offsetable {
fn get_offset(&self) -> usize;
}
impl Offsetable for usize {
fn get_offset(&self) -> usize {
return *self;
}
}
/// A trait for types that can seek to an index.
pub trait Seekable {
fn seek(&mut self, u: usize) -> usize;
}
/// Trait for Inputs that can report current lines and columns in a text input.
pub trait Positioned: Offsetable {
fn line(&self) -> usize;
fn column(&self) -> usize;
}
/// SpanRange encompasses the valid Ops::Range types for use with the Span trait.
pub enum SpanRange {
Range(std::ops::Range<usize>),
RangeTo(std::ops::RangeTo<usize>),
RangeFrom(std::ops::RangeFrom<usize>),
RangeFull(std::ops::RangeFull),
}
impl From<std::ops::Range<usize>> for SpanRange {
fn from(value: std::ops::Range<usize>) -> Self {
SpanRange::Range(value)
}
}
impl From<std::ops::RangeTo<usize>> for SpanRange {
fn from(value: std::ops::RangeTo<usize>) -> Self {
SpanRange::RangeTo(value)
}
}
impl From<std::ops::RangeFrom<usize>> for SpanRange {
fn from(value: std::ops::RangeFrom<usize>) -> Self {
SpanRange::RangeFrom(value)
}
}
impl From<std::ops::RangeFull> for SpanRange {
fn from(value: std::ops::RangeFull) -> Self {
SpanRange::RangeFull(value)
}
}
/// An input that can provide a span of a range of the input.
pub trait Span<O> {
fn span<R: Into<SpanRange>>(&self, idx: R) -> O;
}
/// The interface for types that can peek ahead to the next item.
pub trait Peekable<O> {
fn peek_next(&self) -> Option<O>;
}
/// A Cloneable Iterator that can report an offset as a count of processed Items.
pub trait Cursor: Iterator + Clone + Offsetable {
fn curr(&self) -> Self::Item;
}
/// Implements `InputIter` for any slice of T.
#[derive(Debug)]
pub struct SliceCursor<'a, T: Debug + 'a> {
source: &'a [T],
offset: usize,
}
impl<'a, T: Debug + 'a> SliceCursor<'a, T> {
/// new constructs a SliceIter from a Slice of T.
pub fn new(source: &'a [T]) -> Self {
SliceCursor {
source,
offset: 0,
}
}
}
impl<'a, T: Debug + 'a> Iterator for SliceCursor<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
match self.source.get(self.offset) {
Some(item) => {
self.offset += 1;
Some(item)
}
None => None,
}
}
}
impl<'a, T: Debug + 'a> Offsetable for SliceCursor<'a, T> {
fn get_offset(&self) -> usize {
self.offset
}
}
impl<'a, It> Positioned for SliceCursor<'a, It>
where
It: Positioned + Debug,
{
fn line(&self) -> usize {
match self.peek_next() {
Some(i) => i.line(),
None => 0,
}
}
fn column(&self) -> usize {
match self.peek_next() {
Some(i) => i.column(),
None => 0,
}
}
}
impl<'a, T: Debug + 'a> Clone for SliceCursor<'a, T> {
fn clone(&self) -> Self {
SliceCursor {
source: self.source,
offset: self.offset,
}
}
}
impl<'a, T: Debug + 'a> Cursor for SliceCursor<'a, T> {
fn curr(&self) -> Self::Item {
if self.offset >= self.source.len() {
self.source.get(self.source.len() - 1).unwrap()
} else {
if self.offset == 0 {
self.source.get(self.offset).unwrap()
} else {
self.source.get(self.offset - 1).unwrap()
}
}
}
}
impl<'a, T: Debug + 'a> Span<&'a [T]> for SliceCursor<'a, T> {
fn span<R: Into<SpanRange>>(&self, idx: R) -> &'a [T] {
match idx.into() {
SpanRange::Range(r) => self.source.index(r),
SpanRange::RangeTo(r) => self.source.index(r),
SpanRange::RangeFrom(r) => self.source.index(r),
SpanRange::RangeFull(r) => self.source.index(r),
}
}
}
impl<'a> From<&'a str> for SliceCursor<'a, u8> {
fn from(source: &'a str) -> Self {
SliceCursor::new(source.as_bytes())
}
}
impl<'a, T: Debug> From<&'a [T]> for SliceCursor<'a, T> {
fn from(source: &'a [T]) -> Self {
SliceCursor::new(source)
}
}
impl<'a, T: Debug> From<&'a Vec<T>> for SliceCursor<'a, T> {
fn from(source: &'a Vec<T>) -> Self {
SliceCursor::new(source.as_slice())
}
}
impl<'a, O: Debug> Peekable<&'a O> for SliceCursor<'a, O> {
fn peek_next(&self) -> Option<&'a O> {
self.source.get(self.offset)
}
}
/// Implements `InputIter` for any slice of T.
#[derive(Debug)]
pub struct StrCursor<'a> {
source: &'a str,
offset: usize,
line: usize,
column: usize,
}
impl<'a> StrCursor<'a> {
/// new constructs a StrIter from a Slice of T.
pub fn new(source: &'a str) -> Self {
StrCursor {
source,
offset: 0,
line: 1,
column: 1,
}
}
}
impl<'a> Iterator for StrCursor<'a> {
type Item = &'a u8;
fn next(&mut self) -> Option<Self::Item> {
match self.source.as_bytes().get(self.offset) {
// TODO count lines and columns.
Some(item) => {
self.offset += 1;
if *item == b'\n' {
self.line += 1;
self.column = 1;
} else {
self.column += 1;
}
Some(item)
}
None => None,
}
}
}
impl<'a> Offsetable for StrCursor<'a> {
fn get_offset(&self) -> usize {
self.offset
}
}
impl<'a> Positioned for StrCursor<'a> {
fn line(&self) -> usize {
self.line
}
fn column(&self) -> usize {
self.column
}
}
impl<'a> Clone for StrCursor<'a> {
fn clone(&self) -> Self {
StrCursor {
source: self.source,
offset: self.offset,
line: self.line,
column: self.column,
}
}
}
impl<'a> Cursor for StrCursor<'a> {
fn curr(&self) -> Self::Item {
if self.offset >= self.source.len() {
self.source.as_bytes().get(self.source.len() - 1).unwrap()
} else {
if self.offset == 0 {
self.source.as_bytes().get(self.offset).unwrap()
} else {
self.source.as_bytes().get(self.offset - 1).unwrap()
}
}
}
}
impl<'a> From<&'a str> for StrCursor<'a> {
fn from(source: &'a str) -> Self {
Self::new(source)
}
}
impl<'a> Span<&'a str> for StrCursor<'a> {
fn span<R: Into<SpanRange>>(&self, idx: R) -> &'a str {
match idx.into() {
SpanRange::Range(r) => self.source.index(r),
SpanRange::RangeTo(r) => self.source.index(r),
SpanRange::RangeFrom(r) => self.source.index(r),
SpanRange::RangeFull(r) => self.source.index(r),
}
}
}
impl<'a> Seekable for StrCursor<'a> {
fn seek(&mut self, to: usize) -> usize {
let self_len = self.source.len();
let offset = if self_len > to { to } else { self_len };
self.offset = offset;
self.offset
}
}
impl<'a> Peekable<&'a u8> for StrCursor<'a> {
fn peek_next(&self) -> Option<&'a u8> {
self.source.as_bytes().get(self.offset)
}
}