Skip to content

Commit 10355cb

Browse files
authored
strutils, rstgen: avoid deprecated strutils.delete (#20488)
The strutils `delete` func with signature func delete*(s: var string, first, last: int) was deprecated in adba5eb, in favor of one with signature func delete*(s: var string, slice: Slice[int]) However, a few procedures still used the deprecated form. This commit updates them, resolving these deprecation warnings: rstgen.nim(766, 12) Warning: use `delete(s, first..last)`; delete is deprecated [Deprecated] strutils.nim(1651, 19) Warning: use `delete(s, first..last)`; delete is deprecated [Deprecated] strutils.nim(1679, 7) Warning: use `delete(s, first..last)`; delete is deprecated [Deprecated] strutils.nim(2472, 7) Warning: use `delete(s, first..last)`; delete is deprecated [Deprecated] Before this commit: - `trimZeros` called `s.delete(i+1, i)` for an input that lacks a trailing zero (like "1.23"). - `removePrefix*(s: var string, prefix: string)` called `s.delete(0, -1)` when the prefix was the empty string. which did not modify `s`, nor raise an error. But the newer slice `delete` raises an `IndexDefect` when the start of the slice is greater than the end, so we avoid calling the new `delete` for such a case. Recall that exceptions inheriting from `system.Defect` are not tracked with the `.raises: []` exception tracking mechanism [1], so this commit does not break existing code like: proc foo {.raises: [].} = var s = "abc1.20" s.removePrefix("abc") s.trimZeros() doAssert s == "1.2" The `strutils.delete` deprecation was motivated by a problem with `system.delete` [2][3]: `system.delete` had surprising behavior when the index passed to it was out of bounds (it would delete the last entry then). Compile with `-d:nimStrictDelete` so that an index error is produced instead. Be aware however that your code might depend on this quirky behavior so a review process is required on your part before you can use `-d:nimStrictDelete`. To make this review easier, use the `-d:nimAuditDelete` switch, which pretends that `system.delete` is deprecated so that it is easier to see where it was used in your code. `-d:nimStrictDelete` will become the default in upcoming versions. A similar deprecation happened with `sequtils.delete` [4], but that deprecated form is already not used in this repo. [1] https://github.com/nim-lang/Nim/blob/2dec69fe5aa6/doc/manual.md#exception-tracking [2] https://github.com/nim-lang/Nim/blob/2dec69fe5aa6/changelogs/changelog_1_6_0.md#system [3] 92cb76571432 [4] 1d6863a7899f
1 parent 28c879c commit 10355cb

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

lib/packages/docutils/rstgen.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ proc stripTocHtml(s: string): string =
763763
if last < 0:
764764
# Abort, since we didn't found a closing angled bracket.
765765
return
766-
result.delete(first, last)
766+
result.delete(first..last)
767767
first = result.find('<', first)
768768

769769
proc renderHeadline(d: PDoc, n: PRstNode, result: var string) =

lib/pure/strutils.nim

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ func removePrefix*(s: var string, chars: set[char] = Newlines) {.rtl,
16481648

16491649
var start = 0
16501650
while start < s.len and s[start] in chars: start += 1
1651-
if start > 0: s.delete(0, start - 1)
1651+
if start > 0: s.delete(0..start - 1)
16521652

16531653
func removePrefix*(s: var string, c: char) {.rtl,
16541654
extern: "nsuRemovePrefixChar".} =
@@ -1675,8 +1675,8 @@ func removePrefix*(s: var string, prefix: string) {.rtl,
16751675
var answers = "yesyes"
16761676
answers.removePrefix("yes")
16771677
doAssert answers == "yes"
1678-
if s.startsWith(prefix):
1679-
s.delete(0, prefix.len - 1)
1678+
if s.startsWith(prefix) and prefix.len > 0:
1679+
s.delete(0..prefix.len - 1)
16801680

16811681
func removeSuffix*(s: var string, chars: set[char] = Newlines) {.rtl,
16821682
extern: "nsuRemoveSuffixCharSet".} =
@@ -2469,7 +2469,8 @@ func trimZeros*(x: var string; decimalSep = '.') =
24692469
var pos = last
24702470
while pos >= 0 and x[pos] == '0': dec(pos)
24712471
if pos > sPos: inc(pos)
2472-
x.delete(pos, last)
2472+
if last >= pos:
2473+
x.delete(pos..last)
24732474

24742475
type
24752476
BinaryPrefixMode* = enum ## The different names for binary prefixes.

tests/stdlib/tstrutils.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ template main() =
433433
x = "1e0"
434434
x.trimZeros()
435435
doAssert x == "1e0"
436+
x = "1.23"
437+
x.trimZeros()
438+
doAssert x == "1.23"
436439

437440
block: # countLines
438441
proc assertCountLines(s: string) = doAssert s.countLines == s.splitLines.len

0 commit comments

Comments
 (0)