mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-05 20:47:53 +00:00
add strutils.stripLineEnd (#9346)
This commit is contained in:
committed by
Andreas Rumpf
parent
3c9fcc4c30
commit
e4c76f8a2a
@@ -2392,6 +2392,29 @@ proc removePrefix*(s: var string, prefix: string) {.
|
||||
if s.startsWith(prefix):
|
||||
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:
|
||||
proc nonStaticTests =
|
||||
doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000"
|
||||
|
||||
Reference in New Issue
Block a user