bigtext 0.9.0

Free editor for huge text and log files (1-50+ GB): opens instantly, never freezes, 2.5 GB/s search, atomic save
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Search over byte windows of a [`TextBuf`].
//!
//! * Literal, case-sensitive queries use `memchr::memmem` (SIMD) on the raw bytes, after
//!   transcoding the pattern into the file's encoding (UTF-16 hits must be 2-byte aligned).
//! * Everything else uses `regex-automata`'s meta regex (guaranteed linear time). For UTF-8 it
//!   runs on raw bytes; for other encodings each window is decoded to UTF-8 and match offsets are
//!   mapped back to source bytes exactly.
//! * The document is processed in windows of [`WINDOW`] bytes with an overlap, so search never
//!   needs the line index and memory stays bounded. With no edits the windows are borrowed
//!   straight from the mmap; with edits they are assembled across piece boundaries.
//! * Regex matches longer than the overlap are re-searched with a growing window, capped at
//!   [`MAX_MATCH`] (longer matches are reported truncated).

use std::ops::Range;

use regex_automata::meta::Regex;
use regex_automata::util::captures::Captures;
use regex_automata::util::syntax;
use regex_automata::Input;

use crate::encoding::{Codec, Encoding, NlByte};
use crate::jobs::{CancelToken, Progress};
use crate::text::TextBuf;

pub const WINDOW: u64 = 16 * 1024 * 1024;
/// Regex window overlap: matches up to this length never straddle a window edge unseen.
pub const REGEX_OVERLAP: u64 = 1024 * 1024;
pub const MAX_MATCH: u64 = 256 * 1024 * 1024;
/// Bytes of look-behind/look-ahead context given to the regex engine around a window.
const CTX: u64 = 8;

#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Query {
    pub pattern: String,
    pub regex: bool,
    pub case_sensitive: bool,
    pub whole_word: bool,
    /// Dot matches newline ("multiline" mode).
    pub dot_all: bool,
}

enum Engine {
    Literal { finder: memchr::memmem::Finder<'static>, len: usize },
    Regex(Regex),
}

pub struct Matcher {
    engine: Engine,
    codec: Codec,
    /// Regex runs on decoded UTF-8 (non-UTF-8 encodings).
    decoded: bool,
    pub query: Query,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchEnd {
    Completed,
    Stopped,
    Cancelled,
    Unavailable,
}

/// Builds the regex pattern string for a query (shared with replace).
pub fn regex_pattern(q: &Query) -> String {
    let pat = if q.regex { q.pattern.clone() } else { regex_syntax::escape(&q.pattern) };
    if q.whole_word {
        if q.pattern.is_ascii() {
            format!(r"(?-u:\b)(?:{pat})(?-u:\b)")
        } else {
            format!(r"\b(?:{pat})\b")
        }
    } else {
        pat
    }
}

pub fn build_regex(q: &Query, codec: &Codec) -> Result<Regex, String> {
    let mut sc = syntax::Config::new()
        .case_insensitive(!q.case_sensitive)
        .multi_line(true)
        .dot_matches_new_line(q.dot_all)
        .utf8(false);
    match codec.nl {
        NlByte::Lf => sc = sc.crlf(true),
        NlByte::Cr => sc = sc.line_terminator(b'\r'),
    }
    Regex::builder()
        .syntax(sc)
        .configure(Regex::config().utf8_empty(true))
        .build(&regex_pattern(q))
        // The syntax error carries the pattern with a caret under the problem.
        .map_err(|e| e.syntax_error().map(|s| s.to_string()).unwrap_or_else(|| e.to_string()))
}

impl Matcher {
    pub fn new(q: &Query, codec: Codec) -> Result<Matcher, String> {
        if q.pattern.is_empty() {
            return Err("Empty pattern".into());
        }
        let literal_ok = !q.regex && q.case_sensitive && !q.whole_word && codec.can_encode(&q.pattern);
        if literal_ok {
            let needle = codec.encode(&q.pattern);
            let len = needle.len();
            let finder = memchr::memmem::Finder::new(&needle).into_owned();
            return Ok(Matcher { engine: Engine::Literal { finder, len }, codec, decoded: false, query: q.clone() });
        }
        let re = build_regex(q, &codec)?;
        Ok(Matcher { engine: Engine::Regex(re), codec, decoded: codec.enc != Encoding::Utf8, query: q.clone() })
    }

    pub fn is_literal(&self) -> bool {
        matches!(self.engine, Engine::Literal { .. })
    }

    pub fn overlap(&self) -> u64 {
        match &self.engine {
            Engine::Literal { len, .. } => (*len as u64).saturating_sub(1),
            Engine::Regex(_) => REGEX_OVERLAP,
        }
    }

    pub fn regex(&self) -> Option<&Regex> {
        match &self.engine {
            Engine::Regex(r) => Some(r),
            _ => None,
        }
    }

    pub fn codec(&self) -> Codec {
        self.codec
    }

    /// Finds matches in `hay` (first byte at absolute offset `base`) whose start lies in
    /// `[from, accept_end)` and which end at or before `span_end` (indices into `hay`).
    /// `last_end` suppresses an empty match right after the previous match. Returns false if
    /// `f` asked to stop.
    pub fn find_in(
        &self,
        hay: &[u8],
        base: u64,
        from: usize,
        accept_end: usize,
        span_end: usize,
        last_end: &mut Option<u64>,
        f: &mut dyn FnMut(u64, u64) -> bool,
    ) -> bool {
        let span_end = span_end.min(hay.len());
        match &self.engine {
            Engine::Literal { finder, len } => {
                let utf16 = self.codec.enc.is_utf16();
                let mut pos = from;
                while pos < accept_end && pos < span_end {
                    let Some(i) = finder.find(&hay[pos..span_end]) else { break };
                    let s = pos + i;
                    if s >= accept_end {
                        break;
                    }
                    if utf16 && (base + s as u64) % 2 == 1 {
                        pos = s + 1;
                        continue;
                    }
                    let (sa, ea) = (base + s as u64, base + (s + len) as u64);
                    *last_end = Some(ea);
                    if !f(sa, ea) {
                        return false;
                    }
                    pos = s + len;
                }
                true
            }
            Engine::Regex(re) => {
                if self.decoded {
                    self.find_decoded(re, hay, base, from, accept_end, span_end, last_end, f)
                } else {
                    let mut pos = from;
                    while pos <= span_end && pos < accept_end {
                        let input = Input::new(hay).range(pos..span_end);
                        let Some(m) = re.search(&input) else { break };
                        let (s, e) = (m.start(), m.end());
                        if s >= accept_end {
                            break;
                        }
                        if s == e && *last_end == Some(base + e as u64) {
                            pos = s + utf8_step(hay, s);
                            continue;
                        }
                        *last_end = Some(base + e as u64);
                        if !f(base + s as u64, base + e as u64) {
                            return false;
                        }
                        pos = e;
                    }
                    true
                }
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn find_decoded(
        &self,
        re: &Regex,
        hay: &[u8],
        base: u64,
        from: usize,
        accept_end: usize,
        span_end: usize,
        last_end: &mut Option<u64>,
        f: &mut dyn FnMut(u64, u64) -> bool,
    ) -> bool {
        let codec = self.codec;
        let accept_end = accept_end.min(span_end + 1);
        // Decode [0, from), [from, accept), [accept, span_end) and remember the UTF-8 boundaries.
        let unit = codec.unit();
        let align = |x: usize| x - x % unit;
        let from = align(from);
        let acc = align(accept_end.min(span_end));
        let mut text = String::with_capacity(span_end * 2);
        codec.decode_into(&hay[..from], &mut text);
        let u_from = text.len();
        codec.decode_into(&hay[from..acc], &mut text);
        let u_acc = if accept_end > span_end { usize::MAX } else { text.len() };
        codec.decode_into(&hay[acc..span_end], &mut text);
        let u_end = text.len();
        // Walking cursor for UTF-8 offset -> source offset.
        let mut cur_u = 0usize;
        let mut cur_s = 0usize;
        let mut to_src = |u: usize| -> usize {
            if u < cur_u {
                // Never happens for monotonic queries; recompute from scratch.
                cur_u = 0;
                cur_s = 0;
            }
            for c in text[cur_u..u].chars() {
                cur_s += codec.src_width(c);
            }
            cur_u = u;
            cur_s.min(span_end)
        };
        let mut pos = u_from;
        while pos <= u_end && pos < u_acc {
            let input = Input::new(text.as_bytes()).range(pos..u_end);
            let Some(m) = re.search(&input) else { break };
            let (s, e) = (m.start(), m.end());
            if s >= u_acc {
                break;
            }
            let ss = base + to_src(s) as u64;
            let es = base + to_src(e) as u64;
            if s == e && *last_end == Some(es) {
                pos = s + utf8_step(text.as_bytes(), s);
                continue;
            }
            *last_end = Some(es);
            if !f(ss, es) {
                return false;
            }
            pos = e;
        }
        true
    }
}

fn utf8_step(hay: &[u8], i: usize) -> usize {
    if i >= hay.len() {
        return 1;
    }
    crate::encoding::utf8_char(&hay[i..]).1.max(1)
}

fn align_down(x: u64, unit: u64) -> u64 {
    x - x % unit
}

/// Streams forward over `range`, calling `on_match` for every match in order until it returns
/// false. Works on any snapshot (blocking reads).
pub fn search_forward(
    buf: &TextBuf,
    m: &Matcher,
    range: Range<u64>,
    cancel: &CancelToken,
    progress: Option<&Progress>,
    mut on_match: impl FnMut(u64, u64) -> bool,
) -> SearchEnd {
    search_forward_ctx(buf, m, range, cancel, progress, |s, e, _, _| on_match(s, e))
}

/// Like [`search_forward`], but also passes the current window (`hay`, starting at absolute
/// offset `base`) so callers can copy surrounding bytes without re-reading them. For matches
/// found by extending past the window, `hay` is empty.
pub fn search_forward_ctx(
    buf: &TextBuf,
    m: &Matcher,
    range: Range<u64>,
    cancel: &CancelToken,
    progress: Option<&Progress>,
    mut on_match: impl FnMut(u64, u64, &[u8], u64) -> bool,
) -> SearchEnd {
    let len = buf.len();
    let unit = buf.codec.unit() as u64;
    let end = range.end.min(len);
    let start = align_down(range.start.min(end), unit);
    if let Some(p) = progress {
        p.set_total(end - start);
    }
    let mut pos = start;
    let mut last_end: Option<u64> = None;
    let mut window = Vec::new();
    loop {
        if cancel.is_cancelled() {
            return SearchEnd::Cancelled;
        }
        let win_end = align_down((pos + WINDOW).min(end), unit).max(pos.min(end));
        let span_end = (win_end + m.overlap()).min(end);
        let read_start = align_down(pos.saturating_sub(CTX), unit);
        let read_end = (span_end + CTX).min(len);
        let Some(n) = buf.read_to(read_start..read_end, &mut window) else { return SearchEnd::Unavailable };
        let hay = &window[..n];
        let final_window = win_end >= end;
        let rel = |x: u64| (x - read_start) as usize;
        let accept = if final_window { rel(win_end) + 1 } else { rel(win_end) };
        let mut truncated: Option<u64> = None;
        let mut stopped = false;
        let mut max_end = win_end;
        let literal = m.is_literal();
        m.find_in(hay, read_start, rel(pos), accept, rel(span_end), &mut last_end, &mut |s, e| {
            if !literal && e == span_end && span_end < end {
                truncated = Some(s);
                return false;
            }
            if !on_match(s, e, hay, read_start) {
                stopped = true;
                return false;
            }
            max_end = max_end.max(e);
            true
        });
        if stopped {
            return SearchEnd::Stopped;
        }
        if let Some(s) = truncated {
            // Re-search from the match start with a growing window.
            match extend_match(buf, m, s, end, cancel) {
                Some((s2, e2)) => {
                    last_end = Some(e2);
                    if !on_match(s2, e2, &[], 0) {
                        return SearchEnd::Stopped;
                    }
                    pos = if e2 > s2 { e2 } else { buf.next_char(s2) };
                }
                None => pos = buf.next_char(s),
            }
            if let Some(p) = progress {
                p.set_done(pos.min(end) - start);
            }
            if pos > end {
                break;
            }
            continue;
        }
        if let Some(p) = progress {
            p.set_done(win_end - start);
        }
        if final_window {
            break;
        }
        pos = max_end;
    }
    SearchEnd::Completed
}

fn extend_match(buf: &TextBuf, m: &Matcher, s: u64, end: u64, cancel: &CancelToken) -> Option<(u64, u64)> {
    let len = buf.len();
    let unit = buf.codec.unit() as u64;
    let mut lim = (s + 4 * REGEX_OVERLAP).min(end);
    loop {
        if cancel.is_cancelled() {
            return None;
        }
        let rs = align_down(s.saturating_sub(CTX), unit);
        let hay = buf.read(rs..(lim + CTX).min(len))?;
        let mut found = None;
        let mut le = None;
        m.find_in(&hay, rs, (s - rs) as usize, (s - rs) as usize + 1, (lim - rs) as usize, &mut le, &mut |a, b| {
            found = Some((a, b));
            false
        });
        match found {
            Some((a, b)) if b < lim || lim >= end || lim - s >= MAX_MATCH => return Some((a, b)),
            Some(_) => lim = (s + (lim - s) * 4).min(end).min(s + MAX_MATCH),
            None => return None,
        }
    }
}

/// Last match starting before `before` (and at or after `floor`), searching backwards in windows.
pub fn search_backward(buf: &TextBuf, m: &Matcher, floor: u64, before: u64, cancel: &CancelToken) -> Option<(u64, u64)> {
    let unit = buf.codec.unit() as u64;
    let mut hi = before.min(buf.len());
    while hi > floor {
        if cancel.is_cancelled() {
            return None;
        }
        let lo = align_down(hi.saturating_sub(WINDOW).max(floor), unit);
        let mut best = None;
        let end = (hi + m.overlap()).min(buf.len());
        let r = search_forward(buf, m, lo..end, cancel, None, |s, e| {
            if s < hi {
                best = Some((s, e));
                true
            } else {
                false
            }
        });
        if r == SearchEnd::Cancelled {
            return None;
        }
        if best.is_some() {
            return best;
        }
        if lo == floor {
            break;
        }
        hi = lo + m.overlap().min(WINDOW / 2);
        if hi >= before {
            hi = lo;
        }
    }
    None
}

/// Matches overlapping a small range (viewport highlighting); bounded work, may miss matches
/// longer than a few KB.
pub fn matches_in_view(buf: &TextBuf, m: &Matcher, range: Range<u64>, cap: usize) -> Vec<(u64, u64)> {
    let len = buf.len();
    let unit = buf.codec.unit() as u64;
    let start = align_down(range.start.saturating_sub(4096), unit);
    let end = (range.end + 4096).min(len);
    let rs = align_down(start.saturating_sub(CTX), unit);
    let Some(hay) = buf.read(rs..(end + CTX).min(len)) else { return Vec::new() };
    let mut out = Vec::new();
    let mut le = None;
    let accept = if end == len { (end - rs) as usize + 1 } else { (end - rs) as usize };
    m.find_in(&hay, rs, (start - rs) as usize, accept, (end - rs) as usize, &mut le, &mut |s, e| {
        if e >= range.start && s <= range.end {
            out.push((s, e));
        }
        out.len() < cap
    });
    out
}

/// Captures for the match starting exactly at `start` (regex queries; used by replace).
/// The text of capture group `g` (0: the whole match) of the match `start..end`, if the group
/// took part in it.
pub fn group_text(buf: &TextBuf, m: &Matcher, start: u64, end: u64, g: usize) -> Option<String> {
    let (hay, caps, _) = captures_at(buf, m, start, end)?;
    let span = caps.get_group(g)?;
    let b = &hay[span.range()];
    Some(if m.decoded { String::from_utf8_lossy(b).into_owned() } else { m.codec.decode_lossy(b) })
}

/// The names of the capture groups of a regular expression ("" for unnamed ones), from group 1.
pub fn group_names(m: &Matcher) -> Vec<String> {
    let Some(re) = m.regex() else { return Vec::new() };
    re.group_info().pattern_names(regex_automata::PatternID::ZERO).skip(1).map(|n| n.unwrap_or("").to_string()).collect()
}

pub fn captures_at(buf: &TextBuf, m: &Matcher, start: u64, end: u64) -> Option<(Vec<u8>, Captures, usize)> {
    let re = m.regex()?;
    let len = buf.len();
    let unit = buf.codec.unit() as u64;
    let rs = align_down(start.saturating_sub(CTX), unit);
    let re_end = (end + CTX).min(len);
    let raw = buf.read(rs..re_end)?;
    let (hay, s, e) = if m.decoded {
        let mut t = String::new();
        m.codec.decode_into(&raw[..(start - rs) as usize], &mut t);
        let s = t.len();
        m.codec.decode_into(&raw[(start - rs) as usize..(end - rs) as usize], &mut t);
        let e = t.len();
        m.codec.decode_into(&raw[(end - rs) as usize..], &mut t);
        (t.into_bytes(), s, e)
    } else {
        (raw.into_owned(), (start - rs) as usize, (end - rs) as usize)
    };
    let mut caps = re.create_captures();
    re.search_captures(&Input::new(&hay).range(s..e).anchored(regex_automata::Anchored::Yes), &mut caps);
    if !caps.is_match() {
        return None;
    }
    Some((hay, caps, s))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::file::{OpenMode, Source};
    use crate::index;
    use std::io::Write;
    use std::sync::Arc;

    fn buf_with(data: &[u8], codec: Codec) -> (tempfile::NamedTempFile, TextBuf) {
        let mut f = tempfile::NamedTempFile::new().unwrap();
        f.write_all(data).unwrap();
        f.flush().unwrap();
        let src = Arc::new(Source::open(f.path(), OpenMode::Auto).unwrap());
        let idx = index::build_blocking(&src, codec);
        let mut t = TextBuf::new(src, idx, codec);
        t.blocking = true;
        (f, t)
    }

    fn q(p: &str) -> Query {
        Query { pattern: p.into(), case_sensitive: true, ..Default::default() }
    }

    fn all(buf: &TextBuf, m: &Matcher) -> Vec<(u64, u64)> {
        let mut v = Vec::new();
        let r = search_forward(buf, m, 0..buf.len(), &CancelToken::new(), None, |s, e| {
            v.push((s, e));
            true
        });
        assert_eq!(r, SearchEnd::Completed);
        v
    }

    #[test]
    fn literal_basic() {
        let (_f, t) = buf_with(b"abc foo abc foofoo", Codec::utf8());
        let m = Matcher::new(&q("foo"), Codec::utf8()).unwrap();
        assert!(m.is_literal());
        assert_eq!(all(&t, &m), vec![(4, 7), (12, 15), (15, 18)]);
    }

    #[test]
    fn literal_across_window_boundary() {
        let mut data = vec![b'x'; WINDOW as usize - 2];
        data.extend_from_slice(b"NEEDLE");
        data.extend(vec![b'y'; 100]);
        data.extend_from_slice(b"NEEDLE");
        let (_f, t) = buf_with(&data, Codec::utf8());
        let m = Matcher::new(&q("NEEDLE"), Codec::utf8()).unwrap();
        let w = WINDOW - 2;
        assert_eq!(all(&t, &m), vec![(w, w + 6), (w + 106, w + 112)]);
    }

    #[test]
    fn regex_and_case_folding() {
        let (_f, t) = buf_with("Straße STRASSE straße ПРИВЕТ привет".as_bytes(), Codec::utf8());
        let mut query = q("straße");
        query.case_sensitive = false;
        let m = Matcher::new(&query, Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m).len(), 2);
        let mut query = q("привет");
        query.case_sensitive = false;
        let m = Matcher::new(&query, Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m).len(), 2);
        let mut rq = q(r"\d+");
        rq.regex = true;
        let (_g, t2) = buf_with(b"a1 b22 c333", Codec::utf8());
        let m = Matcher::new(&rq, Codec::utf8()).unwrap();
        assert_eq!(all(&t2, &m), vec![(1, 2), (4, 6), (8, 11)]);
    }

    #[test]
    fn whole_word() {
        let (_f, t) = buf_with(b"cat concat cat_x cat.", Codec::utf8());
        let mut query = q("cat");
        query.whole_word = true;
        let m = Matcher::new(&query, Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m), vec![(0, 3), (17, 20)]);
    }

    #[test]
    fn empty_matches_and_anchors() {
        let (_f, t) = buf_with(b"ab\ncd\n", Codec::utf8());
        let mut rq = q("^");
        rq.regex = true;
        let m = Matcher::new(&rq, Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m), vec![(0, 0), (3, 3), (6, 6)]);
        let mut rq = q("x*");
        rq.regex = true;
        let m = Matcher::new(&rq, Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m).len(), 7);
        let mut rq = q("$");
        rq.regex = true;
        let (_g, crlf) = buf_with(b"ab\r\ncd", Codec::utf8());
        let m = Matcher::new(&rq, Codec::utf8()).unwrap();
        assert_eq!(all(&crlf, &m), vec![(2, 2), (6, 6)]);
    }

    #[test]
    fn regex_match_spanning_windows_and_long_matches() {
        // A match shorter than the overlap that crosses a window edge is found exactly.
        let mut data = vec![b'x'; WINDOW as usize - 1000];
        data.extend(vec![b'a'; 500_000]);
        data.push(b'!');
        let (_f, t) = buf_with(&data, Codec::utf8());
        let mut rq = q("a+!");
        rq.regex = true;
        let m = Matcher::new(&rq, Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m), vec![(WINDOW - 1000, data.len() as u64)]);
        // A match longer than the overlap is extended past the window instead of truncated.
        let data = vec![b'a'; WINDOW as usize + 3 * REGEX_OVERLAP as usize];
        let (_g, t) = buf_with(&data, Codec::utf8());
        let mut rq = q("a+");
        rq.regex = true;
        let m = Matcher::new(&rq, Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m), vec![(0, data.len() as u64)]);
        // Whole-file match with dot-all.
        let mut rq = q("(?s).*");
        rq.regex = true;
        let m = Matcher::new(&rq, Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m)[0], (0, data.len() as u64));
    }

    #[test]
    fn utf16_literal_alignment_and_regex() {
        let text = "a\u{0A61}a and ab"; // U+0A61 encodes as 61 0A: misaligned "a" hits must be rejected
        let mut data = vec![0xFF, 0xFE];
        data.extend(text.encode_utf16().flat_map(|u| u.to_le_bytes()));
        let codec = Codec::new(Encoding::Utf16Le, NlByte::Lf, 2);
        let (_f, t) = buf_with(&data, codec);
        let m = Matcher::new(&q("a"), codec).unwrap();
        let hits = all(&t, &m);
        assert!(hits.iter().all(|(s, _)| s % 2 == 0));
        assert_eq!(hits.len(), 4);
        let mut rq = q("A[N]D");
        rq.regex = true;
        rq.case_sensitive = false;
        let m = Matcher::new(&rq, codec).unwrap();
        let hits = all(&t, &m);
        assert_eq!(hits.len(), 1);
        let (s, e) = hits[0];
        assert_eq!(codec.decode_lossy(&t.read(s..e).unwrap()), "and");
    }

    #[test]
    fn cp1251_search() {
        let codec = Codec::new(Encoding::Windows1251, NlByte::Lf, 0);
        let data = codec.encode("Привет мир, привет!\n");
        let (_f, t) = buf_with(&data, codec);
        let m = Matcher::new(&q("мир"), codec).unwrap();
        assert!(m.is_literal());
        assert_eq!(all(&t, &m), vec![(7, 10)]);
        let mut ci = q("ПРИВЕТ");
        ci.case_sensitive = false;
        let m = Matcher::new(&ci, codec).unwrap();
        assert_eq!(all(&t, &m), vec![(0, 6), (12, 18)]);
    }

    #[test]
    fn search_with_edits_spanning_pieces() {
        let (_f, mut t) = buf_with(b"hello world", Codec::utf8());
        t.replace(5..5, b" big");
        t.replace(0..1, b"J");
        let m = Matcher::new(&q("lo big wo"), Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m), vec![(3, 12)]);
    }

    #[test]
    fn backward_and_view() {
        let (_f, t) = buf_with(b"x1 x2 x3 x4", Codec::utf8());
        let m = Matcher::new(&q("x"), Codec::utf8()).unwrap();
        assert_eq!(search_backward(&t, &m, 0, 7, &CancelToken::new()), Some((6, 7)));
        assert_eq!(search_backward(&t, &m, 0, 0, &CancelToken::new()), None);
        assert_eq!(matches_in_view(&t, &m, 3..8, 100).len(), 2);
    }

    #[test]
    fn invalid_regex_reports_error() {
        let mut rq = q("(unclosed");
        rq.regex = true;
        assert!(Matcher::new(&rq, Codec::utf8()).is_err());
    }

    #[test]
    fn chunked_fallback_search() {
        let mut f = tempfile::NamedTempFile::new().unwrap();
        let mut data = vec![b'.'; 3_000_000];
        data[1_048_570..1_048_580].copy_from_slice(b"BOUNDARY!!");
        f.write_all(&data).unwrap();
        f.flush().unwrap();
        let src = Arc::new(Source::open(f.path(), OpenMode::Chunked).unwrap());
        let idx = index::build_blocking(&src, Codec::utf8());
        let mut t = TextBuf::new(src, idx, Codec::utf8());
        t.blocking = true;
        let m = Matcher::new(&q("BOUNDARY!!"), Codec::utf8()).unwrap();
        assert_eq!(all(&t, &m), vec![(1_048_570, 1_048_580)]);
    }
}