Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add strbasics.strip #16280

Merged
merged 26 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

- `strscans.scanf` now supports parsing single characters.

- Added `strutils.stripInplace`.
ringabout marked this conversation as resolved.
Show resolved Hide resolved


## Language changes

Expand Down
53 changes: 53 additions & 0 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2813,6 +2813,58 @@ proc format*(formatstr: string, a: varargs[string, `$`]): string {.noSideEffect,
result = newStringOfCap(formatstr.len + a.len)
addf(result, formatstr, a)

proc stripInplace*(s: var string, leading = true, trailing = true,
chars: set[char] = Whitespace) {.since: (1, 5, 1).} =
## Inplace version of `strip`. Strips leading or
## trailing `chars` (default: whitespace characters).
##
## If `leading` is true (default), leading `chars` are stripped.
## If `trailing` is true (default), trailing `chars` are stripped.
## If both are false, the string is unchanged.
##
## See also:
## * `strip proc<#strip,string,set[char]>`_
runnableExamples:
var a = " vhellov "
stripInplace(a)
doAssert a == "vhellov"

a = " vhellov "
a.stripInplace(leading = false)
doAssert a == " vhellov"

a = " vhellov "
a.stripInplace(trailing = false)
doAssert a == "vhellov "

var c = "blaXbla"
c.stripInplace(chars = {'b', 'a'})
doAssert c == "laXbl"
c = "blaXbla"
c.stripInplace(chars = {'b', 'a', 'l'})
doAssert c == "X"

var
first = 0
last = high(s)
if leading:
while first <= last and s[first] in chars: inc(first)
if trailing:
while last >= 0 and s[last] in chars: dec(last)

if first > 0:
when nimvm:
for index in first .. last:
ringabout marked this conversation as resolved.
Show resolved Hide resolved
s[index - first] = s[index]
else:
# not JS and not Nimscript
when not declared(moveMem):
for index in first .. last:
s[index - first] = s[index]
else:
moveMem(addr s[0], addr s[first], last - first + 1)

s.setLen(last - first + 1)

proc strip*(s: string, leading = true, trailing = true,
chars: set[char] = Whitespace): string
Expand All @@ -2825,6 +2877,7 @@ proc strip*(s: string, leading = true, trailing = true,
## If both are false, the string is returned unchanged.
##
## See also:
## * `stripInplace proc<#stripInplace,string,set[char]>`_
ringabout marked this conversation as resolved.
Show resolved Hide resolved
## * `stripLineEnd proc<#stripLineEnd,string>`_
runnableExamples:
let a = " vhellov "
Expand Down
57 changes: 57 additions & 0 deletions lib/std/stropt.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import std/strutils
ringabout marked this conversation as resolved.
Show resolved Hide resolved
import std/private/since


proc stripInplace*(s: var string, leading = true, trailing = true,
chars: set[char] = Whitespace) {.since: (1, 5, 1).} =
## Inplace version of `strip`. Strips leading or
## trailing `chars` (default: whitespace characters).
##
## If `leading` is true (default), leading `chars` are stripped.
## If `trailing` is true (default), trailing `chars` are stripped.
## If both are false, the string is unchanged.
##
## See also:
## * `strip proc<#strip,string,set[char]>`_
runnableExamples:
var a = " vhellov "
stripInplace(a)
doAssert a == "vhellov"

a = " vhellov "
a.stripInplace(leading = false)
doAssert a == " vhellov"

a = " vhellov "
a.stripInplace(trailing = false)
doAssert a == "vhellov "

var c = "blaXbla"
c.stripInplace(chars = {'b', 'a'})
doAssert c == "laXbl"
c = "blaXbla"
c.stripInplace(chars = {'b', 'a', 'l'})
doAssert c == "X"

template impl =
for index in first .. last:
s[index - first] = s[index]

var
first = 0
last = high(s)
if leading:
while first <= last and s[first] in chars: inc(first)
if trailing:
while last >= 0 and s[last] in chars: dec(last)
ringabout marked this conversation as resolved.
Show resolved Hide resolved

if first > 0:
when nimvm: impl()
else:
# not JS and not Nimscript
when not declared(moveMem):
impl()
else:
moveMem(addr s[0], addr s[first], last - first + 1)

s.setLen(last - first + 1)
38 changes: 38 additions & 0 deletions tests/stdlib/tstropt.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
discard """
targets: "c cpp js"
"""

import std/stropt


proc testStripInplace() =
var a = " vhellov "
ringabout marked this conversation as resolved.
Show resolved Hide resolved
stripInplace(a)
doAssert a == "vhellov"

a = " vhellov "
a.stripInplace(leading = false)
doAssert a == " vhellov"

a = " vhellov "
a.stripInplace(trailing = false)
doAssert a == "vhellov "

a.stripInplace()
a.stripInplace(chars = {'v'})
doAssert a == "hello"

a = " vhellov "
a.stripInplace()
a.stripInplace(leading = false, chars = {'v'})
doAssert a == "vhello"

var c = "blaXbla"
c.stripInplace(chars = {'b', 'a'})
doAssert c == "laXbl"
c = "blaXbla"
c.stripInplace(chars = {'b', 'a', 'l'})
doAssert c == "X"

static: testStripInplace()
testStripInplace()