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  
31  #![forbid(unsafe_code)]
32  #![deny(missing_docs)]
33  #![doc(
34      html_logo_url = "https://unicode-rs.github.io/unicode-rs_sm.png",
35      html_favicon_url = "https://unicode-rs.github.io/unicode-rs_sm.png"
36  )]
37  #![no_std]
38  #![cfg_attr(feature = "bench", feature(test, unicode_internals))]
39  
40  // #[cfg(test)]
41  // ANDROID: Unconditionally use std to allow building as a dylib.
42  // #[macro_use]
43  #[cfg(android_dylib)]
44  extern crate std;
45  
46  #[cfg(feature = "bench")]
47  extern crate test;
48  
49  use tables::derived_property;
50  pub use tables::UNICODE_VERSION;
51  
52  mod tables;
53  
54  #[cfg(test)]
55  mod tests;
56  
57  /// Methods for determining if a character is a valid identifier character.
58  pub trait UnicodeXID {
59      /// Returns whether the specified character satisfies the 'XID_Start'
60      /// Unicode property.
61      ///
62      /// 'XID_Start' is a Unicode Derived Property specified in
63      /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
64      /// mostly similar to ID_Start but modified for closure under NFKx.
is_xid_start(self) -> bool65      fn is_xid_start(self) -> bool;
66  
67      /// Returns whether the specified `char` satisfies the 'XID_Continue'
68      /// Unicode property.
69      ///
70      /// 'XID_Continue' is a Unicode Derived Property specified in
71      /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
72      /// mostly similar to 'ID_Continue' but modified for closure under NFKx.
is_xid_continue(self) -> bool73      fn is_xid_continue(self) -> bool;
74  }
75  
76  impl UnicodeXID for char {
77      #[inline]
is_xid_start(self) -> bool78      fn is_xid_start(self) -> bool {
79          // Fast-path for ascii idents
80          ('a' <= self && self <= 'z')
81              || ('A' <= self && self <= 'Z')
82              || (self > '\x7f' && derived_property::XID_Start(self))
83      }
84  
85      #[inline]
is_xid_continue(self) -> bool86      fn is_xid_continue(self) -> bool {
87          // Fast-path for ascii idents
88          ('a' <= self && self <= 'z')
89              || ('A' <= self && self <= 'Z')
90              || ('0' <= self && self <= '9')
91              || self == '_'
92              || (self > '\x7f' && derived_property::XID_Continue(self))
93      }
94  }
95