-
Notifications
You must be signed in to change notification settings - Fork 156
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
performance improvements #53
base: master
Are you sure you want to change the base?
Changes from 8 commits
e282be6
d134613
7d4e3ca
b1a1a4c
84af659
9d1e7b3
803995d
0585d3e
38439b9
d095999
9d7901c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.hashids; | ||
|
||
import static java.lang.System.arraycopy; | ||
import static java.util.Arrays.copyOf; | ||
|
||
public final class CharUtils { | ||
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. Remove the public modifier. 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. 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. Because this name crashes with several CharUtils out there, this is a helper class for hashids, not broad use. |
||
|
||
private CharUtils() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public static char[] concatenate(char[] arrA, char[] arrB, char[] arrC) { | ||
final char[] result = new char[arrA.length + arrB.length + arrC.length]; | ||
|
||
arraycopy(arrA, 0, result, 0, arrA.length); | ||
arraycopy(arrB, 0, result, arrA.length, arrB.length); | ||
arraycopy(arrC, 0, result, arrA.length + arrB.length, arrC.length); | ||
|
||
return result; | ||
} | ||
|
||
public static char[] concatenate(char[] arrA, char[] arrB, int bFrom, int bTo) { | ||
final int bCopyLength = bTo - bFrom; | ||
final char[] result = new char[arrA.length + bCopyLength]; | ||
|
||
arraycopy(arrA, 0, result, 0, arrA.length); | ||
arraycopy(arrB, bFrom, result, arrA.length, bCopyLength); | ||
|
||
return result; | ||
} | ||
|
||
public static int indexOf(char[] source, char c) { | ||
int i = 0; | ||
|
||
for (final char s : source) { | ||
if (s == c) { | ||
break; | ||
} | ||
i++; | ||
} | ||
|
||
return i; | ||
} | ||
|
||
public static char[] cleanup(char[] source, char[] allowedChars) { | ||
if ((source == null) || (allowedChars == null)) { | ||
return source; | ||
} | ||
|
||
final char[] result = new char[source.length]; | ||
int i = 0; | ||
|
||
for (final char s : source) { | ||
for (final char a : allowedChars) { | ||
if (s == a) { | ||
result[i++] = s; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return copyOf(result, i); | ||
} | ||
|
||
public static char[] removeAll(char[] source, char[] charsToRemove) { | ||
if ((source == null) || (charsToRemove == null)) { | ||
return source; | ||
} | ||
|
||
final char[] result = new char[source.length]; | ||
int i = 0; | ||
boolean found; | ||
|
||
for (final char s : source) { | ||
found = false; | ||
|
||
for (final char c : charsToRemove) { | ||
if (s == c) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!found) { | ||
result[i++] = s; | ||
} | ||
} | ||
|
||
return copyOf(result, i); | ||
} | ||
|
||
public static boolean validate(char[] source, char[] allowedChars) { | ||
boolean found; | ||
|
||
for (final char s : source) { | ||
found = false; | ||
|
||
for (final char a : allowedChars) { | ||
if (s == a) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!found) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
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.
Why did you add this dependency?
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.
My mistake. I have run a benchmark to check the performance. Thanks for seeing it! :)