add strutils.stripLineEnd (#9346)
This commit is contained in:
parent
3c9fcc4c30
commit
e4c76f8a2a
1 changed files with 23 additions and 0 deletions
|
|
@ -2392,6 +2392,29 @@ proc removePrefix*(s: var string, prefix: string) {.
|
||||||
if s.startsWith(prefix):
|
if s.startsWith(prefix):
|
||||||
s.delete(0, prefix.len - 1)
|
s.delete(0, prefix.len - 1)
|
||||||
|
|
||||||
|
proc stripLineEnd*(s: var string) =
|
||||||
|
## Returns ``s`` stripped from one of these suffixes:
|
||||||
|
## ``\r, \n, \r\n, \f, \v`` (at most once instance).
|
||||||
|
## For example, can be useful in conjunction with ``osproc.execCmdEx``.
|
||||||
|
runnableExamples:
|
||||||
|
var s = "foo\n\n"
|
||||||
|
s.stripLineEnd
|
||||||
|
doAssert s == "foo\n"
|
||||||
|
s = "foo\r\n"
|
||||||
|
s.stripLineEnd
|
||||||
|
doAssert s == "foo"
|
||||||
|
if s.len > 0:
|
||||||
|
case s[^1]
|
||||||
|
of '\n':
|
||||||
|
if s.len > 1 and s[^2] == '\r':
|
||||||
|
s.setLen s.len-2
|
||||||
|
else:
|
||||||
|
s.setLen s.len-1
|
||||||
|
of '\r', '\v', '\f':
|
||||||
|
s.setLen s.len-1
|
||||||
|
else:
|
||||||
|
discard
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
proc nonStaticTests =
|
proc nonStaticTests =
|
||||||
doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000"
|
doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue