diff options
| author | bptato <nincsnevem662@gmail.com> | 2025-03-29 19:00:30 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2025-03-29 19:10:39 +0100 |
| commit | 8429324c5a75e0be10251908c574c80c64c2c8e5 (patch) | |
| tree | 7e6f079b3382a3af8fd94d1e75c9fe2eb78d3882 /src | |
| parent | 7f4be1ea998aa6e5a1e73a3939b1989e5e8cf0c3 (diff) | |
| download | chawan-8429324c5a75e0be10251908c574c80c64c2c8e5.tar.gz | |
url, twtstr: misc cleanup
Diffstat (limited to 'src')
| -rw-r--r-- | src/types/url.nim | 8 | ||||
| -rw-r--r-- | src/utils/twtstr.nim | 36 |
2 files changed, 16 insertions, 28 deletions
diff --git a/src/types/url.nim b/src/types/url.nim index f99faca1..b13ea6b6 100644 --- a/src/types/url.nim +++ b/src/types/url.nim @@ -193,7 +193,7 @@ func parseIpv6(input: openArray[char]): Option[array[8, uint16]] = func parseIpv4Number(s: string): uint32 = var input = s - var R = 10 + var R = 10u32 if input.len >= 2 and input[0] == '0': if input[1] in {'x', 'X'}: input.delete(0..1) @@ -203,11 +203,7 @@ func parseIpv4Number(s: string): uint32 = R = 8 if input == "": return 0 - case R - of 8: return parseOctUInt32(input, allowSign = false).get(uint32.high) - of 10: return parseUInt32(input, allowSign = false).get(uint32.high) - of 16: return parseHexUInt32(input, allowSign = false).get(uint32.high) - else: return 0 + return parseUInt32Base(input, allowSign = false, R).get(uint32.high) func parseIpv4(input: string): Option[uint32] = var numbers: seq[uint32] = @[] diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index c6f7b35b..7b0cb4a4 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -17,10 +17,9 @@ const AsciiAlpha* = AsciiUpperAlpha + AsciiLowerAlpha const NonAscii* = {'\x80'..'\xFF'} const AsciiDigit* = {'0'..'9'} const AsciiAlphaNumeric* = AsciiAlpha + AsciiDigit -const AsciiOctDigit* = {'0'..'7'} const AsciiHexDigit* = AsciiDigit + {'a'..'f', 'A'..'F'} const AsciiWhitespace* = {' ', '\n', '\r', '\t', '\f'} -const HTTPWhitespace* = {'\n', '\r', '\t', ' '} +const HTTPWhitespace* = {' ', '\n', '\r', '\t'} func nextUTF8*(s: openArray[char]; i: var int): uint32 = let j = i @@ -306,8 +305,8 @@ func untilLast*(s: string; c: set[char]; n = 1): string = func untilLast*(s: string; c: char; n = 1): string = s.untilLast({c}, n) -proc c_sprintf(buf, fm: cstring): cint - {.header: "<stdio.h>", importc: "sprintf", varargs} +proc snprintf(str: cstring; size: csize_t; format: cstring): cint + {.header: "<stdio.h>", importc, varargs} # From w3m const SizeUnit = [ @@ -322,7 +321,8 @@ func convertSize*(size: int): string = inc sizepos result = newString(10) let f = floor(csize * 100 + 0.5) / 100 - discard c_sprintf(cstring(result), cstring("%.3g%s"), f, SizeUnit[sizepos]) + discard snprintf(cstring(result), csize_t(result.len), "%.3g%s", f, + SizeUnit[sizepos]) result.setLen(cstring(result).len) # https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#numbers @@ -348,14 +348,12 @@ func parseUInt8*(s: openArray[char]; allowSign = false): Option[uint8] = func parseUInt16*(s: openArray[char]; allowSign = false): Option[uint16] = return parseUIntImpl[uint16](s, allowSign, 10) -func parseUInt32*(s: openArray[char]; allowSign = false): Option[uint32] = - return parseUIntImpl[uint32](s, allowSign, 10) - -func parseOctUInt32*(s: openArray[char]; allowSign = false): Option[uint32] = - return parseUIntImpl[uint32](s, allowSign, 8) +func parseUInt32Base*(s: openArray[char]; allowSign = false; radix: uint32): + Option[uint32] = + return parseUIntImpl[uint32](s, allowSign, radix) -func parseHexUInt32*(s: openArray[char]; allowSign = false): Option[uint32] = - return parseUIntImpl[uint32](s, allowSign, 16) +func parseUInt32*(s: openArray[char]; allowSign = false): Option[uint32] = + return parseUInt32Base(s, allowSign, 10) func parseUInt64*(s: openArray[char]; allowSign = false): Option[uint64] = return parseUIntImpl[uint64](s, allowSign, 10) @@ -874,13 +872,7 @@ iterator mypairs*[T](a: openArray[T]): tuple[key: int; val: lent T] {.inline.} = {.pop.} proc getFileExt*(path: string): string = - var n = 0 - for i in countdown(path.high, 0): - if path[i] == '/': - break - if path[i] == '.': - n = i - break - if n > 0: - return path.substr(n + 1) - return "" + let n = path.rfind({'/', '.'}) + if n == -1 or path[n] != '.': + return "" + return path.substr(n + 1) |