mirror of https://github.com/status-im/NimYAML.git
Code cleanup per comments
This commit is contained in:
parent
2c470c8aa9
commit
6f8dfdcd00
|
@ -64,35 +64,29 @@ proc representObject*(value: string, ts: TagStyle,
|
|||
|
||||
proc parseHex[T: int8|int16|int32|int64|uint8|uint16|uint32|uint64](s: string): T =
|
||||
result = 0
|
||||
var i = 2
|
||||
while true:
|
||||
for i in 2..<s.len:
|
||||
case s[i]
|
||||
of '_': discard
|
||||
of '0'..'9': result = result shl 4 or (T(ord(s[i]) - ord('0')) and 0xFF)
|
||||
of 'a'..'f': result = result shl 4 or (T(ord(s[i]) - ord('a') + 10) and 0xFF)
|
||||
of 'A'..'F': result = result shl 4 or (T(ord(s[i]) - ord('A') + 10) and 0xFF)
|
||||
of '\0': break
|
||||
else: raise newException(ValueError, "Invalid character in hex: " & s[i])
|
||||
inc(i)
|
||||
of '0'..'9': result = result shl 4 or T(ord(s[i]) - ord('0'))
|
||||
of 'a'..'f': result = result shl 4 or T(ord(s[i]) - ord('a') + 10)
|
||||
of 'A'..'F': result = result shl 4 or T(ord(s[i]) - ord('A') + 10)
|
||||
else: raise newException(ValueError, "Invalid character in hex: " & escape("" & s[i]))
|
||||
|
||||
proc parseOctal[T: int8|int16|int32|int64|uint8|uint16|uint32|uint64](s: string): T =
|
||||
var i = 2
|
||||
while true:
|
||||
for i in 2..<s.len:
|
||||
case s[i]
|
||||
of '_': discard
|
||||
of '0'..'8': result = result * 8 + T((ord(s[i]) - ord('0')))
|
||||
of '\0': break
|
||||
else: raise newException(ValueError, "Invalid character in hex: " & s[i])
|
||||
inc(i)
|
||||
of '0'..'7': result = result shl 3 + T((ord(s[i]) - ord('0')))
|
||||
else: raise newException(ValueError, "Invalid character in hex: " & escape("" & s[i]))
|
||||
|
||||
proc constructObject*[T: int8|int16|int32|int64](
|
||||
s: var YamlStream, c: ConstructionContext, result: var T)
|
||||
{.raises: [YamlConstructionError, YamlStreamError].} =
|
||||
## constructs an integer value from a YAML scalar
|
||||
constructScalarItem(s, item, T):
|
||||
if item.scalarContent[0] == '0' and (item.scalarContent[1] == 'x' or item.scalarContent[1] == 'X'):
|
||||
if item.scalarContent[0] == '0' and item.scalarContent[1] in {'x', 'X' }:
|
||||
result = parseHex[T](item.scalarContent)
|
||||
elif item.scalarContent[0] == '0' and (item.scalarContent[1] == 'o' or item.scalarContent[1] == 'O'):
|
||||
elif item.scalarContent[0] == '0' and item.scalarContent[1] in {'o', 'O'}:
|
||||
result = parseOctal[T](item.scalarContent)
|
||||
else:
|
||||
result = T(parseBiggestInt(item.scalarContent))
|
||||
|
@ -127,9 +121,9 @@ proc representObject*(value: int, tagStyle: TagStyle,
|
|||
{.push overflowChecks: on.}
|
||||
proc parseBiggestUInt(s: string): uint64 =
|
||||
result = 0
|
||||
if s[0] == '0' and (s[1] == 'x' or s[1] == 'X'):
|
||||
if s[0] == '0' and s[1] in {'x', 'X'}:
|
||||
result = parseHex[uint64](s)
|
||||
elif s[0] == '0' and (s[1] == 'o' or s[1] == 'O'):
|
||||
elif s[0] == '0' and s[1] in {'o', 'O'}:
|
||||
result = parseOctal[uint64](s)
|
||||
else:
|
||||
for c in s:
|
||||
|
@ -806,7 +800,7 @@ proc load*[K](input: Stream | string, target: var K) =
|
|||
elif e.parent of YamlParserError: raise (ref YamlParserError)(e.parent)
|
||||
else: internalError("Unexpected exception: " & e.parent.repr)
|
||||
|
||||
proc loadMultiDoc*[K](input: Stream, target: var seq[K]) =
|
||||
proc loadMultiDoc*[K](input: Stream | string, target: var seq[K]) =
|
||||
if target.isNil:
|
||||
target = newSeq[K]()
|
||||
var
|
||||
|
|
|
@ -101,49 +101,49 @@ suite "Serialization":
|
|||
except: gotException = true
|
||||
assert gotException, "Expected exception, got none."
|
||||
|
||||
test "Serialization: Load Hex byte (0xFF)":
|
||||
test "Load Hex byte (0xFF)":
|
||||
let input = newStringStream("0xFF")
|
||||
var result: byte
|
||||
load(input, result)
|
||||
assert(result == 255)
|
||||
|
||||
test "Serialization: Load Hex byte (0xC)":
|
||||
test "Load Hex byte (0xC)":
|
||||
let input = newStringStream("0xC")
|
||||
var result: byte
|
||||
load(input, result)
|
||||
assert(result == 12)
|
||||
|
||||
test "Serialization: Load Octal byte (0o14)":
|
||||
test "Load Octal byte (0o14)":
|
||||
let input = newStringStream("0o14")
|
||||
var result: byte
|
||||
load(input, result)
|
||||
assert(result == 12)
|
||||
|
||||
test "Serialization: Load byte (14)":
|
||||
test "Load byte (14)":
|
||||
let input = newStringStream("14")
|
||||
var result: byte
|
||||
load(input, result)
|
||||
assert(result == 14)
|
||||
|
||||
test "Serialization: Load Hex int (0xFF)":
|
||||
test "Load Hex int (0xFF)":
|
||||
let input = newStringStream("0xFF")
|
||||
var result: int
|
||||
load(input, result)
|
||||
assert(result == 255)
|
||||
|
||||
test "Serialization: Load Hex int (0xC)":
|
||||
test "Load Hex int (0xC)":
|
||||
let input = newStringStream("0xC")
|
||||
var result: int
|
||||
load(input, result)
|
||||
assert(result == 12)
|
||||
|
||||
test "Serialization: Load Octal int (0o14)":
|
||||
test "Load Octal int (0o14)":
|
||||
let input = newStringStream("0o14")
|
||||
var result: int
|
||||
load(input, result)
|
||||
assert(result == 12)
|
||||
|
||||
test "Serialization: Load int (14)":
|
||||
test "Load int (14)":
|
||||
let input = newStringStream("14")
|
||||
var result: int
|
||||
load(input, result)
|
||||
|
@ -302,17 +302,19 @@ suite "Serialization":
|
|||
var output = dump(input, tsNone)
|
||||
assertStringEqual "%YAML 1.2\n--- \nstr: value\ni: 42\nb: y", output
|
||||
|
||||
test "Serialization: Load Multiple Documents":
|
||||
test "Load Multiple Documents":
|
||||
let input = newStringStream("1\n---\n2")
|
||||
var result: seq[int]
|
||||
loadMultiDoc(input, result)
|
||||
assert(result.len == 2)
|
||||
assert result[0] == 1
|
||||
assert result[1] == 2
|
||||
|
||||
test "Serialization: Load Multiple Documents":
|
||||
test "Load Multiple Documents (Single Doc)":
|
||||
let input = newStringStream("1")
|
||||
var result: seq[int]
|
||||
loadMultiDoc(input, result)
|
||||
assert(result.len == 1)
|
||||
assert result[0] == 1
|
||||
|
||||
test "Load custom object":
|
||||
|
@ -329,7 +331,7 @@ suite "Serialization":
|
|||
assertStringEqual(
|
||||
"%YAML 1.2\n--- \nfirstnamechar: P\nsurname: Pan\nage: 12", output)
|
||||
|
||||
test "Serialization: Load sequence with explicit tags":
|
||||
test "Load sequence with explicit tags":
|
||||
let input = newStringStream("--- !nim:system:seq(" &
|
||||
"tag:yaml.org,2002:str)\n- !!str one\n- !!str two")
|
||||
var result: seq[string]
|
||||
|
|
Loading…
Reference in New Issue