commit 2042af21047d33c551a7f85c240fdf50750ef6f7
parent ea5dfc8e136e4222fdeabfa3a65f998a7466dc41
Author: Bor Grošelj Simić <bgs@turminal.net>
Date: Tue, 19 Apr 2022 03:57:10 +0200
strings::{l,r,}trim: trim whitespace with bytes::
by doing so we avoid utf8 decoding for this common case
Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>
Diffstat:
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/strings/trim.ha b/strings/trim.ha
@@ -3,16 +3,18 @@
// (c) 2021 Eyal Sawady <ecs@d2evs.net>
// (c) 2021 Miccah Castorina <contact@miccah.io>
// (c) 2021 Sudipto Mallick <smlckz@disroot.org>
+use bytes;
use encoding::utf8;
-const whitespace: [_]rune = [' ', '\n', '\t', '\r'];
+const whitespace: [_]u8 = [' ', '\n', '\t', '\r'];
// Returns a string (borrowed from given input string) after trimming off of
// the start of the input string the characters in the given list of runes. If
// no runes are given, returns the string with leading whitespace stripped off.
export fn ltrim(input: str, trim: rune...) str = {
if (len(trim) == 0) {
- trim = whitespace;
+ let input = toutf8(input);
+ return fromutf8_unsafe(bytes::ltrim(input, whitespace...));
};
let it = iter(input);
for (true) {
@@ -42,7 +44,8 @@ export fn ltrim(input: str, trim: rune...) str = {
// runes are given, returns the string with trailing whitespace stripped off.
export fn rtrim(input: str, trim: rune...) str = {
if (len(trim) == 0) {
- trim = whitespace;
+ let input = toutf8(input);
+ return fromutf8_unsafe(bytes::rtrim(input, whitespace...));
};
let it = riter(input);
for (true) {