| 1 | // This file is part of ICU4X. For terms of use, please see the file |
| 2 | // called LICENSE at the top level of the ICU4X source tree |
| 3 | // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 | |
| 5 | impl_tinystr_subtag!( |
| 6 | /// A region subtag (examples: `"US"`, `"CN"`, `"AR"` etc.) |
| 7 | /// |
| 8 | /// [`Region`] represents a Unicode base language code conformant to the |
| 9 | /// [`unicode_region_id`] field of the Language and Locale Identifier. |
| 10 | /// |
| 11 | /// # Examples |
| 12 | /// |
| 13 | /// ``` |
| 14 | /// use icu::locid::subtags::Region; |
| 15 | /// |
| 16 | /// let region: Region = |
| 17 | /// "DE".parse().expect("Failed to parse a region subtag."); |
| 18 | /// ``` |
| 19 | /// |
| 20 | /// [`unicode_region_id`]: https://unicode.org/reports/tr35/#unicode_region_id |
| 21 | Region, |
| 22 | subtags, |
| 23 | region, |
| 24 | subtags_region, |
| 25 | 2..=3, |
| 26 | s, |
| 27 | if s.len() == 2 { |
| 28 | s.is_ascii_alphabetic() |
| 29 | } else { |
| 30 | s.is_ascii_numeric() |
| 31 | }, |
| 32 | if s.len() == 2 { |
| 33 | s.to_ascii_uppercase() |
| 34 | } else { |
| 35 | s |
| 36 | }, |
| 37 | if s.len() == 2 { |
| 38 | s.is_ascii_alphabetic_uppercase() |
| 39 | } else { |
| 40 | s.is_ascii_numeric() |
| 41 | }, |
| 42 | InvalidSubtag, |
| 43 | ["FR" , "123" ], |
| 44 | ["12" , "FRA" , "b2" ], |
| 45 | ); |
| 46 | |
| 47 | impl Region { |
| 48 | /// Returns true if the Region has an alphabetic code. |
| 49 | /// |
| 50 | /// # Examples |
| 51 | /// |
| 52 | /// ``` |
| 53 | /// use icu::locid::subtags::Region; |
| 54 | /// |
| 55 | /// let region = Region::try_from_bytes(b"us" ).expect("Parsing failed." ); |
| 56 | /// |
| 57 | /// assert!(region.is_alphabetic()); |
| 58 | /// ``` |
| 59 | pub fn is_alphabetic(&self) -> bool { |
| 60 | self.0.len() == 2 |
| 61 | } |
| 62 | } |
| 63 | |