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
// SPDX-FileCopyrightText: 2026 Sébastien Helleu <flashcode@flashtux.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
//! Utilities for escaping and unescaping strings for PO file format.
pub trait EscapePoExt {
/// Escape special characters in a string for PO file format.
fn escape_po(&self) -> String;
/// Unescape special character sequences in a string from a PO file.
fn unescape_po(&self) -> String;
}
impl EscapePoExt for str {
/// Escape special characters in a string for PO file format.
fn escape_po(&self) -> String {
let mut out = String::with_capacity(self.len() * 2);
for ch in self.chars() {
match ch {
'\n' => {
out.push('\\');
out.push('n');
}
'\r' => {
out.push('\\');
out.push('r');
}
'\t' => {
out.push('\\');
out.push('t');
}
'"' => {
out.push('\\');
out.push('"');
}
'\\' => {
out.push('\\');
out.push('\\');
}
_ => out.push(ch),
}
}
out
}
/// Unescape special character sequences in a string from a PO file.
fn unescape_po(&self) -> String {
let mut out = String::with_capacity(self.len());
let mut it = self.chars().peekable();
while let Some(ch) = it.next() {
if ch == '\\' {
match it.peek().copied() {
Some('n') => {
out.push('\n');
it.next();
}
Some('r') => {
out.push('\r');
it.next();
}
Some('t') => {
out.push('\t');
it.next();
}
Some('"') => {
out.push('"');
it.next();
}
Some('\\') => {
out.push('\\');
it.next();
}
Some(other) => {
out.push('\\');
out.push(other);
it.next();
}
None => out.push('\\'),
}
} else {
out.push(ch);
}
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn escape_basic() {
assert_eq!("".escape_po(), "");
assert_eq!("abc".escape_po(), "abc");
}
#[test]
fn escape_specials() {
assert_eq!("\n".escape_po(), "\\n");
assert_eq!("\r".escape_po(), "\\r");
assert_eq!("\t".escape_po(), "\\t");
assert_eq!("\"".escape_po(), "\\\"");
assert_eq!("\\".escape_po(), "\\\\");
}
#[test]
fn escape_mixed() {
let s = "a\\b\nc\td\"e";
let expected = "a\\\\b\\nc\\td\\\"e";
assert_eq!(s.escape_po(), expected);
}
#[test]
fn unescape_basic() {
assert_eq!("".unescape_po(), "");
assert_eq!("abc".unescape_po(), "abc");
}
#[test]
fn unescape_specials() {
assert_eq!("\\n".unescape_po(), "\n");
assert_eq!("\\r".unescape_po(), "\r");
assert_eq!("\\t".unescape_po(), "\t");
assert_eq!("\\\"".unescape_po(), "\"");
assert_eq!("\\".unescape_po(), "\\");
assert_eq!("\\\\".unescape_po(), "\\");
}
#[test]
fn unescape_unknown_sequence_is_kept() {
assert_eq!("\\x".unescape_po(), "\\x");
assert_eq!("test\\qval".unescape_po(), "test\\qval");
}
#[test]
fn roundtrip() {
let samples = [
"",
"plain text",
"line1\nline2",
"tab\there",
"quote:\"",
"backslash \\",
"mix\r\n\t\"end",
"utf8: café – 測試",
];
for &s in &samples {
let escaped = s.escape_po();
let unescaped = escaped.unescape_po();
assert_eq!(unescaped, s, "failed roundtrip for: {s:?} -> {escaped:?}");
}
}
}