mirror of
https://github.com/openharmony/third_party_rust_codespan.git
synced 2026-07-21 03:55:28 -04:00
Rename BytePos to ByteIndex
This commit is contained in:
+12
-12
@@ -2,7 +2,7 @@ use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use {FileMap, FileName};
|
||||
use pos::{ByteOffset, BytePos};
|
||||
use pos::{ByteIndex, ByteOffset};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CodeMap {
|
||||
@@ -15,39 +15,39 @@ impl CodeMap {
|
||||
CodeMap { files: Vec::new() }
|
||||
}
|
||||
|
||||
/// The next start position to use for a new filemap
|
||||
fn next_start_pos(&self) -> BytePos {
|
||||
let end_pos = self.files
|
||||
/// The next start index to use for a new filemap
|
||||
fn next_start_index(&self) -> ByteIndex {
|
||||
let end_index = self.files
|
||||
.last()
|
||||
.map(|x| x.span().end())
|
||||
.unwrap_or(BytePos::none());
|
||||
.unwrap_or(ByteIndex::none());
|
||||
|
||||
// Add one byte of padding between each file
|
||||
end_pos + ByteOffset(1)
|
||||
end_index + ByteOffset(1)
|
||||
}
|
||||
|
||||
/// Adds a filemap to the codemap with the given name and source string
|
||||
pub fn add_filemap(&mut self, name: FileName, src: String) -> Arc<FileMap> {
|
||||
let file = Arc::new(FileMap::new(name, src, self.next_start_pos()));
|
||||
let file = Arc::new(FileMap::new(name, src, self.next_start_index()));
|
||||
self.files.push(file.clone());
|
||||
file
|
||||
}
|
||||
|
||||
/// Adds a filemap to the codemap with the given name and source string
|
||||
pub fn add_filemap_from_disk<P: Into<PathBuf>>(&mut self, name: P) -> io::Result<Arc<FileMap>> {
|
||||
let file = Arc::new(FileMap::from_disk(name, self.next_start_pos())?);
|
||||
let file = Arc::new(FileMap::from_disk(name, self.next_start_index())?);
|
||||
self.files.push(file.clone());
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
/// Looks up the `File` that contains the specified byte position.
|
||||
pub fn find_file(&self, pos: BytePos) -> Option<&Arc<FileMap>> {
|
||||
/// Looks up the `File` that contains the specified byte index.
|
||||
pub fn find_file(&self, index: ByteIndex) -> Option<&Arc<FileMap>> {
|
||||
use std::cmp::Ordering;
|
||||
|
||||
self.files
|
||||
.binary_search_by(|file| match () {
|
||||
() if file.span().start() > pos => Ordering::Greater,
|
||||
() if file.span().end() < pos => Ordering::Less,
|
||||
() if file.span().start() > index => Ordering::Greater,
|
||||
() if file.span().end() < index => Ordering::Less,
|
||||
() => Ordering::Equal,
|
||||
})
|
||||
.ok()
|
||||
|
||||
+56
-55
@@ -4,7 +4,7 @@ use std::borrow::Cow;
|
||||
use std::{fmt, io};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use pos::{ByteOffset, BytePos, ByteSpan, ColumnIndex, LineIndex, RawOffset, RawPos};
|
||||
use pos::{ByteIndex, ByteOffset, ByteSpan, ColumnIndex, LineIndex, RawIndex, RawOffset};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum FileName {
|
||||
@@ -40,11 +40,11 @@ pub enum LineIndexError {
|
||||
}
|
||||
|
||||
#[derive(Debug, Fail, PartialEq)]
|
||||
pub enum BytePosError {
|
||||
#[fail(display = "Byte position out of bounds - given: {}, span: {}", given, span)]
|
||||
OutOfBounds { given: BytePos, span: ByteSpan },
|
||||
#[fail(display = "Byte position points within a character boundary - given: {}", given)]
|
||||
InvalidCharBoundary { given: BytePos },
|
||||
pub enum ByteIndexError {
|
||||
#[fail(display = "Byte index out of bounds - given: {}, span: {}", given, span)]
|
||||
OutOfBounds { given: ByteIndex, span: ByteSpan },
|
||||
#[fail(display = "Byte index points within a character boundary - given: {}", given)]
|
||||
InvalidCharBoundary { given: ByteIndex },
|
||||
}
|
||||
|
||||
#[derive(Debug, Fail, PartialEq)]
|
||||
@@ -68,10 +68,10 @@ pub struct FileMap {
|
||||
|
||||
impl FileMap {
|
||||
/// Construct a new filemap, creating an index of line start locations
|
||||
pub(crate) fn new(name: FileName, src: String, start_pos: BytePos) -> FileMap {
|
||||
pub(crate) fn new(name: FileName, src: String, start: ByteIndex) -> FileMap {
|
||||
use std::iter;
|
||||
|
||||
let span = ByteSpan::from_offset(start_pos, ByteOffset::from_str(&src));
|
||||
let span = ByteSpan::from_offset(start, ByteOffset::from_str(&src));
|
||||
let lines = {
|
||||
let newline_off = ByteOffset::from_char_utf8('\n');
|
||||
let offsets = src.match_indices('\n')
|
||||
@@ -89,7 +89,7 @@ impl FileMap {
|
||||
}
|
||||
|
||||
/// Read some source code from a file, loading it into a filemap
|
||||
pub(crate) fn from_disk<P: Into<PathBuf>>(name: P, start_pos: BytePos) -> io::Result<FileMap> {
|
||||
pub(crate) fn from_disk<P: Into<PathBuf>>(name: P, start: ByteIndex) -> io::Result<FileMap> {
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
|
||||
@@ -98,7 +98,7 @@ impl FileMap {
|
||||
let mut src = String::new();
|
||||
file.read_to_string(&mut src)?;
|
||||
|
||||
Ok(FileMap::new(FileName::Real(name), src, start_pos))
|
||||
Ok(FileMap::new(FileName::Real(name), src, start))
|
||||
}
|
||||
|
||||
/// The name of the file that the source came from
|
||||
@@ -123,12 +123,12 @@ impl FileMap {
|
||||
.cloned()
|
||||
.ok_or_else(|| LineIndexError::OutOfBounds {
|
||||
given: index,
|
||||
max: LineIndex(self.lines.len() as RawPos - 1),
|
||||
max: LineIndex(self.lines.len() as RawIndex - 1),
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the byte offset to the start of `line`
|
||||
pub fn line_pos(&self, index: LineIndex) -> Result<BytePos, LineIndexError> {
|
||||
/// Returns the byte index of the start of `line`
|
||||
pub fn line_byte_index(&self, index: LineIndex) -> Result<ByteIndex, LineIndexError> {
|
||||
self.line_offset(index)
|
||||
.map(|offset| self.span.start() + offset)
|
||||
}
|
||||
@@ -145,38 +145,39 @@ impl FileMap {
|
||||
}
|
||||
|
||||
/// Returns the line and column location of `byte`
|
||||
pub fn location(&self, pos: BytePos) -> Result<(LineIndex, ColumnIndex), BytePosError> {
|
||||
let line_index = self.find_line_at_pos(pos)?;
|
||||
pub fn location(&self, index: ByteIndex) -> Result<(LineIndex, ColumnIndex), ByteIndexError> {
|
||||
let line_index = self.find_line(index)?;
|
||||
let line_span = self.line_span(line_index).unwrap(); // line_index should be valid!
|
||||
let line_slice = self.src_slice(line_span).unwrap(); // line_span should be valid!
|
||||
let byte_col = pos - line_span.start();
|
||||
let column_index = ColumnIndex(line_slice[..byte_col.to_usize()].chars().count() as RawPos);
|
||||
let byte_col = index - line_span.start();
|
||||
let column_index =
|
||||
ColumnIndex(line_slice[..byte_col.to_usize()].chars().count() as RawIndex);
|
||||
|
||||
Ok((line_index, column_index))
|
||||
}
|
||||
|
||||
/// Returns the line index that the byte position points to
|
||||
pub fn find_line_at_pos(&self, pos: BytePos) -> Result<LineIndex, BytePosError> {
|
||||
if pos < self.span.start() {
|
||||
Err(BytePosError::OutOfBounds {
|
||||
given: pos,
|
||||
/// Returns the line index that the byte index points to
|
||||
pub fn find_line(&self, index: ByteIndex) -> Result<LineIndex, ByteIndexError> {
|
||||
if index < self.span.start() {
|
||||
Err(ByteIndexError::OutOfBounds {
|
||||
given: index,
|
||||
span: self.span,
|
||||
})
|
||||
} else if pos > self.span.end() {
|
||||
Err(BytePosError::OutOfBounds {
|
||||
given: pos,
|
||||
} else if index > self.span.end() {
|
||||
Err(ByteIndexError::OutOfBounds {
|
||||
given: index,
|
||||
span: self.span,
|
||||
})
|
||||
} else {
|
||||
let offset = pos - self.span.start();
|
||||
let offset = index - self.span.start();
|
||||
|
||||
if self.src.is_char_boundary(offset.to_usize()) {
|
||||
match self.lines.binary_search(&offset) {
|
||||
Ok(i) => Ok(LineIndex(i as RawPos)),
|
||||
Err(i) => Ok(LineIndex(i as RawPos - 1)),
|
||||
Ok(i) => Ok(LineIndex(i as RawIndex)),
|
||||
Err(i) => Ok(LineIndex(i as RawIndex - 1)),
|
||||
}
|
||||
} else {
|
||||
Err(BytePosError::InvalidCharBoundary {
|
||||
Err(ByteIndexError::InvalidCharBoundary {
|
||||
given: self.span.start(),
|
||||
})
|
||||
}
|
||||
@@ -239,9 +240,9 @@ mod tests {
|
||||
byte_offsets
|
||||
}
|
||||
|
||||
fn byte_positions(&self) -> Vec<BytePos> {
|
||||
let mut offsets = vec![BytePos::none()];
|
||||
offsets.extend(self.byte_offsets().iter().map(|&off| BytePos(1) + off));
|
||||
fn byte_indices(&self) -> Vec<ByteIndex> {
|
||||
let mut offsets = vec![ByteIndex::none()];
|
||||
offsets.extend(self.byte_offsets().iter().map(|&off| ByteIndex(1) + off));
|
||||
let out_of_bounds = *offsets.last().unwrap() + ByteOffset(1);
|
||||
offsets.push(out_of_bounds);
|
||||
offsets
|
||||
@@ -249,7 +250,7 @@ mod tests {
|
||||
|
||||
fn line_indices(&self) -> Vec<LineIndex> {
|
||||
(0..self.lines.len() + 2)
|
||||
.map(|i| LineIndex(i as RawPos))
|
||||
.map(|i| LineIndex(i as RawIndex))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
@@ -281,12 +282,12 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_pos() {
|
||||
fn line_byte_index() {
|
||||
let test_data = TestData::new();
|
||||
let offsets: Vec<_> = test_data
|
||||
.line_indices()
|
||||
.iter()
|
||||
.map(|&i| test_data.filemap.line_pos(i))
|
||||
.map(|&i| test_data.filemap.line_byte_index(i))
|
||||
.collect();
|
||||
|
||||
assert_eq!(
|
||||
@@ -311,29 +312,29 @@ mod tests {
|
||||
// let filemap = filemap();
|
||||
// let start = filemap.span().start();
|
||||
|
||||
// assert_eq!(filemap.line_pos(Li(0)), Some(start + BOff(0)));
|
||||
// assert_eq!(filemap.line_pos(Li(1)), Some(start + BOff(7)));
|
||||
// assert_eq!(filemap.line_pos(Li(2)), Some(start + BOff(13)));
|
||||
// assert_eq!(filemap.line_pos(Li(3)), Some(start + BOff(14)));
|
||||
// assert_eq!(filemap.line_pos(Li(4)), Some(start + BOff(20)));
|
||||
// assert_eq!(filemap.line_pos(Li(5)), Some(start + BOff(26)));
|
||||
// assert_eq!(filemap.line_pos(Li(6)), None);
|
||||
// assert_eq!(filemap.line_byte_index(Li(0)), Some(start + BOff(0)));
|
||||
// assert_eq!(filemap.line_byte_index(Li(1)), Some(start + BOff(7)));
|
||||
// assert_eq!(filemap.line_byte_index(Li(2)), Some(start + BOff(13)));
|
||||
// assert_eq!(filemap.line_byte_index(Li(3)), Some(start + BOff(14)));
|
||||
// assert_eq!(filemap.line_byte_index(Li(4)), Some(start + BOff(20)));
|
||||
// assert_eq!(filemap.line_byte_index(Li(5)), Some(start + BOff(26)));
|
||||
// assert_eq!(filemap.line_byte_index(Li(6)), None);
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn location() {
|
||||
let test_data = TestData::new();
|
||||
let lines: Vec<_> = test_data
|
||||
.byte_positions()
|
||||
.byte_indices()
|
||||
.iter()
|
||||
.map(|&pos| test_data.filemap.location(pos))
|
||||
.map(|&index| test_data.filemap.location(index))
|
||||
.collect();
|
||||
|
||||
assert_eq!(
|
||||
lines,
|
||||
vec![
|
||||
Err(BytePosError::OutOfBounds {
|
||||
given: BytePos(0),
|
||||
Err(ByteIndexError::OutOfBounds {
|
||||
given: ByteIndex(0),
|
||||
span: test_data.filemap.span(),
|
||||
}),
|
||||
Ok((LineIndex(0), ColumnIndex(0))),
|
||||
@@ -347,8 +348,8 @@ mod tests {
|
||||
Ok((LineIndex(4), ColumnIndex(0))),
|
||||
Ok((LineIndex(4), ColumnIndex(5))),
|
||||
Ok((LineIndex(5), ColumnIndex(0))),
|
||||
Err(BytePosError::OutOfBounds {
|
||||
given: BytePos(28),
|
||||
Err(ByteIndexError::OutOfBounds {
|
||||
given: ByteIndex(28),
|
||||
span: test_data.filemap.span(),
|
||||
}),
|
||||
],
|
||||
@@ -356,19 +357,19 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_line_at_pos() {
|
||||
fn find_line() {
|
||||
let test_data = TestData::new();
|
||||
let lines: Vec<_> = test_data
|
||||
.byte_positions()
|
||||
.byte_indices()
|
||||
.iter()
|
||||
.map(|&pos| test_data.filemap.find_line_at_pos(pos))
|
||||
.map(|&index| test_data.filemap.find_line(index))
|
||||
.collect();
|
||||
|
||||
assert_eq!(
|
||||
lines,
|
||||
vec![
|
||||
Err(BytePosError::OutOfBounds {
|
||||
given: BytePos(0),
|
||||
Err(ByteIndexError::OutOfBounds {
|
||||
given: ByteIndex(0),
|
||||
span: test_data.filemap.span(),
|
||||
}),
|
||||
Ok(LineIndex(0)),
|
||||
@@ -382,8 +383,8 @@ mod tests {
|
||||
Ok(LineIndex(4)),
|
||||
Ok(LineIndex(4)),
|
||||
Ok(LineIndex(5)),
|
||||
Err(BytePosError::OutOfBounds {
|
||||
given: BytePos(28),
|
||||
Err(ByteIndexError::OutOfBounds {
|
||||
given: ByteIndex(28),
|
||||
span: test_data.filemap.span(),
|
||||
}),
|
||||
],
|
||||
|
||||
+2
-2
@@ -13,5 +13,5 @@ mod pos;
|
||||
|
||||
pub use self::codemap::CodeMap;
|
||||
pub use self::filemap::{FileMap, FileName};
|
||||
pub use self::pos::{ByteOffset, BytePos, ByteSpan, ColumnIndex, ColumnNumber, LineIndex,
|
||||
LineNumber, RawOffset, RawPos};
|
||||
pub use self::pos::{ByteIndex, ByteOffset, ByteSpan, ColumnIndex, ColumnNumber, LineIndex,
|
||||
LineNumber, RawIndex, RawOffset};
|
||||
|
||||
+71
-71
@@ -3,16 +3,16 @@
|
||||
use std::{cmp, fmt};
|
||||
use std::ops::{Add, AddAssign, Neg, Sub};
|
||||
|
||||
/// The raw, untyped position. We use a 32-bit integer here for space efficiency,
|
||||
/// The raw, untyped index. We use a 32-bit integer here for space efficiency,
|
||||
/// assuming we won't be working with sources larger than 4GB.
|
||||
pub type RawPos = u32;
|
||||
pub type RawIndex = u32;
|
||||
|
||||
/// The raw, untyped offset.
|
||||
pub type RawOffset = i64;
|
||||
|
||||
/// A zero-indexed line offest into a source file
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct LineIndex(pub RawPos);
|
||||
pub struct LineIndex(pub RawIndex);
|
||||
|
||||
impl LineIndex {
|
||||
/// The 1-indexed line number. Useful for pretty printing source locations.
|
||||
@@ -49,7 +49,7 @@ impl fmt::Debug for LineIndex {
|
||||
|
||||
/// A 1-indexed line number. Useful for pretty printing source locations.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct LineNumber(RawPos);
|
||||
pub struct LineNumber(RawIndex);
|
||||
|
||||
impl fmt::Debug for LineNumber {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
@@ -67,7 +67,7 @@ impl fmt::Display for LineNumber {
|
||||
|
||||
/// A zero-indexed column offest into a source file
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ColumnIndex(pub RawPos);
|
||||
pub struct ColumnIndex(pub RawIndex);
|
||||
|
||||
impl ColumnIndex {
|
||||
/// The 1-indexed column number. Useful for pretty printing source locations.
|
||||
@@ -104,7 +104,7 @@ impl fmt::Debug for ColumnIndex {
|
||||
|
||||
/// A 1-indexed column number. Useful for pretty printing source locations.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct ColumnNumber(RawPos);
|
||||
pub struct ColumnNumber(RawIndex);
|
||||
|
||||
impl fmt::Debug for ColumnNumber {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
@@ -122,12 +122,12 @@ impl fmt::Display for ColumnNumber {
|
||||
|
||||
/// A byte position in a source file
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct BytePos(pub RawPos);
|
||||
pub struct ByteIndex(pub RawIndex);
|
||||
|
||||
impl BytePos {
|
||||
impl ByteIndex {
|
||||
/// A byte position that will never point to a valid file
|
||||
pub fn none() -> BytePos {
|
||||
BytePos(0)
|
||||
pub fn none() -> ByteIndex {
|
||||
ByteIndex(0)
|
||||
}
|
||||
|
||||
/// Convert the position into a `usize`, for use in array indexing
|
||||
@@ -136,21 +136,21 @@ impl BytePos {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BytePos {
|
||||
fn default() -> BytePos {
|
||||
BytePos(0)
|
||||
impl Default for ByteIndex {
|
||||
fn default() -> ByteIndex {
|
||||
ByteIndex(0)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for BytePos {
|
||||
impl fmt::Debug for ByteIndex {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "BytePos(")?;
|
||||
write!(f, "ByteIndex(")?;
|
||||
self.0.fmt(f)?;
|
||||
write!(f, ")")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for BytePos {
|
||||
impl fmt::Display for ByteIndex {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
@@ -215,15 +215,15 @@ impl fmt::Display for ByteOffset {
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<ByteOffset> for BytePos {
|
||||
type Output = BytePos;
|
||||
impl Add<ByteOffset> for ByteIndex {
|
||||
type Output = ByteIndex;
|
||||
|
||||
fn add(self, rhs: ByteOffset) -> BytePos {
|
||||
BytePos((self.0 as RawOffset + rhs.0) as RawPos)
|
||||
fn add(self, rhs: ByteOffset) -> ByteIndex {
|
||||
ByteIndex((self.0 as RawOffset + rhs.0) as RawIndex)
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<ByteOffset> for BytePos {
|
||||
impl AddAssign<ByteOffset> for ByteIndex {
|
||||
fn add_assign(&mut self, rhs: ByteOffset) {
|
||||
*self = *self + rhs;
|
||||
}
|
||||
@@ -251,10 +251,10 @@ impl AddAssign<ByteOffset> for ByteOffset {
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for BytePos {
|
||||
impl Sub for ByteIndex {
|
||||
type Output = ByteOffset;
|
||||
|
||||
fn sub(self, rhs: BytePos) -> ByteOffset {
|
||||
fn sub(self, rhs: ByteIndex) -> ByteOffset {
|
||||
ByteOffset(self.0 as RawOffset - rhs.0 as RawOffset)
|
||||
}
|
||||
}
|
||||
@@ -262,31 +262,31 @@ impl Sub for BytePos {
|
||||
/// A region of code in a source file
|
||||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Ord, PartialOrd)]
|
||||
pub struct ByteSpan {
|
||||
start: BytePos,
|
||||
end: BytePos,
|
||||
start: ByteIndex,
|
||||
end: ByteIndex,
|
||||
}
|
||||
|
||||
impl ByteSpan {
|
||||
/// Create a new span
|
||||
///
|
||||
/// ```rust
|
||||
/// use codespan::{BytePos, ByteSpan};
|
||||
/// use codespan::{ByteIndex, ByteSpan};
|
||||
///
|
||||
/// let span = ByteSpan::new(BytePos(3), BytePos(6));
|
||||
/// assert_eq!(span.start(), BytePos(3));
|
||||
/// assert_eq!(span.end(), BytePos(6));
|
||||
/// let span = ByteSpan::new(ByteIndex(3), ByteIndex(6));
|
||||
/// assert_eq!(span.start(), ByteIndex(3));
|
||||
/// assert_eq!(span.end(), ByteIndex(6));
|
||||
/// ```
|
||||
///
|
||||
/// `start` are reordered `end` to maintain the invariant that `start <= end`
|
||||
/// `start` and `end` are reordered to maintain the invariant that `start <= end`
|
||||
///
|
||||
/// ```rust
|
||||
/// use codespan::{BytePos, ByteSpan};
|
||||
/// use codespan::{ByteIndex, ByteSpan};
|
||||
///
|
||||
/// let span = ByteSpan::new(BytePos(6), BytePos(3));
|
||||
/// assert_eq!(span.start(), BytePos(3));
|
||||
/// assert_eq!(span.end(), BytePos(6));
|
||||
/// let span = ByteSpan::new(ByteIndex(6), ByteIndex(3));
|
||||
/// assert_eq!(span.start(), ByteIndex(3));
|
||||
/// assert_eq!(span.end(), ByteIndex(6));
|
||||
/// ```
|
||||
pub fn new(start: BytePos, end: BytePos) -> ByteSpan {
|
||||
pub fn new(start: ByteIndex, end: ByteIndex) -> ByteSpan {
|
||||
if start <= end {
|
||||
ByteSpan { start, end }
|
||||
} else {
|
||||
@@ -298,15 +298,15 @@ impl ByteSpan {
|
||||
}
|
||||
|
||||
/// Create a new span from a byte start and an offset
|
||||
pub fn from_offset(start: BytePos, off: ByteOffset) -> ByteSpan {
|
||||
pub fn from_offset(start: ByteIndex, off: ByteOffset) -> ByteSpan {
|
||||
ByteSpan::new(start, start + off)
|
||||
}
|
||||
|
||||
/// A span that will never point to a valid byte range
|
||||
pub fn none() -> ByteSpan {
|
||||
ByteSpan {
|
||||
start: BytePos::none(),
|
||||
end: BytePos::none(),
|
||||
start: ByteIndex::none(),
|
||||
end: ByteIndex::none(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,55 +320,55 @@ impl ByteSpan {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the low byte position
|
||||
pub fn start(self) -> BytePos {
|
||||
/// Get the start index
|
||||
pub fn start(self) -> ByteIndex {
|
||||
self.start
|
||||
}
|
||||
|
||||
/// Get the high byte position
|
||||
pub fn end(self) -> BytePos {
|
||||
/// Get the end index
|
||||
pub fn end(self) -> ByteIndex {
|
||||
self.end
|
||||
}
|
||||
|
||||
/// Return a new span with the low byte position replaced with the supplied byte position
|
||||
///
|
||||
/// ```rust
|
||||
/// use codespan::{BytePos, ByteSpan};
|
||||
/// use codespan::{ByteIndex, ByteSpan};
|
||||
///
|
||||
/// let span = ByteSpan::new(BytePos(3), BytePos(6));
|
||||
/// assert_eq!(span.with_lo(BytePos(2)), ByteSpan::new(BytePos(2), BytePos(6)));
|
||||
/// assert_eq!(span.with_lo(BytePos(5)), ByteSpan::new(BytePos(5), BytePos(6)));
|
||||
/// assert_eq!(span.with_lo(BytePos(7)), ByteSpan::new(BytePos(6), BytePos(7)));
|
||||
/// let span = ByteSpan::new(ByteIndex(3), ByteIndex(6));
|
||||
/// assert_eq!(span.with_lo(ByteIndex(2)), ByteSpan::new(ByteIndex(2), ByteIndex(6)));
|
||||
/// assert_eq!(span.with_lo(ByteIndex(5)), ByteSpan::new(ByteIndex(5), ByteIndex(6)));
|
||||
/// assert_eq!(span.with_lo(ByteIndex(7)), ByteSpan::new(ByteIndex(6), ByteIndex(7)));
|
||||
/// ```
|
||||
pub fn with_lo(self, start: BytePos) -> ByteSpan {
|
||||
pub fn with_lo(self, start: ByteIndex) -> ByteSpan {
|
||||
ByteSpan::new(start, self.end())
|
||||
}
|
||||
|
||||
/// Return a new span with the high byte position replaced with the supplied byte position
|
||||
///
|
||||
/// ```rust
|
||||
/// use codespan::{BytePos, ByteSpan};
|
||||
/// use codespan::{ByteIndex, ByteSpan};
|
||||
///
|
||||
/// let span = ByteSpan::new(BytePos(3), BytePos(6));
|
||||
/// assert_eq!(span.with_hi(BytePos(7)), ByteSpan::new(BytePos(3), BytePos(7)));
|
||||
/// assert_eq!(span.with_hi(BytePos(5)), ByteSpan::new(BytePos(3), BytePos(5)));
|
||||
/// assert_eq!(span.with_hi(BytePos(2)), ByteSpan::new(BytePos(2), BytePos(3)));
|
||||
/// let span = ByteSpan::new(ByteIndex(3), ByteIndex(6));
|
||||
/// assert_eq!(span.with_hi(ByteIndex(7)), ByteSpan::new(ByteIndex(3), ByteIndex(7)));
|
||||
/// assert_eq!(span.with_hi(ByteIndex(5)), ByteSpan::new(ByteIndex(3), ByteIndex(5)));
|
||||
/// assert_eq!(span.with_hi(ByteIndex(2)), ByteSpan::new(ByteIndex(2), ByteIndex(3)));
|
||||
/// ```
|
||||
pub fn with_hi(self, end: BytePos) -> ByteSpan {
|
||||
pub fn with_hi(self, end: ByteIndex) -> ByteSpan {
|
||||
ByteSpan::new(self.start(), end)
|
||||
}
|
||||
|
||||
/// Return true if `self` fully encloses `other`.
|
||||
///
|
||||
/// ```rust
|
||||
/// use codespan::{BytePos, ByteSpan};
|
||||
/// use codespan::{ByteIndex, ByteSpan};
|
||||
///
|
||||
/// let a = ByteSpan::new(BytePos(5), BytePos(8));
|
||||
/// let a = ByteSpan::new(ByteIndex(5), ByteIndex(8));
|
||||
///
|
||||
/// assert_eq!(a.contains(a), true);
|
||||
/// assert_eq!(a.contains(ByteSpan::new(BytePos(6), BytePos(7))), true);
|
||||
/// assert_eq!(a.contains(ByteSpan::new(BytePos(6), BytePos(10))), false);
|
||||
/// assert_eq!(a.contains(ByteSpan::new(BytePos(3), BytePos(6))), false);
|
||||
/// assert_eq!(a.contains(ByteSpan::new(ByteIndex(6), ByteIndex(7))), true);
|
||||
/// assert_eq!(a.contains(ByteSpan::new(ByteIndex(6), ByteIndex(10))), false);
|
||||
/// assert_eq!(a.contains(ByteSpan::new(ByteIndex(3), ByteIndex(6))), false);
|
||||
/// ```
|
||||
pub fn contains(self, other: ByteSpan) -> bool {
|
||||
self.start() <= other.start() && other.end() <= self.end()
|
||||
@@ -383,12 +383,12 @@ impl ByteSpan {
|
||||
/// ```
|
||||
///
|
||||
/// ```rust
|
||||
/// use codespan::{BytePos, ByteSpan};
|
||||
/// use codespan::{ByteIndex, ByteSpan};
|
||||
///
|
||||
/// let a = ByteSpan::new(BytePos(2), BytePos(5));
|
||||
/// let b = ByteSpan::new(BytePos(10), BytePos(14));
|
||||
/// let a = ByteSpan::new(ByteIndex(2), ByteIndex(5));
|
||||
/// let b = ByteSpan::new(ByteIndex(10), ByteIndex(14));
|
||||
///
|
||||
/// assert_eq!(a.to(b), ByteSpan::new(BytePos(2), BytePos(14)));
|
||||
/// assert_eq!(a.to(b), ByteSpan::new(ByteIndex(2), ByteIndex(14)));
|
||||
/// ```
|
||||
pub fn to(self, end: ByteSpan) -> ByteSpan {
|
||||
ByteSpan::new(
|
||||
@@ -406,12 +406,12 @@ impl ByteSpan {
|
||||
/// ```
|
||||
///
|
||||
/// ```rust
|
||||
/// use codespan::{BytePos, ByteSpan};
|
||||
/// use codespan::{ByteIndex, ByteSpan};
|
||||
///
|
||||
/// let a = ByteSpan::new(BytePos(2), BytePos(5));
|
||||
/// let b = ByteSpan::new(BytePos(10), BytePos(14));
|
||||
/// let a = ByteSpan::new(ByteIndex(2), ByteIndex(5));
|
||||
/// let b = ByteSpan::new(ByteIndex(10), ByteIndex(14));
|
||||
///
|
||||
/// assert_eq!(a.between(b), ByteSpan::new(BytePos(5), BytePos(10)));
|
||||
/// assert_eq!(a.between(b), ByteSpan::new(ByteIndex(5), ByteIndex(10)));
|
||||
/// ```
|
||||
pub fn between(self, end: ByteSpan) -> ByteSpan {
|
||||
ByteSpan::new(self.end(), end.start())
|
||||
@@ -426,12 +426,12 @@ impl ByteSpan {
|
||||
/// ```
|
||||
///
|
||||
/// ```rust
|
||||
/// use codespan::{BytePos, ByteSpan};
|
||||
/// use codespan::{ByteIndex, ByteSpan};
|
||||
///
|
||||
/// let a = ByteSpan::new(BytePos(2), BytePos(5));
|
||||
/// let b = ByteSpan::new(BytePos(10), BytePos(14));
|
||||
/// let a = ByteSpan::new(ByteIndex(2), ByteIndex(5));
|
||||
/// let b = ByteSpan::new(ByteIndex(10), ByteIndex(14));
|
||||
///
|
||||
/// assert_eq!(a.until(b), ByteSpan::new(BytePos(2), BytePos(10)));
|
||||
/// assert_eq!(a.until(b), ByteSpan::new(ByteIndex(2), ByteIndex(10)));
|
||||
/// ```
|
||||
pub fn until(self, end: ByteSpan) -> ByteSpan {
|
||||
ByteSpan::new(self.start(), end.start())
|
||||
|
||||
Reference in New Issue
Block a user