From 5d479bcaf17ada6cd03fe56672c142913e82e579 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Thu, 21 Nov 2024 16:01:22 -0500 Subject: [PATCH] init: initial project setup --- .envrc | 12 +++ .gitignore | 1 + Cargo.lock | 7 ++ Cargo.toml | 6 ++ flake.nix | 44 ++++++++ src/lib.rs | 295 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/test.rs | 82 +++++++++++++++ 7 files changed, 447 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 flake.nix create mode 100644 src/lib.rs create mode 100644 src/test.rs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..217fd75 --- /dev/null +++ b/.envrc @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +if has lorri; then + echo Using lorri + eval "$(lorri direnv)" +elif has nix; then + echo Using flake fallback + use flake +else + # noop + echo unsupported direnv configuration +fi diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..dd27afb --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "slice-cursor" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e15b07a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "slice-cursor" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..f21159c --- /dev/null +++ b/flake.nix @@ -0,0 +1,44 @@ +{ + inputs = { + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + naersk.url = "github:nix-community/naersk"; + flake-utils = {url = "github:numtide/flake-utils"; }; + flake-compat = { url = "github:edolstra/flake-compat"; flake = false; }; + + }; + + outputs = {self, nixpkgs, flake-utils, rust-overlay, naersk, flake-compat}: + flake-utils.lib.eachDefaultSystem (system: let + overlays = [ + rust-overlay.overlays.default + ]; + pkgs = import nixpkgs { inherit system overlays; }; + rust-bin = pkgs.rust-bin.stable.latest.default; + naersk-lib = pkgs.callPackage naersk { + rustc = rust-bin; + cargo = rust-bin; + }; + project = naersk-lib.buildPackage { + src = ./.; + root = ./.; + nativeBuildInputs = [pkgs.pkg-config]; + buildInputs = + ( + if pkgs.stdenv.isDarwin + then with pkgs.darwin.apple_sdk.frameworks; [Security SystemConfiguration] + else [pkgs.openssl] + ) + ++ [rust-bin]; + }; + in + { + packages.default = project; + devShells.default = pkgs.mkShell { + buildInputs = [ rust-bin pkgs.rust-analyzer ]; + packages = with pkgs; [ gnumake ]; + }; + }); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7f95a6b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,295 @@ +// Copyright 2024 Jeremy Wall +// +// 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), + RangeTo(std::ops::RangeTo), + RangeFrom(std::ops::RangeFrom), + RangeFull(std::ops::RangeFull), +} + +/// An input that can provide a span of a range of the input. +pub trait Span { + fn span(&self, idx: SpanRange) -> O; +} + +/// The interface for types that can peek ahead to the next item. +pub trait Peekable { + fn peek_next(&self) -> Option; +} + +/// 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 { + 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(&self, idx: SpanRange) -> &'a [T] { + match idx { + 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> for SliceCursor<'a, T> { + fn from(source: &'a Vec) -> 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 { + 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(&self, idx: SpanRange) -> &'a str { + match idx { + 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) + } +} diff --git a/src/test.rs b/src/test.rs new file mode 100644 index 0000000..00357ef --- /dev/null +++ b/src/test.rs @@ -0,0 +1,82 @@ +// Copyright 2017 Jeremy Wall +// +// 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. + +use super::{ + SliceCursor, StrCursor, + Cursor, Offsetable, +}; + +#[test] +fn test_slice_iter() { + let input_str = "foo"; + let mut iter = SliceCursor::new(input_str.as_bytes()); + let cloned = iter.clone(); + assert_eq!(0, iter.get_offset()); + let mut out = Vec::new(); + loop { + let b = match iter.next() { + None => break, + Some(b) => b, + }; + assert_eq!(*b as char, *iter.curr() as char); + out.push(b.clone()); + } + assert_eq!(*iter.curr(), 'o' as u8); + assert_eq!(3, out.len()); + assert_eq!('f' as u8, out[0]); + assert_eq!('o' as u8, out[1]); + assert_eq!('o' as u8, out[2]); + assert_eq!(3, iter.get_offset()); + + out = Vec::new(); + for b in cloned { + out.push(b.clone()); + } + assert_eq!(3, out.len()); + assert_eq!('f' as u8, out[0]); + assert_eq!('o' as u8, out[1]); + assert_eq!('o' as u8, out[2]); +} + +#[test] +fn test_str_iter() { + let input_str = "foo"; + let mut iter = StrCursor::new(input_str); + let cloned = iter.clone(); + assert_eq!(0, iter.get_offset()); + let mut out = Vec::new(); + loop { + let b = match iter.next() { + None => break, + Some(b) => b, + }; + assert_eq!(*b as char, *iter.curr() as char); + out.push(b.clone()); + } + assert_eq!(*iter.curr(), 'o' as u8); + assert_eq!(3, out.len()); + assert_eq!('f' as u8, out[0]); + assert_eq!('o' as u8, out[1]); + assert_eq!('o' as u8, out[2]); + assert_eq!(3, iter.get_offset()); + + out = Vec::new(); + for b in cloned { + out.push(b.clone()); + } + assert_eq!(3, out.len()); + assert_eq!('f' as u8, out[0]); + assert_eq!('o' as u8, out[1]); + assert_eq!('o' as u8, out[2]); +}