unic_char_basics/notation.rs
1// Copyright 2018 The UNIC Project Developers.
2//
3// See the COPYRIGHT file at the top-level directory of this distribution.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Format Unicode Code-Points in the style recommended by The Unicode Standard.
12//!
13//! As defined by The Unicode Standard (Appendix A—Notational Conventions), a Unicode code point is
14//! referred to by writing "U+" followed by its hexadecimal number. For code points in the Basic
15//! Multilingual Plane (BMP), four digits are used; for code points outside the BMP, five or six
16//! digits are used, as required.
17//!
18//! References:
19//! - https://www.unicode.org/versions/Unicode10.0.0/appA.pdf
20
21use core::fmt;
22
23/// Represent the Unicode Notation of a code-point.
24///
25/// - https://www.unicode.org/versions/Unicode10.0.0/appA.pdf
26#[derive(Debug)]
27pub struct UnicodeNotation {
28 codepoint: char,
29}
30
31impl fmt::Display for UnicodeNotation {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 write!(f, "U+{:04X}", self.codepoint as u32)
34 }
35}
36
37/// Return a `UnicodeNotation` for the code-point to be used in string format.
38pub fn unicode_notation(codepoint: char) -> UnicodeNotation {
39 UnicodeNotation { codepoint }
40}