Clarify JS cstring len (#14184)

This commit is contained in:
hlaaftana 2020-05-05 11:34:37 +03:00 • committed by GitHub
commit 6b7b5fb4fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 9 deletions

View file

@ -692,6 +692,11 @@ proc len*(x: string): int {.magic: "LengthStr", noSideEffect.}
proc len*(x: cstring): int {.magic: "LengthStr", noSideEffect.}
## Returns the length of a compatible string. This is sometimes
## 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
## var str: cstring = "Hello world!"
@ -2137,16 +2142,12 @@ when notJSnotNims:
when not defined(js):
proc cmp(x, y: string): int =
when defined(nimscript):
when nimvm:
if x < y: result = -1
elif x > y: result = 1
else: result = 0
else:
when nimvm:
if x < y: result = -1
elif x > y: result = 1
else: result = 0
else:
when not defined(nimscript): # avoid semantic checking
let minlen = min(x.len, y.len)
result = int(nimCmpMem(x.cstring, y.cstring, cast[csize_t](minlen)))
if result == 0: