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

topdown: improve scientific notation parsing in extractNumAndUnit #7147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
110 changes: 110 additions & 0 deletions test/cases/testdata/v1/units/test-parse-bytes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,113 @@ cases:
}
want_result:
- x: true
- note: units_parse_bytes/scientific notation with KB
query: data.test.p = x
modules:
- |
package test

p if {
units.parse_bytes("1e3KB") == 1000000
}
want_result:
- x: true
- note: units_parse_bytes/uppercase scientific notation with MiB
query: data.test.p = x
modules:
- |
package test

p if {
units.parse_bytes("3.2E2MiB") == 335544320
}
want_result:
- x: true
- note: units_parse_bytes/mixed case scientific notation with GiB
query: data.test.p = x
modules:
- |
package test

p if {
units.parse_bytes("4.5e1GiB") == 48318382080
}
want_result:
- x: true
- note: units_parse_bytes/scientific notation without unit
query: data.test.p = x
modules:
- |
package test

p if {
units.parse_bytes("5e6") == 5000000
}
want_result:
- x: true
- note: units_parse_bytes/scientific notation with negative exponent and KB
query: data.test.p = x
modules:
- |
package test

p if {
units.parse_bytes("1e-2KB") == 10
}
want_result:
- x: true
- note: units_parse_bytes/uppercase scientific notation with GB
query: data.test.p = x
modules:
- |
package test

p if {
units.parse_bytes("6.4E1GB") == 64000000000
}
want_result:
- x: true
- note: units_parse_bytes/mixed case scientific notation with TiB
query: data.test.p = x
modules:
- |
package test

p if {
units.parse_bytes("7.5e1TiB") == 82463372083200
}
want_result:
- x: true
- note: units_parse_bytes/scientific notation with lowercase MiB
query: data.test.p = x
modules:
- |
package test

p if {
units.parse_bytes("5e2MiB") == 524288000
}
want_result:
- x: true
- note: units_parse_bytes/invalid format with only E or e
query: data.test.p = x
modules:
- |
package test

p if {
not units.parse_bytes("5E") == 5
}
want_result:
- x: true
- note: units_parse_bytes/scientific notation with lowercase scientific notation and GiB
query: data.test.p = x
modules:
- |
package test

p if {
units.parse_bytes("2e2GiB") == 214748364800
}
want_result:
- x: true
132 changes: 132 additions & 0 deletions test/cases/testdata/v1/units/test-parse-units.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,135 @@ cases:
}
want_result:
- x: true
- note: units_parse/1e10 lowercase scientific notation without unit
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("1e10") == 10000000000
}
want_result:
- x: true
- note: units_parse/3.2E4 uppercase scientific notation without unit
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("3.2E4") == 32000
}
want_result:
- x: true
- note: units_parse/2.5e3 mixed case scientific notation with K
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("2.5e3K") == 2500000
}
want_result:
- x: true
- note: units_parse/1e3M lowercase scientific notation with M
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("1e3M") == 1000000000
}
want_result:
- x: true
- note: units_parse/4E2G uppercase scientific notation with G
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("4E2G") == 400000000000
}
want_result:
- x: true
- note: units_parse/5e1Gi mixed case scientific notation with Gi
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("5e1Gi") == 53687091200
}
want_result:
- x: true
- note: units_parse/1e-2 lowercase scientific notation with negative exponent
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("1e-2") == 0.01
}
want_result:
- x: true
- note: units_parse/7.8E-1 uppercase scientific notation with negative exponent
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("7.8E-1") == 0.78
}
want_result:
- x: true
- note: units_parse/6e3Mi mixed case scientific notation with binary Mi
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("6e3Mi") == 6291456000
}
want_result:
- x: true
- note: units_parse/invalid notation with single E
query: data.test.p = x
modules:
- |
package test

p if {
not units.parse("5E") == 5
}
want_result:
- x: true
- note: units_parse/number without exponent or unit
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("42") == 42
}
want_result:
- x: true
- note: units_parse/negative number in scientific notation with unit
query: data.test.p = x
modules:
- |
package test

p if {
units.parse("-3.5E2m") == -0.35
}
want_result:
- x: true
21 changes: 17 additions & 4 deletions topdown/parse_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,34 @@ func extractNumAndUnit(s string) (string, string) {
}

firstNonNumIdx := -1
for idx, r := range s {
if !isNum(r) {
for idx := 0; idx < len(s); idx++ {
r := rune(s[idx])
if !isNum(r) && r != 'e' && r != 'E' && r != '+' && r != '-' {
Copy link
Member

Choose a reason for hiding this comment

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

Adding a small note for this check will be helpful.

firstNonNumIdx = idx
break
}
if r == 'e' || r == 'E' {
// Check if the next character is a valid digit or +/- for scientific notation
if idx == len(s)-1 || (!unicode.IsDigit(rune(s[idx+1])) && rune(s[idx+1]) != '+' && rune(s[idx+1]) != '-') {
firstNonNumIdx = idx
break
}
// Skip the next character if it is '+' or '-'
if idx+1 < len(s) && (s[idx+1] == '+' || s[idx+1] == '-') {
idx++
}
}
}

if firstNonNumIdx == -1 { // only digits and '.'
if firstNonNumIdx == -1 { // only digits, '.', or valid scientific notation
return s, ""
}
if firstNonNumIdx == 0 { // only units (starts with non-digit)
return "", s
}

return s[0:firstNonNumIdx], s[firstNonNumIdx:]
// Return the number and the rest as the unit
return s[:firstNonNumIdx], s[firstNonNumIdx:]
}

func init() {
Expand Down