Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong behavior of "runeSubstr" from module "unicode". #17768

Closed
lscrd opened this issue Apr 18, 2021 · 1 comment · Fixed by #18317
Closed

Wrong behavior of "runeSubstr" from module "unicode". #17768

lscrd opened this issue Apr 18, 2021 · 1 comment · Fixed by #18317

Comments

@lscrd
Copy link

lscrd commented Apr 18, 2021

Proc "runeSubstr" from module "unicode" is broken for negative lengths.

Example

import unicode

let s1 = "abcdef"
let s2 = "abcdéf"

echo s1.runeSubstr(0, -1)
echo s2.runeSubstr(0, -1)

Current Output

abcd
abcd�

Expected Output

abcde
abcde

Possible Solution

Replacing the negative indexes with the following works:

echo s1.runeSubstr(0, s1.runeLen - 1)
echo s2.runeSubstr(0, s2.runeLen - 1)

Additional Information

$ nim -v
Nim Compiler Version 1.4.6 [Linux: amd64]
...

Issue also exists in development version.

Araq added a commit that referenced this issue Apr 20, 2021
@ringabout ringabout assigned ringabout and unassigned ringabout Apr 23, 2021
@ringabout
Copy link
Member

ringabout commented Apr 23, 2021

runeSubStr is very fragile for negative number, confusing and hard to use(or fix).

A worse example:

import std/unicode

let s4 = "白头如新倾盖如故"
echo s4.runeSubStr(-10, 3)
/usercode/in.nim(4)      in
/playground/nim/lib/pure/unicode.nim(414) runeSubStr
/playground/nim/lib/system/fatal.nim(49) sysFatal
Error: unhandled exception: value out of range: -2 notin 0 .. 9223372036854775807 [RangeDefect]

Not to say

import std/unicode

let s4 = "白头如新倾盖如故"
doAssert s4.runeSubStr(-10, 10) == "白头如新倾盖如故"

works.

Well as documentation said, it is meant to work. But it is really terrible and unnatural.

import std/unicode

let s4 = "白头如新倾盖如故"
echo s4.runeSubStr(-10, -1)

This doesn't work

/usercode/in.nim(4)      in
/playground/nim/lib/pure/unicode.nim(412) runeSubStr
/playground/nim/lib/system/fatal.nim(49) sysFatal
Error: unhandled exception: value out of range: -2 notin 0 .. 9223372036854775807 [RangeDefect]

@ringabout ringabout self-assigned this Jun 21, 2021
ringabout added a commit to ringabout/Nim that referenced this issue Jun 21, 2021
ringabout added a commit to ringabout/Nim that referenced this issue Jun 21, 2021
@ringabout ringabout removed their assignment Jun 21, 2021
Araq pushed a commit that referenced this issue Jun 21, 2021
* fixes #17768 [backport:1.4]

* tiny
narimiran pushed a commit that referenced this issue Aug 24, 2021
* fixes #17768 [backport:1.4]

* tiny

(cherry picked from commit 2deb701)
PMunch pushed a commit to PMunch/Nim that referenced this issue Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants