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

bandwidth: parse bit suffix correctly #764

Merged
merged 1 commit into from
Jun 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/freenet/support/Fields.java
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ public static String trimPerSecond(String limit) {
final String lower = limit.toLowerCase();
for(String ending :
new String[] {
"/s", "/sec", "/second", "bps", NodeL10n.getBase().getString("FirstTimeWizardToadlet.bandwidthPerSecond").toLowerCase()
"/s", "/sec", "/second", "ps", NodeL10n.getBase().getString("FirstTimeWizardToadlet.bandwidthPerSecond").toLowerCase()
}) {
if(lower.endsWith(ending)) {
return limit.substring(0, limit.length() - ending.length());
Expand All @@ -732,11 +732,16 @@ public static int parseInt(String s, Dimension dimension) throws NumberFormatExc

/**
* Parse a human-readable string possibly including SI and ICE units into an integer.
*
* If it is a size (suffix b für bits or B for bytes), the size is returned as bytes.
* 8b = 1, 8B = 8.
* @throws NumberFormatException
* if the string is not parseable
*/
public static int parseInt(String s) throws NumberFormatException {
s = s.replaceFirst("(i)*B$", "");
boolean isSizeInBits = s.endsWith("b");
// strip bit/byte suffix
s = s.replaceFirst((isSizeInBits ? "(i)*b$" : "(i)*B$"), "");
int res = 1;
int x = s.length() - 1;
int idx;
Expand All @@ -750,7 +755,7 @@ public static int parseInt(String s) throws NumberFormatException {
res = Integer.MAX_VALUE;
throw new NumberFormatException(e.getMessage());
}
return res;
return isSizeInBits ? res / 8 : res;
}

/**
Expand Down