unicode_xid/
lib.rs

1// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
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//! Determine if a `char` is a valid identifier for a parser and/or lexer according to
12//! [Unicode Standard Annex #31](http://www.unicode.org/reports/tr31/) rules.
13//!
14//! ```rust
15//! extern crate unicode_xid;
16//!
17//! use unicode_xid::UnicodeXID;
18//!
19//! fn main() {
20//!     let ch = 'a';
21//!     println!("Is {} a valid start of an identifier? {}", ch, UnicodeXID::is_xid_start(ch));
22//! }
23//! ```
24//!
25//! # features
26//!
27//! unicode-xid supports a `no_std` feature. This eliminates dependence
28//! on std, and instead uses equivalent functions from core.
29//!
30//! # crates.io
31//!
32//! You can use this package in your project by adding the following
33//! to your `Cargo.toml`:
34//!
35//! ```toml
36//! [dependencies]
37//! unicode-xid = "0.0.4"
38//! ```
39
40#![deny(missing_docs, unsafe_code)]
41#![doc(html_logo_url = "https://unicode-rs.github.io/unicode-rs_sm.png",
42       html_favicon_url = "https://unicode-rs.github.io/unicode-rs_sm.png")]
43
44#![no_std]
45#![cfg_attr(feature = "bench", feature(test, unicode_internals))]
46
47#[cfg(test)]
48#[macro_use]
49extern crate std;
50
51#[cfg(feature = "bench")]
52extern crate test;
53
54use tables::derived_property;
55pub use tables::UNICODE_VERSION;
56
57mod tables;
58
59#[cfg(test)]
60mod tests;
61
62/// Methods for determining if a character is a valid identifier character.
63pub trait UnicodeXID {
64    /// Returns whether the specified character satisfies the 'XID_Start'
65    /// Unicode property.
66    ///
67    /// 'XID_Start' is a Unicode Derived Property specified in
68    /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
69    /// mostly similar to ID_Start but modified for closure under NFKx.
70    fn is_xid_start(self) -> bool;
71
72    /// Returns whether the specified `char` satisfies the 'XID_Continue'
73    /// Unicode property.
74    ///
75    /// 'XID_Continue' is a Unicode Derived Property specified in
76    /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
77    /// mostly similar to 'ID_Continue' but modified for closure under NFKx.
78    fn is_xid_continue(self) -> bool;
79}
80
81impl UnicodeXID for char {
82    #[inline]
83    fn is_xid_start(self) -> bool { derived_property::XID_Start(self) }
84
85    #[inline]
86    fn is_xid_continue(self) -> bool { derived_property::XID_Continue(self) }
87}