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 13 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
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
and `lists.toDoublyLinkedList` convert from `openArray`s; `lists.copy` implements
shallow copying; `lists.add` concatenates two lists - an O(1) variation that consumes
its argument, `addMoved`, is also supplied.

- Added `stropt.strip`.


## Language changes

Expand Down
1 change: 1 addition & 0 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2791,6 +2791,7 @@ func strip*(s: string, leading = true, trailing = true,
## If both are false, the string is returned unchanged.
##
## See also:
## * `strip proc<stropt.html#strip,string,set[char]>`_ Inplace version.
## * `stripLineEnd func<#stripLineEnd,string>`_
runnableExamples:
let a = " vhellov "
Expand Down
59 changes: 59 additions & 0 deletions lib/std/stropt.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
#
# The Nim Compiler
# (c) Copyright 2020 Nim Contributors
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#

proc strip*(s: var string, leading = true, trailing = true,
ringabout marked this conversation as resolved.
Show resolved Hide resolved
chars: set[char] = {' ', '\t', '\v', '\r', '\l', '\f'}) =
ringabout marked this conversation as resolved.
Show resolved Hide resolved
## 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.
runnableExamples:
var a = " vhellov "
strip(a)
doAssert a == "vhellov"

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

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

var c = "blaXbla"
c.strip(chars = {'b', 'a'})
doAssert c == "laXbl"
c = "blaXbla"
c.strip(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 teststrip() =
var a = " vhellov "
ringabout marked this conversation as resolved.
Show resolved Hide resolved
strip(a)
doAssert a == "vhellov"

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

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

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

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

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

static: teststrip()
teststrip()