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
// Copyright 2017 The UNIC Project Developers.
//
// See the COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.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.

use std::io;
use std::str::Chars;

use super::Result;

// TODO(FUTURE_RUST): Replace with char::MAX_UTF*_LEN when available.
const MAX_UTF8_LEN: usize = 4; // x u8 per char
const MAX_UTF16_LEN: usize = 2; // x u16 per char

macro_rules! write_join_with_space {
    ($output:expr, $values:expr, $write_fn:expr) => {{
        let mut first = true;
        for v in $values {
            if !first {
                write!($output, " ")?;
            }
            first = false;
            ($write_fn)($output, v)?;
        }
        Ok(())
    }};
}

// == Single Char ==

pub fn write_char_as_codepoint<W: io::Write>(output: &mut W, ch: char) -> Result<()> {
    write!(output, "U+{:04X}", ch as u32)
}

pub fn write_char_as_utf8_hex<W: io::Write>(output: &mut W, ch: char) -> Result<()> {
    let mut buf = [0; MAX_UTF8_LEN];
    let utf8 = ch.encode_utf8(&mut buf).as_bytes();
    write_join_with_space!(output, utf8.iter(), |output: &mut W, b8| -> Result<()> {
        write!(output, "0x{:02X}", b8)
    })
}

pub fn write_char_as_utf16_hex<W: io::Write>(output: &mut W, ch: char) -> Result<()> {
    let mut buf = [0; MAX_UTF16_LEN];
    let utf16 = ch.encode_utf16(&mut buf);
    write_join_with_space!(output, utf16.iter(), |output: &mut W, b16| -> Result<()> {
        write!(output, "0x{:04X}", b16)
    })
}

// == Multiple Chars ==

pub fn write_as_codepoints<'a, W: io::Write>(output: &mut W, chars: Chars<'a>) -> Result<()> {
    write_join_with_space!(output, chars, write_char_as_codepoint)
}

pub fn write_as_utf8_hex<'a, W: io::Write>(output: &mut W, chars: Chars<'a>) -> Result<()> {
    write_join_with_space!(output, chars, write_char_as_utf8_hex)
}

pub fn write_as_utf16_hex<'a, W: io::Write>(output: &mut W, chars: Chars<'a>) -> Result<()> {
    write_join_with_space!(output, chars, write_char_as_utf16_hex)
}

pub fn write_with_control_n_unicode_braces_escape<'a, W: io::Write>(
    output: &mut W,
    chars: Chars<'a>,
) -> Result<()> {
    write!(output, "\"")?;
    for ch in chars {
        write!(output, "{}", ch.escape_default())?;
    }
    write!(output, "\"")?;
    Ok(())
}

pub fn write_with_control_braces_escape<'a, W: io::Write>(
    output: &mut W,
    chars: Chars<'a>,
) -> Result<()> {
    write!(output, "\"")?;
    for ch in chars {
        write!(output, "{}", ch.escape_debug())?;
    }
    write!(output, "\"")?;
    Ok(())
}

pub fn write_with_all_braces_escape<'a, W: io::Write>(
    output: &mut W,
    chars: Chars<'a>,
) -> Result<()> {
    write!(output, "\"")?;
    for ch in chars {
        write!(output, "{}", ch.escape_unicode())?;
    }
    write!(output, "\"")?;
    Ok(())
}