Update removeSuffix implementations to match removePrefix (#6636)
This commit is contained in:
parent
badba83d38
commit
c182d37f45
2 changed files with 53 additions and 31 deletions
|
|
@ -2326,40 +2326,37 @@ proc format*(formatstr: string, a: varargs[string, `$`]): string {.noSideEffect,
|
||||||
|
|
||||||
proc removeSuffix*(s: var string, chars: set[char] = Newlines) {.
|
proc removeSuffix*(s: var string, chars: set[char] = Newlines) {.
|
||||||
rtl, extern: "nsuRemoveSuffixCharSet".} =
|
rtl, extern: "nsuRemoveSuffixCharSet".} =
|
||||||
## Removes the first matching character from the string (in-place) given a
|
## Removes all characters from `chars` from the end of the string `s`
|
||||||
## set of characters. If the set of characters is only equal to `Newlines`
|
## (in-place).
|
||||||
## then it will remove both the newline and return feed.
|
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## var
|
## var userInput = "Hello World!*~\r\n"
|
||||||
## userInput = "Hello World!\r\n"
|
|
||||||
## otherInput = "Hello!?!"
|
|
||||||
## userInput.removeSuffix
|
## userInput.removeSuffix
|
||||||
## userInput == "Hello World!"
|
## doAssert userInput == "Hello World!*~"
|
||||||
## userInput.removeSuffix({'!', '?'})
|
## userInput.removeSuffix({'~', '*'})
|
||||||
## userInput == "Hello World"
|
## doAssert userInput == "Hello World!"
|
||||||
|
##
|
||||||
|
## var otherInput = "Hello!?!"
|
||||||
## otherInput.removeSuffix({'!', '?'})
|
## otherInput.removeSuffix({'!', '?'})
|
||||||
## otherInput == "Hello!?"
|
## doAssert otherInput == "Hello"
|
||||||
if s.len == 0: return
|
if s.len == 0: return
|
||||||
var last = len(s) - 1
|
var last = s.high
|
||||||
if chars == Newlines:
|
while last > -1 and s[last] in chars: last -= 1
|
||||||
if s[last] == '\10':
|
|
||||||
last -= 1
|
|
||||||
if s[last] == '\13':
|
|
||||||
last -= 1
|
|
||||||
else:
|
|
||||||
if s[last] in chars:
|
|
||||||
last -= 1
|
|
||||||
s.setLen(last + 1)
|
s.setLen(last + 1)
|
||||||
|
|
||||||
proc removeSuffix*(s: var string, c: char) {.
|
proc removeSuffix*(s: var string, c: char) {.
|
||||||
rtl, extern: "nsuRemoveSuffixChar".} =
|
rtl, extern: "nsuRemoveSuffixChar".} =
|
||||||
## Removes a single character (in-place) from the end of a string.
|
## Removes all occurrences of a single character (in-place) from the end
|
||||||
|
## of a string.
|
||||||
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## var
|
## var table = "users"
|
||||||
## table = "users"
|
|
||||||
## table.removeSuffix('s')
|
## table.removeSuffix('s')
|
||||||
## table == "user"
|
## doAssert table == "user"
|
||||||
|
##
|
||||||
|
## var dots = "Trailing dots......."
|
||||||
|
## dots.removeSuffix('.')
|
||||||
|
## doAssert dots == "Trailing dots"
|
||||||
removeSuffix(s, chars = {c})
|
removeSuffix(s, chars = {c})
|
||||||
|
|
||||||
proc removeSuffix*(s: var string, suffix: string) {.
|
proc removeSuffix*(s: var string, suffix: string) {.
|
||||||
|
|
@ -2367,10 +2364,9 @@ proc removeSuffix*(s: var string, suffix: string) {.
|
||||||
## Remove the first matching suffix (in-place) from a string.
|
## Remove the first matching suffix (in-place) from a string.
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## var
|
## var answers = "yeses"
|
||||||
## answers = "yeses"
|
|
||||||
## answers.removeSuffix("es")
|
## answers.removeSuffix("es")
|
||||||
## answers == "yes"
|
## doAssert answers == "yes"
|
||||||
var newLen = s.len
|
var newLen = s.len
|
||||||
if s.endsWith(suffix):
|
if s.endsWith(suffix):
|
||||||
newLen -= len(suffix)
|
newLen -= len(suffix)
|
||||||
|
|
@ -2380,6 +2376,7 @@ proc removePrefix*(s: var string, chars: set[char] = Newlines) {.
|
||||||
rtl, extern: "nsuRemovePrefixCharSet".} =
|
rtl, extern: "nsuRemovePrefixCharSet".} =
|
||||||
## Removes all characters from `chars` from the start of the string `s`
|
## Removes all characters from `chars` from the start of the string `s`
|
||||||
## (in-place).
|
## (in-place).
|
||||||
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## var userInput = "\r\n*~Hello World!"
|
## var userInput = "\r\n*~Hello World!"
|
||||||
## userInput.removePrefix
|
## userInput.removePrefix
|
||||||
|
|
@ -2396,7 +2393,9 @@ proc removePrefix*(s: var string, chars: set[char] = Newlines) {.
|
||||||
|
|
||||||
proc removePrefix*(s: var string, c: char) {.
|
proc removePrefix*(s: var string, c: char) {.
|
||||||
rtl, extern: "nsuRemovePrefixChar".} =
|
rtl, extern: "nsuRemovePrefixChar".} =
|
||||||
## Removes a single character (in-place) from the start of a string.
|
## Removes all occurrences of a single character (in-place) from the start
|
||||||
|
## of a string.
|
||||||
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## var ident = "pControl"
|
## var ident = "pControl"
|
||||||
## ident.removePrefix('p')
|
## ident.removePrefix('p')
|
||||||
|
|
@ -2406,6 +2405,7 @@ proc removePrefix*(s: var string, c: char) {.
|
||||||
proc removePrefix*(s: var string, prefix: string) {.
|
proc removePrefix*(s: var string, prefix: string) {.
|
||||||
rtl, extern: "nsuRemovePrefixString".} =
|
rtl, extern: "nsuRemovePrefixString".} =
|
||||||
## Remove the first matching prefix (in-place) from a string.
|
## Remove the first matching prefix (in-place) from a string.
|
||||||
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## var answers = "yesyes"
|
## var answers = "yesyes"
|
||||||
## answers.removePrefix("yes")
|
## answers.removePrefix("yes")
|
||||||
|
|
|
||||||
|
|
@ -13,15 +13,13 @@ proc testStrip() =
|
||||||
proc testRemoveSuffix =
|
proc testRemoveSuffix =
|
||||||
var s = "hello\n\r"
|
var s = "hello\n\r"
|
||||||
s.removeSuffix
|
s.removeSuffix
|
||||||
assert s == "hello\n"
|
|
||||||
s.removeSuffix
|
|
||||||
assert s == "hello"
|
assert s == "hello"
|
||||||
s.removeSuffix
|
s.removeSuffix
|
||||||
assert s == "hello"
|
assert s == "hello"
|
||||||
|
|
||||||
s = "hello\n\n"
|
s = "hello\n\n"
|
||||||
s.removeSuffix
|
s.removeSuffix
|
||||||
assert s == "hello\n"
|
assert s == "hello"
|
||||||
|
|
||||||
s = "hello\r"
|
s = "hello\r"
|
||||||
s.removeSuffix
|
s.removeSuffix
|
||||||
|
|
@ -41,7 +39,31 @@ proc testRemoveSuffix =
|
||||||
s.removeSuffix({'s','z'})
|
s.removeSuffix({'s','z'})
|
||||||
assert s == "hello"
|
assert s == "hello"
|
||||||
s.removeSuffix({'l','o'})
|
s.removeSuffix({'l','o'})
|
||||||
assert s == "hell"
|
assert s == "he"
|
||||||
|
|
||||||
|
s = "aeiou"
|
||||||
|
s.removeSuffix("")
|
||||||
|
assert s == "aeiou"
|
||||||
|
|
||||||
|
s = ""
|
||||||
|
s.removeSuffix("")
|
||||||
|
assert s == ""
|
||||||
|
|
||||||
|
s = " "
|
||||||
|
s.removeSuffix
|
||||||
|
assert s == " "
|
||||||
|
|
||||||
|
s = " "
|
||||||
|
s.removeSuffix("")
|
||||||
|
assert s == " "
|
||||||
|
|
||||||
|
s = " "
|
||||||
|
s.removeSuffix(" ")
|
||||||
|
assert s == " "
|
||||||
|
|
||||||
|
s = " "
|
||||||
|
s.removeSuffix(' ')
|
||||||
|
assert s == ""
|
||||||
|
|
||||||
# Contrary to Chomp in other languages
|
# Contrary to Chomp in other languages
|
||||||
# empty string does not change behaviour
|
# empty string does not change behaviour
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue