From 74e7406034e497b262136680524c18d280f44388 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 26 Nov 2025 10:22:04 +0100 Subject: [PATCH] fix: guarantee progress in find_all_ranges when pattern is shorter than a utf8 sequence --- src/buffer/Buffer.zig | 6 +++++- src/buffer/unicode.zig | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/buffer/Buffer.zig b/src/buffer/Buffer.zig index bcf1b4b..b05b3d4 100644 --- a/src/buffer/Buffer.zig +++ b/src/buffer/Buffer.zig @@ -989,7 +989,11 @@ const Node = union(enum) { .case_folded => { const input_consume_size = @min(ctx.buf.len - ctx.rest.len, input.len); var writer = std.Io.Writer.fixed(ctx.buf[ctx.rest.len..]); - const folded = unicode.case_folded_write_partial(&writer, input[0..input_consume_size]) catch return error.WriteFailed; + var folded = unicode.case_folded_write_partial(&writer, input[0..input_consume_size]) catch return error.WriteFailed; + if (folded.len == 0) { + try writer.writeByte(input[0]); + folded = input[0..1]; + } ctx.rest = ctx.buf[0 .. ctx.rest.len + folded.len]; input = input[folded.len..]; }, diff --git a/src/buffer/unicode.zig b/src/buffer/unicode.zig index db24595..4346f84 100644 --- a/src/buffer/unicode.zig +++ b/src/buffer/unicode.zig @@ -212,7 +212,7 @@ const Utf8PartialIterator = struct { return null; } - const cp_len = utf8ByteSequenceLength(it.bytes[it.end]) catch unreachable; + const cp_len = utf8ByteSequenceLength(it.bytes[it.end]) catch return null; if (it.end + cp_len > it.bytes.len) { return null; }