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>
Copy link
Collaborator

Choose a reason for hiding this comment

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

No caps please.1

<url>https://github.com/cazacugmihai</url>
</contributor>
</contributors>
</project>
158 changes: 158 additions & 0 deletions src/main/java/org/hashids/CharUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.hashids;

import static java.util.Arrays.copyOfRange;

public final class CharUtils {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove the public modifier.

Copy link
Author

Choose a reason for hiding this comment

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

Why? Is an utility class (like Math, Arrays, etc.).

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 a, char[] arrB, char[] arrC, int maxSize) {
if (maxSize == 0) {
return new char[0];
}

final char[] result = new char[maxSize];
int i = 0;

result[i++] = a;

if (i == maxSize) {
return result;
}

for (final char c : arrB) {
result[i++] = c;

if (i == maxSize) {
return result;
}
}

for (final char c : arrC) {
result[i++] = c;

if (i == maxSize) {
return result;
}
}

return result;
}

public static char[] concatenate(char[] arrA, char[] arrB, char[] arrC) {
final char[] result = new char[arrA.length + arrB.length + arrC.length];
int i = 0;

for (final char c : arrA) {
result[i++] = c;
}

for (final char c : arrB) {
result[i++] = c;
}

for (final char c : arrC) {
result[i++] = c;
}

return result;
}

public static char[] concatenate(char[] arrA, char[] arrB, int bFrom, int bTo) {
final char[] result = new char[arrA.length + bTo - bFrom];
int i = 0;

for (final char c : arrA) {
result[i++] = c;
}

for (int j = bFrom; j < bTo; j++) {
result[i++] = arrB[j];
}

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 copyOfRange(result, 0, 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 copyOfRange(result, 0, 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;
}
}
Loading