Skip to content

util: parseSizeInBytes returns 0 for single-byte strings like 1B#2132

Open
c-tonneslan wants to merge 1 commit into
spf13:masterfrom
c-tonneslan:fix/parseSizeInBytes-single-byte
Open

util: parseSizeInBytes returns 0 for single-byte strings like 1B#2132
c-tonneslan wants to merge 1 commit into
spf13:masterfrom
c-tonneslan:fix/parseSizeInBytes-single-byte

Conversation

@c-tonneslan

Copy link
Copy Markdown

`parseSizeInBytes` only stripped the trailing `b`/`B` inside the `lastChar > 1` branch, so `1B` and `1b` fell through with the unit still attached and `cast.ToInt("1B")` returned 0.

```
parseSizeInBytes("1B") // current: 0, expected: 1
parseSizeInBytes("12 b") // current: 12, still 12 with the fix
parseSizeInBytes("43 MB") // current: 45088768, unchanged
```

Hoisted the strip so it runs whether or not a k/m/g prefix is in front. Added `1b` and `1B` cases to `TestSizeInBytes`; the existing cases still pass.

…ount

parseSizeInBytes only stripped the trailing 'b'/'B' inside the
"lastChar > 1" branch, so "1B" or "1b" fell through to cast.ToInt
with the unit still attached and returned 0. Hoisted the strip out
of that branch so single-digit byte sizes parse like the rest. Test
cases for "1b" and "1B" added; the existing cases still pass.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant