diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-08-01 11:27:39 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-08-01 11:27:39 +0200 |
commit | 1bb160f9dc45c3cdbe929e8965be69bcf8415d0c (patch) | |
tree | 4e3dfd7795a15082ed5218d25f852be80f3fd89c /src/util/slice.rs | |
parent | e97ad954e1468b90722cf91996d7dfc069fedf78 (diff) | |
download | markdown-rs-1bb160f9dc45c3cdbe929e8965be69bcf8415d0c.tar.gz markdown-rs-1bb160f9dc45c3cdbe929e8965be69bcf8415d0c.tar.bz2 markdown-rs-1bb160f9dc45c3cdbe929e8965be69bcf8415d0c.zip |
Add missing docs, refactor some code
Diffstat (limited to 'src/util/slice.rs')
-rw-r--r-- | src/util/slice.rs | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/src/util/slice.rs b/src/util/slice.rs index 13b664d..f287978 100644 --- a/src/util/slice.rs +++ b/src/util/slice.rs @@ -43,7 +43,11 @@ impl<'a> Position<'a> { } } - /// To do. + /// Turn a position into indices. + /// + /// Indices are places in `bytes` where this position starts and ends. + /// + /// > 👉 **Note**: indices cannot represent virtual spaces. pub fn to_indices(&self) -> (usize, usize) { (self.start.index, self.end.index) } @@ -60,7 +64,7 @@ pub struct Slice<'a> { } impl<'a> Slice<'a> { - /// Get the slice belonging to a position. + /// Get the slice belonging to a point. pub fn from_point(bytes: &'a [u8], point: &Point) -> Slice<'a> { let mut before = point.vs; let mut start = point.index; @@ -84,13 +88,13 @@ impl<'a> Slice<'a> { } } - /// To do. + /// Create a slice from one index. + /// + /// Indices are places in `bytes`. + /// + /// > 👉 **Note**: indices cannot represent virtual spaces. pub fn from_index(bytes: &'a [u8], index: usize) -> Slice<'a> { - Slice { - bytes: &bytes[index..=index], - before: 0, - after: 0, - } + Slice::from_indices(bytes, index, index + 1) } /// Get the slice belonging to a position. @@ -121,7 +125,11 @@ impl<'a> Slice<'a> { } } - /// To do. + /// Create a slice from two indices. + /// + /// Indices are places in `bytes`. + /// + /// > 👉 **Note**: indices cannot represent virtual spaces. pub fn from_indices(bytes: &'a [u8], start: usize, end: usize) -> Slice<'a> { Slice { bytes: &bytes[start..end], @@ -130,12 +138,13 @@ impl<'a> Slice<'a> { } } - /// To do. + /// Get the size of this slice, including virtual spaces. pub fn len(&self) -> usize { self.bytes.len() + self.before + self.after } - /// To do. + /// Get the first byte in this slice, representing a virtual space as a + /// space. pub fn head(&self) -> Option<u8> { if self.before > 0 { Some(b' ') @@ -146,12 +155,16 @@ impl<'a> Slice<'a> { } } - // To do: + /// Turn the slice into a `&str`. + /// + /// Does not support virtual spaces. pub fn as_str(&self) -> &str { str::from_utf8(self.bytes).unwrap() } - /// To do. + /// Turn the slice into a `String`. + /// + /// Support virtual spaces. pub fn serialize(&self) -> String { let mut string = String::with_capacity(self.len()); let mut index = self.before; |