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 language subtag (examples: `"en"`, `"csb"`, `"zh"`, `"und"`, etc.) |
7 | /// |
8 | /// [`Language`] represents a Unicode base language code conformant to the |
9 | /// [`unicode_language_id`] field of the Language and Locale Identifier. |
10 | /// |
11 | /// # Examples |
12 | /// |
13 | /// ``` |
14 | /// use icu::locid::subtags::Language; |
15 | /// |
16 | /// let language: Language = |
17 | /// "en".parse().expect("Failed to parse a language subtag."); |
18 | /// ``` |
19 | /// |
20 | /// If the [`Language`] has no value assigned, it serializes to a string `"und"`, which |
21 | /// can be then parsed back to an empty [`Language`] field. |
22 | /// |
23 | /// # Examples |
24 | /// |
25 | /// ``` |
26 | /// use icu::locid::subtags::Language; |
27 | /// |
28 | /// assert_eq!(Language::default().as_str(), "und"); |
29 | /// ``` |
30 | /// |
31 | /// `Notice`: ICU4X uses a narrow form of language subtag of 2-3 characters. |
32 | /// The specification allows language subtag to optionally also be 5-8 characters |
33 | /// but that form has not been used and ICU4X does not support it right now. |
34 | /// |
35 | /// [`unicode_language_id`]: https://unicode.org/reports/tr35/#unicode_language_id |
36 | Language, |
37 | subtags, |
38 | language, |
39 | subtags_language, |
40 | 2..=3, |
41 | s, |
42 | s.is_ascii_alphabetic(), |
43 | s.to_ascii_lowercase(), |
44 | s.is_ascii_alphabetic_lowercase(), |
45 | InvalidLanguage, |
46 | ["en" , "foo" ], |
47 | ["419" , "german" , "en1" ], |
48 | ); |
49 | |
50 | impl Language { |
51 | /// The default undefined language "und". Same as [`default()`](Default::default()). |
52 | /// |
53 | /// # Examples |
54 | /// |
55 | /// ``` |
56 | /// use icu::locid::subtags::Language; |
57 | /// |
58 | /// assert_eq!(Language::default(), Language::UND); |
59 | /// ``` |
60 | pub const UND: Self = unsafe { Self::from_raw_unchecked(*b"und" ) }; |
61 | |
62 | /// Resets the [`Language`] subtag to an empty one (equal to `"und"`). |
63 | /// |
64 | /// # Examples |
65 | /// |
66 | /// ``` |
67 | /// use icu::locid::subtags::{language, Language}; |
68 | /// |
69 | /// let mut lang = language!("csb" ); |
70 | /// |
71 | /// assert_ne!(lang, Language::UND); |
72 | /// |
73 | /// lang.clear(); |
74 | /// |
75 | /// assert_eq!(lang, Language::UND); |
76 | /// ``` |
77 | #[inline ] |
78 | pub fn clear(&mut self) { |
79 | *self = Self::UND |
80 | } |
81 | |
82 | /// Tests if the [`Language`] subtag is empty (equal to `"und"`). |
83 | /// |
84 | /// # Examples |
85 | /// |
86 | /// ``` |
87 | /// use icu::locid::subtags::Language; |
88 | /// |
89 | /// let mut lang = Language::UND; |
90 | /// |
91 | /// assert!(lang.is_empty()); |
92 | /// |
93 | /// lang.clear(); |
94 | /// |
95 | /// assert!(lang.is_empty()); |
96 | /// ``` |
97 | #[inline ] |
98 | pub fn is_empty(self) -> bool { |
99 | self == Self::UND |
100 | } |
101 | } |
102 | |
103 | impl Default for Language { |
104 | fn default() -> Language { |
105 | Language::UND |
106 | } |
107 | } |
108 | |