diff options
author | bptato <nincsnevem662@gmail.com> | 2023-07-07 19:12:31 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-07-07 19:12:31 +0200 |
commit | 45f58316b4da12e893856e47a4296ee09c49449e (patch) | |
tree | 20ae54604051d9a7c2250b24cf6df9a802214a55 | |
parent | da3c7385d1e19ba97aee0b6220d26994eef985e8 (diff) | |
download | chawan-45f58316b4da12e893856e47a4296ee09c49449e.tar.gz |
Fix toHex weirdness
-rw-r--r-- | src/types/color.nim | 9 | ||||
-rw-r--r-- | src/utils/twtstr.nim | 13 |
2 files changed, 11 insertions, 11 deletions
diff --git a/src/types/color.nim b/src/types/color.nim index d3526a43..e628b9ad 100644 --- a/src/types/color.nim +++ b/src/types/color.nim @@ -251,10 +251,11 @@ func a*(c: RGBAColorPremul): uint8 {.borrow.} # https://html.spec.whatwg.org/#serialisation-of-a-color func serialize*(color: RGBAColor): string = if color.a == 255: - let r = toHex(cast[uint8](color.r)) - let g = toHex(cast[uint8](color.g)) - let b = toHex(cast[uint8](color.b)) - return "#" & r & g & b + var res = "#" + res.pushHex(color.r) + res.pushHex(color.g) + res.pushHex(color.b) + return res let a = float64(color.a) / 255 return "rgba(" & $color.r & ", " & $color.g & ", " & $color.b & ", " & $a & ")" diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index a32cc8da..df67b8f3 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -174,10 +174,9 @@ func isAscii*(s: string): bool = const HexCharsUpper = "0123456789ABCDEF" const HexCharsLower = "0123456789abcdef" -func toHex*(c: char): string = - result = newString(2) - result[0] = HexCharsUpper[(uint8(c) shr 4)] - result[1] = HexCharsLower[(uint8(c) and 0xF)] +func pushHex*(buf: var string, c: char) = + buf &= HexCharsUpper[(uint8(c) shr 4)] + buf &= HexCharsUpper[(uint8(c) and 0xF)] func toHexLower*(u: uint16): string = var x = u @@ -195,8 +194,8 @@ func toHexLower*(u: uint16): string = x = x shr 4 return s -func toHex*(i: uint8): string = - return toHex(cast[char](i)) +func pushHex*(buf: var string, i: uint8) = + buf.pushHex(cast[char](i)) func equalsIgnoreCase*(s1: seq[Rune], s2: string): bool = var i = 0 @@ -558,7 +557,7 @@ proc percentEncode*(append: var string, c: char, set: set[char], spaceAsPlus = f append &= c else: append &= '%' - append &= c.toHex() + append.pushHex(c) proc percentEncode*(append: var string, s: string, set: set[char], spaceAsPlus = false) {.inline.} = for c in s: |