Merge pull request #4282 from abudden/4252-startsWithChar

Added startsWith/endsWith implementations for character prefix/suffix…
This commit is contained in:
Andreas Rumpf
2016-06-11 09:20:20 +02:00
committed by GitHub

View File

@@ -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, inline.} =
## 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, inline.} =
## 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")