Repository: Canop/broot (language: rust)

Issue:
Incorrect match highlight in regex search
In broot 1.44.6 (latest release as of this writing), the match highlight can sometimes be incorrect when performing a regex search on file name or sub path (`/query` or `rp/query`). This error happens when the filename contains a multi-byte unicode character.

To reproduce, create a file named `é-broot`.

```
$ ls | hexdump -C
00000000  c3 a9 2d 62 72 6f 6f 74  0a                       |..-broot.|
00000009
```

The attached screenshot shows a regex search `/br` will cause `ro` getting highlighted (in green).

![Image](https://github.com/user-attachments/assets/86f2b40e-3a59-4f9e-a8be-6b3fa047602e)

Other search modes (fuzzy, exact, token) do not show this problem.




Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d576f211..c0d9dd61 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,5 @@
 ### next
+- fix bad regex match position - Fix #979
 - update resvg dependency to 0.44 - Thanks @NoisyCoil
 
 ### v1.44.6 - 2025-01-12
diff --git a/src/pattern/regex_pattern.rs b/src/pattern/regex_pattern.rs
index 09e38d9a..82002e77 100644
--- a/src/pattern/regex_pattern.rs
+++ b/src/pattern/regex_pattern.rs
@@ -36,9 +36,11 @@ impl RegexPattern {
         // note that there's no significative cost related to using
         //  find over is_match
         self.rex.find(candidate).map(|rm| {
-            let mut pos = SmallVec::with_capacity(rm.end() - rm.start());
-            for i in rm.start()..rm.end() {
-                pos.push(i);
+            let chars_before = candidate[..rm.start()].chars().count();
+            let rm_chars = rm.as_str().chars().count();
+            let mut pos = SmallVec::with_capacity(rm_chars);
+            for i in 0..rm_chars {
+                pos.push(chars_before + i);
             }
             super::NameMatch { score: 1, pos }
         })


Test patch: