diff options
author | Manish Goregaokar <manishsmail@gmail.com> | 2017-01-06 16:26:31 -0800 |
---|---|---|
committer | Manish Goregaokar <manishsmail@gmail.com> | 2017-01-06 20:28:17 -0800 |
commit | fbeded8ac543613b89af2ed7fd856e978493cde4 (patch) | |
tree | 64d761480a43f6935aad4e0d9171674cc9c0c868 | |
parent | c579d079939b5d4ee3127579de732f037e612c28 (diff) | |
download | alacritty-fbeded8ac543613b89af2ed7fd856e978493cde4.tar.gz alacritty-fbeded8ac543613b89af2ed7fd856e978493cde4.zip |
Remove need for inclusive ranges
-rw-r--r-- | src/ansi.rs | 2 | ||||
-rw-r--r-- | src/grid.rs | 3 | ||||
-rw-r--r-- | src/index.rs | 94 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/renderer/mod.rs | 4 | ||||
-rw-r--r-- | src/selection.rs | 5 | ||||
-rw-r--r-- | src/term/mod.rs | 4 |
7 files changed, 99 insertions, 15 deletions
diff --git a/src/ansi.rs b/src/ansi.rs index 345e4e64..f24708bd 100644 --- a/src/ansi.rs +++ b/src/ansi.rs @@ -743,7 +743,7 @@ fn parse_color(attrs: &[i64], i: &mut usize) -> Option<Color> { *i += 4; - let range = 0...255; + let range = 0..256; if !range.contains_(r) || !range.contains_(g) || !range.contains_(b) { err_println!("Invalid RGB color spec: ({}, {}, {})", r, g, b); return None; diff --git a/src/grid.rs b/src/grid.rs index c5a7f70e..e044a8c3 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -24,10 +24,9 @@ use std::borrow::ToOwned; use std::cmp::Ordering; use std::iter::IntoIterator; use std::ops::{Deref, DerefMut, Range, RangeTo, RangeFrom, RangeFull, Index, IndexMut}; -use std::ops::RangeInclusive; use std::slice::{self, Iter, IterMut}; -use index::{self, Point, IndexRange}; +use index::{self, Point, IndexRange, RangeInclusive}; /// Convert a type to a linear index range. pub trait ToRange { diff --git a/src/index.rs b/src/index.rs index daec02f7..21ce0df5 100644 --- a/src/index.rs +++ b/src/index.rs @@ -17,7 +17,7 @@ /// Indexing types and implementations for Grid and Line use std::cmp::{Ord, Ordering}; use std::fmt; -use std::ops::{self, Deref, Add, Range, RangeInclusive}; +use std::ops::{self, Deref, Add, Range}; /// The side of a cell #[derive(Debug, Copy, Clone, Eq, PartialEq)] @@ -210,8 +210,95 @@ impl<T> From<Range<T>> for IndexRange<T> { } } -// can be removed if range_contains is stabilized +pub enum RangeInclusive<Idx> { + Empty { + at: Idx, + }, + NonEmpty { + start: Idx, + end: Idx, + }, +} + +impl<Idx> RangeInclusive<Idx> { + pub fn new(from: Idx, to: Idx) -> Self { + RangeInclusive::NonEmpty { + start: from, + end: to + } + } +} + +macro_rules! inclusive { + ($ty:ty, $steps_add_one:expr) => { + // impl copied from stdlib, can be removed when inclusive_range is stabilized + impl Iterator for RangeInclusive<$ty> { + type Item = $ty; + #[inline] + fn next(&mut self) -> Option<$ty> { + use index::RangeInclusive::*; + + // this function has a sort of odd structure due to borrowck issues + // we may need to replace self.range, so borrows of start and end need to end early + + let at_end; + match *self { + Empty { .. } => return None, // empty iterators yield no values + + NonEmpty { ref mut start, ref mut end } => { + + // march start towards (maybe past!) end and yield the old value + if start <= end { + let old = *start; + *start = old + 1; + return Some(old); + } + at_end = *end; + } + }; + + // got this far; the range is empty, replace it + *self = Empty { at: at_end }; + None + } + + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + use index::RangeInclusive::*; + + match *self { + Empty { .. } => (0, Some(0)), + + NonEmpty { ref start, ref end } => { + let added = $steps_add_one(start, end); + match added { + Some(hint) => (hint.saturating_add(1), hint.checked_add(1)), + None => (0, None) + } + } + } + } + } + } +} + +fn steps_add_one_u8(start: &u8, end: &u8) -> Option<usize> { + if *start < *end { + Some((*end - *start) as usize) + } else { + None + } +} +inclusive!(u8, steps_add_one_u8); + +#[test] +fn test_range() { + assert_eq!(RangeInclusive::new(1,10).collect::<Vec<_>>(), + vec![1,2,3,4,5,6,7,8,9,10]); +} + +// can be removed if range_contains is stabilized pub trait Contains { type Content; fn contains_(&self, item: Self::Content) -> bool; @@ -286,6 +373,8 @@ macro_rules! ops { } } + inclusive!($ty, <$ty>::steps_between_by_one); + impl DoubleEndedIterator for IndexRange<$ty> { #[inline] fn next_back(&mut self) -> Option<$ty> { @@ -298,7 +387,6 @@ macro_rules! ops { } } } - impl ops::AddAssign<$ty> for $ty { #[inline] fn add_assign(&mut self, rhs: $ty) { @@ -13,8 +13,6 @@ // limitations under the License. // //! Alacritty - The GPU Enhanced Terminal -#![feature(inclusive_range_syntax)] -#![feature(inclusive_range)] #![cfg_attr(feature = "clippy", feature(plugin))] #![cfg_attr(feature = "clippy", plugin(clippy))] #![cfg_attr(feature = "clippy", deny(clippy))] diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 10d3d838..a70b1ca3 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -24,7 +24,7 @@ use font::{self, Rasterizer, Rasterize, RasterizedGlyph, FontDesc, GlyphKey, Fon use gl::types::*; use gl; use notify::{Watcher as WatcherApi, RecommendedWatcher as Watcher, op}; -use index::{Line, Column}; +use index::{Line, Column, RangeInclusive}; use window::{Size, Pixels}; @@ -225,7 +225,7 @@ impl GlyphCache { macro_rules! load_glyphs_for_font { ($font:expr) => { - for i in 32u8...128u8 { + for i in RangeInclusive::new(32u8, 128u8) { cache.load_and_cache_glyph(GlyphKey { font_key: $font, c: i as char, diff --git a/src/selection.rs b/src/selection.rs index ebc84bee..ff587b9d 100644 --- a/src/selection.rs +++ b/src/selection.rs @@ -19,9 +19,8 @@ //! when text is added/removed/scrolled on the screen. The selection should //! also be cleared if the user clicks off of the selection. use std::mem; -use std::ops::RangeInclusive; -use index::{Point, Column, Side, Linear, Line}; +use index::{Point, Column, RangeInclusive, Side, Linear, Line}; use grid::ToRange; /// The area selected @@ -248,7 +247,7 @@ impl ToRange for Span { SpanType::ExcludeTail => (start, Span::exclude_end(end)) }; - start...end + RangeInclusive::new(start, end) } } diff --git a/src/term/mod.rs b/src/term/mod.rs index 975ba376..09ed5d41 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -13,14 +13,14 @@ // limitations under the License. // //! Exports the `Term` type which is a high-level API for the Grid -use std::ops::{Deref, Range, RangeInclusive}; +use std::ops::{Deref, Range}; use std::ptr; use std::cmp; use std::io; use ansi::{self, Color, NamedColor, Attr, Handler}; use grid::{Grid, ClearRegion, ToRange}; -use index::{self, Point, Column, Line, Linear, IndexRange, Contains}; +use index::{self, Point, Column, Line, Linear, IndexRange, Contains, RangeInclusive}; use selection::{Span, Selection}; pub mod cell; |