From 72dbd12ca43682318ea16153980a0a2bdc602b92 Mon Sep 17 00:00:00 2001 From: def Date: Wed, 23 Jul 2014 16:32:20 +0200 Subject: [PATCH 1/2] Add count procedures to strutils --- lib/pure/strutils.nim | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 6f8924d83b..acb803a4c0 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -803,6 +803,44 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} = if result != -1: return return -1 +proc count*(s: string, sub: string): int {.noSideEffect, + rtl, extern: "nsuCountString".} = + ## Count the occurences of a substring `sub` in the string `s`. Overlapping + ## occurences of `sub` do not count. + var i = 0 + while true: + i = s.find(sub, i) + if i < 0: + break + i += sub.len + inc result + +proc count*(s: string, sub: char): int {.noSideEffect, + rtl, extern: "nsuCountChar".} = + ## Count the occurences of the character `sub` in the string `s`. + for c in s: + if c == sub: + inc result + +proc count*(s: string, subs: set[char]): int {.noSideEffect, + rtl, extern: "nsuCountCharSet".} = + ## Count the occurences of the group of character `subs` in the string `s`. + for c in s: + if c in subs: + inc result + +proc countOverlapping*(s: string, sub: string): int {.noSideEffect, + rtl, extern: "nsuCountOverlapping".} = + ## Count the occurences of a substring `sub` in the string `s`. Overlapping + ## occurences of `sub` do count. + var i = 0 + while true: + i = s.find(sub, i) + if i < 0: + break + inc i + inc result + proc quoteIfContainsWhite*(s: string): string {.deprecated.} = ## Returns ``'"' & s & '"'`` if `s` contains a space and does not ## start with a quote, else returns `s`. @@ -1354,3 +1392,8 @@ when isMainModule: doAssert parseEnum[TMyEnum]("enu_D") == enuD doAssert parseEnum("invalid enum value", enC) == enC + + doAssert count("foofoofoo", "foofoo") == 1 + doAssert countOverlapping("foofoofoo", "foofoo") == 2 + doAssert count("foofoofoo", 'f') == 3 + doAssert count("foofoofoobar", {'f','b'}) == 4 From c78b1070a8dec61ae6aa660fe724bf9fb67fb6a9 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 28 Jul 2014 16:48:07 +0200 Subject: [PATCH 2/2] overlapping as a parameter for count instead --- lib/pure/strutils.nim | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index acb803a4c0..1d17de2338 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -803,16 +803,20 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} = if result != -1: return return -1 -proc count*(s: string, sub: string): int {.noSideEffect, +proc count*(s: string, sub: string, overlapping: bool = false): int {.noSideEffect, rtl, extern: "nsuCountString".} = - ## Count the occurences of a substring `sub` in the string `s`. Overlapping - ## occurences of `sub` do not count. + ## Count the occurences of a substring `sub` in the string `s`. + ## Overlapping occurences of `sub` only count when `overlapping` + ## is set to true. var i = 0 while true: i = s.find(sub, i) if i < 0: break - i += sub.len + if overlapping: + inc i + else: + i += sub.len inc result proc count*(s: string, sub: char): int {.noSideEffect, @@ -829,18 +833,6 @@ proc count*(s: string, subs: set[char]): int {.noSideEffect, if c in subs: inc result -proc countOverlapping*(s: string, sub: string): int {.noSideEffect, - rtl, extern: "nsuCountOverlapping".} = - ## Count the occurences of a substring `sub` in the string `s`. Overlapping - ## occurences of `sub` do count. - var i = 0 - while true: - i = s.find(sub, i) - if i < 0: - break - inc i - inc result - proc quoteIfContainsWhite*(s: string): string {.deprecated.} = ## Returns ``'"' & s & '"'`` if `s` contains a space and does not ## start with a quote, else returns `s`. @@ -1394,6 +1386,6 @@ when isMainModule: doAssert parseEnum("invalid enum value", enC) == enC doAssert count("foofoofoo", "foofoo") == 1 - doAssert countOverlapping("foofoofoo", "foofoo") == 2 + doAssert count("foofoofoo", "foofoo", overlapping = true) == 2 doAssert count("foofoofoo", 'f') == 3 doAssert count("foofoofoobar", {'f','b'}) == 4