mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-03 19:52:36 +00:00
Added startsWith/endsWith implementations for character prefix/suffix (fixes #4252).
This commit is contained in:
@@ -862,6 +862,10 @@ proc startsWith*(s, prefix: string): bool {.noSideEffect,
|
||||
if s[i] != prefix[i]: return false
|
||||
inc(i)
|
||||
|
||||
proc startsWith*(s: string, prefix: char): bool {.noSideEffect.} =
|
||||
## Returns true iff ``s`` starts with ``prefix``.
|
||||
result = s[0] == prefix
|
||||
|
||||
proc endsWith*(s, suffix: string): bool {.noSideEffect,
|
||||
rtl, extern: "nsuEndsWith".} =
|
||||
## Returns true iff ``s`` ends with ``suffix``.
|
||||
@@ -874,6 +878,10 @@ proc endsWith*(s, suffix: string): bool {.noSideEffect,
|
||||
inc(i)
|
||||
if suffix[i] == '\0': return true
|
||||
|
||||
proc endsWith*(s: string, suffix: char): bool {.noSideEffect.} =
|
||||
## Returns true iff ``s`` ends with ``suffix``.
|
||||
result = s[s.high] == suffix
|
||||
|
||||
proc continuesWith*(s, substr: string, start: Natural): bool {.noSideEffect,
|
||||
rtl, extern: "nsuContinuesWith".} =
|
||||
## Returns true iff ``s`` continues with ``substr`` at position ``start``.
|
||||
@@ -2029,4 +2037,12 @@ bar
|
||||
# Don't use SI prefix as number is too small
|
||||
doAssert formatEng(3.1e-25, siPrefix=true, unit="A") == "310e-27 A"
|
||||
|
||||
block: # startsWith / endsWith char tests
|
||||
var s = "abcdef"
|
||||
doAssert s.startsWith('a')
|
||||
doAssert s.startsWith('b') == false
|
||||
doAssert s.endsWith('f')
|
||||
doAssert s.endsWith('a') == false
|
||||
doAssert s.endsWith('\0') == false
|
||||
|
||||
#echo("strutils tests passed")
|
||||
|
||||
Reference in New Issue
Block a user