import strutils type HexQuantityStr* = distinct string HexDataStr* = distinct string # Hex validation template stripLeadingZeros(value: string): string = var cidx = 0 # ignore the last character so we retain '0' on zero value while cidx < value.len - 1 and value[cidx] == '0': cidx.inc value[cidx .. ^1] proc encodeQuantity*(value: SomeUnsignedInt): string = var hValue = value.toHex.stripLeadingZeros result = "0x" & hValue func hasHexHeader*(value: string): bool = if value != "" and value[0] == '0' and value[1] in {'x', 'X'} and value.len > 2: true else: false template hasHexHeader*(value: HexDataStr|HexQuantityStr): bool = value.string.hasHexHeader func isHexChar*(c: char): bool = if c notin {'0'..'9'} and c notin {'a'..'f'} and c notin {'A'..'F'}: false else: true proc validate*(value: HexQuantityStr): bool = template strVal: untyped = value.string if not value.hasHexHeader: return false # No leading zeros if strVal[2] == '0': return false for i in 2..