mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-05 03:14:08 +00:00
add strutils.removePrefix proc (#6473)
This commit is contained in:
@@ -2345,8 +2345,7 @@ proc removeSuffix*(s: var string, chars: set[char] = Newlines) {.
|
||||
|
||||
proc removeSuffix*(s: var string, c: char) {.
|
||||
rtl, extern: "nsuRemoveSuffixChar".} =
|
||||
## Removes a single character (in-place) from a string.
|
||||
##
|
||||
## Removes a single character (in-place) from the end of a string.
|
||||
## .. code-block:: nim
|
||||
## var
|
||||
## table = "users"
|
||||
@@ -2368,6 +2367,43 @@ proc removeSuffix*(s: var string, suffix: string) {.
|
||||
newLen -= len(suffix)
|
||||
s.setLen(newLen)
|
||||
|
||||
proc removePrefix*(s: var string, chars: set[char] = Newlines) {.
|
||||
rtl, extern: "nsuRemovePrefixCharSet".} =
|
||||
## Removes all characters from `chars` from the start of the string `s`
|
||||
## (in-place).
|
||||
## .. code-block:: nim
|
||||
## var userInput = "\r\n*~Hello World!"
|
||||
## userInput.removePrefix
|
||||
## doAssert userInput == "*~Hello World!"
|
||||
## userInput.removePrefix({'~', '*'})
|
||||
## doAssert userInput == "Hello World!"
|
||||
##
|
||||
## var otherInput = "?!?Hello!?!"
|
||||
## otherInput.removePrefix({'!', '?'})
|
||||
## doAssert otherInput == "Hello!?!"
|
||||
var start = 0
|
||||
while start < s.len and s[start] in chars: start += 1
|
||||
if start > 0: s.delete(0, start - 1)
|
||||
|
||||
proc removePrefix*(s: var string, c: char) {.
|
||||
rtl, extern: "nsuRemovePrefixChar".} =
|
||||
## Removes a single character (in-place) from the start of a string.
|
||||
## .. code-block:: nim
|
||||
## var ident = "pControl"
|
||||
## ident.removePrefix('p')
|
||||
## doAssert ident == "Control"
|
||||
removePrefix(s, chars = {c})
|
||||
|
||||
proc removePrefix*(s: var string, prefix: string) {.
|
||||
rtl, extern: "nsuRemovePrefixString".} =
|
||||
## Remove the first matching prefix (in-place) from a string.
|
||||
## .. code-block:: nim
|
||||
## var answers = "yesyes"
|
||||
## answers.removePrefix("yes")
|
||||
## doAssert answers == "yes"
|
||||
if s.startsWith(prefix):
|
||||
s.delete(0, prefix.len - 1)
|
||||
|
||||
when isMainModule:
|
||||
doAssert align("abc", 4) == " abc"
|
||||
doAssert align("a", 0) == "a"
|
||||
|
||||
@@ -49,9 +49,71 @@ proc testRemoveSuffix =
|
||||
s.removeSuffix("")
|
||||
assert s == "hello\r\n\r\n"
|
||||
|
||||
proc testRemovePrefix =
|
||||
var s = "\n\rhello"
|
||||
s.removePrefix
|
||||
assert s == "hello"
|
||||
s.removePrefix
|
||||
assert s == "hello"
|
||||
|
||||
s = "\n\nhello"
|
||||
s.removePrefix
|
||||
assert s == "hello"
|
||||
|
||||
s = "\rhello"
|
||||
s.removePrefix
|
||||
assert s == "hello"
|
||||
|
||||
s = "hello \n there"
|
||||
s.removePrefix
|
||||
assert s == "hello \n there"
|
||||
|
||||
s = "hello"
|
||||
s.removePrefix("hel")
|
||||
assert s == "lo"
|
||||
s.removePrefix('l')
|
||||
assert s == "o"
|
||||
|
||||
s = "hellos"
|
||||
s.removePrefix({'h','e'})
|
||||
assert s == "llos"
|
||||
s.removePrefix({'l','o'})
|
||||
assert s == "s"
|
||||
|
||||
s = "aeiou"
|
||||
s.removePrefix("")
|
||||
assert s == "aeiou"
|
||||
|
||||
s = ""
|
||||
s.removePrefix("")
|
||||
assert s == ""
|
||||
|
||||
s = " "
|
||||
s.removePrefix
|
||||
assert s == " "
|
||||
|
||||
s = " "
|
||||
s.removePrefix("")
|
||||
assert s == " "
|
||||
|
||||
s = " "
|
||||
s.removePrefix(" ")
|
||||
assert s == " "
|
||||
|
||||
s = " "
|
||||
s.removePrefix(' ')
|
||||
assert s == ""
|
||||
|
||||
# Contrary to Chomp in other languages
|
||||
# empty string does not change behaviour
|
||||
s = "\r\n\r\nhello"
|
||||
s.removePrefix("")
|
||||
assert s == "\r\n\r\nhello"
|
||||
|
||||
proc main() =
|
||||
testStrip()
|
||||
testRemoveSuffix()
|
||||
testRemovePrefix()
|
||||
for p in split("/home/a1:xyz:/usr/bin", {':'}):
|
||||
write(stdout, p)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user