diff options
author | bptato <nincsnevem662@gmail.com> | 2023-11-20 17:20:41 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-11-20 17:20:41 +0100 |
commit | 342a1a7f787fc260448842ee312cf29825f96ba6 (patch) | |
tree | e4e1d28e424e60da8b2618195c40442b79bd2ba8 | |
parent | d20fc30e10aeecfb2ede7adc4547b9ae394565b9 (diff) | |
download | chawan-342a1a7f787fc260448842ee312cf29825f96ba6.tar.gz |
twtstr: remove tolower, isWhitespace
* tolower: strutils toLowerAscii is good enough for the cases where we need it. Also, it's easy to confuse with unicode toLower and vice versa. * isWhitespace: in AsciiWhitespace is more idiomatic. Also has a naming collision with unicode toLower.
-rw-r--r-- | src/config/mailcap.nim | 6 | ||||
-rw-r--r-- | src/config/mimetypes.nim | 5 | ||||
-rw-r--r-- | src/css/cssparser.nim | 11 | ||||
-rw-r--r-- | src/html/dom.nim | 2 | ||||
-rw-r--r-- | src/types/blob.nim | 3 | ||||
-rw-r--r-- | src/types/urimethodmap.nim | 2 | ||||
-rw-r--r-- | src/types/url.nim | 6 | ||||
-rw-r--r-- | src/utils/twtstr.nim | 22 |
8 files changed, 23 insertions, 34 deletions
diff --git a/src/config/mailcap.nim b/src/config/mailcap.nim index 4370f775..a53ce24a 100644 --- a/src/config/mailcap.nim +++ b/src/config/mailcap.nim @@ -80,7 +80,7 @@ proc consumeTypeField(state: var MailcapParser): Result[string, string] = break if c notin AsciiAlphaNumeric + {'-', '*'}: return err("Invalid character encountered in type field") - s &= c.tolower() + s &= c.toLowerAscii() if not state.has(): return err("Missing subtype") # subtype @@ -91,7 +91,7 @@ proc consumeTypeField(state: var MailcapParser): Result[string, string] = break if c notin AsciiAlphaNumeric + {'-', '.', '*', '_', '+'}: return err("Invalid character encountered in subtype field") - s &= c.tolower() + s &= c.toLowerAscii() var c: char if not state.skipBlanks(c) or c != ';': return err("Semicolon not found") @@ -245,7 +245,7 @@ proc unquoteCommand*(ecmd, contentType, outpath: string, url: URL, cmd &= c state = STATE_NORMAL of STATE_ATTR_QUOTED: - attrname &= c.tolower() + attrname &= c.toLowerAscii() state = STATE_ATTR of STATE_NORMAL, STATE_DOLLAR: let prev_dollar = state == STATE_DOLLAR diff --git a/src/config/mimetypes.nim b/src/config/mimetypes.nim index 2133d863..577fcbed 100644 --- a/src/config/mimetypes.nim +++ b/src/config/mimetypes.nim @@ -1,4 +1,5 @@ import streams +import strutils import tables import utils/twtstr @@ -18,7 +19,7 @@ proc parseMimeTypes*(mimeTypes: var MimeTypes, stream: Stream) = var t = "" var i = 0 while i < line.len and line[i] notin AsciiWhitespace: - t &= line[i].tolower() + t &= line[i].toLowerAscii() inc i if t == "": continue while i < line.len: @@ -26,7 +27,7 @@ proc parseMimeTypes*(mimeTypes: var MimeTypes, stream: Stream) = inc i var ext = "" while i < line.len and line[i] notin AsciiWhitespace: - ext &= line[i].tolower() + ext &= line[i].toLowerAscii() inc i if ext == "": continue discard mimeTypes.hasKeyOrPut(ext, t) diff --git a/src/css/cssparser.nim b/src/css/cssparser.nim index ea337ab1..a8081ecc 100644 --- a/src/css/cssparser.nim +++ b/src/css/cssparser.nim @@ -248,7 +248,7 @@ proc consumeEscape(state: var CSSTokenizerState): string = num *= 0x10 num += hexValue(c) inc i - if state.peek().isWhitespace(): + if state.peek() in AsciiWhitespace: discard state.consume() if num == 0 or num > 0x10FFFF or num in 0xD800..0xDFFF: return $Rune(0xFFFD) @@ -347,7 +347,7 @@ const NonPrintable = {char(0x00)..char(0x08), char(0x0B), char(0x0E)..char(0x1F) proc consumeURL(state: var CSSTokenizerState): CSSToken = result = CSSToken(tokenType: CSS_URL_TOKEN) - while state.has() and state.peek().isWhitespace(): + while state.has() and state.peek() in AsciiWhitespace: discard state.consume() while state.has(): @@ -359,7 +359,7 @@ proc consumeURL(state: var CSSTokenizerState): CSSToken = state.consumeBadURL() return CSSToken(tokenType: CSS_BAD_URL_TOKEN) of AsciiWhitespace: - while state.has() and state.peek().isWhitespace(): + while state.has() and state.peek() in AsciiWhitespace: discard state.consume() if not state.has(): return result @@ -382,7 +382,8 @@ proc consumeIdentLikeToken(state: var CSSTokenizerState): CSSToken = let s = state.consumeIdentSequence() if s.equalsIgnoreCase("url") and state.has() and state.peek() == '(': discard state.consume() - while state.has(1) and state.peek().isWhitespace() and state.peek(1).isWhitespace(): + while state.has(1) and state.peek() in AsciiWhitespace and + state.peek(1) in AsciiWhitespace: discard state.consume() if state.has() and state.peek() in {'"', '\''} or state.has(1) and state.peek() in {'"', '\''} + AsciiWhitespace and state.peek(1) in {'"', '\''}: @@ -413,7 +414,7 @@ proc consumeToken(state: var CSSTokenizerState): CSSToken = let c = state.consume() case c of AsciiWhitespace: - while state.has() and state.peek().isWhitespace(): + while state.has() and state.peek() in AsciiWhitespace: discard state.consume() return CSSToken(tokenType: CSS_WHITESPACE_TOKEN) of '"', '\'': diff --git a/src/html/dom.nim b/src/html/dom.nim index 8834f4e5..ec3304ab 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -2303,7 +2303,7 @@ func IDLAttributeToCSSProperty(s: string, dashPrefix = false): string = for c in s: if c in AsciiUpperAlpha: result &= '-' - result &= c.tolower() + result &= c.toLowerAscii() else: result &= c diff --git a/src/types/blob.nim b/src/types/blob.nim index b6ef4adf..9ddca2b5 100644 --- a/src/types/blob.nim +++ b/src/types/blob.nim @@ -1,4 +1,5 @@ import options +import strutils import js/dict import js/fromjs @@ -86,7 +87,7 @@ proc newWebFile(ctx: JSContext, fileBits: seq[string], fileName: string, for c in options.`type`: if c notin char(0x20)..char(0x7E): break ctype - file.ctype &= c.tolower() + file.ctype &= c.toLowerAscii() return file #TODO File, Blob constructors diff --git a/src/types/urimethodmap.nim b/src/types/urimethodmap.nim index 8453e554..283564a2 100644 --- a/src/types/urimethodmap.nim +++ b/src/types/urimethodmap.nim @@ -51,7 +51,7 @@ proc parseURIMethodMap*(this: var URIMethodMap, s: string) = var k = "" var i = 0 while i < line.len and line[i] notin AsciiWhitespace + {':'}: - k &= line[i].tolower() + k &= line[i].toLowerAscii() inc i if i >= line.len or line[i] != ':': continue # invalid diff --git a/src/types/url.nim b/src/types/url.nim index 69a3a78e..e115d54c 100644 --- a/src/types/url.nim +++ b/src/types/url.nim @@ -232,7 +232,7 @@ func endsInNumber(input: string): bool = if parts.len == 0: return false var last = parts[^1] if last != "": - if last.len == 2 and last[0] in Digits and last[1].tolower() == 'x': + if last.len == 2 and last[0] in Digits and last[1].toLowerAscii() == 'x': last = last.substr(2) for c in last: if c notin Digits: @@ -366,7 +366,7 @@ proc basicParseURL*(input: string, base = none(URL), url: URL = URL(), case state of SCHEME_START_STATE: if has and c.isAlphaAscii(): - buffer &= c.tolower() + buffer &= c.toLowerAscii() state = SCHEME_STATE elif not override: state = NO_SCHEME_STATE @@ -375,7 +375,7 @@ proc basicParseURL*(input: string, base = none(URL), url: URL = URL(), return none(URL) of SCHEME_STATE: if has and c in AsciiAlphaNumeric + {'+', '-', '.'}: - buffer &= c.tolower() + buffer &= c.toLowerAscii() elif has and c == ':': if override: if url.scheme in SpecialSchemes and buffer notin SpecialSchemes: diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index 327fae81..78c92883 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -28,12 +28,9 @@ const AsciiAlphaNumeric* = AsciiAlpha + AsciiDigit const AsciiHexDigit* = (AsciiDigit + {'a'..'f', 'A'..'F'}) const AsciiWhitespace* = {' ', '\n', '\r', '\t', '\f'} -func isWhitespace*(c: char): bool {.inline.} = - return c in AsciiWhitespace - func onlyWhitespace*(s: string): bool = for c in s: - if not c.isWhitespace(): + if c notin AsciiWhitespace: return false return true @@ -70,20 +67,9 @@ const controlLetterMap = genControlLetterMap() func getControlLetter*(c: char): char = return controlLetterMap[int(c)] -const lowerChars = (func(): array[char, char] = - for i in 0..255: - if char(i) in 'A'..'Z': - result[char(i)] = char(i + 32) - else: - result[char(i)] = char(i) -)() - -func tolower*(c: char): char = - return lowerChars[c] - proc mtoLowerAscii*(str: var string) = for i in 0 ..< str.len: - str[i] = str[i].tolower() + str[i] = str[i].toLowerAscii() func toHeaderCase*(str: string): string = result = str @@ -122,7 +108,7 @@ func startsWithNoCase*(str, prefix: string): bool = var i = 0 while true: if i == prefix.len: return true - if str[i].tolower() != prefix[i].tolower(): return false + if str[i].toLowerAscii() != prefix[i].toLowerAscii(): return false inc i const hexCharMap = (func(): array[char, int] = @@ -203,7 +189,7 @@ func stripAndCollapse*(s: string): string = func skipBlanks*(buf: string, at: int): int = result = at - while result < buf.len and buf[result].isWhitespace(): + while result < buf.len and buf[result] in AsciiWhitespace: inc result func until*(s: string, c: set[char], starti = 0): string = |