fcoreutils 0.22.0

High-performance GNU coreutils replacement with SIMD and parallelism
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
use std::collections::HashSet;
use std::io::{self, BufRead, Write};

/// Output format for ptx.
#[derive(Clone, Debug, PartialEq)]
pub enum OutputFormat {
    /// Default GNU ptx output format (roff-like).
    Roff,
    /// TeX output format.
    Tex,
    /// Dumb terminal / plain text format.
    Plain,
}

/// Configuration for the ptx command.
#[derive(Clone, Debug)]
pub struct PtxConfig {
    pub width: usize,
    pub ignore_case: bool,
    pub auto_reference: bool,
    pub traditional: bool,
    pub format: OutputFormat,
    pub ignore_words: HashSet<String>,
    pub only_words: Option<HashSet<String>>,
    pub references: bool,
    pub gap_size: usize,
    pub right_reference: bool,
    pub sentence_regexp: Option<String>,
    pub word_regexp: Option<String>,
    pub flag_truncation: Option<String>,
    pub macro_name: Option<String>,
}

impl Default for PtxConfig {
    fn default() -> Self {
        Self {
            width: 72,
            ignore_case: false,
            auto_reference: false,
            traditional: false,
            format: OutputFormat::Plain,
            ignore_words: HashSet::new(),
            only_words: None,
            references: false,
            gap_size: 3,
            right_reference: false,
            sentence_regexp: None,
            word_regexp: None,
            flag_truncation: None,
            macro_name: None,
        }
    }
}

/// Pre-normalized word sets for O(1) case-insensitive lookup.
struct NormalizedSets {
    ignore_lower: HashSet<String>,
    only_lower: Option<HashSet<String>>,
}

impl NormalizedSets {
    fn new(config: &PtxConfig) -> Self {
        if config.ignore_case {
            let ignore_lower = config
                .ignore_words
                .iter()
                .map(|w| w.to_lowercase())
                .collect();
            let only_lower = config
                .only_words
                .as_ref()
                .map(|s| s.iter().map(|w| w.to_lowercase()).collect());
            Self {
                ignore_lower,
                only_lower,
            }
        } else {
            // No normalization needed - we'll use the original sets directly
            Self {
                ignore_lower: HashSet::new(),
                only_lower: None,
            }
        }
    }
}

/// Compact KWIC entry — no owned strings, just indices.
struct KwicEntry {
    line_idx: u32,
    word_start: u32,
    word_len: u16,
}

/// Computed layout fields for a KWIC entry — all borrowed slices.
struct LayoutFields<'a> {
    tail: &'a str,
    before: &'a str,
    keyafter: &'a str,
    keyword: &'a str,
    after: &'a str,
    head: &'a str,
    tail_truncated: bool,
    before_truncated: bool,
    keyafter_truncated: bool,
    head_truncated: bool,
}

// Static padding buffer for fast space-filling
const SPACES: &[u8; 256] = b"                                                                                                                                                                                                                                                                ";

/// Write `n` spaces to the writer.
#[inline]
fn write_spaces<W: Write>(out: &mut W, n: usize) -> io::Result<()> {
    let mut remaining = n;
    while remaining > 0 {
        let chunk = remaining.min(SPACES.len());
        out.write_all(&SPACES[..chunk])?;
        remaining -= chunk;
    }
    Ok(())
}

/// Extract words from a line of text.
///
/// GNU ptx's default word regex is effectively `[a-zA-Z][a-zA-Z0-9]*`.
fn extract_words(line: &str) -> Vec<(usize, &str)> {
    let mut words = Vec::new();
    let bytes = line.as_bytes();
    let len = bytes.len();
    let mut i = 0;

    while i < len {
        if bytes[i].is_ascii_alphabetic() {
            let start = i;
            i += 1;
            while i < len && bytes[i].is_ascii_alphanumeric() {
                i += 1;
            }
            words.push((start, &line[start..i]));
        } else {
            i += 1;
        }
    }

    words
}

/// Check if a word should be indexed, using pre-normalized sets.
#[inline]
fn should_index(word: &str, config: &PtxConfig, norm: &NormalizedSets) -> bool {
    if config.ignore_case {
        // Use pre-normalized lowercase sets for O(1) lookup
        if let Some(ref only) = norm.only_lower {
            // Must be in only-set
            let lower = word.to_ascii_lowercase();
            return only.contains(lower.as_str());
        }
        let lower = word.to_ascii_lowercase();
        !norm.ignore_lower.contains(lower.as_str())
    } else {
        if let Some(ref only) = config.only_words {
            return only.contains(word);
        }
        !config.ignore_words.contains(word)
    }
}

/// Generate KWIC entries and compute max_word_length in a single pass.
fn generate_entries(
    lines: &[(String, String)],
    config: &PtxConfig,
    norm: &NormalizedSets,
) -> (Vec<KwicEntry>, usize) {
    let mut entries = Vec::new();
    let mut max_word_length: usize = 0;

    for (line_idx, (_reference, line)) in lines.iter().enumerate() {
        let words = extract_words(line);

        for &(word_start, word) in &words {
            let wlen = word.len();
            if wlen > max_word_length {
                max_word_length = wlen;
            }

            if !should_index(word, config, norm) {
                continue;
            }

            debug_assert!(
                wlen <= u16::MAX as usize,
                "word length {} exceeds u16::MAX",
                wlen
            );
            entries.push(KwicEntry {
                line_idx: line_idx as u32,
                word_start: word_start as u32,
                word_len: wlen as u16,
            });
        }
    }

    // Sort by keyword (case-insensitive if requested), then by reference.
    // Must use stable sort: glibc's qsort is merge-sort (stable), so GNU ptx
    // preserves input order for entries with equal keywords and references.
    if config.ignore_case {
        entries.sort_by(|a, b| {
            let a_line = &lines[a.line_idx as usize].1;
            let b_line = &lines[b.line_idx as usize].1;
            let a_kw = &a_line[a.word_start as usize..a.word_start as usize + a.word_len as usize];
            let b_kw = &b_line[b.word_start as usize..b.word_start as usize + b.word_len as usize];
            a_kw.bytes()
                .map(|c| c.to_ascii_lowercase())
                .cmp(b_kw.bytes().map(|c| c.to_ascii_lowercase()))
                .then_with(|| {
                    lines[a.line_idx as usize]
                        .0
                        .cmp(&lines[b.line_idx as usize].0)
                })
        });
    } else {
        entries.sort_by(|a, b| {
            let a_line = &lines[a.line_idx as usize].1;
            let b_line = &lines[b.line_idx as usize].1;
            let a_kw = &a_line[a.word_start as usize..a.word_start as usize + a.word_len as usize];
            let b_kw = &b_line[b.word_start as usize..b.word_start as usize + b.word_len as usize];
            a_kw.cmp(b_kw).then_with(|| {
                lines[a.line_idx as usize]
                    .0
                    .cmp(&lines[b.line_idx as usize].0)
            })
        });
    }

    (entries, max_word_length)
}

/// Advance past one "word" or one non-word char.
#[inline]
fn skip_something(s: &str, pos: usize) -> usize {
    if pos >= s.len() {
        return pos;
    }
    let bytes = s.as_bytes();
    if bytes[pos].is_ascii_alphabetic() {
        let mut p = pos + 1;
        while p < s.len() && bytes[p].is_ascii_alphanumeric() {
            p += 1;
        }
        p
    } else {
        pos + 1
    }
}

/// Skip whitespace forward from position.
#[inline]
fn skip_white(s: &str, pos: usize) -> usize {
    let bytes = s.as_bytes();
    let mut p = pos;
    while p < s.len() && bytes[p].is_ascii_whitespace() {
        p += 1;
    }
    p
}

/// Skip whitespace backward from position (exclusive end).
#[inline]
fn skip_white_backwards(s: &str, pos: usize, start: usize) -> usize {
    let bytes = s.as_bytes();
    let mut p = pos;
    while p > start && bytes[p - 1].is_ascii_whitespace() {
        p -= 1;
    }
    p
}

/// Compute the layout fields for a KWIC entry.
fn compute_layout<'a>(
    sentence: &'a str,
    word_start: usize,
    keyword_len: usize,
    ref_str: &str,
    config: &PtxConfig,
    max_word_length: usize,
    ref_max_width: usize,
) -> LayoutFields<'a> {
    let total_width = config.width;
    let gap = config.gap_size;
    let trunc_len = 1; // "/" is 1 char

    let ref_width = if ref_str.is_empty() || config.right_reference {
        0
    } else {
        ref_max_width + gap
    };

    let line_width = if total_width > ref_width {
        total_width - ref_width
    } else {
        total_width
    };

    let half_line_width = line_width / 2;

    let before_max_width = if half_line_width > gap + 2 * trunc_len {
        half_line_width - gap - 2 * trunc_len
    } else {
        0
    };
    let keyafter_max_width = if half_line_width > 2 * trunc_len {
        half_line_width - 2 * trunc_len
    } else {
        0
    };

    let line_len = sentence.len();

    // ========== Step 1: Compute keyafter ==========
    let keyafter_start = word_start;
    let mut keyafter_end = word_start + keyword_len;
    {
        let mut cursor = keyafter_end;
        while cursor < line_len && cursor <= keyafter_start + keyafter_max_width {
            keyafter_end = cursor;
            cursor = skip_something(sentence, cursor);
        }
        if cursor <= keyafter_start + keyafter_max_width {
            keyafter_end = cursor;
        }
    }
    let mut keyafter_truncation = keyafter_end < line_len;
    keyafter_end = skip_white_backwards(sentence, keyafter_end, keyafter_start);

    // ========== Compute left_field_start ==========
    let left_context_start: usize = 0;
    let left_field_start = if word_start > half_line_width + max_word_length {
        let lfs = word_start - (half_line_width + max_word_length);
        skip_something(sentence, lfs)
    } else {
        left_context_start
    };

    // ========== Step 2: Compute before ==========
    let mut before_start: usize = left_field_start;
    let mut before_end = keyafter_start;
    before_end = skip_white_backwards(sentence, before_end, before_start);

    while before_start + before_max_width < before_end {
        before_start = skip_something(sentence, before_start);
    }

    let mut before_truncation = {
        let cursor = skip_white_backwards(sentence, before_start, 0);
        cursor > left_context_start
    };

    before_start = skip_white(sentence, before_start);
    let before_len = if before_end > before_start {
        before_end - before_start
    } else {
        0
    };

    // ========== Step 3: Compute tail ==========
    let tail_max_width_raw: isize = before_max_width as isize - before_len as isize - gap as isize;
    let mut tail_start: usize = 0;
    let mut tail_end: usize = 0;
    let mut tail_truncation = false;
    let mut has_tail = false;

    if tail_max_width_raw > 0 {
        let tail_max_width = tail_max_width_raw as usize;
        tail_start = skip_white(sentence, keyafter_end);
        tail_end = tail_start;
        let mut cursor = tail_end;
        while cursor < line_len && cursor < tail_start + tail_max_width {
            tail_end = cursor;
            cursor = skip_something(sentence, cursor);
        }
        if cursor < tail_start + tail_max_width {
            tail_end = cursor;
        }

        if tail_end > tail_start {
            has_tail = true;
            keyafter_truncation = false;
            tail_truncation = tail_end < line_len;
        } else {
            tail_truncation = false;
        }

        tail_end = skip_white_backwards(sentence, tail_end, tail_start);
    }

    // ========== Step 4: Compute head ==========
    let keyafter_len = if keyafter_end > keyafter_start {
        keyafter_end - keyafter_start
    } else {
        0
    };
    let head_max_width_raw: isize =
        keyafter_max_width as isize - keyafter_len as isize - gap as isize;
    let mut head_start: usize = 0;
    let mut head_end: usize = 0;
    let mut head_truncation = false;
    let mut has_head = false;

    if head_max_width_raw > 0 {
        let head_max_width = head_max_width_raw as usize;
        head_end = skip_white_backwards(sentence, before_start, 0);

        head_start = left_field_start;
        while head_start + head_max_width < head_end {
            head_start = skip_something(sentence, head_start);
        }

        if head_end > head_start {
            has_head = true;
            before_truncation = false;
            head_truncation = {
                let cursor = skip_white_backwards(sentence, head_start, 0);
                cursor > left_context_start
            };
        } else {
            head_truncation = false;
        }

        if head_end > head_start {
            head_start = skip_white(sentence, head_start);
        }
    }

    // ========== Extract text fields as slices ==========
    let before_text = if before_len > 0 {
        &sentence[before_start..before_end]
    } else {
        ""
    };
    let keyafter_text = if keyafter_end > keyafter_start {
        &sentence[keyafter_start..keyafter_end]
    } else {
        ""
    };
    let tail_text = if has_tail && tail_end > tail_start {
        &sentence[tail_start..tail_end]
    } else {
        ""
    };
    let head_text = if has_head && head_end > head_start {
        &sentence[head_start..head_end]
    } else {
        ""
    };

    let keyword_text = &sentence[word_start..word_start + keyword_len];
    let after_start = word_start + keyword_len;
    let after_text = if keyafter_end > after_start {
        &sentence[after_start..keyafter_end]
    } else {
        ""
    };

    LayoutFields {
        tail: tail_text,
        before: before_text,
        keyafter: keyafter_text,
        keyword: keyword_text,
        after: after_text,
        head: head_text,
        tail_truncated: tail_truncation,
        before_truncated: before_truncation,
        keyafter_truncated: keyafter_truncation,
        head_truncated: head_truncation,
    }
}

/// Write a KWIC entry in plain text format directly to the output.
fn write_plain<W: Write>(
    out: &mut W,
    ref_str: &str,
    config: &PtxConfig,
    layout: &LayoutFields<'_>,
    ref_max_width: usize,
) -> io::Result<()> {
    let total_width = config.width;
    let gap = config.gap_size;
    let trunc_str = config.flag_truncation.as_deref().unwrap_or("/");
    let trunc_len = trunc_str.len();

    let ref_width = if ref_str.is_empty() || config.right_reference {
        0
    } else {
        ref_max_width + gap
    };

    let line_width = if total_width > ref_width {
        total_width - ref_width
    } else {
        total_width
    };

    let half_line_width = line_width / 2;

    let before_trunc_len = if layout.before_truncated {
        trunc_len
    } else {
        0
    };
    let keyafter_trunc_len = if layout.keyafter_truncated {
        trunc_len
    } else {
        0
    };
    let tail_trunc_len = if layout.tail_truncated { trunc_len } else { 0 };
    let head_trunc_len = if layout.head_truncated { trunc_len } else { 0 };

    // Reference prefix (if not right_reference)
    if !config.right_reference {
        if !ref_str.is_empty() && config.auto_reference {
            out.write_all(ref_str.as_bytes())?;
            out.write_all(b":")?;
            let ref_total = ref_str.len() + 1;
            let ref_pad_total = ref_max_width + gap;
            write_spaces(out, ref_pad_total.saturating_sub(ref_total))?;
        } else if !ref_str.is_empty() {
            out.write_all(ref_str.as_bytes())?;
            let ref_pad_total = ref_max_width + gap;
            write_spaces(out, ref_pad_total.saturating_sub(ref_str.len()))?;
        } else {
            write_spaces(out, gap)?;
        }
    }

    // Left half: [tail][tail_trunc] ... padding ... [before_trunc][before]
    if !layout.tail.is_empty() {
        out.write_all(layout.tail.as_bytes())?;
        if layout.tail_truncated {
            out.write_all(trunc_str.as_bytes())?;
        }
        let tail_used = layout.tail.len() + tail_trunc_len;
        let before_used = layout.before.len() + before_trunc_len;
        let padding = half_line_width
            .saturating_sub(gap)
            .saturating_sub(tail_used)
            .saturating_sub(before_used);
        write_spaces(out, padding)?;
    } else {
        let before_used = layout.before.len() + before_trunc_len;
        let padding = half_line_width
            .saturating_sub(gap)
            .saturating_sub(before_used);
        write_spaces(out, padding)?;
    }

    if layout.before_truncated {
        out.write_all(trunc_str.as_bytes())?;
    }
    out.write_all(layout.before.as_bytes())?;

    // Gap
    write_spaces(out, gap)?;

    // Right half: [keyafter][keyafter_trunc] ... padding ... [head_trunc][head]
    out.write_all(layout.keyafter.as_bytes())?;
    if layout.keyafter_truncated {
        out.write_all(trunc_str.as_bytes())?;
    }

    if !layout.head.is_empty() {
        let keyafter_used = layout.keyafter.len() + keyafter_trunc_len;
        let head_used = layout.head.len() + head_trunc_len;
        let padding = half_line_width
            .saturating_sub(keyafter_used)
            .saturating_sub(head_used);
        write_spaces(out, padding)?;
        if layout.head_truncated {
            out.write_all(trunc_str.as_bytes())?;
        }
        out.write_all(layout.head.as_bytes())?;
    } else if !ref_str.is_empty() && config.right_reference {
        let keyafter_used = layout.keyafter.len() + keyafter_trunc_len;
        let padding = half_line_width.saturating_sub(keyafter_used);
        write_spaces(out, padding)?;
    }

    // Reference on the right (if right_reference)
    if !ref_str.is_empty() && config.right_reference {
        write_spaces(out, gap)?;
        out.write_all(ref_str.as_bytes())?;
    }

    out.write_all(b"\n")
}

/// Escape a string for roff output (backslashes and quotes).
fn escape_roff(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

/// Write a KWIC entry in roff format directly to output.
fn write_roff<W: Write>(
    out: &mut W,
    ref_str: &str,
    config: &PtxConfig,
    layout: &LayoutFields<'_>,
    escaped_trunc: &str,
) -> io::Result<()> {
    let macro_name = config.macro_name.as_deref().unwrap_or("xx");

    out.write_all(b".")?;
    out.write_all(macro_name.as_bytes())?;

    // tail
    out.write_all(b" \"")?;
    out.write_all(escape_roff(layout.tail).as_bytes())?;
    if layout.tail_truncated {
        out.write_all(escaped_trunc.as_bytes())?;
    }

    // before
    out.write_all(b"\" \"")?;
    if layout.before_truncated {
        out.write_all(escaped_trunc.as_bytes())?;
    }
    out.write_all(escape_roff(layout.before).as_bytes())?;

    // keyafter
    out.write_all(b"\" \"")?;
    out.write_all(escape_roff(layout.keyafter).as_bytes())?;
    if layout.keyafter_truncated {
        out.write_all(escaped_trunc.as_bytes())?;
    }

    // head
    out.write_all(b"\" \"")?;
    if layout.head_truncated {
        out.write_all(escaped_trunc.as_bytes())?;
    }
    out.write_all(escape_roff(layout.head).as_bytes())?;
    out.write_all(b"\"")?;

    // reference
    if !ref_str.is_empty() {
        out.write_all(b" \"")?;
        out.write_all(escape_roff(ref_str).as_bytes())?;
        out.write_all(b"\"")?;
    }

    out.write_all(b"\n")
}

/// Escape a string for TeX output.
fn escape_tex(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '\\' => result.push_str("\\backslash "),
            '{' => result.push_str("\\{"),
            '}' => result.push_str("\\}"),
            '$' => result.push_str("\\$"),
            '&' => result.push_str("\\&"),
            '#' => result.push_str("\\#"),
            '_' => result.push_str("\\_"),
            '^' => result.push_str("\\^{}"),
            '~' => result.push_str("\\~{}"),
            '%' => result.push_str("\\%"),
            _ => result.push(ch),
        }
    }
    result
}

/// Write a KWIC entry in TeX format directly to output.
fn write_tex<W: Write>(
    out: &mut W,
    ref_str: &str,
    config: &PtxConfig,
    layout: &LayoutFields<'_>,
) -> io::Result<()> {
    let macro_name = config.macro_name.as_deref().unwrap_or("xx");

    out.write_all(b"\\")?;
    out.write_all(macro_name.as_bytes())?;
    out.write_all(b" {")?;
    out.write_all(escape_tex(layout.tail).as_bytes())?;
    out.write_all(b"}{")?;
    out.write_all(escape_tex(layout.before).as_bytes())?;
    out.write_all(b"}{")?;
    out.write_all(escape_tex(layout.keyword).as_bytes())?;
    out.write_all(b"}{")?;
    out.write_all(escape_tex(layout.after).as_bytes())?;
    out.write_all(b"}{")?;
    out.write_all(escape_tex(layout.head).as_bytes())?;
    out.write_all(b"}")?;

    if !ref_str.is_empty() {
        out.write_all(b"{")?;
        out.write_all(escape_tex(ref_str).as_bytes())?;
        out.write_all(b"}")?;
    }

    out.write_all(b"\n")
}

/// Process lines from a single source, grouping them into sentence contexts.
fn process_lines_into_contexts(
    content: &str,
    filename: Option<&str>,
    config: &PtxConfig,
    lines_out: &mut Vec<(String, String)>,
    global_line_num: &mut usize,
) {
    let mut current_text = String::new();
    let mut context_ref = String::new();
    let mut first_line_of_context = true;

    for line in content.lines() {
        *global_line_num += 1;

        let reference = if config.auto_reference {
            match filename {
                Some(name) => format!("{}:{}", name, global_line_num),
                None => format!("{}", global_line_num),
            }
        } else {
            String::new()
        };

        if first_line_of_context {
            context_ref = reference;
            first_line_of_context = false;
        }

        if !current_text.is_empty() {
            current_text.push(' ');
        }
        current_text.push_str(line);

        let trimmed = line.trim_end();
        let ends_with_terminator =
            trimmed.ends_with('.') || trimmed.ends_with('?') || trimmed.ends_with('!');

        if ends_with_terminator || line.is_empty() {
            if !current_text.trim().is_empty() {
                lines_out.push((context_ref.clone(), current_text.clone()));
            }
            current_text.clear();
            first_line_of_context = true;
        }
    }

    if !current_text.trim().is_empty() {
        lines_out.push((context_ref.clone(), current_text.clone()));
    }
}

fn format_and_write<W: Write>(
    lines: &[(String, String)],
    output: &mut W,
    config: &PtxConfig,
) -> io::Result<()> {
    let norm = NormalizedSets::new(config);
    let (entries, max_word_length) = generate_entries(lines, config, &norm);

    // Compute maximum reference width
    let ref_max_width = if config.auto_reference || config.references {
        entries
            .iter()
            .map(|e| lines[e.line_idx as usize].0.len())
            .max()
            .unwrap_or(0)
    } else {
        0
    };

    // Pre-compute escaped truncation flag for roff mode (avoids per-entry allocation)
    let escaped_trunc = if config.format == OutputFormat::Roff {
        escape_roff(config.flag_truncation.as_deref().unwrap_or("/"))
    } else {
        String::new()
    };

    for entry in &entries {
        let line_data = &lines[entry.line_idx as usize];
        let ref_str = if config.auto_reference || config.references {
            &line_data.0
        } else {
            ""
        };
        let sentence = &line_data.1;
        let word_start = entry.word_start as usize;
        let keyword_len = entry.word_len as usize;

        let layout = compute_layout(
            sentence,
            word_start,
            keyword_len,
            ref_str,
            config,
            max_word_length,
            ref_max_width,
        );

        match config.format {
            OutputFormat::Plain => write_plain(output, ref_str, config, &layout, ref_max_width)?,
            OutputFormat::Roff => {
                write_roff(output, ref_str, config, &layout, &escaped_trunc)?;
            }
            OutputFormat::Tex => write_tex(output, ref_str, config, &layout)?,
        }
    }

    Ok(())
}

/// Generate a permuted index from input.
pub fn generate_ptx<R: BufRead, W: Write>(
    mut input: R,
    output: &mut W,
    config: &PtxConfig,
) -> io::Result<()> {
    let mut content = String::new();
    input.read_to_string(&mut content)?;

    let mut lines: Vec<(String, String)> = Vec::new();
    let mut global_line_num = 0usize;
    process_lines_into_contexts(&content, None, config, &mut lines, &mut global_line_num);

    format_and_write(&lines, output, config)
}

/// Generate a permuted index from multiple named file contents.
pub fn generate_ptx_multi<W: Write>(
    file_contents: &[(Option<String>, String)],
    output: &mut W,
    config: &PtxConfig,
) -> io::Result<()> {
    let mut lines: Vec<(String, String)> = Vec::new();
    let mut global_line_num = 0usize;

    for (filename, content) in file_contents {
        process_lines_into_contexts(
            content,
            filename.as_deref(),
            config,
            &mut lines,
            &mut global_line_num,
        );
    }

    format_and_write(&lines, output, config)
}

/// Read a word list file (one word per line) into a HashSet.
pub fn read_word_file(path: &str) -> io::Result<HashSet<String>> {
    let content = std::fs::read_to_string(path)?;
    Ok(content
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect())
}