浏览代码

chore: Clippy lints, tests

Josh Megnauth 1 月之前
父节点
当前提交
7d9aa051e1
共有 7 个文件被更改,包括 80 次插入45 次删除
  1. 1 1
      Cargo.toml
  2. 3 4
      src/compile.rs
  3. 11 11
      src/ctype.rs
  4. 58 16
      src/immut_vec.rs
  5. 0 7
      src/lib.rs
  6. 6 4
      src/matcher.rs
  7. 1 2
      src/tree.rs

+ 1 - 1
Cargo.toml

@@ -7,7 +7,7 @@ license = "MIT"
 name = "posix-regex"
 readme = "README.md"
 repository = "https://gitlab.redox-os.org/redox-os/posix-regex"
-version = "0.1.1"
+version = "0.1.2"
 edition = "2021"
 
 [dependencies]

+ 3 - 4
src/compile.rs

@@ -2,17 +2,16 @@
 //! Produces a matcher ready to match input.
 
 #[cfg(feature = "no_std")]
-use crate::std::collections::HashMap;
+use alloc::collections::BTreeMap as HashMap;
 
 #[cfg(not(feature = "no_std"))]
 use std::collections::HashMap;
 
-use crate::{ctype, tree::*, PosixRegex};
-
-extern crate alloc;
 use alloc::{borrow::Cow, vec, vec::Vec};
 use core::fmt;
 
+use crate::{ctype, tree::*, PosixRegex};
+
 /// Repetition bounds, for example + is (1, None), and ? is (0, Some(1))
 #[derive(Clone, Copy, PartialEq, Eq)]
 pub struct Range(pub u32, pub Option<u32>);

+ 11 - 11
src/ctype.rs

@@ -1,40 +1,40 @@
-pub fn is_alnum(c: u8) -> bool {
+pub const fn is_alnum(c: u8) -> bool {
     is_alpha(c) || is_digit(c)
 }
-pub fn is_alpha(c: u8) -> bool {
+pub const fn is_alpha(c: u8) -> bool {
     c.is_ascii_alphabetic()
 }
-pub fn is_blank(c: u8) -> bool {
+pub const fn is_blank(c: u8) -> bool {
     c == b' ' || c == b'\t'
 }
-pub fn is_cntrl(c: u8) -> bool {
+pub const fn is_cntrl(c: u8) -> bool {
     c.is_ascii_control()
 }
-pub fn is_digit(c: u8) -> bool {
+pub const fn is_digit(c: u8) -> bool {
     c.is_ascii_digit()
 }
-pub fn is_graph(c: u8) -> bool {
+pub const fn is_graph(c: u8) -> bool {
     c.is_ascii_graphic()
 }
-pub fn is_lower(c: u8) -> bool {
+pub const fn is_lower(c: u8) -> bool {
     c.is_ascii_lowercase()
 }
 pub fn is_print(c: u8) -> bool {
     (0x20..=0x7e).contains(&c)
 }
-pub fn is_punct(c: u8) -> bool {
+pub const fn is_punct(c: u8) -> bool {
     is_graph(c) && !is_alnum(c)
 }
 pub fn is_space(c: u8) -> bool {
     c == b' ' || (0x9..=0xD).contains(&c)
 }
-pub fn is_upper(c: u8) -> bool {
+pub const fn is_upper(c: u8) -> bool {
     c.is_ascii_uppercase()
 }
-pub fn is_xdigit(c: u8) -> bool {
+pub const fn is_xdigit(c: u8) -> bool {
     c.is_ascii_hexdigit()
 }
 
-pub fn is_word_boundary(c: u8) -> bool {
+pub const fn is_word_boundary(c: u8) -> bool {
     !is_alnum(c) && c != b'_'
 }

+ 58 - 16
src/immut_vec.rs

@@ -1,25 +1,17 @@
-use core::cell::RefCell;
-
-extern crate alloc;
 use alloc::vec::Vec;
+use core::cell::RefCell;
 
 pub struct ImmutVecItem<T> {
     prev: Option<usize>,
     data: T,
 }
+
+#[derive(Copy, Clone)]
 pub struct ImmutVec<'a, T> {
     inner: &'a RefCell<Vec<ImmutVecItem<T>>>,
     id: Option<usize>,
 }
-impl<'a, T> Copy for ImmutVec<'a, T> {}
-impl<'a, T> Clone for ImmutVec<'a, T> {
-    fn clone(&self) -> Self {
-        Self {
-            inner: self.inner,
-            id: self.id,
-        }
-    }
-}
+
 impl<'a, T> ImmutVec<'a, T> {
     pub fn new(inner: &'a RefCell<Vec<ImmutVecItem<T>>>) -> Self {
         Self { inner, id: None }
@@ -38,7 +30,8 @@ impl<'a, T> ImmutVec<'a, T> {
         }
     }
 }
-impl<'a, T: Clone> ImmutVec<'a, T> {
+
+impl<'a, T: Copy> ImmutVec<'a, T> {
     #[must_use = "pop does nothing to the original vector"]
     pub fn pop(self) -> (Self, Option<T>) {
         let inner = self.inner.borrow();
@@ -52,7 +45,7 @@ impl<'a, T: Clone> ImmutVec<'a, T> {
                 id: item.prev,
                 ..self
             },
-            Some(item.data.clone()),
+            Some(item.data),
         )
     }
     pub fn iter_rev(self) -> ImmutVecIter<'a, T> {
@@ -60,8 +53,8 @@ impl<'a, T: Clone> ImmutVec<'a, T> {
     }
 }
 
-pub struct ImmutVecIter<'a, T: Clone>(ImmutVec<'a, T>);
-impl<'a, T: Clone> Iterator for ImmutVecIter<'a, T> {
+pub struct ImmutVecIter<'a, T: Copy>(ImmutVec<'a, T>);
+impl<T: Copy> Iterator for ImmutVecIter<'_, T> {
     type Item = T;
 
     fn next(&mut self) -> Option<Self::Item> {
@@ -70,3 +63,52 @@ impl<'a, T: Clone> Iterator for ImmutVecIter<'a, T> {
         item
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use alloc::vec::Vec;
+    use core::cell::RefCell;
+
+    use super::ImmutVec;
+
+    #[test]
+    fn immutvec_iter_works() {
+        let cell = RefCell::new(Vec::new());
+        let mut iv = ImmutVec::new(&cell);
+
+        for i in 0..=100 {
+            iv = iv.push(i);
+        }
+        assert_eq!(Some(100), iv.id);
+
+        for (i, j) in iv.iter_rev().zip(100..=0) {
+            assert_eq!(i, j);
+        }
+    }
+
+    #[test]
+    fn push_to_rewound_vec_sets_correct_id() {
+        let cell = RefCell::new(Vec::new());
+        let mut iv = ImmutVec::new(&cell);
+
+        for i in 0..10 {
+            iv = iv.push(i);
+        }
+        assert_eq!(Some(9), iv.id);
+
+        for _ in 0..10 {
+            (iv, _) = iv.pop();
+        }
+        assert_eq!(
+            10,
+            iv.inner.borrow().len(),
+            "Length shouldn't change from popping items"
+        );
+
+        // Pushing an item should append to the end regardless of where we are in the vector
+        assert_eq!(None, iv.id);
+        iv = iv.push(10);
+        assert_eq!(11, iv.inner.borrow().len());
+        assert_eq!(Some(10), iv.id);
+    }
+}

+ 0 - 7
src/lib.rs

@@ -3,13 +3,6 @@
 
 extern crate alloc;
 
-#[cfg(feature = "no_std")]
-mod std {
-    pub mod collections {
-        pub use alloc::collections::{BTreeMap as HashMap, BTreeSet as HashSet};
-    }
-}
-
 pub mod compile;
 pub mod ctype;
 pub mod immut_vec;

+ 6 - 4
src/matcher.rs

@@ -1,12 +1,11 @@
 //! The matcher: Can find substrings in a string that match any compiled regex
 
 #[cfg(feature = "no_std")]
-use crate::std::collections::HashSet;
+use alloc::collections::BTreeSet as HashSet;
 
 #[cfg(not(feature = "no_std"))]
 use std::collections::HashSet;
 
-extern crate alloc;
 use alloc::{borrow::Cow, boxed::Box, rc::Rc, vec, vec::Vec};
 use core::{cell::RefCell, fmt};
 
@@ -95,6 +94,7 @@ impl<'a> PosixRegex<'a> {
     }
     /// Match the string starting at the current position. This does not find
     /// substrings.
+    #[allow(clippy::type_complexity)]
     pub fn matches_exact(&self, input: &[u8]) -> Option<Box<[Option<(usize, usize)>]>> {
         let mut matcher = PosixRegexMatcher {
             base: self,
@@ -124,6 +124,7 @@ impl<'a> PosixRegex<'a> {
         }
     }
     /// Match any substrings in the string, but optionally no more than `max`
+    #[allow(clippy::type_complexity)]
     pub fn matches(
         &self,
         input: &[u8],
@@ -215,7 +216,7 @@ struct Node<'a> {
     repeated: u32,
     backref: Option<BackRef>,
 }
-impl<'a> fmt::Debug for Node<'a> {
+impl fmt::Debug for Node<'_> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let mut range = self.node().range;
         range.0 = range.0.saturating_sub(self.repeated);
@@ -478,7 +479,7 @@ struct PosixRegexMatcher<'a> {
     offset: usize,
     max_groups: usize,
 }
-impl<'a> PosixRegexMatcher<'a> {
+impl PosixRegexMatcher<'_> {
     fn expand<'b>(
         &mut self,
         skip: &mut HashSet<NodeId>,
@@ -514,6 +515,7 @@ impl<'a> PosixRegexMatcher<'a> {
         insert
     }
 
+    #[allow(clippy::type_complexity)]
     fn matches_exact(&mut self, mut branches: Vec<Node>) -> Option<Box<[Option<(usize, usize)>]>> {
         // Whether or not any branch, at any point, got fully explored. This
         // means at least one path of the regex successfully completed!

+ 1 - 2
src/tree.rs

@@ -3,7 +3,6 @@ use core::{
     ops::{Index, IndexMut},
 };
 
-extern crate alloc;
 use alloc::{boxed::Box, vec::Vec};
 
 use crate::compile::{Range, Token};
@@ -42,7 +41,7 @@ impl fmt::Debug for Node {
 }
 
 pub struct NodeIter<'a>(&'a Tree, Option<NodeId>);
-impl<'a> Iterator for NodeIter<'a> {
+impl Iterator for NodeIter<'_> {
     type Item = NodeId;
     fn next(&mut self) -> Option<Self::Item> {
         if let Some(next) = self.1 {