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 extern crate std; 44 45 #[cfg(feature = "bench")] 46 extern crate test; 47 48 use tables::derived_property; 49 pub use tables::UNICODE_VERSION; 50 51 mod tables; 52 53 #[cfg(test)] 54 mod tests; 55 56 /// Methods for determining if a character is a valid identifier character. 57 pub trait UnicodeXID { 58 /// Returns whether the specified character satisfies the 'XID_Start' 59 /// Unicode property. 60 /// 61 /// 'XID_Start' is a Unicode Derived Property specified in 62 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications), 63 /// mostly similar to ID_Start but modified for closure under NFKx. is_xid_start(self) -> bool64 fn is_xid_start(self) -> bool; 65 66 /// Returns whether the specified `char` satisfies the 'XID_Continue' 67 /// Unicode property. 68 /// 69 /// 'XID_Continue' is a Unicode Derived Property specified in 70 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications), 71 /// mostly similar to 'ID_Continue' but modified for closure under NFKx. is_xid_continue(self) -> bool72 fn is_xid_continue(self) -> bool; 73 } 74 75 impl UnicodeXID for char { 76 #[inline] is_xid_start(self) -> bool77 fn is_xid_start(self) -> bool { 78 derived_property::XID_Start(self) 79 } 80 81 #[inline] is_xid_continue(self) -> bool82 fn is_xid_continue(self) -> bool { 83 derived_property::XID_Continue(self) 84 } 85 } 86