-
Notifications
You must be signed in to change notification settings - Fork 379
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
Make IsBlank matcher consistent with String.isBlank #326
Open
Thorn1089
wants to merge
6
commits into
hamcrest:master
Choose a base branch
from
Thorn1089:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+32
−5
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd267b3
Adds additional unit test that checks all characters considered 'whit…
Thorn1089 6d5f972
Changes IsBlank matcher to use String.isBlank
Thorn1089 fbae71b
Fixes checkstyle violation; replaces tabs with spaces
Thorn1089 c802ff7
Replaces isBlank implementation
Thorn1089 62e747d
Removes unavailable #isBlank method from test
Thorn1089 620bbbc
Adds spaces to match style guide
Thorn1089 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,6 @@ | |
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import static org.hamcrest.core.AnyOf.anyOf; | ||
import static org.hamcrest.core.IsNull.nullValue; | ||
|
||
|
@@ -18,13 +16,20 @@ public final class IsBlankString extends TypeSafeMatcher<String> { | |
@SuppressWarnings("unchecked") | ||
private static final Matcher<String> NULL_OR_BLANK_INSTANCE = anyOf(nullValue(), BLANK_INSTANCE); | ||
|
||
private static final Pattern REGEX_WHITESPACE = Pattern.compile("\\s*"); | ||
|
||
private IsBlankString() { } | ||
|
||
@Override | ||
public boolean matchesSafely(String item) { | ||
return REGEX_WHITESPACE.matcher(item).matches(); | ||
final int length = item.length(); | ||
int offset = 0; | ||
while(offset < length) { | ||
final int codePoint = item.codePointAt(offset); | ||
if(!Character.isWhitespace(codePoint)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above: consider sticking a space between |
||
return false; | ||
} | ||
offset += Character.charCount(codePoint); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Consider sticking to the Hamcrest code style, which has a space before
if
and opening parenthesis.It's not enforced in the build apparently, but still...
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.
Can do. Are you using an autoformatter like spotless? It has Maven/Gradle plugins. Might be worth pulling into a separate PR :)
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.
Thanks for fixing.
I was talking about a checker rather than a formatter.
JavaHamcrest is already using CheckStyle, but apparently it doesn't check much besides the fact that there are no tabs.
For the record: I'm not an official contributor to JavaHamcrest, I'm just a random someone with a stake in the project like you, I don't have the power to merge this PR even if I wanted to.
I imagine you inspired your implementation on Java 11 JDK's sources?
They look similar, yet a little bit different, and I wonder why...
The JDK's implementation distinguishes between Latin-1 and UTF-16 encoded strings. From what I can tell a
String
can be encoded in either of these 2. Your implementation only reflects the JDK's UTF-16 implementation. Is it safe to assume UTF-16? Does that also cover Latin-1? If such an assumption were safe, why does the JDK's implementation bother to make a difference?In principle, I agree with this change, I'm just wondering if we should hold off fixing until JavaHamcrest bumps source/target compatibility to Java 11, in which case the fix will become simpler (i.e. your first commit).