Skip to content

Commit

Permalink
MCStrings: Simplify asInteger & asLong
Browse files Browse the repository at this point in the history
  • Loading branch information
xDec0de committed Sep 1, 2023
1 parent e878730 commit 7999d44
Showing 1 changed file with 2 additions and 24 deletions.
26 changes: 2 additions & 24 deletions src/main/java/me/xdec0de/mcutils/java/strings/MCStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -694,18 +694,7 @@ public static int asUnsignedInteger(@Nullable CharSequence str) {
*/
@Nullable
public static Integer asInteger(@Nullable CharSequence str, @Nullable Integer def) {
final int size = str == null ? 0 : str.length();
if (size == 0)
return def;
int result = 0;
final char sign = str.charAt(0);
for (int i = (sign == '-' || sign == '+') ? 1 : 0; i < size; i++) {
final char ch = str.charAt(i);
if (ch < '0' || ch > '9')
return def; // Not fully numeric, return null now.
result = (result * 10) + (ch - '0'); // Any numeric char - the char '0' will equal to it's numeric value.
}
return sign == '-' ? -result : result;
return isInteger(str) ? Integer.valueOf(str.toString()) : def;
}

/**
Expand Down Expand Up @@ -746,18 +735,7 @@ public static Integer asInteger(@Nullable CharSequence str) {
*/
@Nullable
public static Long asLong(@Nullable CharSequence str, @Nullable Long def) {
final int size = str == null ? 0 : str.length();
if (size == 0)
return def;
long result = 0;
final char sign = str.charAt(0);
for (int i = (sign == '-' || sign == '+') ? 1 : 0; i < size; i++) {
final char ch = str.charAt(i);
if (ch < '0' || ch > '9')
return def; // Not fully numeric, return null now.
result = (result * 10) + (ch - '0'); // Any numeric char - the char '0' will equal to it's numeric value.
}
return sign == '-' ? -result : result;
return isInteger(str) ? Long.valueOf(str.toString()) : def;
}

/**
Expand Down

0 comments on commit 7999d44

Please sign in to comment.