toXXAscii use xor op, saving 30%~50% time (#16193)

* toXXAscii use xor op, saving 30%~50% time

* Update lib/pure/strutils.nim

Co-authored-by: hlaaftana <10591326+hlaaftana@users.noreply.github.com>

* Update lib/pure/strutils.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: hlaaftana <10591326+hlaaftana@users.noreply.github.com>
This commit is contained in:
Bung 2020-12-01 01:24:12 +08:00 • committed by GitHub
commit 5a43a20f53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -209,7 +209,7 @@ proc toLowerAscii*(c: char): char {.noSideEffect,
doAssert toLowerAscii('A') == 'a' doAssert toLowerAscii('A') == 'a'
doAssert toLowerAscii('e') == 'e' doAssert toLowerAscii('e') == 'e'
if c in {'A'..'Z'}: if c in {'A'..'Z'}:
result = chr(ord(c) + (ord('a') - ord('A'))) result = char(uint8(c) xor 0b0010_0000'u8)
else: else:
result = c result = c
@ -248,7 +248,7 @@ proc toUpperAscii*(c: char): char {.noSideEffect,
doAssert toUpperAscii('a') == 'A' doAssert toUpperAscii('a') == 'A'
doAssert toUpperAscii('E') == 'E' doAssert toUpperAscii('E') == 'E'
if c in {'a'..'z'}: if c in {'a'..'z'}:
result = chr(ord(c) - (ord('a') - ord('A'))) result = char(uint8(c) xor 0b0010_0000'u8)
else: else:
result = c result = c