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

Issue #2617 add lastIndexOf(byte[] array, byte[] target) #3785

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fdesu
Copy link
Contributor

@fdesu fdesu commented Jan 28, 2020

Hi, I hope this issue still makes sense. I wanted to attempt to add the lastIndexOf method to the Primitive's utility classes to fix #2617.

To try it out I've started with simple implementation of Bytes#lastIndexOf to gather initial feedback. I want to implement lastIndexOf for other classes in the same manner. Do you think it is ok or I need to consider any other approach/put some annotations/etc. ?

}

outer:
for (int i = array.length - target.length; i >= 0; i--) {
Copy link

Choose a reason for hiding this comment

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

    for (int i = array.length - target.length; i >= 0; i--) {
        int j = 0;
        while (j < target.length && array[i + j] == target[j]) j++; 
        if (j == target.length) 
            return i;
    }

it's clearer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xindoo thanks for taking a look.
Do you think it is really easier to read? For me double for is kind of easier/clearer.

Copy link

Choose a reason for hiding this comment

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

labeled statement is easy to make me confusing, and I think it’s same to some of the others

Copy link
Contributor Author

@fdesu fdesu Feb 12, 2020

Choose a reason for hiding this comment

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

@xindoo yep, but problem with the label might be solved with simple break statement instead of continue to a label, right? I've sticked to the way how other methods were implemented, so I wanted to be consistent in regards with the labels.

Choose a reason for hiding this comment

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

@fdesu No, breaking out would hit return i;. You could do

    for (int i = array.length - target.length; i >= 0; i--) {
      for (int j = 0; ; j++) {
        if (j == target.length) {
          return i;
        } else if (array[i + j] != target[j]) {
          break;
        }
      }
    }
    return -1;

but I don't claim it's nicer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm quite flexible in this regard and don't have strong preferences, but e.g. in java.lang.String.lastIndexOf(char[],int,int,char[],int,int) they do

startSearchForLastChar:
        while (true) {
            ...
            while (j > start) {
                ...
                    continue startSearchForLastChar;
                ...
            }
            ...
        }
    }

and it's pretty clear, so I think marks are fine when they are properly named.

What do you @Maaartinus @xindoo think?

checkNotNull(array, "array");
checkNotNull(target, "target");
if (target.length == 0) {
return 0;
Copy link

@Maaartinus Maaartinus Feb 13, 2020

Choose a reason for hiding this comment

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

IMHO the last occurrence of an empty string starts at array.length. AFAIK that's how String.lastIndexOf works and also what you get when you leave this test out.

Copy link
Contributor Author

@fdesu fdesu Feb 13, 2020

Choose a reason for hiding this comment

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

Yep, you're right, in this case array.target should be a return value

Copy link
Contributor

@szarnekow szarnekow Feb 13, 2020

Choose a reason for hiding this comment

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

I think you meant array.length, didn't you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@szarnekow shouldn't have published the comment in hurry, yes, array.length

@fdesu fdesu force-pushed the feature/issue-2617-lastIndexByte-symmetry branch from 4056ae8 to 4bddd12 Compare February 14, 2020 14:20
@fdesu fdesu force-pushed the feature/issue-2617-lastIndexByte-symmetry branch from 4bddd12 to e308d68 Compare April 23, 2021 13:27
@fdesu
Copy link
Contributor Author

fdesu commented Apr 23, 2021

I've rebased this branch against latest master

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

Successfully merging this pull request may close these issues.

Add lastIndexOf with array target in primitives' utility classes
6 participants