ferrocat-po 2.0.0

Performance-first PO parsing, serialization, and catalog update primitives for ferrocat.
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
use std::borrow::Cow;

use crate::ParseError;
use crate::scan::{find_byte, find_escapable_byte, find_quoted_bounds, has_byte};
use crate::utf8::{input_slice_as_str, string_from_utf8};

/// Escapes a PO string literal payload.
#[must_use]
pub fn escape_string(input: &str) -> String {
    let bytes = input.as_bytes();
    let Some(first_escape) = find_escapable_byte(bytes) else {
        return input.to_owned();
    };

    let mut out = String::with_capacity(input.len() + 8);
    out.push_str(&input[..first_escape]);
    escape_string_from(&mut out, input, bytes, first_escape);

    out
}

pub fn escape_string_into(out: &mut String, input: &str) {
    let bytes = input.as_bytes();
    let Some(first_escape) = find_escapable_byte(bytes) else {
        out.push_str(input);
        return;
    };

    escape_string_into_known(out, input, first_escape);
}

pub fn escape_string_into_with_first_escape(
    out: &mut String,
    input: &str,
    first_escape: Option<usize>,
) {
    let Some(first_escape) = first_escape else {
        out.push_str(input);
        return;
    };

    escape_string_into_known(out, input, first_escape);
}

/// Unescapes a PO string literal payload.
///
/// # Errors
///
/// Returns [`ParseError`] when the escape sequence is malformed.
pub fn unescape_string(input: &str) -> Result<String, ParseError> {
    if !has_byte(b'\\', input.as_bytes()) {
        return Ok(input.to_owned());
    }

    unescape_string_known(input)
}

/// Unescapes a PO string literal payload that is already known to contain at
/// least one backslash, skipping the redundant lookup scan.
///
/// # Errors
///
/// Returns [`ParseError`] when the escape sequence is malformed.
pub(crate) fn unescape_string_known(input: &str) -> Result<String, ParseError> {
    let bytes = input.as_bytes();
    let mut out = Vec::with_capacity(input.len());
    let mut index = 0;

    while index < bytes.len() {
        let next_escape = if let Some(relative) = find_byte(b'\\', &bytes[index..]) {
            index + relative
        } else {
            out.extend_from_slice(&bytes[index..]);
            break;
        };

        out.extend_from_slice(&bytes[index..next_escape]);
        index = next_escape + 1;
        if index >= bytes.len() {
            return Err(ParseError::new("unterminated escape sequence"));
        }

        let escaped = bytes[index];
        match escaped {
            b'a' => out.push(b'\x07'),
            b'b' => out.push(b'\x08'),
            b't' => out.push(b'\t'),
            b'n' => out.push(b'\n'),
            b'v' => out.push(b'\x0b'),
            b'f' => out.push(b'\x0c'),
            b'r' => out.push(b'\r'),
            b'\'' => out.push(b'\''),
            b'"' => out.push(b'"'),
            b'\\' => out.push(b'\\'),
            b'?' => out.push(b'?'),
            b'0'..=b'7' => {
                let mut value = u32::from(escaped - b'0');
                let mut consumed = 1;
                while consumed < 3 && index + consumed < bytes.len() {
                    let next = bytes[index + consumed];
                    if !(b'0'..=b'7').contains(&next) {
                        break;
                    }
                    value = (value * 8) + u32::from(next - b'0');
                    consumed += 1;
                }
                match char::from_u32(value) {
                    Some(ch) => push_char_bytes(&mut out, ch),
                    None => return Err(ParseError::new("invalid octal escape value")),
                }
                index += consumed - 1;
            }
            b'x' => {
                if index + 2 >= bytes.len() {
                    return Err(ParseError::new("incomplete hex escape"));
                }
                let hi = decode_hex(bytes[index + 1])?;
                let lo = decode_hex(bytes[index + 2])?;
                let value = u32::from((hi << 4) | lo);
                match char::from_u32(value) {
                    Some(ch) => push_char_bytes(&mut out, ch),
                    None => return Err(ParseError::new("invalid hex escape value")),
                }
                index += 2;
            }
            other => out.push(other),
        }

        index += 1;
    }

    Ok(string_from_utf8(out))
}

/// Extracts and unescapes the first quoted PO string from `line`, borrowing
/// from the input when no escapes are present.
///
/// # Errors
///
/// Returns [`ParseError`] when the quoted content is malformed.
pub fn extract_quoted_cow(line: &str) -> Result<Cow<'_, str>, ParseError> {
    extract_quoted_bytes_cow(line.as_bytes())
}

pub fn extract_quoted_bytes_cow(line: &[u8]) -> Result<Cow<'_, str>, ParseError> {
    let Some((start, end)) = find_quoted_bounds(line) else {
        return Ok(Cow::Borrowed(""));
    };

    let raw = &line[start..end];
    if !validate_quoted_content(raw)? {
        return Ok(Cow::Borrowed(bytes_to_str(raw)));
    }

    Ok(Cow::Owned(unescape_string_known(bytes_to_str(raw))?))
}

/// Extracts and unescapes the first quoted PO string from `line`.
///
/// # Errors
///
/// Returns [`ParseError`] when the quoted content is malformed.
pub fn extract_quoted(line: &str) -> Result<String, ParseError> {
    Ok(extract_quoted_bytes_cow(line.as_bytes())?.into_owned())
}

/// Visits each reference token in a `#:` comment line, borrowing from `input`
/// where possible and allocating nothing in the common single-reference case.
///
/// The parsers push tokens straight into their item buffers through this entry
/// point, avoiding the throwaway `Vec` that collecting first would require for
/// every reference line.
pub fn for_each_reference_token<'a>(input: &'a str, mut visit: impl FnMut(Cow<'a, str>)) {
    let trimmed = input.trim();
    if trimmed.is_empty() {
        visit(Cow::Borrowed(""));
        return;
    }

    // Fast path for the overwhelmingly common single-reference line (e.g.
    // `src/app.rs:42`). A run of graphic ASCII bytes cannot contain a
    // whitespace split point or a multi-byte directional isolate, so it is one
    // token by definition. Emitting it directly skips the char-by-char scan and
    // the `parts` buffer the slow path needs to validate multi-token splits.
    if trimmed.bytes().all(|byte| byte.is_ascii_graphic()) {
        visit(Cow::Borrowed(trimmed));
        return;
    }

    let mut parts = Vec::new();
    let mut start = None;
    let mut isolate_depth = 0usize;

    for (index, ch) in trimmed.char_indices() {
        match ch {
            '\u{2068}' => {
                if start.is_none() {
                    start = Some(index);
                }
                isolate_depth += 1;
            }
            '\u{2069}' => {
                if start.is_none() {
                    start = Some(index);
                }
                isolate_depth = isolate_depth.saturating_sub(1);
            }
            _ if ch.is_whitespace() && isolate_depth == 0 => {
                if let Some(segment_start) = start.take()
                    && segment_start < index
                {
                    parts.push(normalize_reference_token(&trimmed[segment_start..index]));
                }
            }
            _ => {
                if start.is_none() {
                    start = Some(index);
                }
            }
        }
    }

    if let Some(segment_start) = start
        && segment_start < trimmed.len()
    {
        parts.push(normalize_reference_token(&trimmed[segment_start..]));
    }

    if parts.len() == 1 {
        visit(normalize_reference_token(trimmed));
        return;
    }

    if parts.iter().all(|part| part.contains(':')) {
        for part in parts {
            visit(part);
        }
        return;
    }

    visit(Cow::Borrowed(trimmed));
}

/// Validates the content between PO quotes and reports whether it contains any
/// backslash, so callers can skip a redundant escape-lookup scan.
///
/// Returns `Ok(true)` when an unescape pass is required, `Ok(false)` when the
/// content can be borrowed verbatim.
///
/// # Errors
///
/// Returns [`ParseError`] when an unescaped quote is present.
pub fn validate_quoted_content(raw: &[u8]) -> Result<bool, ParseError> {
    let mut trailing_backslashes = 0usize;
    let mut saw_backslash = false;

    for &byte in raw {
        match byte {
            b'\\' => {
                trailing_backslashes += 1;
                saw_backslash = true;
            }
            b'"' if has_even_trailing_backslashes(trailing_backslashes) => {
                return Err(ParseError::new("unescaped quote in string literal"));
            }
            _ => trailing_backslashes = 0,
        }
    }

    Ok(saw_backslash)
}

fn has_even_trailing_backslashes(count: usize) -> bool {
    count.is_multiple_of(2)
}

fn escape_string_from(out: &mut String, input: &str, bytes: &[u8], first_escape: usize) {
    let mut start = first_escape;

    loop {
        push_escape(out, bytes[start]);
        let next_index = start + 1;
        let Some(relative) = find_escapable_byte(&bytes[next_index..]) else {
            out.push_str(&input[next_index..]);
            break;
        };

        let absolute = next_index + relative;
        out.push_str(&input[next_index..absolute]);
        start = absolute;
    }
}

#[inline]
fn escape_string_into_known(out: &mut String, input: &str, first_escape: usize) {
    let bytes = input.as_bytes();
    out.push_str(&input[..first_escape]);
    escape_string_from(out, input, bytes, first_escape);
}

fn push_escape(out: &mut String, byte: u8) {
    out.push('\\');
    out.push(match byte {
        b'\x07' => 'a',
        b'\x08' => 'b',
        b'\t' => 't',
        b'\n' => 'n',
        b'\x0b' => 'v',
        b'\x0c' => 'f',
        b'\r' => 'r',
        b'"' => '"',
        b'\\' => '\\',
        _ => unreachable!("unexpected escape byte"),
    });
}

fn decode_hex(byte: u8) -> Result<u8, ParseError> {
    match byte {
        b'0'..=b'9' => Ok(byte - b'0'),
        b'a'..=b'f' => Ok(byte - b'a' + 10),
        b'A'..=b'F' => Ok(byte - b'A' + 10),
        _ => Err(ParseError::new("invalid hex escape")),
    }
}

fn push_char_bytes(out: &mut Vec<u8>, ch: char) {
    if ch.is_ascii() {
        out.push(ch as u8);
        return;
    }

    let mut buf = [0u8; 4];
    out.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
}

fn bytes_to_str(bytes: &[u8]) -> &str {
    input_slice_as_str(bytes)
}

fn normalize_reference_token(input: &str) -> Cow<'_, str> {
    if !input.contains('\u{2068}') && !input.contains('\u{2069}') {
        return Cow::Borrowed(input);
    }

    Cow::Owned(
        input
            .chars()
            .filter(|ch| *ch != '\u{2068}' && *ch != '\u{2069}')
            .collect(),
    )
}

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use super::{
        escape_string, escape_string_into, escape_string_into_with_first_escape, extract_quoted,
        extract_quoted_bytes_cow, extract_quoted_cow, for_each_reference_token, unescape_string,
        validate_quoted_content,
    };

    fn split_reference_comment(input: &str) -> Vec<Cow<'_, str>> {
        let mut parts = Vec::new();
        for_each_reference_token(input, |token| parts.push(token));
        parts
    }

    #[test]
    fn escapes_special_characters() {
        assert_eq!(escape_string("Say \"Hi\""), "Say \\\"Hi\\\"");
        assert_eq!(escape_string("a\tb"), "a\\tb");
    }

    #[test]
    fn escapes_all_po_control_sequences_and_plain_strings() {
        assert_eq!(escape_string("plain"), "plain");
        assert_eq!(
            escape_string("\u{0007}\u{0008}\u{000b}\u{000c}\r\\"),
            "\\a\\b\\v\\f\\r\\\\"
        );
    }

    #[test]
    fn unescapes_c_sequences() {
        assert_eq!(
            unescape_string("\\a\\b\\t\\n\\v\\f\\r\\'\\\"\\\\\\?").as_deref(),
            Ok("\u{0007}\u{0008}\t\n\u{000b}\u{000c}\r'\"\\?")
        );
    }

    #[test]
    fn unescapes_plain_unicode_and_uppercase_hex_sequences() {
        assert_eq!(unescape_string("plain").as_deref(), Ok("plain"));
        assert_eq!(unescape_string("\\351\\x41").as_deref(), Ok("\u{00e9}A"));
    }

    #[test]
    fn extracts_and_unescapes_quoted_text() {
        assert_eq!(
            extract_quoted(
                "msgid \"The name field must not contain characters like \\\" or \\\\\""
            )
            .as_deref(),
            Ok("The name field must not contain characters like \" or \\")
        );
    }

    #[test]
    fn borrows_simple_quoted_text_without_escape() {
        assert_eq!(
            extract_quoted_cow("msgid \"plain text\""),
            Ok(Cow::Borrowed("plain text"))
        );
    }

    #[test]
    fn appends_escaped_text_into_existing_buffer() {
        let mut out = String::from("prefix:");
        escape_string_into(&mut out, "Say \"Hi\"\n");
        assert_eq!(out, "prefix:Say \\\"Hi\\\"\\n");
    }

    #[test]
    fn appends_escaped_text_into_existing_buffer_with_known_escape() {
        let mut out = String::from("prefix:");
        escape_string_into_with_first_escape(&mut out, "Say \"Hi\"\n", Some(4));
        assert_eq!(out, "prefix:Say \\\"Hi\\\"\\n");
    }

    #[test]
    fn appends_plain_text_when_no_escape_index_is_known() {
        let mut out = String::from("prefix:");
        escape_string_into_with_first_escape(&mut out, "plain", None);
        assert_eq!(out, "prefix:plain");

        escape_string_into(&mut out, "-more");
        assert_eq!(out, "prefix:plain-more");
    }

    #[test]
    fn extracts_quoted_text_from_bytes() {
        assert_eq!(
            extract_quoted_bytes_cow(br#"msgid "byte path""#),
            Ok(Cow::Borrowed("byte path"))
        );
    }

    #[test]
    fn extracts_owned_quoted_text_when_unescaping_is_required() {
        assert_eq!(
            extract_quoted_bytes_cow(br#"msgid "line\nbreak""#),
            Ok(Cow::Owned("line\nbreak".to_owned()))
        );
        assert_eq!(extract_quoted("msgid bare"), Ok(String::new()));
    }

    #[test]
    fn splits_multiple_reference_tokens() {
        assert_eq!(
            split_reference_comment("src/app.js:1 src/lib.js:2"),
            vec![Cow::Borrowed("src/app.js:1"), Cow::Borrowed("src/lib.js:2")]
        );
    }

    #[test]
    fn borrows_single_reference_token_via_fast_path() {
        assert_eq!(
            split_reference_comment("src/app.js:1"),
            vec![Cow::Borrowed("src/app.js:1")]
        );
    }

    #[test]
    fn borrows_non_ascii_single_reference_token() {
        // Non-ASCII bytes skip the graphic-ASCII fast path and exercise the
        // slow path's single-token branch, which still borrows when there are
        // no directional isolates to strip.
        assert_eq!(
            split_reference_comment("café.py:1"),
            vec![Cow::Borrowed("café.py:1")]
        );
    }

    #[test]
    fn strips_leading_isolate_from_single_reference_token() {
        // A token that opens with a closing isolate reaches the slow path with
        // no active segment, covering the isolate handling before any content.
        assert_eq!(
            split_reference_comment("\u{2069}app.py:1"),
            vec![Cow::<str>::Owned("app.py:1".to_owned())]
        );
    }

    #[test]
    fn preserves_standard_input_reference_lines() {
        assert_eq!(
            split_reference_comment("standard input:12 standard input:17"),
            vec![Cow::Borrowed("standard input:12 standard input:17")]
        );
    }

    #[test]
    fn strips_isolates_when_splitting_reference_tokens() {
        assert_eq!(
            split_reference_comment("\u{2068}main 1.py\u{2069}:1 other.py:2"),
            vec![
                Cow::Owned("main 1.py:1".to_owned()),
                Cow::Borrowed("other.py:2"),
            ]
        );
    }

    #[test]
    fn keeps_non_reference_whitespace_groups_and_empty_input_stable() {
        assert_eq!(
            split_reference_comment("foo bar"),
            vec![Cow::Borrowed("foo bar")]
        );
        assert_eq!(split_reference_comment("   "), vec![Cow::Borrowed("")]);
    }

    #[test]
    fn rejects_unescaped_quote_in_string_literal() {
        assert_eq!(
            validate_quoted_content(br#"Some msgstr with "double\" quotes"#)
                .expect_err("expected unescaped quote error")
                .to_string(),
            "unescaped quote in string literal"
        );
    }

    #[test]
    fn unescape_string_covers_octal_hex_and_error_paths() {
        assert_eq!(unescape_string("\\101\\x42").as_deref(), Ok("AB"));
        assert_eq!(
            unescape_string("\\x4")
                .expect_err("incomplete hex escape")
                .to_string(),
            "incomplete hex escape"
        );
        assert_eq!(
            unescape_string("\\xZZ")
                .expect_err("invalid hex escape")
                .to_string(),
            "invalid hex escape"
        );
        assert!(validate_quoted_content(br#"still safe\""#).is_ok());
    }
}