tick-encoding 0.1.4

A simple encoding scheme to encode binary data into ASCII strings
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
#![cfg_attr(feature = "safe", deny(unsafe_code))]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "std", doc = include_str!("../README.md"))]

pub(crate) mod decoder;
pub(crate) mod encoder;
pub mod iter;

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::{borrow::Cow, string::String, vec::Vec};

/// Lookup table for knowing if a byte requires escaping.
const REQUIRES_ESCAPE_TABLE: [bool; 256] = {
    let mut table = [true; 256]; // Default: requires escape

    // Whitespace that doesn't require escaping
    table[b'\t' as usize] = false;
    table[b'\n' as usize] = false;
    table[b'\r' as usize] = false;

    // Printable ASCII (space through tilde) except backtick
    let mut i = b' ';
    while i <= b'~' {
        if i != b'`' {
            table[i as usize] = false;
        }
        i += 1;
    }

    table
};

const HEX_NIBBLE_DECODE_INVALID_ERR: u8 = 0xFF;
const HEX_NIBBLE_DECODE_LOWERCASE_ERR: u8 = 0xFE;

/// Lookup table for hex ASCII character to nibble.
///
/// Values:
/// - 0x00-0x0F: Valid uppercase hex digit
/// - `HEX_LOWERCASE`: Lowercase hex digit (a-f)
/// - `HEX_INVALID`: Invalid character
const HEX_NIBBLE_DECODE_TABLE: [u8; 256] = {
    let mut table = [HEX_NIBBLE_DECODE_INVALID_ERR; 256];

    // Digits '0'-'9' -> 0-9
    let mut i = b'0';
    while i <= b'9' {
        table[i as usize] = i - b'0';
        i += 1;
    }

    // Uppercase 'A'-'F' -> 10-15
    i = b'A';
    while i <= b'F' {
        table[i as usize] = i - b'A' + 10;
        i += 1;
    }

    // Lowercase 'a'-'f' -> lowercase error
    i = b'a';
    while i <= b'f' {
        table[i as usize] = HEX_NIBBLE_DECODE_LOWERCASE_ERR;
        i += 1;
    }

    table
};

/// Encode the given input as a string, escaping any bytes that require it.
/// If no bytes require escaping, then the result will be borrowed from
/// the input.
///
/// ## Example
///
/// ```
/// # #![cfg(feature = "alloc")]
/// let encoded = tick_encoding::encode(b"hello world!");
/// assert_eq!(encoded, "hello world!");
///
/// let encoded = tick_encoding::encode(&[0x00, 0xFF]);
/// assert_eq!(encoded, "`00`FF");
/// ```
#[cfg(feature = "alloc")]
#[must_use]
pub fn encode(input: &[u8]) -> Cow<'_, str> {
    // Get the first index that needs to be escaped
    input
        .iter()
        .position(|byte| requires_escape(*byte))
        // If no escape needed, borrow input. Otherwise encode from that index
        .map_or_else(
            || {
                debug_assert!(input.is_ascii());

                // SAFETY: We know the entire input is valid ASCII and UTF-8, and
                // additionally doesn't require any bytes to be escaped
                Cow::Borrowed(from_utf8_unchecked_potentially_unsafe(input))
            },
            |index| {
                // We know everything up to `index` does not require escaping
                let validated = &input[..index];
                debug_assert!(validated.is_ascii());

                // SAFETY: We know the input up to this point is valid ASCII and
                // UTF-8, since nothing up to this point needs escaping
                let validated = from_utf8_unchecked_potentially_unsafe(validated);

                let mut output = String::with_capacity(input.len() + 1);
                output.push_str(validated);

                // Encode the remainder of the input
                let requires_encoding = &input[index..];
                encode_to_string(requires_encoding, &mut output);
                Cow::Owned(output)
            },
        )
}

/// Return an iterator that encodes the bytes from the input iterator.
///
/// ## Example
///
/// ```
/// let iter = tick_encoding::encode_iter(b"x: \x00".iter().copied());
/// assert_eq!(iter.collect::<String>(), "x: `00");
/// ```
pub fn encode_iter<I>(iter: I) -> iter::EncodeIter<I::IntoIter>
where
    I: IntoIterator<Item = u8>,
{
    iter::EncodeIter::new(iter.into_iter())
}

/// Decode the given encoded input into a byte array. If no bytes need to
/// be un-escaped, then the result will be borrowed from the input.
///
/// Returns an error if the input isn't a valid ASCII string, or isn't a
/// valid canonical tick-encoding.
///
/// # Errors
///
/// Returns a [`DecodeError`] if the input is not valid tick-encoded data.
///
/// ## Example
///
/// ```
/// # #![cfg(feature = "alloc")]
/// let decoded = tick_encoding::decode(b"hello world!").unwrap();
/// assert_eq!(decoded, "hello world!".as_bytes());
///
/// let decoded = tick_encoding::decode(b"`00`FF").unwrap();
/// assert_eq!(decoded, [0x00, 0xFF].as_slice());
/// ```
#[cfg(feature = "alloc")]
pub fn decode(input: &[u8]) -> Result<Cow<'_, [u8]>, DecodeError> {
    // Get the first index that isn't already a valid unescaped byte
    let escape_index = input.iter().position(|byte| requires_escape(*byte));

    match escape_index {
        Some(index) => {
            // We know everything up to `index` does not need to be unescaped
            let validated = &input[..index];

            // Start by copying the validated input as-is. For the capacity,
            // we use a formula that gives the minimum length of the output
            // (i.e. assuming every remaining byte is escaped)
            let output_est_capacity = validated.len() + (input.len() - validated.len() + 2) / 3;
            let mut output = Vec::with_capacity(output_est_capacity);
            output.extend_from_slice(validated);

            // Decode the remainder of the input
            let requires_decoding = &input[index..];
            decode_to_vec(requires_decoding, &mut output)?;
            Ok(Cow::Owned(output))
        }
        None => Ok(Cow::Borrowed(input)),
    }
}

/// Return an iterator that decodes the tick-encoded characters from the input
/// iterator. Returns `Some(Err(_))` if the input character sequence is invalid,
/// then returns `None` after that.
///
/// ## Example
///
/// ```
/// let iter = tick_encoding::decode_iter(b"`00`01".iter().copied());
/// assert_eq!(iter.collect::<Result<Vec<_>, _>>().unwrap(), vec![0x00, 0x01]);
/// ```
pub fn decode_iter<I>(iter: I) -> iter::DecodeIter<I::IntoIter>
where
    I: IntoIterator<Item = u8>,
{
    iter::DecodeIter::new(iter.into_iter())
}

/// Decode a tick-encoded ASCII string in-place.
///
/// Takes a byte slice containing a tick-encoded ASCII string, and decodes it
/// in-place, writing back into the same byte slice. Returns a sub-slice
/// containing just the decoded bytes (the bytes past the returned sub-slice
/// are left unchanged).
///
/// # Errors
///
/// Returns a [`DecodeError`] if the input is not valid tick-encoded data.
///
/// ## Example
///
/// ```rust
/// let mut buffer = b"bytes: `00`01`02`03".to_vec();
/// let decoded = tick_encoding::decode_in_place(&mut buffer).unwrap();
/// assert_eq!(decoded, b"bytes: \x00\x01\x02\x03");
/// ```
pub fn decode_in_place(input: &mut [u8]) -> Result<&mut [u8], DecodeError> {
    // Get the first index that isn't already a valid unescaped byte
    let Some(escape_index) = input.iter().position(|byte| requires_escape(*byte)) else {
        // Nothing needs to be unescaped
        return Ok(input);
    };

    // Walk through the rest of the input. The bytes between `0..head` have been
    // decoded, and the bytes between `tail..input.len()` are still encoded.
    // Since the encoded form is always as long as the decoded form or longer,
    // `head` will always be less than or equal to `tail`.
    //
    // This technique is very similar to the one from `in-place-string-map` (see
    // https://crates.io/crates/in-place-string-map), but works on a byte slice
    // instead.
    let mut head = escape_index;
    let mut tail = escape_index;
    while tail < input.len() {
        if input[tail] == b'`' {
            let escaped = input.get(tail + 1).ok_or(DecodeError::UnexpectedEnd)?;
            match escaped {
                b'`' => {
                    input[head] = b'`';
                    tail += 2;
                    head += 1;
                }
                high => {
                    let low = input.get(tail + 2).ok_or(DecodeError::UnexpectedEnd)?;
                    let byte = hex_bytes_to_byte(*high, *low)?;
                    input[head] = byte;
                    tail += 3;
                    head += 1;
                }
            }
        } else if requires_escape(input[tail]) {
            return Err(DecodeError::InvalidByte(input[tail]));
        } else {
            input[head] = input[tail];
            tail += 1;
            head += 1;
        }
    }

    let decoded = &mut input[..head];
    Ok(decoded)
}

/// Returns true if the given byte must be escaped with a backtick.
///
/// The following ASCII bytes **do not** require escaping, and are left
/// un-escaped in a tick-encoded string:
///
/// - Tab (`\t`, 0x09)
/// - Newline (`\n`, 0x0A)
/// - Carriage return (`\r`, 0x0D)
/// - Space (` `, 0x20)
/// - Printable characters except backtick (0x21 to 0x59, 0x61 to 0x7E)
#[inline]
#[must_use]
pub const fn requires_escape(byte: u8) -> bool {
    REQUIRES_ESCAPE_TABLE[byte as usize]
}

/// Encode the given input, and append the result to `output`. Returns
/// the number of bytes / characters appended (only ASCII characters are
/// appended).
///
/// ## Example
///
/// ```
/// # #![cfg(feature = "alloc")]
/// let mut output = String::new();
/// let count = tick_encoding::encode_to_string("hello, world! 🙂".as_bytes(), &mut output);
/// assert_eq!(output, "hello, world! `F0`9F`99`82");
/// assert_eq!(count, 26);
/// ```
#[cfg(feature = "alloc")]
pub fn encode_to_string(input: &[u8], output: &mut String) -> usize {
    let mut written = 0;
    output.reserve(input.len());
    for &byte in input {
        if byte == b'`' {
            output.push_str("``");
            written += 2;
        } else if requires_escape(byte) {
            let [high, low] = byte_to_hex_chars(byte);
            output.push('`');
            output.push(high);
            output.push(low);

            written += 3;
        } else {
            output.push(byte as char);
            written += 1;
        }
    }

    written
}

/// Encode the given input, and append the result to `output`. Returns
/// the number of bytes appended.
///
/// ## Example
///
/// ```
/// let mut output = vec![];
/// let count = tick_encoding::encode_to_vec("hello, world! 🙂".as_bytes(), &mut output);
/// assert_eq!(output, b"hello, world! `F0`9F`99`82");
/// assert_eq!(count, 26);
/// ```
#[cfg(feature = "alloc")]
pub fn encode_to_vec(input: &[u8], output: &mut Vec<u8>) -> usize {
    let mut written = 0;
    output.reserve(input.len());
    for &byte in input {
        if byte == b'`' {
            output.extend_from_slice(b"``");
            written += 2;
        } else if requires_escape(byte) {
            let [high, low] = byte_to_hex_bytes(byte);
            output.extend_from_slice(&[b'`', high, low]);

            written += 3;
        } else {
            output.push(byte);
            written += 1;
        }
    }

    written
}

/// Decode tick-encoded ASCII input and append the result to a vector.
///
/// Returns the number of bytes appended. Returns an error if the result
/// isn't a valid ASCII string, or isn't a valid canonical tick-encoding.
///
/// # Errors
///
/// Returns a [`DecodeError`] if the input is not valid tick-encoded data.
///
/// ## Example
///
/// ```
/// let mut output = vec![];
/// let count = tick_encoding::decode_to_vec(b"hello, world! `F0`9F`99`82", &mut output).unwrap();
/// let output_str = core::str::from_utf8(&output).unwrap();
/// assert_eq!(output_str, "hello, world! 🙂");
/// assert_eq!(count, 18);
/// ```
#[cfg(feature = "alloc")]
pub fn decode_to_vec(input: &[u8], output: &mut Vec<u8>) -> Result<usize, DecodeError> {
    let mut written = 0;
    let mut iter = input.iter();
    while let Some(&byte) = iter.next() {
        if byte == b'`' {
            let escaped = iter.next().ok_or(DecodeError::UnexpectedEnd)?;
            match escaped {
                b'`' => {
                    output.push(b'`');
                    written += 1;
                }
                high => {
                    let low = iter.next().ok_or(DecodeError::UnexpectedEnd)?;
                    let byte = hex_bytes_to_byte(*high, *low)?;
                    output.push(byte);
                    written += 1;
                }
            }
        } else if requires_escape(byte) {
            return Err(DecodeError::InvalidByte(byte));
        } else {
            output.push(byte);
            written += 1;
        }
    }

    Ok(written)
}

/// Convert a nibble to its uppercase hex ASCII character.
#[inline]
const fn nibble_to_hex(n: u8) -> u8 {
    // 0-9 → '0'-'9'
    // 10-15 → 'A'-'F' (add 7 to skip the ASCII gap between '9' and 'A')
    n + b'0' + ((n > 9) as u8) * 7
}

/// Convert a byte to its two-character uppercase hex representation.
#[inline]
const fn byte_to_hex_bytes(byte: u8) -> [u8; 2] {
    [nibble_to_hex(byte >> 4), nibble_to_hex(byte & 0x0F)]
}

const fn byte_to_hex_chars(byte: u8) -> [char; 2] {
    let [high_byte, low_byte] = byte_to_hex_bytes(byte);
    [high_byte as char, low_byte as char]
}

/// Decode two hex ASCII characters into a single byte.
///
/// Returns an error if:
/// - Either character is not a valid hex digit (`InvalidHex`)
/// - Either character is lowercase a-f (`LowercaseHex`)
/// - The decoded byte doesn't require escaping (`UnexpectedEscape`)
#[inline]
const fn hex_bytes_to_byte(high: u8, low: u8) -> Result<u8, DecodeError> {
    let high_value = HEX_NIBBLE_DECODE_TABLE[high as usize];
    let low_value = HEX_NIBBLE_DECODE_TABLE[low as usize];

    match (high_value, low_value) {
        // Both valid hex digits (0x00-0x0F)
        (0..=0x0F, 0..=0x0F) => {
            let byte = (high_value << 4) | low_value;

            if byte == b'`' || !requires_escape(byte) {
                return Err(DecodeError::UnexpectedEscape(
                    EscapedHex(high, low),
                    byte as char,
                ));
            }

            Ok(byte)
        }
        // At least one invalid character
        (HEX_NIBBLE_DECODE_INVALID_ERR, _) | (_, HEX_NIBBLE_DECODE_INVALID_ERR) => {
            Err(DecodeError::InvalidHex(EscapedHex(high, low)))
        }
        // Must be lowercase
        _ => Err(DecodeError::LowercaseHex(EscapedHex(high, low))),
    }
}

#[cfg(feature = "safe")]
fn from_utf8_unchecked_potentially_unsafe(bytes: &[u8]) -> &str {
    core::str::from_utf8(bytes).unwrap()
}

#[cfg(not(feature = "safe"))]
fn from_utf8_unchecked_potentially_unsafe(bytes: &[u8]) -> &str {
    debug_assert!(bytes.is_ascii());
    unsafe { core::str::from_utf8_unchecked(bytes) }
}

/// An error trying to decode a tick-encoded string.
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum DecodeError {
    /// Encountered an invalid byte in the string. This could either by a
    /// non-ASCII byte or an ASCII byte that requires escaping (see
    /// [`requires_escape`]).
    #[cfg_attr(feature = "std", error("invalid encoded byte 0x{0:02x}"))]
    InvalidByte(u8),
    /// Reached the end of the string following a backtick (\`). A backtick
    /// must be followed by either another backtick or a 2-digit hex value.
    #[cfg_attr(feature = "std", error("unexpected end after `"))]
    UnexpectedEnd,
    /// Tried to decode a 2-digit hex value, but the value does not require
    /// escaping (see [`requires_escape`]).
    #[cfg_attr(feature = "std", error("unexpected escape {0}, expected {1}"))]
    UnexpectedEscape(EscapedHex, char),
    /// Tried to decode a 2-digit hex value, but the hex value contained
    /// the values `[a-f]`. Escaped hex values must use `[A-F]`.
    #[cfg_attr(feature = "std", error("expected uppercase hex sequence, found {0}"))]
    LowercaseHex(EscapedHex),
    /// Tried to decode a 2-digit hex value, but an invalid hex digit
    /// was found. Escaped hex values must use the characters `[0-9A-F]`.
    #[cfg_attr(feature = "std", error("invalid hex sequence {0}"))]
    InvalidHex(EscapedHex),
}

/// A two-digit escaped hex sequence, prefixed with a backtick.
pub struct EscapedHex(pub u8, pub u8);

impl core::fmt::Debug for EscapedHex {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let Self(high, low) = self;
        if requires_escape(*high) || requires_escape(*low) {
            f.debug_tuple("EscapedHex")
                .field(&self.0)
                .field(&self.1)
                .finish()
        } else {
            f.debug_tuple("EscapedHex")
                .field(&(*high as char))
                .field(&(*low as char))
                .finish()
        }
    }
}

impl core::fmt::Display for EscapedHex {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let Self(high, low) = self;
        if requires_escape(*high) || requires_escape(*low) {
            write!(f, "0x{high:02X} 0x{low:02X}")
        } else {
            write!(f, "`{}{}", *high as char, *low as char)
        }
    }
}