diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
commit | 837b550238aa671a591ccf282dddeab29cadb206 (patch) | |
tree | 914b6b8862bace72bd3245ca184d374b08d8a672 /vendor/unicode-bom/src | |
parent | Adding debian version 1.70.0+dfsg2-1. (diff) | |
download | rustc-837b550238aa671a591ccf282dddeab29cadb206.tar.xz rustc-837b550238aa671a591ccf282dddeab29cadb206.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/unicode-bom/src')
-rw-r--r-- | vendor/unicode-bom/src/lib.rs | 31 | ||||
-rw-r--r-- | vendor/unicode-bom/src/test.rs | 69 |
2 files changed, 86 insertions, 14 deletions
diff --git a/vendor/unicode-bom/src/lib.rs b/vendor/unicode-bom/src/lib.rs index eae541106..6c8ba03d7 100644 --- a/vendor/unicode-bom/src/lib.rs +++ b/vendor/unicode-bom/src/lib.rs @@ -128,6 +128,35 @@ impl AsRef<str> for Bom { } } +impl AsRef<[u8]> for Bom { + /// Returns the BOM byte-array literal. + /// + /// Note that for UTF-7, + /// only the first three bytes of the BOM are returned. + /// That's because the last two bits of the fourth byte + /// belong to the following character, + /// so it's impossible to return the fourth byte + /// without further context. + /// Possible values for the missing fourth byte + /// are `0x38`, `0x39`, `0x2a` and `0x2b`. + fn as_ref(&self) -> &[u8] { + match *self { + Bom::Null => &[], + Bom::Bocu1 => &[0xfb, 0xee, 0x28], + Bom::Gb18030 => &[0x84, 0x31, 0x95, 0x33], + Bom::Scsu => &[0x0e, 0xfe, 0xff], + Bom::UtfEbcdic => &[0xdd, 0x73, 0x66, 0x73], + Bom::Utf1 => &[0xf7, 0x64, 0x4c], + Bom::Utf7 => &[0x2b, 0x2f, 0x76], + Bom::Utf8 => &[0xef, 0xbb, 0xbf], + Bom::Utf16Be => &[0xfe, 0xff], + Bom::Utf16Le => &[0xff, 0xfe], + Bom::Utf32Be => &[0, 0, 0xfe, 0xff], + Bom::Utf32Le => &[0xff, 0xfe, 0, 0], + } + } +} + impl Default for Bom { /// Returns the default/empty BOM type, `Bom::Null`. fn default() -> Self { @@ -138,7 +167,7 @@ impl Default for Bom { impl Display for Bom { /// Formats the BOM type as a `String`. fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { - write!(formatter, "{}", self.as_ref()) + write!(formatter, "{}", AsRef::<str>::as_ref(self)) } } diff --git a/vendor/unicode-bom/src/test.rs b/vendor/unicode-bom/src/test.rs index 7e88829dd..1d023ec18 100644 --- a/vendor/unicode-bom/src/test.rs +++ b/vendor/unicode-bom/src/test.rs @@ -15,19 +15,25 @@ use super::*; #[test] -fn as_ref() { - assert_eq!(Bom::Null.as_ref(), "[not set]"); - assert_eq!(Bom::Bocu1.as_ref(), "BOCU-1"); - assert_eq!(Bom::Gb18030.as_ref(), "GB 18030"); - assert_eq!(Bom::Scsu.as_ref(), "SCSU"); - assert_eq!(Bom::UtfEbcdic.as_ref(), "UTF-EBCDIC"); - assert_eq!(Bom::Utf1.as_ref(), "UTF-1"); - assert_eq!(Bom::Utf7.as_ref(), "UTF-7"); - assert_eq!(Bom::Utf8.as_ref(), "UTF-8"); - assert_eq!(Bom::Utf16Be.as_ref(), "UTF-16 (big-endian)"); - assert_eq!(Bom::Utf16Le.as_ref(), "UTF-16 (little-endian)"); - assert_eq!(Bom::Utf32Be.as_ref(), "UTF-32 (big-endian)"); - assert_eq!(Bom::Utf32Le.as_ref(), "UTF-32 (little-endian)"); +fn as_ref_str() { + assert_eq!(AsRef::<str>::as_ref(&Bom::Null), "[not set]"); + assert_eq!(AsRef::<str>::as_ref(&Bom::Bocu1), "BOCU-1"); + assert_eq!(AsRef::<str>::as_ref(&Bom::Gb18030), "GB 18030"); + assert_eq!(AsRef::<str>::as_ref(&Bom::Scsu), "SCSU"); + assert_eq!(AsRef::<str>::as_ref(&Bom::UtfEbcdic), "UTF-EBCDIC"); + assert_eq!(AsRef::<str>::as_ref(&Bom::Utf1), "UTF-1"); + assert_eq!(AsRef::<str>::as_ref(&Bom::Utf7), "UTF-7"); + assert_eq!(AsRef::<str>::as_ref(&Bom::Utf8), "UTF-8"); + assert_eq!(AsRef::<str>::as_ref(&Bom::Utf16Be), "UTF-16 (big-endian)"); + assert_eq!( + AsRef::<str>::as_ref(&Bom::Utf16Le), + "UTF-16 (little-endian)" + ); + assert_eq!(AsRef::<str>::as_ref(&Bom::Utf32Be), "UTF-32 (big-endian)"); + assert_eq!( + AsRef::<str>::as_ref(&Bom::Utf32Le), + "UTF-32 (little-endian)" + ); } #[test] @@ -36,6 +42,28 @@ fn to_string() { } #[test] +fn as_ref_arr() { + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Null), &[]); + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Bocu1), &[0xfb, 0xee, 0x28]); + assert_eq!( + AsRef::<[u8]>::as_ref(&Bom::Gb18030), + &[0x84, 0x31, 0x95, 0x33] + ); + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Scsu), &[0x0e, 0xfe, 0xff]); + assert_eq!( + AsRef::<[u8]>::as_ref(&Bom::UtfEbcdic), + &[0xdd, 0x73, 0x66, 0x73] + ); + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Utf1), &[0xf7, 0x64, 0x4c]); + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Utf7), &[0x2b, 0x2f, 0x76]); + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Utf8), &[0xef, 0xbb, 0xbf]); + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Utf16Be), &[0xfe, 0xff]); + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Utf16Le), &[0xff, 0xfe]); + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Utf32Be), &[0, 0, 0xfe, 0xff]); + assert_eq!(AsRef::<[u8]>::as_ref(&Bom::Utf32Le), &[0xff, 0xfe, 0, 0]); +} + +#[test] fn len() { assert_eq!(Bom::Null.len(), 0); assert_eq!(Bom::Bocu1.len(), 3); @@ -136,6 +164,21 @@ fn from_slice() { assert_bom(&[0xff, 0xfe, 0, 1, 42], Bom::Utf16Le); } +#[test] +fn from_as_ref_arr() { + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Null), Bom::Null); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Bocu1), Bom::Bocu1); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Gb18030), Bom::Gb18030); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Scsu), Bom::Scsu); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::UtfEbcdic), Bom::UtfEbcdic); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Utf1), Bom::Utf1); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Utf8), Bom::Utf8); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Utf16Be), Bom::Utf16Be); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Utf16Le), Bom::Utf16Le); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Utf32Be), Bom::Utf32Be); + assert_bom(AsRef::<[u8]>::as_ref(&Bom::Utf32Le), Bom::Utf32Le); +} + fn assert_bom(bytes: &[u8], expected: Bom) { assert_eq!(Bom::from(bytes), expected); } |