Clarify JS cstring len (#14184)
This commit is contained in:
parent
eefada8a88
commit
6b7b5fb4fa
3 changed files with 10 additions and 9 deletions
|
|
@ -82,7 +82,7 @@ available. This includes:
|
||||||
* proper 64 bit integer arithmetic
|
* proper 64 bit integer arithmetic
|
||||||
|
|
||||||
To compensate, the standard library has modules `catered to the JS backend
|
To compensate, the standard library has modules `catered to the JS backend
|
||||||
<https://nim-lang.org/docs/lib.html#pure-libraries-modules-for-js-backend>`_
|
<lib.html#pure-libraries-modules-for-js-backend>`_
|
||||||
and more support will come in the future (for instance, Node.js bindings
|
and more support will come in the future (for instance, Node.js bindings
|
||||||
to get OS info).
|
to get OS info).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ proc cmpIgnoreStyle*(a, b: cstring): int {.noSideEffect,
|
||||||
## | > 0 if a > b
|
## | > 0 if a > b
|
||||||
##
|
##
|
||||||
## Not supported for JS backend, use `strutils.cmpIgnoreStyle
|
## Not supported for JS backend, use `strutils.cmpIgnoreStyle
|
||||||
## <https://nim-lang.org/docs/strutils.html#cmpIgnoreStyle%2Cstring%2Cstring>`_ instead.
|
## <strutils.html#cmpIgnoreStyle%2Cstring%2Cstring>`_ instead.
|
||||||
var i = 0
|
var i = 0
|
||||||
var j = 0
|
var j = 0
|
||||||
while true:
|
while true:
|
||||||
|
|
@ -91,7 +91,7 @@ proc cmpIgnoreCase*(a, b: cstring): int {.noSideEffect,
|
||||||
## | > 0 if a > b
|
## | > 0 if a > b
|
||||||
##
|
##
|
||||||
## Not supported for JS backend, use `strutils.cmpIgnoreCase
|
## Not supported for JS backend, use `strutils.cmpIgnoreCase
|
||||||
## <https://nim-lang.org/docs/strutils.html#cmpIgnoreCase%2Cstring%2Cstring>`_ instead.
|
## <strutils.html#cmpIgnoreCase%2Cstring%2Cstring>`_ instead.
|
||||||
var i = 0
|
var i = 0
|
||||||
while true:
|
while true:
|
||||||
var aa = toLowerAscii(a[i])
|
var aa = toLowerAscii(a[i])
|
||||||
|
|
|
||||||
|
|
@ -692,6 +692,11 @@ proc len*(x: string): int {.magic: "LengthStr", noSideEffect.}
|
||||||
proc len*(x: cstring): int {.magic: "LengthStr", noSideEffect.}
|
proc len*(x: cstring): int {.magic: "LengthStr", noSideEffect.}
|
||||||
## Returns the length of a compatible string. This is sometimes
|
## Returns the length of a compatible string. This is sometimes
|
||||||
## an O(n) operation.
|
## an O(n) operation.
|
||||||
|
##
|
||||||
|
## **Note:** On the JS backend this currently counts UTF-16 code points
|
||||||
|
## instead of bytes at runtime (not at compile time). For now, if you
|
||||||
|
## need the byte length of the UTF-8 encoding, convert to string with
|
||||||
|
## `$` first then call `len`.
|
||||||
##
|
##
|
||||||
## .. code-block:: Nim
|
## .. code-block:: Nim
|
||||||
## var str: cstring = "Hello world!"
|
## var str: cstring = "Hello world!"
|
||||||
|
|
@ -2137,16 +2142,12 @@ when notJSnotNims:
|
||||||
|
|
||||||
when not defined(js):
|
when not defined(js):
|
||||||
proc cmp(x, y: string): int =
|
proc cmp(x, y: string): int =
|
||||||
when defined(nimscript):
|
when nimvm:
|
||||||
if x < y: result = -1
|
if x < y: result = -1
|
||||||
elif x > y: result = 1
|
elif x > y: result = 1
|
||||||
else: result = 0
|
else: result = 0
|
||||||
else:
|
else:
|
||||||
when nimvm:
|
when not defined(nimscript): # avoid semantic checking
|
||||||
if x < y: result = -1
|
|
||||||
elif x > y: result = 1
|
|
||||||
else: result = 0
|
|
||||||
else:
|
|
||||||
let minlen = min(x.len, y.len)
|
let minlen = min(x.len, y.len)
|
||||||
result = int(nimCmpMem(x.cstring, y.cstring, cast[csize_t](minlen)))
|
result = int(nimCmpMem(x.cstring, y.cstring, cast[csize_t](minlen)))
|
||||||
if result == 0:
|
if result == 0:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue