mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Make RangeIndex trait more general
This commit is contained in:
parent
309c9db2ac
commit
2e37b5e8fe
3 changed files with 63 additions and 55 deletions
|
@ -4,7 +4,7 @@
|
|||
|
||||
use servo_util::vec::*;
|
||||
use servo_util::range;
|
||||
use servo_util::range::{Range, RangeIndex, EachIndex};
|
||||
use servo_util::range::{Range, RangeIndex, IntRangeIndex, EachIndex};
|
||||
use servo_util::geometry::Au;
|
||||
|
||||
use std::cmp::{Ord, Eq};
|
||||
|
@ -526,7 +526,7 @@ pub struct GlyphStore {
|
|||
is_whitespace: bool,
|
||||
}
|
||||
|
||||
range_index! {
|
||||
int_range_index! {
|
||||
#[doc = "An index that refers to a character in a text run. This could \
|
||||
point to the middle of a glyph."]
|
||||
struct CharIndex(int)
|
||||
|
|
|
@ -20,7 +20,7 @@ use gfx::font::FontMetrics;
|
|||
use gfx::font_context::FontContext;
|
||||
use servo_util::geometry::Au;
|
||||
use servo_util::geometry;
|
||||
use servo_util::range::{Range, RangeIndex};
|
||||
use servo_util::range::{Range, RangeIndex, IntRangeIndex};
|
||||
use std::iter::Enumerate;
|
||||
use std::fmt;
|
||||
use std::mem;
|
||||
|
@ -61,7 +61,7 @@ pub struct LineBox {
|
|||
pub green_zone: Size2D<Au>
|
||||
}
|
||||
|
||||
range_index! {
|
||||
int_range_index! {
|
||||
struct BoxIndex(int)
|
||||
}
|
||||
|
||||
|
@ -981,7 +981,7 @@ impl fmt::Show for InlineFlow {
|
|||
}
|
||||
}
|
||||
|
||||
range_index! {
|
||||
int_range_index! {
|
||||
#[doc = "The index of a DOM element into the flat list of fragments."]
|
||||
struct FragmentIndex(int)
|
||||
}
|
||||
|
|
|
@ -9,21 +9,26 @@ use std::num;
|
|||
use std::num::{Bounded, Zero};
|
||||
|
||||
/// An index type to be used by a `Range`
|
||||
pub trait RangeIndex<T>: Eq + Ord
|
||||
+ Clone
|
||||
+ Copy
|
||||
+ Zero
|
||||
+ TotalEq
|
||||
+ TotalOrd
|
||||
+ Add<Self, Self>
|
||||
+ Sub<Self, Self>
|
||||
+ Neg<Self>
|
||||
+ fmt::Show {
|
||||
pub trait RangeIndex: Copy
|
||||
+ Clone
|
||||
+ fmt::Show
|
||||
+ Eq
|
||||
+ Ord
|
||||
+ TotalEq
|
||||
+ TotalOrd
|
||||
+ Add<Self, Self>
|
||||
+ Sub<Self, Self>
|
||||
+ Neg<Self>
|
||||
+ Zero {}
|
||||
|
||||
pub trait IntRangeIndex<T>: RangeIndex + Copy {
|
||||
fn new(x: T) -> Self;
|
||||
fn get(self) -> T;
|
||||
}
|
||||
|
||||
impl RangeIndex<int> for int {
|
||||
impl RangeIndex for int {}
|
||||
|
||||
impl IntRangeIndex<int> for int {
|
||||
#[inline]
|
||||
fn new(x: int) -> int { x }
|
||||
|
||||
|
@ -33,7 +38,7 @@ impl RangeIndex<int> for int {
|
|||
|
||||
/// Implements a range index type with operator overloads
|
||||
#[macro_export]
|
||||
macro_rules! range_index {
|
||||
macro_rules! int_range_index {
|
||||
($(#[$attr:meta])* struct $Self:ident($T:ty)) => (
|
||||
#[deriving(Clone, Eq, Ord, TotalEq, TotalOrd, Show, Zero)]
|
||||
$(#[$attr])*
|
||||
|
@ -46,7 +51,9 @@ macro_rules! range_index {
|
|||
}
|
||||
}
|
||||
|
||||
impl RangeIndex<$T> for $Self {
|
||||
impl RangeIndex for $Self {}
|
||||
|
||||
impl IntRangeIndex<$T> for $Self {
|
||||
#[inline]
|
||||
fn new(x: $T) -> $Self {
|
||||
$Self(x)
|
||||
|
@ -99,7 +106,7 @@ pub struct Range<I> {
|
|||
len: I,
|
||||
}
|
||||
|
||||
impl<T: Int, I: fmt::Show + RangeIndex<T>> fmt::Show for Range<I> {
|
||||
impl<I: RangeIndex> fmt::Show for Range<I> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f.buf, "[{} .. {})", self.begin(), self.end())
|
||||
}
|
||||
|
@ -110,16 +117,14 @@ pub struct EachIndex<T, I> {
|
|||
it: iter::Range<T>,
|
||||
}
|
||||
|
||||
pub fn each_index<T: Int, I: RangeIndex<T>>(start: I, stop: I) -> EachIndex<T, I> {
|
||||
EachIndex {
|
||||
it: iter::range(start.get(), stop.get())
|
||||
}
|
||||
pub fn each_index<T: Int, I: IntRangeIndex<T>>(start: I, stop: I) -> EachIndex<T, I> {
|
||||
EachIndex { it: iter::range(start.get(), stop.get()) }
|
||||
}
|
||||
|
||||
impl<T: Int, I: RangeIndex<T>> Iterator<I> for EachIndex<T, I> {
|
||||
impl<T: Int, I: IntRangeIndex<T>> Iterator<I> for EachIndex<T, I> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I> {
|
||||
self.it.next().map(|i| RangeIndex::new(i))
|
||||
self.it.next().map(|i| IntRangeIndex::new(i))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -128,7 +133,7 @@ impl<T: Int, I: RangeIndex<T>> Iterator<I> for EachIndex<T, I> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Int, I: RangeIndex<T>> Range<I> {
|
||||
impl<I: RangeIndex> Range<I> {
|
||||
#[inline]
|
||||
pub fn new(off: I, len: I) -> Range<I> {
|
||||
Range { off: off, len: len }
|
||||
|
@ -146,39 +151,11 @@ impl<T: Int, I: RangeIndex<T>> Range<I> {
|
|||
#[inline]
|
||||
pub fn end(&self) -> I { self.off + self.len }
|
||||
|
||||
#[inline]
|
||||
pub fn each_index(&self) -> EachIndex<T, I> {
|
||||
each_index(self.off, self.off + self.len)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn contains(&self, i: I) -> bool {
|
||||
i >= self.begin() && i < self.end()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_valid_for_string(&self, s: &str) -> bool {
|
||||
let s_len = s.len();
|
||||
match num::cast::<uint, T>(s_len) {
|
||||
Some(len) => {
|
||||
let len = RangeIndex::new(len);
|
||||
self.begin() < len
|
||||
&& self.end() <= len
|
||||
&& self.length() <= len
|
||||
},
|
||||
None => {
|
||||
debug!("Range<T>::is_valid_for_string: string length (len={}) is longer than the \
|
||||
max value for the range index (max={})", s_len,
|
||||
{
|
||||
let max: T = Bounded::max_value();
|
||||
let val: I = RangeIndex::new(max);
|
||||
val
|
||||
});
|
||||
false
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len.is_zero()
|
||||
|
@ -254,6 +231,37 @@ impl<T: Int, I: RangeIndex<T>> Range<I> {
|
|||
fail!("relation_to_range(): didn't classify self={}, other={}",
|
||||
self, other);
|
||||
}
|
||||
}
|
||||
|
||||
/// Methods for `Range`s with indices based on integer values
|
||||
impl<T: Int, I: IntRangeIndex<T>> Range<I> {
|
||||
#[inline]
|
||||
pub fn each_index(&self) -> EachIndex<T, I> {
|
||||
each_index(self.off, self.off + self.len)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_valid_for_string(&self, s: &str) -> bool {
|
||||
let s_len = s.len();
|
||||
match num::cast::<uint, T>(s_len) {
|
||||
Some(len) => {
|
||||
let len = IntRangeIndex::new(len);
|
||||
self.begin() < len
|
||||
&& self.end() <= len
|
||||
&& self.length() <= len
|
||||
},
|
||||
None => {
|
||||
debug!("Range<T>::is_valid_for_string: string length (len={}) is longer than the \
|
||||
max value for the range index (max={})", s_len,
|
||||
{
|
||||
let max: T = Bounded::max_value();
|
||||
let val: I = IntRangeIndex::new(max);
|
||||
val
|
||||
});
|
||||
false
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn repair_after_coalesced_range(&mut self, other: &Range<I>) {
|
||||
|
@ -261,7 +269,7 @@ impl<T: Int, I: RangeIndex<T>> Range<I> {
|
|||
debug!("repair_after_coalesced_range: possibly repairing range {}", *self);
|
||||
debug!("repair_after_coalesced_range: relation of original range and coalesced range {}: {}",
|
||||
*other, relation);
|
||||
let _1: I = RangeIndex::new(num::one::<T>());
|
||||
let _1: I = IntRangeIndex::new(num::one::<T>());
|
||||
match relation {
|
||||
EntirelyBefore => {},
|
||||
EntirelyAfter => { self.shift_by(-other.length()); },
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue