Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion size.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type unitMap map[string]int64
var (
decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
binaryMap = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
sizeRegex = regexp.MustCompile(`^(\d+(\.\d+)*) ?([kKmMgGtTpP])?[bB]?$`)
sizeRegex = regexp.MustCompile(`^(\d+(\.\d+)*) ?([kKmMgGtTpP])?[iI]?[bB]?$`)
)

var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
Expand Down
2 changes: 2 additions & 0 deletions size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ func TestRAMInBytes(t *testing.T) {
assertSuccessEquals(t, 32*KiB, RAMInBytes, "32K")
assertSuccessEquals(t, 32*KiB, RAMInBytes, "32kb")
assertSuccessEquals(t, 32*KiB, RAMInBytes, "32Kb")
assertSuccessEquals(t, 32*KiB, RAMInBytes, "32Kib")
assertSuccessEquals(t, 32*KiB, RAMInBytes, "32KIB")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking at these, I wonder if it's correct that we treat kB, KiB and KB equal; from https://en.wikipedia.org/wiki/Kilobyte

screen shot 2017-12-21 at 18 38 01

32 KB == 32 KiB == 32768 byte, but 32kB == 32000 byte

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually, wondering if I'm reading KB (JEDEC) correctly in that table, so please double-check 😅

Copy link
Copy Markdown
Contributor Author

@FlorinAsavoaie FlorinAsavoaie Dec 21, 2017

Choose a reason for hiding this comment

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

Well, that was, obviously, already there. What I added should be the correct behavior imho. However, I wonder how many things will break if we decide to mess with it.

On the other hand, there's no function here to make a difference between KB and KiB, it just depends on what function does the user of the library call.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, I noticed it was there 😞 not sure what's best; perhaps we need additional functions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, I don't think this module is organized in such a way to properly support the standards. I'd rather go for a rebuild of this module (go-units2 or go-standard-units :D) and organize the functions something like ReadHumanSizeBinary which properly parses IEC and JEDEC, then ReadHumanSizeDecimal which properly parses metric data. Also organize those maps and regexes per "Metric/Decimal" and "Binary/IEC/JEDEC".

My guess is that it would break a lot of stuff because these conventions are badly used all around.

Until then, would you think this is a blocker to merge this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed, this change doesn't make things worse, so don't see a reason to not go ahead.

Let me know if you're interested in a follow up for those things though!

assertSuccessEquals(t, 32*MiB, RAMInBytes, "32Mb")
assertSuccessEquals(t, 32*GiB, RAMInBytes, "32Gb")
assertSuccessEquals(t, 32*TiB, RAMInBytes, "32Tb")
Expand Down