From: Brendan Hansen Date: Tue, 23 Aug 2022 18:54:06 +0000 (-0500) Subject: added "string.read_until" for reading upto a string X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=821324cc6d6bcc43602749be9029d15c8029feaa;p=onyx.git added "string.read_until" for reading upto a string --- diff --git a/core/string.onyx b/core/string.onyx index dae084dc..ef876113 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -325,27 +325,70 @@ replace :: (s: str, to_replace: u8, replace_with: u8) { } } -read_until :: (s: ^str, upto: u8, skip := 0) -> str { - if s.count == 0 do return ""; +read_until :: #match #locked { + (s: ^str, upto: u8, skip := 0) -> str { + if s.count == 0 do return ""; - out : str; - out.data = s.data; - out.count = 0; + out : str; + out.data = s.data; + out.count = 0; - rem := skip; - for ch: *s { - if ch == upto { - if rem <= 0 do break; - else do rem -= 1; + rem := skip; + for ch: *s { + if ch == upto { + if rem <= 0 do break; + else do rem -= 1; + } + + out.count += 1; } - out.count += 1; - } + s.data += out.count; + s.count -= out.count; - s.data += out.count; - s.count -= out.count; + return out; + }, - return out; + (s: ^str, upto: str, skip := 0) -> str { + if s.count == 0 do return ""; + + out := str.{ data = s.data }; + + rem := skip; + i := 0; + while i <= s.count - upto.count { + match := true; + j := i; + for upto { + if s.data[j] != it { + match = false; + break; + } + + j += 1; + } + + if match { + if rem <= 0 do break; + else do rem -= 1; + } + + i += 1; + } + + if i > s.count - upto.count { + out = *s; + s.data += out.count; + s.count = 0; + + } else { + out.count = i - 1; + s.data += out.count + (upto.count - 1); + s.count -= out.count + (upto.count - 1); + } + + return out; + } } read_until_any :: (s: ^str, skip: u32, uptos: ..u8) -> str {