From aa4242de558192c48139902bfb7f97e2ef9899fc Mon Sep 17 00:00:00 2001 From: litlighilit Date: Mon, 6 May 2024 08:51:09 +0800 Subject: [PATCH] feat: str.count --- src/pylib/pystring/strmeth.nim | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/pylib/pystring/strmeth.nim b/src/pylib/pystring/strmeth.nim index 165958de3..271c53a89 100644 --- a/src/pylib/pystring/strmeth.nim +++ b/src/pylib/pystring/strmeth.nim @@ -7,6 +7,17 @@ import ./strip export strip import ./errHandle +func count*(a: StringLike, sub: StringLike): int = + if sub.len == 0: return str(a).len + 1 + +func count*(a: StringLike, sub: StringLike, start: int): int = + let subA = substr($a, start) + if sub.len == 0: return str(subA).len + 1 + count($a, sub) + +func count*(a: StringLike, sub: StringLike, start=0, `end`: int): int = + count(substr($a, start, `end`-1), sub) + template casefold*(a: StringLike): string = ## Mimics Python str.casefold() -> str unicode.toLower(a)