Skip to content

chore: allow empty str #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,33 @@ impl<T> PathTree<T> {
}

pub fn insert(&mut self, path: &str, value: T) -> usize {
if path.is_empty() {
return self.id;
}

let mut node = &mut self.node;
let pieces = Parser::new(path).collect::<Vec<_>>();

for piece in &pieces {
match piece {
Piece::String(s) => {
node = node.insert_bytes(&s[..]);
}
Piece::Parameter(_, k) => {
node = node.insert_parameter(*k);
let (overwritten, pieces) = if path.is_empty() {
(false, Vec::new())
} else {
let pieces = Parser::new(path).collect::<Vec<_>>();
for piece in &pieces {
match piece {
Piece::String(s) => {
node = node.insert_bytes(&s[..]);
}
Piece::Parameter(_, k) => {
node = node.insert_parameter(*k);
}
}
}
}
(true, pieces)
};

self.routes.push((value, pieces));
if let Some(id) = node.value {
self.routes[id].0 = value;
if overwritten {
self.routes[id].1 = pieces;
}
id
} else {
self.routes.push((value, pieces));
let id = self.id;
node.value = Some(id);
self.id += 1;
Expand Down
14 changes: 7 additions & 7 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ impl<T: fmt::Debug> Node<T> {

pub fn insert_bytes(&mut self, mut bytes: &[u8]) -> &mut Self {
let diff = match &mut self.kind {
NodeKind::String(p) => {
if p.is_empty() {
*p = bytes.to_vec();
NodeKind::String(s) => {
if s.is_empty() {
*s = bytes.to_vec();
return self;
}

let cursor = p
let cursor = s
.iter()
.zip(bytes.iter())
.take_while(|(a, b)| a == b)
Expand All @@ -51,10 +51,10 @@ impl<T: fmt::Debug> Node<T> {
true
} else {
// split node
if cursor < p.len() {
let (prefix, suffix) = p.split_at(cursor);
if cursor < s.len() {
let (prefix, suffix) = s.split_at(cursor);
let mut node = Node::new(NodeKind::String(prefix.to_vec()), None);
*p = suffix.to_vec();
*s = suffix.to_vec();
::std::mem::swap(self, &mut node);
self.nodes0.get_or_insert_with(Vec::new).push(node);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,16 @@ fn basic() {
assert_eq!(r.value, &0);
assert_eq!(r.params(), vec![]);

tree.insert("", 14);
let r = tree.find("/").unwrap();
assert_eq!(r.value, &14);
Comment on lines +1696 to +1698
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing. The path goes in as "/" but comes out as "/"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tree.insert("", 14) allow change the start node value , but it does not modify the node prefix path.

  1. tree.insert("", 14) node path = ''
  2. tree.insert("/", 15) node path = '/', overwrite the empty string

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or do not allow empty string, users handle their own paths

Copy link
Member Author

@fundon fundon Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is another way to allow '' to be the root node of the tree and '/' to be its child node. (@adriangb This should be what you expect).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I would expect “/“ to be a child of ””

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because, I always consider that the real request path does not appear '', when organizing the routing system, it has to pre-process path at the users

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth I was easily able to work around this one level of abstraction up: adriangb/routrie@70fc7c5

I think the fact that this can be solved one level of abstraction up indicates that the limitation is artificial/due to the implementation, not a conceptual one.

assert_eq!(r.params(), vec![]);

tree.insert("/", 15);
let r = tree.find("/").unwrap();
assert_eq!(r.value, &15);
assert_eq!(r.params(), vec![]);

let r = tree.find("/login").unwrap();
assert_eq!(r.value, &1);
assert_eq!(r.params(), vec![]);
Expand Down