-
Notifications
You must be signed in to change notification settings - Fork 11k
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
base: master
Are you sure you want to change the base?
Issue #2617 add lastIndexOf(byte[] array, byte[] target) #3785
Conversation
} | ||
|
||
outer: | ||
for (int i = array.length - target.length; i >= 0; i--) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
4056ae8
to
4bddd12
Compare
4bddd12
to
e308d68
Compare
I've rebased this branch against latest master |
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 implementlastIndexOf
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. ?