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
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-Exception
// Copyright 2024 Daniel Fox Franke.

//! [`Utf32String`] and [`MysteryString`].

use alloc::borrow::Borrow;

use bytes::{Buf, BufMut, Bytes, BytesMut};
use core::{
    fmt::{Debug, Display, Formatter, Write},
    num::NonZeroUsize,
};

#[cfg(feature = "std")]
use std::error::Error;

/// A string encoded as UTF-32.
///
/// Strings of this type can be serialized into a story file (via the
/// [`Item::Utf32String`](`crate::Item::Utf32String`) constructor) formatted
/// compatibly with the `streamstr` instruction. Constructors ensure that the
/// string is valid Unicode with no embedded nulls. Internally it's a [`Bytes`],
/// so cloning it is cheap.
///
/// This is not at all a full-featured alternative to [`std::String`](`String`).
/// `Utf32String`s are immutable once constructed and not intended for anything
/// other than being serialized into a story file.
#[derive(Clone, PartialEq, Eq, Hash, Default)]
pub struct Utf32String(Bytes);

/// A string whose encoding is defined by the IO system, but *probably* treated
/// as Latin-1.
///
/// Strings of this type can be serialized into a story file (via the
/// [`Item::MysteryString`](`crate::Item::MysteryString`)) constructor formatted
/// compatibly with the `streamstr` instruction. Constructors ensure that it
/// will not contain any embedded nulls. Internally it's a [`Bytes`], so cloning
/// it is cheap.
///
/// This corresponds to a Glulx `E0` string, of which the spec says "the
/// encoding scheme is the business of the I/O system; in Glk, it will be the
/// Latin-1 character set". It is in any case required to be a single-byte
/// encoding which uses a zero byte as a terminator.
///
/// When building a `MysteryString` from a `char` iterator or using its
/// `Display` impl, Latin1 is assumed. However, you can also build it from a
/// `u8` iterator in which case no assumption is made about the encoding.
#[derive(Clone, PartialEq, Eq, Hash, Default)]
pub struct MysteryString(Bytes);

/// Error returned when constructing a [`Utf32String`] or [`MysteryString`] from
/// malformed input.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StringConversionError<T> {
    /// The number of errors which were encountered when encoding the string.
    pub num_errors: NonZeroUsize,
    /// The index at which the first error was encountered.
    pub first_error: usize,
    /// A lossy representation of the string.
    pub lossy: T,
}

impl<T> Display for StringConversionError<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if usize::from(self.num_errors) > 1 {
            write!(
                f,
                "string conversion encountered {} unrepresentable characters, the first one at index {}.", 
                self.num_errors,
                self.first_error
            )
        } else {
            write!(
                f,
                "string conversion encountered an unrepresentable character at index {}.",
                self.first_error
            )
        }
    }
}

#[cfg(feature = "std")]
impl<T> Error for StringConversionError<T> where T: Debug {}

impl Utf32String {
    /// Construct a `Utf32String` from an iterator over `char`s (or over any type
    /// that lets you borrow a `char`).
    ///
    /// If the string contains embedded nulls, an error is returned, but a lossy
    /// version can be extracted from the error struct. The lossy string
    /// replaces nulls with `U+2400 SYMBOL FOR NULL` (␀), which belongs to the
    /// Control Pictures block, which is a really neat block that I bet you
    /// didn't know existed.
    pub fn from_chars<I, C>(chars: I) -> Result<Self, StringConversionError<Self>>
    where
        I: IntoIterator<Item = C>,
        C: Borrow<char>,
    {
        let mut num_errors: usize = 0;
        let mut first_error: usize = usize::MAX;

        let iter = chars.into_iter();
        let mut bm = BytesMut::with_capacity(4 * iter.size_hint().0);

        for (i, cref) in iter.enumerate() {
            let c = *cref.borrow();
            if c == '\0' {
                bm.put_u32('\u{2400}'.into());
                num_errors += 1;
                first_error = first_error.min(i);
            } else {
                bm.put_u32(c.into())
            }
        }

        if let Some(num_errors) = NonZeroUsize::new(num_errors) {
            Err(StringConversionError {
                num_errors,
                first_error,
                lossy: Self(bm.freeze()),
            })
        } else {
            Ok(Self(bm.freeze()))
        }
    }

    /// Like [`from_chars`](`Self::from_chars`), but in case of error will
    /// silently unwrap the error and return the lossy version.
    pub fn from_chars_lossy<I, C>(chars: I) -> Self
    where
        I: IntoIterator<Item = C>,
        C: Borrow<char>,
    {
        match Self::from_chars(chars) {
            Ok(s) => s,
            Err(e) => e.lossy,
        }
    }

    /// Returns true if the string is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the length of the string in characters.
    pub fn char_len(&self) -> usize {
        self.0.len() / 4
    }

    /// Returns the length of the string in bytes, excluding prefix and null
    /// terminator.
    pub fn byte_len(&self) -> usize {
        self.0.len()
    }

    /// Returns the length of the string in bytes, including prefix and null
    /// terminator.
    pub fn byte_len_with_prefix_and_nul(&self) -> usize {
        self.0.len() + 8
    }

    /// Returns a clone of the underlying [`Bytes`].
    pub fn to_bytes(&self) -> Bytes {
        self.clone().into_bytes()
    }

    /// Unwraps the string into its underlying [`Bytes`].
    pub fn into_bytes(self) -> Bytes {
        self.0
    }
}

impl Display for Utf32String {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut buf = self.0.clone();

        while buf.has_remaining() {
            let c: char = buf
                .get_u32()
                .try_into()
                .expect("Utf32String should always contain valid characters");
            f.write_char(c)?
        }

        Ok(())
    }
}

impl Debug for Utf32String {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let s = self.to_string();
        f.debug_tuple("Utf32String").field(&s).finish()
    }
}

impl TryFrom<String> for Utf32String {
    type Error = StringConversionError<Utf32String>;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Utf32String::from_chars(value.chars())
    }
}

impl TryFrom<&String> for Utf32String {
    type Error = StringConversionError<Utf32String>;

    fn try_from(value: &String) -> Result<Self, Self::Error> {
        Utf32String::from_chars(value.chars())
    }
}

impl TryFrom<&str> for Utf32String {
    type Error = StringConversionError<Utf32String>;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Utf32String::from_chars(value.chars())
    }
}

impl TryFrom<&[char]> for Utf32String {
    type Error = StringConversionError<Utf32String>;

    fn try_from(value: &[char]) -> Result<Self, Self::Error> {
        Utf32String::from_chars(value)
    }
}

impl MysteryString {
    /// Constructs a `MysteryString` from an iterator over `char`s (or over any
    /// type that lets you borrow a `char`).
    ///
    /// If the string contains embedded nulls or any character which cannot be
    /// represented in Latin-1, an error is returned, but a lossy version can be
    /// extracted from the error struct. The lossy string replaces nulls and
    /// unrepresentable characters with `b'?'`.
    pub fn from_chars<I, C>(chars: I) -> Result<Self, StringConversionError<Self>>
    where
        I: IntoIterator<Item = C>,
        C: Borrow<char>,
    {
        let mut num_errors: usize = 0;
        let mut first_error: usize = usize::MAX;

        let iter = chars.into_iter();
        let mut bm = BytesMut::with_capacity(4 * iter.size_hint().0);

        for (i, cref) in iter.enumerate() {
            match u8::try_from(*cref.borrow()) {
                Ok(b) if b != 0 => {
                    bm.put_u8(b);
                }
                _ => {
                    bm.put_u8(b'?');
                    num_errors += 1;
                    first_error = first_error.min(i);
                }
            }
        }

        if let Some(num_errors) = NonZeroUsize::new(num_errors) {
            Err(StringConversionError {
                num_errors,
                first_error,
                lossy: Self(bm.freeze()),
            })
        } else {
            Ok(Self(bm.freeze()))
        }
    }

    /// Constructs a `MysteryString` from an iterator over `u8`s (or over any
    /// type that lets you borrow a `u8`).
    ///
    /// If the string contains embedded nulls, an error is returned, but a lossy
    /// version can be extracted from the error struct. The lossy string is
    /// truncated at the first occurence of a null. (Unlike `from_chars`, this
    /// constructor doesn't that the string is Latin-1 or even any ASCII
    /// superset, so therefore it can't know what would be a reasonable
    /// replacement character to substitute.)
    pub fn from_bytes<I, C>(chars: I) -> Result<Self, StringConversionError<Self>>
    where
        I: IntoIterator<Item = C>,
        C: Borrow<u8>,
    {
        let mut failed = false;
        let mut num_errors: usize = 0;
        let mut first_error: usize = usize::MAX;

        let iter = chars.into_iter();
        let mut bm = BytesMut::with_capacity(4 * iter.size_hint().0);

        for (i, bref) in iter.enumerate() {
            let b = *bref.borrow();

            if b != 0 {
                // We use this separate boolean rather than testing on
                // num_errors == 0 so the optimizer can prove that it's
                // monotonic. The num_errors increment could wrap if we're
                // handed an infinite iterator.
                if !failed {
                    bm.put_u8(b);
                }
            } else {
                failed = true;
                num_errors += 1;
                first_error = first_error.min(i);
            }
        }

        if let Some(num_errors) = NonZeroUsize::new(num_errors) {
            Err(StringConversionError {
                num_errors,
                first_error,
                lossy: Self(bm.freeze()),
            })
        } else {
            Ok(Self(bm.freeze()))
        }
    }

    /// Like [`from_chars`](`Self::from_chars`), but in case of error will
    /// silently unwrap the error and return the lossy version.
    pub fn from_chars_lossy<I, C>(chars: I) -> Self
    where
        I: IntoIterator<Item = C>,
        C: Borrow<char>,
    {
        match Self::from_chars(chars) {
            Ok(s) => s,
            Err(e) => e.lossy,
        }
    }

    /// Like [`from_bytes`](`Self::from_bytes`), but in case of error will
    /// silently unwrap the error and return the lossy version.
    pub fn from_bytes_lossy<I, C>(chars: I) -> Self
    where
        I: IntoIterator<Item = C>,
        C: Borrow<u8>,
    {
        match Self::from_bytes(chars) {
            Ok(s) => s,
            Err(e) => e.lossy,
        }
    }

    /// Returns true if the string is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the length of the string, excluding prefix and null terminator.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns the length of the string, including prefix and null terminator.
    pub fn len_with_prefix_and_nul(&self) -> usize {
        self.len() + 2
    }

    /// Returns a clone of the string's underlying `Bytes`.
    pub fn to_bytes(&self) -> Bytes {
        self.clone().into_bytes()
    }

    /// Unwraps the string into its underlying `Bytes`.
    pub fn into_bytes(self) -> Bytes {
        self.0
    }
}

impl Display for MysteryString {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut buf = self.0.clone();
        while buf.has_remaining() {
            let byte = buf.get_u8();
            let c = char::from_u32(byte.into()).unwrap();
            f.write_char(c)?
        }

        Ok(())
    }
}

impl Debug for MysteryString {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let s = self.to_string();
        f.debug_tuple("MysteryString").field(&s).finish()
    }
}

impl TryFrom<String> for MysteryString {
    type Error = StringConversionError<MysteryString>;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        MysteryString::from_chars(value.chars())
    }
}

impl TryFrom<&String> for MysteryString {
    type Error = StringConversionError<MysteryString>;

    fn try_from(value: &String) -> Result<Self, Self::Error> {
        MysteryString::from_chars(value.chars())
    }
}

impl TryFrom<&str> for MysteryString {
    type Error = StringConversionError<MysteryString>;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        MysteryString::from_chars(value.chars())
    }
}

impl TryFrom<Vec<u8>> for MysteryString {
    type Error = StringConversionError<MysteryString>;

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        MysteryString::from_bytes(value)
    }
}

impl TryFrom<&Vec<u8>> for MysteryString {
    type Error = StringConversionError<MysteryString>;

    fn try_from(value: &Vec<u8>) -> Result<Self, Self::Error> {
        MysteryString::from_bytes(value)
    }
}

impl TryFrom<&[u8]> for MysteryString {
    type Error = StringConversionError<MysteryString>;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        MysteryString::from_bytes(value)
    }
}