Skip to content

Simple bracket expressions #1138

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

noahboa27
Copy link

I decided to go with the existing loop style as the wildcard_match function for continuity, simplicity, and index control. With this draft I have a few questions:

  1. Can I assume that the incoming patterns are formatted correctly? So nothing weird like [[[//**//]]] or invalid patterns such as [].
  2. Should I include room for things like [a-], [-a], or [\w]?
  3. Should combinations of the wildcard and brackets within a single pattern be possible?

Thank you for your time and comments 😁

Noah Whited added 2 commits May 21, 2025 14:09
I decided to go with the existing loop style
as the `wildcard_match` function for continuity,
simplicity, and index control.
let mut is_negated = false;
let mut last_dash = None;

while let Some(p) = pattern.get(pattern_index) {
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be better to use an iterator instead of using an explicit index? I see a lot of += 1 below.

@@ -44,8 +44,45 @@ pub(super) fn wildcard_match(test: &[u8], pattern: &[u8]) -> bool {
}
}

#[allow(dead_code)]
pub(super) fn bracket_match(test: &[u8], pattern: &[u8]) -> bool {
let mut match_cases: Vec<u8> = Vec::new();
Copy link
Member

@squell squell Jun 2, 2025

Choose a reason for hiding this comment

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

Using a Vec for a pattern wouldn't by my solution.

For range patterns like [a-z] it feels wasteful, but also a pattern like [ëä] should ideally be supported and Vec<u8> won't work very well there.

I don't think it's absolutely necessary in code like this to first build a full representation, and then check against the representation. I.e. first see if the first character is ! or ^, and then interpret it as a negated pattern. And then there are two cases: either it is a range or the pattern is simply "itself" (i.e. a slice of characters)

Maybe the wildcard_match function (and this one) should also take &str instead of &[u8].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants