hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 737cd29e7b354d35db3a5a02cfd32521e7dcef2f
parent d1e0a02360f42a5f664c984d112673c0e6eef447
Author: Sebastian <sebastian@sebsite.pw>
Date:   Mon, 27 Nov 2023 01:18:14 -0500

strings: simplify ltrim and rtrim

Taking advantage of labelled loops

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mstrings/trim.ha | 25++++++++-----------------
1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/strings/trim.ha b/strings/trim.ha @@ -2,7 +2,6 @@ // (c) Hare authors <https://harelang.org> use bytes; -use encoding::utf8; const whitespace: [_]u8 = [' ', '\n', '\t', '\r']; @@ -15,24 +14,20 @@ export fn ltrim(input: str, trim: rune...) str = { return fromutf8_unsafe(bytes::ltrim(input, whitespace...)); }; let it = iter(input); - for (true) { + for (true) :outer { const r = match (next(&it)) { case let r: rune => yield r; case void => break; }; - let found = false; for (let i = 0z; i < len(trim); i += 1) { if (r == trim[i]) { - found = true; - break; + continue :outer; }; }; - if (!found) { - prev(&it); - break; - }; + prev(&it); + break; }; return fromutf8_unsafe(it.dec.src[it.dec.offs..]); }; @@ -46,24 +41,20 @@ export fn rtrim(input: str, trim: rune...) str = { return fromutf8_unsafe(bytes::rtrim(input, whitespace...)); }; let it = riter(input); - for (true) { + for (true) :outer { const r = match (next(&it)) { case let r: rune => yield r; case void => break; }; - let found = false; for (let i = 0z; i < len(trim); i += 1) { if (r == trim[i]) { - found = true; - break; + continue :outer; }; }; - if (!found) { - prev(&it); - break; - }; + prev(&it); + break; }; return fromutf8_unsafe(it.dec.src[..it.dec.offs]); };