File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
//! String representations.
4
4
5
+ use core:: fmt:: { self , Write } ;
5
6
use core:: ops:: { self , Deref , Index } ;
6
7
7
8
use crate :: bindings;
@@ -209,6 +210,35 @@ impl CStr {
209
210
}
210
211
}
211
212
213
+ impl fmt:: Display for CStr {
214
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
215
+ for & c in self . as_bytes ( ) {
216
+ if ( 0x20 ..0x7f ) . contains ( & c) {
217
+ // Printable character
218
+ f. write_char ( c as char ) ?;
219
+ } else {
220
+ write ! ( f, "\\ x{:02x}" , c) ?;
221
+ }
222
+ }
223
+ Ok ( ( ) )
224
+ }
225
+ }
226
+
227
+ impl fmt:: Debug for CStr {
228
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
229
+ f. write_str ( "\" " ) ?;
230
+ for & c in self . as_bytes ( ) {
231
+ if ( 0x20 ..0x7f ) . contains ( & c) {
232
+ // Printable character
233
+ f. write_char ( c as char ) ?;
234
+ } else {
235
+ write ! ( f, "\\ x{:02x}" , c) ?;
236
+ }
237
+ }
238
+ f. write_str ( "\" " )
239
+ }
240
+ }
241
+
212
242
impl AsRef < BStr > for CStr {
213
243
#[ inline]
214
244
fn as_ref ( & self ) -> & BStr {
You can’t perform that action at this time.
0 commit comments