Skip to content
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

Add contains methods to ByteMatcher #497

Closed

Conversation

fbruton
Copy link
Collaborator

@fbruton fbruton commented Jun 5, 2023

No description provided.

@jpdahlke jpdahlke added this to the v7.19.0 milestone Jun 5, 2023
sambish5
sambish5 previously approved these changes Jun 6, 2023
Copy link
Collaborator

@drivenflywheel drivenflywheel left a comment

Choose a reason for hiding this comment

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

The functionality is good, and well implemented, but the code finish in ByteMatcher.java is a bit poor:

  • There are 6 new methods that are well-suited for use by downstream codebases, but none of the methods have a Javadoc comment. Especially for the methods that take parameters in the form (beginIndex, endIndex, array) or (array, beginIndex, endIndex), that's not being very kind, and likely to lead to future incorrect usage. Are the indexes in reference to the arrays that were also passed in? (no, they're not) Is the beginIndex inclusive or exclusive? Is the same true for the endIndex?
  • overloads of the new methods aren't near each other. They're interleaved in no discernable pattern, which makes this file harder to read and maintain.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I should have been more clear about the organizing the overloads. From the Google Java Style Guide,
Ordering of class contents:

3.4.2.1 Overloads: never split

Methods of a class that share the same name appear in a single contiguous group with no other members in between. The same applies to multiple constructors (which always have the same name). This rule applies even when modifiers such as static or private differ between the methods.

Copy link
Collaborator

Choose a reason for hiding this comment

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

i.e.,

    public boolean contains(String pattern) {
        return contains(pattern.getBytes());
    }

    public boolean contains(byte[] pattern) {
        return indexOf(pattern) >= 0;
    }

    public boolean contains(String pattern, int beginIndex, int endIndex) {
        return contains(pattern.getBytes(), beginIndex, endIndex);
    }

    public boolean contains(byte[] pattern, int beginIndex, int endIndex) {
        return indexOf(pattern, beginIndex, endIndex) >= 0;
    }

    public boolean containsAll(int beginIndex, int endIndex, String... patterns) {
        return Arrays.stream(patterns).allMatch(pattern -> contains(pattern, beginIndex, endIndex));
    }

    public boolean containsAll(int beginIndex, int endIndex, String... patterns) {
        return Arrays.stream(patterns).allMatch(pattern -> contains(pattern, beginIndex, endIndex));
    }

    public boolean containsAny(String... patterns) {
        return Arrays.stream(patterns).anyMatch(this::contains);
    }

    public boolean containsAny(int beginIndex, int endIndex, String... patterns) {
        return Arrays.stream(patterns).anyMatch(pattern -> contains(pattern, beginIndex, endIndex));
    }

  ...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Other references, such as Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition (an oldie but goodie, available in Safari Books Online), also suggest organizing overloads from simplest to the most complex, and I tend to agree with that guideline.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll add the Javadocs once we're good with the ordering.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Overload ordering looks good now

@jpdahlke jpdahlke removed this from the v7.19.0 milestone Jun 27, 2023
@jdcove2
Copy link
Collaborator

jdcove2 commented Jun 28, 2023

The methods that have String as an argument use String.getBytes(), which uses the default Charset when converting to bytes. If these methods are to stay, then it would probably be useful to have some way of specifying the Charset to be used when converting to bytes.

@dev-mlb dev-mlb added the rebase Things have happened and now a rebase is needed label Jul 11, 2023
@fbruton
Copy link
Collaborator Author

fbruton commented May 3, 2024

This is no longer a priority.

@fbruton fbruton closed this May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rebase Things have happened and now a rebase is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants