-
Notifications
You must be signed in to change notification settings - Fork 57
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
Supports numbers of different bases #219
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package example | ||
|
||
//go:generate ../bin/go-enum --forcelower -b example | ||
|
||
/* | ||
ENUM( | ||
|
||
B3 = 03 | ||
B4 = 04 | ||
B5 = 5 | ||
B6 = 0b110 | ||
B7 = 0b111 | ||
B8 = 0x08 | ||
B9 = 0x09 | ||
|
||
) | ||
*/ | ||
type DiffBase int |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//go:build example | ||
// +build example | ||
|
||
package example | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestDiffBase(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
actual int | ||
expected DiffBase | ||
}{ | ||
{ | ||
name: "DiffBaseB3", | ||
actual: 3, | ||
expected: DiffBaseB3, | ||
}, | ||
{ | ||
name: "DiffBaseB4", | ||
actual: 4, | ||
expected: DiffBaseB4, | ||
}, | ||
{ | ||
name: "DiffBaseB5", | ||
actual: 5, | ||
expected: DiffBaseB5, | ||
}, { | ||
name: "DiffBaseB6", | ||
actual: 6, | ||
expected: DiffBaseB6, | ||
}, { | ||
name: "DiffBaseB7", | ||
actual: 7, | ||
expected: DiffBaseB7, | ||
}, { | ||
name: "DiffBaseB8", | ||
actual: 8, | ||
expected: DiffBaseB8, | ||
}, { | ||
name: "DiffBaseB9", | ||
actual: 9, | ||
expected: DiffBaseB9, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(tt *testing.T) { | ||
assert.Equal(tt, int(test.expected), test.actual) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -446,15 +446,17 @@ func (g *Generator) parseEnum(ts *ast.TypeSpec) (*Enum, error) { | |
valueStr = trimQuotes(dataVal) | ||
} | ||
} else if unsigned { | ||
newData, err := strconv.ParseUint(dataVal, 10, 64) | ||
dataVal, base := getNumberBase(dataVal) | ||
newData, err := strconv.ParseUint(dataVal, base, 64) | ||
if err != nil { | ||
err = fmt.Errorf("failed parsing the data part of enum value '%s': %w", value, err) | ||
fmt.Println(err) | ||
return nil, err | ||
} | ||
data = newData | ||
} else { | ||
newData, err := strconv.ParseInt(dataVal, 10, 64) | ||
dataVal, base := getNumberBase(dataVal) | ||
newData, err := strconv.ParseInt(dataVal, base, 64) | ||
if err != nil { | ||
err = fmt.Errorf("failed parsing the data part of enum value '%s': %w", value, err) | ||
fmt.Println(err) | ||
|
@@ -522,6 +524,18 @@ func unescapeComment(comment string) string { | |
return val | ||
} | ||
|
||
// getNumberBase will return the base of the number if it is prefixed with 0x, 0, or 0b | ||
func getNumberBase(value string) (string, int) { | ||
if strings.HasPrefix(strings.ToLower(value), "0x") { | ||
return value[2:], 16 | ||
} else if strings.HasPrefix(strings.ToLower(value), "0b") { | ||
return value[2:], 2 | ||
} else if strings.HasPrefix(value, "0") && len(value) > 1 { | ||
return value[1:], 8 | ||
} | ||
Comment on lines
+533
to
+535
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand the benefit of this case. Do you mind elaborating a bit on why you would want this? And what happens when you specify a value larger than the max? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My intention is to support different representations of values in different binaries, in my project I usually use hexadecimal identifiers for these numbers, and if I convert to decimal for these values, it would be a problem to read the code. Of course, I could add comments to it and state the given value in the comments, but it feels like I'm adding to the problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0XX is the number written in the base of the eight. |
||
return value, 10 | ||
} | ||
|
||
// sanitizeValue will ensure the value name generated adheres to golang's | ||
// identifier syntax as described here: https://golang.org/ref/spec#Identifiers | ||
// identifier = letter { letter | unicode_digit } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some values in here that contain non standard hex values (letters, specifically)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I support not only hexadecimal values, but also binary and octal values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, just make sure that 0x0f and 0xff parse correctly into the right values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already added