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
use std::convert::TryFrom;
use std::io::{Read, Write};
use std::io::Result as Res;
use endio::{Deserialize, LE, Serialize};
use super::{AbstractLuStr, AsciiChar, AsciiError, LuChar, LuStrExt, Ucs2Char, Ucs2Error};
macro_rules! abstract_lu_str {
($name:ident, $c:ty, $null:expr, $n:literal) => {
pub struct $name([$c; $n]);
impl PartialEq for $name {
fn eq(&self, other: &Self) -> bool {
dbg!((&**self) == (&**other))
}
}
impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
(&**self).fmt(f)
}
}
impl std::ops::Deref for $name {
type Target = AbstractLuStr<$c>;
#[inline]
fn deref(&self) -> &Self::Target {
let terminator = self.0.iter().position(|&c| c == $null).unwrap();
&self.0[..terminator]
}
}
impl std::ops::DerefMut for $name {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
let terminator = self.0.iter().position(|&c| c == $null).unwrap();
&mut self.0[..terminator]
}
}
impl<R: Read> Deserialize<LE, R> for $name {
fn deserialize(reader: &mut R) -> Res<Self> {
let mut bytes = [0u8; $n * std::mem::size_of::<$c>()];
reader.read(&mut bytes)?;
Ok(Self(unsafe { std::mem::transmute(bytes) }))
}
}
impl<W: Write> Serialize<LE, W> for &$name {
fn serialize(self, writer: &mut W) -> Res<()> {
let x: [u8; $n * std::mem::size_of::<$c>()] = unsafe { std::mem::transmute(self.0) };
writer.write_all(&x)
}
}
};
}
macro_rules! lu_str {
($name:ident, $n:literal) => {
abstract_lu_str!($name, AsciiChar, AsciiChar(0), $n);
impl TryFrom<&[u8]> for $name {
type Error = AsciiError;
fn try_from(string: &[u8]) -> Result<Self, Self::Error> {
if string.len() >= $n {
return Err(AsciiError);
}
let mut bytes = [0u8; $n];
for (i, chr) in string.iter().enumerate() {
bytes[i] = *chr;
}
let bytes = unsafe { std::mem::transmute(bytes) };
Ok(Self(bytes))
}
}
impl<const N: usize> TryFrom<&[u8; N]> for $name {
type Error = AsciiError;
fn try_from(string: &[u8; N]) -> Result<Self, Self::Error> {
Self::try_from(&string[..])
}
}
};
}
macro_rules! lu_wstr {
($name:ident, $n:literal) => {
abstract_lu_str!($name, Ucs2Char, Ucs2Char(0), $n);
impl TryFrom<&str> for $name {
type Error = Ucs2Error;
fn try_from(string: &str) -> Result<Self, Self::Error> {
let mut bytes = [0u16; $n];
for (i, chr) in string.encode_utf16().take($n - 1).enumerate() {
bytes[i] = chr;
}
let bytes = unsafe { std::mem::transmute(bytes) };
Ok(Self(bytes))
}
}
impl From<&$name> for String {
fn from(wstr: &$name) -> Self {
String::from_utf16(unsafe { &*(&**wstr as *const [Ucs2Char] as *const [<Ucs2Char as LuChar>::Int]) }).unwrap()
}
}
};
}
lu_str!(LuString3, 3);
lu_str!(LuString33, 33);
lu_str!(LuString37, 37);
lu_wstr!(LuWString32, 32);
lu_wstr!(LuWString33, 33);
lu_wstr!(LuWString41, 41);
lu_wstr!(LuWString42, 42);
lu_wstr!(LuWString50, 50);
lu_wstr!(LuWString128, 128);
lu_wstr!(LuWString256, 256);
lu_wstr!(LuWString400, 400);