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
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
use alloc::borrow::Cow;
use core::fmt;
pub mod code_pages;
pub(crate) mod decoder;
mod encoder;
pub(crate) use encoder::Encoder;
#[derive(Debug)]
pub struct EncodeError {}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Character in UTF-8 string has no mapping defined in code page")
}
}
#[cfg(feature = "std")]
impl std::error::Error for EncodeError {}
pub trait CodePage: Encoder {
/// Encode UTF-8 string into single-byte encoding
///
/// Undefined characters will result in [`EncodeError`]
///
/// # Examples
///
/// ```
/// use yore::{CodePage, EncodeError};
///
/// // Erase type for example - prefer concrete type over trait object whenever possible
/// let cp850: &dyn CodePage = &yore::code_pages::CP850;
/// assert_eq!(cp850.encode("text").unwrap(), vec![116, 101, 120, 116]);
/// assert!(matches!(cp850.encode("text 🦀"), EncodeError));
/// ```
#[inline]
fn encode<'a>(&self, s: &'a str) -> Result<Cow<'a, [u8]>, EncodeError> {
self.encode_helper(s, None)
}
/// Encode UTF-8 string into single-byte encoding
///
/// Undefined characters will be replaced with byte `fallback`
///
/// # Examples
///
/// ```
/// use yore::CodePage;
///
/// // Erase type for example - prefer concrete type over trait object whenever possible
/// let cp850: &dyn CodePage = &yore::code_pages::CP850;
/// assert_eq!(cp850.encode_lossy("text 🦀", 168), vec![116, 101, 120, 116, 32, 168])
/// ```
#[inline]
fn encode_lossy<'a>(&self, s: &'a str, fallback: u8) -> Cow<'a, [u8]> {
self.encode_helper(s, Some(fallback)).unwrap()
}
/// Decode single-byte encoding into UTF-8 string
///
/// Undefined codepoints will result in [`DecodeError`]
///
/// # Examples
///
/// ```
/// use yore::{CodePage, DecodeError};
///
/// // Erase types for example - prefer concrete type over trait object whenever possible
/// let cp850: &dyn CodePage = &yore::code_pages::CP850;
/// let cp857: &dyn CodePage = &yore::code_pages::CP857;
/// assert_eq!(cp850.decode(&[116, 101, 120, 116]).unwrap(), "text");
///
/// //codepoint 231 is undefined
/// assert!(matches!(cp857.decode(&[116, 101, 120, 116, 231]), Err(DecodeError{position: 4, value: 231})));
/// ```
fn decode<'a>(&self, bytes: &'a [u8]) -> Result<Cow<'a, str>, DecodeError>;
/// Decode single-byte encoding into UTF-8 string
///
/// Undefined codepoints will be replaced with `'�'`
///
/// # Examples
///
/// ```
/// use yore::CodePage;
///
/// // Erase type for example - prefer concrete type over trait object whenever possible
/// let cp857: &dyn CodePage = &yore::code_pages::CP857;
/// //codepoint 231 is undefined
/// assert_eq!(cp857.decode_lossy(&[116, 101, 120, 116, 32, 231]), "text �");
/// ```
#[inline(always)]
fn decode_lossy<'a>(&self, bytes: &'a [u8]) -> Cow<'a, str> {
self.decode(bytes).unwrap()
}
/// Decode single-byte encoding into UTF-8 string
///
/// Undefined codepoints will be replaced with `fallback`
///
/// # Examples
///
/// ```
/// use yore::CodePage;
///
/// // Erase type for example - prefer concrete type over trait object whenever possible
/// let cp857: &dyn CodePage = &yore::code_pages::CP857;
/// //codepoint 231 is undefined
/// assert_eq!(cp857.decode_lossy_fallback(&[116, 101, 120, 116, 32, 231], '�'), "text �");
/// ```
#[inline(always)]
fn decode_lossy_fallback<'a>(&self, bytes: &'a [u8], _fallback: char) -> Cow<'a, str> {
self.decode(bytes).unwrap()
}
}
#[derive(Debug)]
pub struct DecodeError {
pub position: usize,
pub value: u8,
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Undefined codepoint {} at offset {}",
self.value, self.position
)
}
}
#[cfg(feature = "std")]
impl std::error::Error for DecodeError {}
#[cfg(test)]
mod tests {
use crate::code_pages::{CP1253, CP1255, CP1257, CP857, CP864, CP869, CP874};
use crate::CodePage;
#[test]
fn test_nonstandard_ascii() {
let bytes = [0x25, 253];
//CP864 has nonstandard mapping for 0x25
let s = "٪ﻱ";
assert_eq!(CP864.decode(&bytes).unwrap(), s);
assert_eq!(bytes, *CP864.encode(s).unwrap());
//Standard '%' should still map to 0x25
let s = "%ﻱ";
assert_eq!(bytes, *CP864.encode(s).unwrap());
let s = "AAAAAAA٪";
let bytes = [65, 65, 65, 65, 65, 65, 65, 0x25];
//Should decode to nonstandard, even if whole usize-len is ascii
assert_eq!(CP864.decode(&bytes).unwrap(), s);
}
/// Verify that code pages using the ASCII-optimized decode path
/// have standard ASCII mappings for bytes 0-127.
#[test]
fn verify_ascii_optimized_codepages() {
let codepages: &[&dyn CodePage] = &[&CP857, &CP869, &CP874, &CP1253, &CP1255, &CP1257];
for cp in codepages {
for b in 0u8..128 {
let bytes = [b];
let expected = core::str::from_utf8(&bytes).unwrap();
// undefined byte mappings are fine; only check the ones that decode
if let Ok(decoded) = cp.decode(&bytes) {
assert_eq!(
&*decoded, expected,
"byte {b} should decode to ASCII '{expected}'"
);
}
}
}
}
}