1
- //! The Unicode Collation Protocol
1
+ //! The Unicode Collation Protocol.
2
2
//!
3
- //! Used in the boot services environment to perform
4
- //! lexical comparison functions on Unicode strings for given languages
3
+ //! This protocol is used in the boot services environment to perform
4
+ //! lexical comparison functions on Unicode strings for given languages.
5
5
6
6
use core:: cmp:: Ordering ;
7
+ use uefi:: data_types:: { CStr16 , CStr8 , Char16 , Char8 } ;
7
8
use uefi_macros:: { unsafe_guid, Protocol } ;
8
- use uefi:: data_types:: { Char16 , CStr16 , Char8 , CStr8 } ;
9
9
10
- /// The Unicode Collation Protocol
10
+ /// The Unicode Collation Protocol.
11
11
///
12
- /// Used to perform case-insensitive comaprisons of strings
12
+ /// Used to perform case-insensitive comaprisons of strings.
13
13
#[ repr( C ) ]
14
14
#[ unsafe_guid( "a4c751fc-23ae-4c3e-92e9-4964cf63f349" ) ]
15
15
#[ derive( Protocol ) ]
16
16
pub struct UnicodeCollation {
17
- stri_coll : extern "efiapi" fn (
18
- this : & Self ,
19
- s1 : * const Char16 ,
20
- s2 : * const Char16
21
- ) -> isize ,
22
- metai_match : extern "efiapi" fn (
23
- this : & Self ,
24
- string : * const Char16 ,
25
- pattern : * const Char16
26
- ) -> bool ,
27
- str_lwr : extern "efiapi" fn (
28
- this : & Self ,
29
- s : * mut Char16
30
- ) ,
31
- str_upr : extern "efiapi" fn (
32
- this : & Self ,
33
- s : * mut Char16
34
- ) ,
35
- fat_to_str : extern "efiapi" fn (
36
- this : & Self ,
37
- fat_size : usize ,
38
- fat : * const Char8 ,
39
- s : * mut Char16
40
- ) ,
41
- str_to_fat : extern "efiapi" fn (
42
- this : & Self ,
43
- s : * const Char16 ,
44
- fat_size : usize ,
45
- fat : * mut Char8
46
- ) -> bool
17
+ stri_coll : extern "efiapi" fn ( this : & Self , s1 : * const Char16 , s2 : * const Char16 ) -> isize ,
18
+ metai_match :
19
+ extern "efiapi" fn ( this : & Self , string : * const Char16 , pattern : * const Char16 ) -> bool ,
20
+ str_lwr : extern "efiapi" fn ( this : & Self , s : * mut Char16 ) ,
21
+ str_upr : extern "efiapi" fn ( this : & Self , s : * mut Char16 ) ,
22
+ fat_to_str : extern "efiapi" fn ( this : & Self , fat_size : usize , fat : * const Char8 , s : * mut Char16 ) ,
23
+ str_to_fat :
24
+ extern "efiapi" fn ( this : & Self , s : * const Char16 , fat_size : usize , fat : * mut Char8 ) -> bool ,
47
25
}
48
26
49
27
impl UnicodeCollation {
50
28
/// Performs a case insensitive comparison of two
51
- /// null-terminated strings
29
+ /// null-terminated strings.
52
30
pub fn stri_coll ( & self , s1 : & CStr16 , s2 : & CStr16 ) -> Ordering {
53
- let order = ( self . stri_coll ) (
54
- self ,
55
- s1. as_ptr ( ) ,
56
- s2. as_ptr ( )
57
- ) ;
58
- if order == 0 {
59
- Ordering :: Equal
60
- } else if order < 0 {
61
- Ordering :: Less
62
- } else {
63
- Ordering :: Greater
64
- }
31
+ let order = ( self . stri_coll ) ( self , s1. as_ptr ( ) , s2. as_ptr ( ) ) ;
32
+ order. cmp ( & 0 )
65
33
}
66
34
67
35
/// Performs a case insensitive comparison between a null terminated
68
- /// pattern string and a null terminated string
36
+ /// pattern string and a null terminated string.
69
37
///
70
38
/// This function checks if character pattern described in `pattern`
71
39
/// is found in `string`. If the pattern match succeeds, true is returned.
72
- /// Otherwise, false is returned
40
+ /// Otherwise, false is returned.
73
41
///
74
42
/// The following syntax can be used to build the string `pattern`:
75
43
///
@@ -85,77 +53,81 @@ impl UnicodeCollation {
85
53
/// in ".FW", ".fw", ".Fw" or ".fW". The pattern "[a-z]" will match any
86
54
/// letter in the alphabet. The pattern "z" will match the letter "z".
87
55
/// The pattern "d?.*" will match the character "D" or "d" followed by
88
- /// any single character followed by a "." followed by any string
56
+ /// any single character followed by a "." followed by any string.
89
57
pub fn metai_match ( & self , s : & CStr16 , pattern : & CStr16 ) -> bool {
90
- ( self . metai_match ) (
91
- self ,
92
- s. as_ptr ( ) ,
93
- pattern. as_ptr ( )
94
- )
58
+ ( self . metai_match ) ( self , s. as_ptr ( ) , pattern. as_ptr ( ) )
95
59
}
96
60
97
- /// Converts the characters in `s` to lower case characters
98
- pub fn str_lwr < ' a > ( & self , s : & CStr16 , buf : & ' a mut [ u16 ] ) -> Result < & ' a CStr16 , StrConversionError > {
61
+ /// Converts the characters in `s` to lower case characters.
62
+ pub fn str_lwr < ' a > (
63
+ & self ,
64
+ s : & CStr16 ,
65
+ buf : & ' a mut [ u16 ] ,
66
+ ) -> Result < & ' a CStr16 , StrConversionError > {
99
67
let mut last_index = 0 ;
100
68
for ( i, c) in s. iter ( ) . enumerate ( ) {
101
- * buf. get_mut ( i)
102
- . ok_or ( StrConversionError :: BufferTooSmall ) ? = ( * c) . into ( ) ;
69
+ * buf. get_mut ( i) . ok_or ( StrConversionError :: BufferTooSmall ) ? = ( * c) . into ( ) ;
103
70
last_index = i;
104
71
}
105
72
* buf. get_mut ( last_index + 1 )
106
73
. ok_or ( StrConversionError :: BufferTooSmall ) ? = 0 ;
107
-
108
- ( self . str_lwr ) (
109
- self ,
110
- buf. as_ptr ( ) as * mut _
111
- ) ;
74
+
75
+ ( self . str_lwr ) ( self , buf. as_ptr ( ) as * mut _ ) ;
112
76
113
77
Ok ( unsafe { CStr16 :: from_u16_with_nul_unchecked ( buf) } )
114
78
}
115
79
116
- /// Coverts the characters in `s` to upper case characters
117
- pub fn str_upr < ' a > ( & self , s : & CStr16 , buf : & ' a mut [ u16 ] ) -> Result < & ' a CStr16 , StrConversionError > {
80
+ /// Converts the characters in `s` to upper case characters.
81
+ pub fn str_upr < ' a > (
82
+ & self ,
83
+ s : & CStr16 ,
84
+ buf : & ' a mut [ u16 ] ,
85
+ ) -> Result < & ' a CStr16 , StrConversionError > {
118
86
let mut last_index = 0 ;
119
87
for ( i, c) in s. iter ( ) . enumerate ( ) {
120
- * buf. get_mut ( i)
121
- . ok_or ( StrConversionError :: BufferTooSmall ) ? = ( * c) . into ( ) ;
88
+ * buf. get_mut ( i) . ok_or ( StrConversionError :: BufferTooSmall ) ? = ( * c) . into ( ) ;
122
89
last_index = i;
123
90
}
124
91
* buf. get_mut ( last_index + 1 )
125
92
. ok_or ( StrConversionError :: BufferTooSmall ) ? = 0 ;
126
-
127
- ( self . str_upr ) (
128
- self ,
129
- buf. as_ptr ( ) as * mut _
130
- ) ;
93
+
94
+ ( self . str_upr ) ( self , buf. as_ptr ( ) as * mut _ ) ;
131
95
132
96
Ok ( unsafe { CStr16 :: from_u16_with_nul_unchecked ( buf) } )
133
97
}
134
98
135
- /// Converts the 8.3 FAT file name `fat` to a null terminated string
136
- pub fn fat_to_str < ' a > ( & self , fat : & CStr8 , buf : & ' a mut [ u16 ] ) -> Result < & ' a CStr16 , StrConversionError > {
99
+ /// Converts the 8.3 FAT file name `fat` to a null terminated string.
100
+ pub fn fat_to_str < ' a > (
101
+ & self ,
102
+ fat : & CStr8 ,
103
+ buf : & ' a mut [ u16 ] ,
104
+ ) -> Result < & ' a CStr16 , StrConversionError > {
137
105
if buf. len ( ) < fat. to_bytes_with_nul ( ) . len ( ) {
138
106
return Err ( StrConversionError :: BufferTooSmall ) ;
139
107
}
140
108
( self . fat_to_str ) (
141
109
self ,
142
110
fat. to_bytes_with_nul ( ) . len ( ) ,
143
111
fat. as_ptr ( ) ,
144
- buf. as_ptr ( ) as * mut _
112
+ buf. as_ptr ( ) as * mut _ ,
145
113
) ;
146
114
Ok ( unsafe { CStr16 :: from_u16_with_nul_unchecked ( buf) } )
147
115
}
148
116
149
- /// Converts the null terminated string `s` to legal characters in a FAT file name
150
- pub fn str_to_fat < ' a > ( & self , s : & CStr16 , buf : & ' a mut [ u8 ] ) -> Result < & ' a CStr8 , StrConversionError > {
117
+ /// Converts the null terminated string `s` to legal characters in a FAT file name.
118
+ pub fn str_to_fat < ' a > (
119
+ & self ,
120
+ s : & CStr16 ,
121
+ buf : & ' a mut [ u8 ] ,
122
+ ) -> Result < & ' a CStr8 , StrConversionError > {
151
123
if s. as_slice_with_nul ( ) . len ( ) > buf. len ( ) {
152
124
return Err ( StrConversionError :: BufferTooSmall ) ;
153
125
}
154
126
let failed = ( self . str_to_fat ) (
155
127
self ,
156
128
s. as_ptr ( ) ,
157
129
s. as_slice_with_nul ( ) . len ( ) ,
158
- buf. as_ptr ( ) as * mut _
130
+ buf. as_ptr ( ) as * mut _ ,
159
131
) ;
160
132
if failed {
161
133
Err ( StrConversionError :: ConversionFailed )
@@ -178,11 +150,11 @@ impl UnicodeCollation {
178
150
}
179
151
}
180
152
181
- /// Errors returned by [`UnicodeCollation::str_lwr`] and [`UnicodeCollation::str_upr`]
153
+ /// Errors returned by [`UnicodeCollation::str_lwr`] and [`UnicodeCollation::str_upr`].
182
154
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
183
155
pub enum StrConversionError {
184
- /// The conversion failed
156
+ /// The conversion failed.
185
157
ConversionFailed ,
186
- /// The buffer given is too small to hold the string
187
- BufferTooSmall
188
- }
158
+ /// The buffer given is too small to hold the string.
159
+ BufferTooSmall ,
160
+ }
0 commit comments