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

performance improvements #53

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.hashids</groupId>
<artifactId>hashids</artifactId>
<version>1.0.3-SNAPSHOT</version>
<version>1.0.4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hashids</name>
<description>Implementing Hashids algorithm v1.0.0 version(http://hashids.org)</description>
Expand Down Expand Up @@ -207,5 +207,9 @@
<name>Matthias Vill</name>
<url>https://github.com/TheConstructor</url>
</contributor>
<contributor>
<name>Mihai Cazacu</name>
<url>https://github.com/cazacugmihai</url>
</contributor>
</contributors>
</project>
113 changes: 113 additions & 0 deletions src/main/java/org/hashids/CharUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package org.hashids;

import static java.lang.System.arraycopy;
import static java.util.Arrays.copyOf;

final class CharUtils {

private CharUtils() {
throw new UnsupportedOperationException();
}

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;
}

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;
}

static int indexOf(char[] source, char c) {
int i = 0;

for (final char s : source) {
if (s == c) {
break;
}
i++;
}

return i;
}

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);
}

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);
}

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;
}

}
Loading