Skip to content

Commit

Permalink
fixes nim-lang#17768 [backport:1.4]
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Jun 21, 2021
1 parent a31e601 commit 6d25534
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 5 additions & 6 deletions lib/pure/unicode.nim
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ proc runeReverseOffset*(s: string, rev: Positive): (int, int) =
## from the end (starting with 1) and the total
## number of runes in the string.
##
## Returns a negative value for offset if there are to few runes in
## Returns a negative value for offset if there are too few runes in
## the string to satisfy the request.
##
## **Beware:** This can lead to unoptimized code and slow execution!
Expand All @@ -346,16 +346,15 @@ proc runeReverseOffset*(s: string, rev: Positive): (int, int) =
a = rev.int
o = 0
x = 0
let
times = 2*rev.int-s.runeLen # transformed from rev.int - a < s.runeLen - rev.int
while o < s.len:
let r = runeLenAt(s, o)
o += r
if a < 0:
if a > times:
x += r
dec a

if a > 0:
return (-a, rev.int-a)
return (x, -a+rev.int)
result = if a > 0: (-a, rev.int-a) else: (x, -a+rev.int)

proc runeAtPos*(s: string, pos: int): Rune =
## Returns the rune at position ``pos``.
Expand Down
9 changes: 8 additions & 1 deletion tests/stdlib/tunicode.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import unicode
import std/unicode


proc asRune(s: static[string]): Rune =
Expand Down Expand Up @@ -213,3 +213,10 @@ block differentSizes:
doAssert swapCase("ⱥbCd") == "ȺBcD"
doAssert swapCase("XyꟆaB") == "xYᶎAb"
doAssert swapCase("aᵹcᲈd") == "AꝽCꙊD"

block: # bug #17768
let s1 = "abcdef"
let s2 = "abcdéf"

doAssert s1.runeSubstr(0, -1) == "abcde"
doAssert s2.runeSubstr(0, -1) == "abcdé"

0 comments on commit 6d25534

Please sign in to comment.