Skip to content
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
32 changes: 32 additions & 0 deletions pkg/yqlib/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,38 @@ var jsonScenarios = []formatScenario{
expected: "{\n \"cat\": \"meow\"\n}\n",
scenarioType: "encode",
},
{
description: "Encode json: signed hex int",
skipDoc: true,
input: `+0x12`,
indent: 0,
expected: "18\n",
scenarioType: "encode",
},
{
description: "Encode json: negative hex int",
skipDoc: true,
input: `-0x12`,
indent: 0,
expected: "-18\n",
scenarioType: "encode",
},
{
description: "Encode json: signed octal int",
skipDoc: true,
input: `+0o22`,
indent: 0,
expected: "18\n",
scenarioType: "encode",
},
{
description: "Encode json: negative octal int",
skipDoc: true,
input: `-0o22`,
indent: 0,
expected: "-18\n",
scenarioType: "encode",
},
{
description: "Encode json: simple - in one line",
input: `cat: meow # this is a comment, and it will be dropped.`,
Expand Down
19 changes: 14 additions & 5 deletions pkg/yqlib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,21 @@ func parseInt64(numberString string) (string, int64, error) {
numberString = strings.ReplaceAll(numberString, "_", "")
}

if strings.HasPrefix(numberString, "0x") ||
strings.HasPrefix(numberString, "0X") {
num, err := strconv.ParseInt(numberString[2:], 16, 64)
// A leading +/- sign would hide the 0x/0o prefix below, so peel it off and
// hand it back to ParseInt with the digits.
sign := ""
digits := numberString
if len(digits) > 0 && (digits[0] == '+' || digits[0] == '-') {
sign = digits[:1]
digits = digits[1:]
}

if strings.HasPrefix(digits, "0x") ||
strings.HasPrefix(digits, "0X") {
num, err := strconv.ParseInt(sign+digits[2:], 16, 64)
return "0x%X", num, err
} else if strings.HasPrefix(numberString, "0o") {
num, err := strconv.ParseInt(numberString[2:], 8, 64)
} else if strings.HasPrefix(digits, "0o") {
num, err := strconv.ParseInt(sign+digits[2:], 8, 64)
return "0o%o", num, err
}
num, err := strconv.ParseInt(numberString, 10, 64)
Expand Down
10 changes: 10 additions & 0 deletions pkg/yqlib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ var parseInt64Scenarios = []parseInt64Scenario{
numberString: "0o10",
expectedParsedNumber: 8,
},
{
numberString: "+0x12",
expectedParsedNumber: 18,
expectedFormatString: "0x12",
},
{
numberString: "+0o22",
expectedParsedNumber: 18,
expectedFormatString: "0o22",
},
}

func TestParseInt64(t *testing.T) {
Expand Down