strutils, rstgen: avoid deprecated strutils.delete (#20488)
The strutils `delete` func with signature
func delete*(s: var string, first, last: int)
was deprecated in adba5eb45e, 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] 2dec69fe5a/doc/manual.md (exception-tracking)
[2] 2dec69fe5a/changelogs/changelog_1_6_0.md (system)
[3] 92cb765714
[4] 1d6863a789
This commit is contained in:
parent
28c879c8bb
commit
10355cb48a
3 changed files with 9 additions and 5 deletions
|
|
@ -763,7 +763,7 @@ proc stripTocHtml(s: string): string =
|
||||||
if last < 0:
|
if last < 0:
|
||||||
# Abort, since we didn't found a closing angled bracket.
|
# Abort, since we didn't found a closing angled bracket.
|
||||||
return
|
return
|
||||||
result.delete(first, last)
|
result.delete(first..last)
|
||||||
first = result.find('<', first)
|
first = result.find('<', first)
|
||||||
|
|
||||||
proc renderHeadline(d: PDoc, n: PRstNode, result: var string) =
|
proc renderHeadline(d: PDoc, n: PRstNode, result: var string) =
|
||||||
|
|
|
||||||
|
|
@ -1648,7 +1648,7 @@ func removePrefix*(s: var string, chars: set[char] = Newlines) {.rtl,
|
||||||
|
|
||||||
var start = 0
|
var start = 0
|
||||||
while start < s.len and s[start] in chars: start += 1
|
while start < s.len and s[start] in chars: start += 1
|
||||||
if start > 0: s.delete(0, start - 1)
|
if start > 0: s.delete(0..start - 1)
|
||||||
|
|
||||||
func removePrefix*(s: var string, c: char) {.rtl,
|
func removePrefix*(s: var string, c: char) {.rtl,
|
||||||
extern: "nsuRemovePrefixChar".} =
|
extern: "nsuRemovePrefixChar".} =
|
||||||
|
|
@ -1675,8 +1675,8 @@ func removePrefix*(s: var string, prefix: string) {.rtl,
|
||||||
var answers = "yesyes"
|
var answers = "yesyes"
|
||||||
answers.removePrefix("yes")
|
answers.removePrefix("yes")
|
||||||
doAssert answers == "yes"
|
doAssert answers == "yes"
|
||||||
if s.startsWith(prefix):
|
if s.startsWith(prefix) and prefix.len > 0:
|
||||||
s.delete(0, prefix.len - 1)
|
s.delete(0..prefix.len - 1)
|
||||||
|
|
||||||
func removeSuffix*(s: var string, chars: set[char] = Newlines) {.rtl,
|
func removeSuffix*(s: var string, chars: set[char] = Newlines) {.rtl,
|
||||||
extern: "nsuRemoveSuffixCharSet".} =
|
extern: "nsuRemoveSuffixCharSet".} =
|
||||||
|
|
@ -2469,7 +2469,8 @@ func trimZeros*(x: var string; decimalSep = '.') =
|
||||||
var pos = last
|
var pos = last
|
||||||
while pos >= 0 and x[pos] == '0': dec(pos)
|
while pos >= 0 and x[pos] == '0': dec(pos)
|
||||||
if pos > sPos: inc(pos)
|
if pos > sPos: inc(pos)
|
||||||
x.delete(pos, last)
|
if last >= pos:
|
||||||
|
x.delete(pos..last)
|
||||||
|
|
||||||
type
|
type
|
||||||
BinaryPrefixMode* = enum ## The different names for binary prefixes.
|
BinaryPrefixMode* = enum ## The different names for binary prefixes.
|
||||||
|
|
|
||||||
|
|
@ -433,6 +433,9 @@ template main() =
|
||||||
x = "1e0"
|
x = "1e0"
|
||||||
x.trimZeros()
|
x.trimZeros()
|
||||||
doAssert x == "1e0"
|
doAssert x == "1e0"
|
||||||
|
x = "1.23"
|
||||||
|
x.trimZeros()
|
||||||
|
doAssert x == "1.23"
|
||||||
|
|
||||||
block: # countLines
|
block: # countLines
|
||||||
proc assertCountLines(s: string) = doAssert s.countLines == s.splitLines.len
|
proc assertCountLines(s: string) = doAssert s.countLines == s.splitLines.len
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue