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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Copyright 2019 Eduardo Sánchez Muñoz
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! This crate provides auxiliary functions used to encode and decode
//! S-expression atom values.
//!
//! # Minimum Rust version
//!
//! The minimum Rust version required by this crate is 1.55.

#![deny(
    rust_2018_idioms,
    trivial_casts,
    trivial_numeric_casts,
    unreachable_pub,
    unused_must_use,
    unused_qualifications
)]
#![forbid(unsafe_code)]
#![no_std]

extern crate alloc;

use alloc::string::{String, ToString as _};
use alloc::vec::Vec;
use core::convert::TryFrom as _;
use core::fmt::Write as _;

#[cfg(test)]
mod tests;

// Make this crate only compile on Rust >=1.55
// because previous versions have buggy float parsing.
// Open range patterns (e.g., `0..`) have been stabilized
// in Rust 1.55, so this will fail to compile on previous
// versions.
const _: bool = matches!(0, 0..);

// Encode
/// Encodes a boolean value
///
///
/// Returns `"true"` if `value` is `true`, otherwise returns `"false"`.
pub fn encode_bool(value: bool) -> &'static str {
    if value {
        "true"
    } else {
        "false"
    }
}

/// Encodes a signed 8-bit integer
#[inline]
pub fn encode_i8(value: i8) -> String {
    value.to_string()
}

/// Encodes a signed 16-bit integer
#[inline]
pub fn encode_i16(value: i16) -> String {
    value.to_string()
}

/// Encodes a signed 32-bit integer
#[inline]
pub fn encode_i32(value: i32) -> String {
    value.to_string()
}

/// Encodes a signed 64-bit integer
#[inline]
pub fn encode_i64(value: i64) -> String {
    value.to_string()
}

/// Encodes a signed 128-bit integer
#[inline]
pub fn encode_i128(value: i128) -> String {
    value.to_string()
}

/// Encodes an unsigned 8-bit integer
#[inline]
pub fn encode_u8(value: u8) -> String {
    value.to_string()
}

/// Encodes an unsigned 16-bit integer
#[inline]
pub fn encode_u16(value: u16) -> String {
    value.to_string()
}

/// Encodes an unsigned 32-bit integer
#[inline]
pub fn encode_u32(value: u32) -> String {
    value.to_string()
}

/// Encodes an unsigned 64-bit integer
#[inline]
pub fn encode_u64(value: u64) -> String {
    value.to_string()
}

/// Encodes an unsigned 128-bit integer
#[inline]
pub fn encode_u128(value: u128) -> String {
    value.to_string()
}

fn reformat_float(s: String) -> String {
    if s == "NaN" || s == "inf" || s == "-inf" {
        return s;
    }

    const ZEROS_THRESHOLD: u32 = 9;

    let mut result = String::new();
    let s = if let Some(remaining) = s.strip_prefix('-') {
        result.push('-');
        remaining
    } else {
        s.as_str()
    };

    if let Some(mut remaining) = s.strip_prefix("0.") {
        let mut exp_abs: u32 = 1;
        while let Some(new_remaining) = remaining.strip_prefix('0') {
            remaining = new_remaining;
            exp_abs += 1;
        }
        if exp_abs > ZEROS_THRESHOLD {
            result.push_str(&remaining[..1]);
            if remaining.len() > 1 {
                result.push('.');
                result.push_str(&remaining[1..]);
            } else {
                result.push_str(".0");
            }
            result.push_str("e-");
            write!(result, "{}", exp_abs).unwrap();
        } else {
            result.push_str(s);
        }
    } else {
        let s = s.strip_suffix(".0").unwrap_or(s);
        if s.contains('.') {
            result.push_str(s);
        } else {
            let mut remaining = s;
            let mut num_zeros: u32 = 0;
            while let Some(new_remaining) = remaining.strip_suffix('0') {
                remaining = new_remaining;
                num_zeros += 1;
            }
            if num_zeros > ZEROS_THRESHOLD {
                result.push_str(&remaining[..1]);
                if remaining.len() > 1 {
                    result.push('.');
                    result.push_str(&remaining[1..]);
                } else {
                    result.push_str(".0");
                }
                result.push('e');
                write!(result, "{}", num_zeros + (remaining.len() as u32 - 1)).unwrap();
            } else {
                result.push_str(s);
                result.push_str(".0");
            }
        }
    }

    result
}

/// Encodes a 32-bit floating point number
#[inline]
pub fn encode_f32(value: f32) -> String {
    reformat_float(value.to_string())
}

/// Encodes a 64-bit floating point number
#[inline]
pub fn encode_f64(value: f64) -> String {
    reformat_float(value.to_string())
}

#[inline]
fn nibble_to_hex(nibble: u8) -> char {
    if nibble < 10 {
        char::from(b'0' + nibble)
    } else {
        char::from(b'a' + nibble - 10)
    }
}

pub fn encode_byte_string(string: &[u8]) -> String {
    let mut result = String::with_capacity(string.len() + 2);
    result.push('"');
    for &chr in string {
        #[allow(clippy::match_overlapping_arm)]
        match chr {
            b'\0' => result.push_str("\\0"),
            b'\t' => result.push_str("\\t"),
            b'\n' => result.push_str("\\n"),
            b'\r' => result.push_str("\\r"),
            b'"' => result.push_str("\\\""),
            b'\\' => result.push_str("\\\\"),
            0x20..=0x7E => result.push(char::from(chr)),
            _ => {
                result.push_str("\\x");
                result.push(nibble_to_hex(chr >> 4));
                result.push(nibble_to_hex(chr & 0xF));
            }
        }
    }
    result.push('"');
    result
}

pub fn encode_ascii_string(string: &str) -> String {
    let mut result = String::with_capacity(string.len() + 2);
    result.push('"');
    for &chr in string.as_bytes() {
        #[allow(clippy::match_overlapping_arm)]
        match chr {
            b'\0' => result.push_str("\\0"),
            b'\t' => result.push_str("\\t"),
            b'\n' => result.push_str("\\n"),
            b'\r' => result.push_str("\\r"),
            b'"' => result.push_str("\\\""),
            b'\\' => result.push_str("\\\\"),
            0x20..=0x7E => result.push(char::from(chr)),
            _ => {
                assert!(chr <= 0x7F, "Invalid ASCII character");
                result.push_str("\\x");
                result.push(nibble_to_hex(chr >> 4));
                result.push(nibble_to_hex(chr & 0xF));
            }
        }
    }
    result.push('"');
    result
}

pub fn encode_utf8_string(string: &str) -> String {
    let mut result = String::with_capacity(string.len() + 2);
    result.push('"');
    for chr in string.chars() {
        match chr {
            '\0' => result.push_str("\\0"),
            '\t' => result.push_str("\\t"),
            '\n' => result.push_str("\\n"),
            '\r' => result.push_str("\\r"),
            '"' => result.push_str("\\\""),
            '\\' => result.push_str("\\\\"),
            '\x20'..='\x7E' => result.push(chr),
            _ => {
                result.push_str("\\u{");
                let code_beginning = result.len();
                let mut remaining_digits = u32::from(chr);
                loop {
                    result.insert(
                        code_beginning,
                        nibble_to_hex((remaining_digits & 0xF) as u8),
                    );
                    remaining_digits >>= 4;
                    if remaining_digits == 0 {
                        break;
                    }
                }
                result.push('}');
            }
        }
    }
    result.push('"');
    result
}

// Decode
/// Decodes a boolean value
///
/// Returns `Some(true)` if `atom` is `"true"`, `Some(false)`
/// if `atom` is `"false"`, or `None` otherwise.
#[inline]
pub fn decode_bool(atom: &str) -> Option<bool> {
    match atom {
        "false" => Some(false),
        "true" => Some(true),
        _ => None,
    }
}

/// Decodes a signed 8-bit integer
#[inline]
pub fn decode_i8(atom: &str) -> Option<i8> {
    atom.parse().ok()
}

/// Decodes a signed 16-bit integer
#[inline]
pub fn decode_i16(atom: &str) -> Option<i16> {
    atom.parse().ok()
}

/// Decodes a signed 32-bit integer
#[inline]
pub fn decode_i32(atom: &str) -> Option<i32> {
    atom.parse().ok()
}

/// Decodes a signed 64-bit integer
#[inline]
pub fn decode_i64(atom: &str) -> Option<i64> {
    atom.parse().ok()
}

/// Decodes a signed 128-bit integer
#[inline]
pub fn decode_i128(atom: &str) -> Option<i128> {
    atom.parse().ok()
}

/// Decodes an unsigned 8-bit integer
#[inline]
pub fn decode_u8(atom: &str) -> Option<u8> {
    atom.parse().ok()
}

/// Decodes an unsigned 16-bit integer
#[inline]
pub fn decode_u16(atom: &str) -> Option<u16> {
    atom.parse().ok()
}

/// Decodes an unsigned 32-bit integer
#[inline]
pub fn decode_u32(atom: &str) -> Option<u32> {
    atom.parse().ok()
}

/// Decodes an unsigned 64-bit integer
#[inline]
pub fn decode_u64(atom: &str) -> Option<u64> {
    atom.parse().ok()
}

/// Decodes an unsigned 128-bit integer
#[inline]
pub fn decode_u128(atom: &str) -> Option<u128> {
    atom.parse().ok()
}

/// Decodes a 32-bit floating point number
#[inline]
pub fn decode_f32(atom: &str) -> Option<f32> {
    if atom == "+NaN" || atom == "-NaN" {
        None
    } else {
        atom.parse().ok()
    }
}

/// Decodes a 64-bit floating point number
#[inline]
pub fn decode_f64(atom: &str) -> Option<f64> {
    if atom == "+NaN" || atom == "-NaN" {
        None
    } else {
        atom.parse().ok()
    }
}

#[inline]
fn hex_digit_byte_to_u8(chr: u8) -> Option<u8> {
    match chr {
        b'0'..=b'9' => Some(chr - b'0'),
        b'A'..=b'F' => Some(chr - b'A' + 10),
        b'a'..=b'f' => Some(chr - b'a' + 10),
        _ => None,
    }
}

#[inline]
fn hex_digit_char_to_u8(chr: char) -> Option<u8> {
    match chr {
        '0'..='9' => Some(chr as u8 - b'0'),
        'A'..='F' => Some(chr as u8 - b'A' + 10),
        'a'..='f' => Some(chr as u8 - b'a' + 10),
        _ => None,
    }
}

pub fn decode_byte_string(atom: &str) -> Option<Vec<u8>> {
    let mut iter = atom.bytes();
    if iter.next() != Some(b'"') {
        return None;
    }

    let mut string = Vec::new();
    loop {
        match iter.next() {
            None => return None,
            Some(b'"') => {
                if iter.next().is_some() {
                    return None;
                }
                break;
            }
            Some(b'\\') => match iter.next() {
                Some(b'0') => string.push(b'\0'),
                Some(b't') => string.push(b'\t'),
                Some(b'n') => string.push(b'\n'),
                Some(b'r') => string.push(b'\r'),
                Some(b'"') => string.push(b'\"'),
                Some(b'\\') => string.push(b'\\'),
                Some(b'x') => {
                    let hex1 = hex_digit_byte_to_u8(iter.next()?)?;
                    let hex2 = hex_digit_byte_to_u8(iter.next()?)?;
                    string.push((hex1 << 4) | hex2);
                }
                Some(_) | None => return None,
            },
            Some(byte) => string.push(byte),
        }
    }

    Some(string)
}

pub fn decode_ascii_string(atom: &str) -> Option<String> {
    let mut iter = atom.bytes();
    if iter.next() != Some(b'"') {
        return None;
    }

    let mut string = String::new();
    loop {
        match iter.next() {
            None => return None,
            Some(b'"') => {
                if iter.next().is_some() {
                    return None;
                }
                break;
            }
            Some(b'\\') => match iter.next() {
                Some(b'0') => string.push('\0'),
                Some(b't') => string.push('\t'),
                Some(b'n') => string.push('\n'),
                Some(b'r') => string.push('\r'),
                Some(b'"') => string.push('\"'),
                Some(b'\\') => string.push('\\'),
                Some(b'x') => {
                    let hex1 = hex_digit_byte_to_u8(iter.next()?)?;
                    let hex2 = hex_digit_byte_to_u8(iter.next()?)?;
                    let chr = (hex1 << 4) | hex2;
                    if chr > 0x7F {
                        return None;
                    }
                    string.push(char::from(chr));
                }
                Some(_) | None => return None,
            },
            Some(byte @ b'\x00'..=b'\x7F') => string.push(char::from(byte)),
            Some(_) => return None,
        }
    }

    Some(string)
}

pub fn decode_utf8_string(atom: &str) -> Option<String> {
    let mut iter = atom.chars();
    if iter.next() != Some('"') {
        return None;
    }

    let mut string = String::new();
    loop {
        match iter.next() {
            None => return None,
            Some('"') => {
                if iter.next().is_some() {
                    return None;
                }
                break;
            }
            Some('\\') => match iter.next() {
                Some('0') => string.push('\0'),
                Some('t') => string.push('\t'),
                Some('n') => string.push('\n'),
                Some('r') => string.push('\r'),
                Some('"') => string.push('\"'),
                Some('\\') => string.push('\\'),
                Some('x') => {
                    let hex1 = hex_digit_char_to_u8(iter.next()?)?;
                    let hex2 = hex_digit_char_to_u8(iter.next()?)?;
                    let chr = (hex1 << 4) | hex2;
                    if chr > 0x7F {
                        return None;
                    }
                    string.push(char::from(chr));
                }
                Some('u') => {
                    if iter.next() != Some('{') {
                        return None;
                    }
                    let mut num_digits = 0;
                    let mut esc_chr: u32 = 0;
                    loop {
                        match iter.next() {
                            Some('}') => {
                                if num_digits == 0 {
                                    return None;
                                } else {
                                    break;
                                }
                            }
                            Some(chr) => {
                                if num_digits == 6 {
                                    return None;
                                }
                                esc_chr <<= 4;
                                esc_chr |= u32::from(hex_digit_char_to_u8(chr)?);
                                num_digits += 1;
                            }
                            None => return None,
                        }
                    }
                    string.push(char::try_from(esc_chr).ok()?);
                }
                Some(_) | None => return None,
            },
            Some(chr) => string.push(chr),
        }
    }

    Some(string)
}