diff options
author | Justin Van Patten <jvp@justinvp.com> | 2017-08-17 19:29:25 +0300 |
---|---|---|
committer | Stephen Toub <stoub@microsoft.com> | 2017-08-17 19:29:25 +0300 |
commit | 6d7be63179868eac8d799c32846c3f724950abdd (patch) | |
tree | a0a926f1339075de3d13929df59fb6721dd86874 /src | |
parent | 0547375d71a7b0ea8e81e7690e2e6b0ceb6c70e6 (diff) |
System.Private.Xml: Use Encoding.Preamble (#23323)
* System.Private.Xml: Use Encoding.Preamble
Use the new `Encoding.Preamble` property to avoid unnecessary `byte[]`
allocations for encodings that return cached instances from `Preamble`
(e.g. all of the built-in encodings that have preambles).
* Avoid TypeLoadException in async code
Factor out the "eat preamble" code into its own method (that can be
reused by both the sync and async code) to prevent the
`ReadOnlySpan<byte>` preamble from being captured in a field of the
async state machine. This avoids the System.TypeLoadException : A value
type containing a by-ref instance field, such as Span<T>, cannot be used
as the type for a class instance field.
Diffstat (limited to 'src')
5 files changed, 30 insertions, 38 deletions
diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlEncodedRawTextWriter.cs b/src/System.Private.Xml/src/System/Xml/Core/XmlEncodedRawTextWriter.cs index 4c7860d105..a1e6c3e1d2 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlEncodedRawTextWriter.cs +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlEncodedRawTextWriter.cs @@ -180,10 +180,10 @@ namespace System.Xml if (!stream.CanSeek || stream.Position == 0) { - byte[] bom = encoding.GetPreamble(); + ReadOnlySpan<byte> bom = encoding.Preamble; if (bom.Length != 0) { - this.stream.Write(bom, 0, bom.Length); + this.stream.Write(bom); } } diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGenerator.cxx b/src/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGenerator.cxx index 478b502c5c..b06d403251 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGenerator.cxx +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGenerator.cxx @@ -210,9 +210,9 @@ namespace System.Xml { // Output UTF-8 byte order mark if Encoding object wants it if ( !stream.CanSeek || stream.Position == 0 ) { - byte[] bom = encoding.GetPreamble(); + ReadOnlySpan<byte> bom = encoding.Preamble; if ( bom.Length != 0 ) { - Buffer.BlockCopy( bom, 0, bufBytes, 1, bom.Length ); + bom.CopyTo(new Span<byte>(bufBytes, 1)); bufPos += bom.Length; textPos += bom.Length; } @@ -237,9 +237,9 @@ namespace System.Xml { encoder = encoding.GetEncoder(); if ( !stream.CanSeek || stream.Position == 0 ) { - byte[] bom = encoding.GetPreamble(); + ReadOnlySpan<byte> bom = encoding.Preamble; if ( bom.Length != 0 ) { - this.stream.Write( bom, 0, bom.Length ); + this.stream.Write( bom ); } } #endif diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs index ea72797ef0..72bbed177d 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs @@ -2914,21 +2914,8 @@ namespace System.Xml } SetupEncoding(encoding); - // eat preamble - byte[] preamble = _ps.encoding.GetPreamble(); - int preambleLen = preamble.Length; - int i; - for (i = 0; i < preambleLen && i < _ps.bytesUsed; i++) - { - if (_ps.bytes[i] != preamble[i]) - { - break; - } - } - if (i == preambleLen) - { - _ps.bytePos = preambleLen; - } + // eat preamble + EatPreamble(); _documentStartBytePos = _ps.bytePos; @@ -3225,6 +3212,24 @@ namespace System.Xml } } + private void EatPreamble() + { + ReadOnlySpan<byte> preamble = _ps.encoding.Preamble; + int preambleLen = preamble.Length; + int i; + for (i = 0; i < preambleLen && i < _ps.bytesUsed; i++) + { + if (_ps.bytes[i] != preamble[i]) + { + break; + } + } + if (i == preambleLen) + { + _ps.bytePos = preambleLen; + } + } + // Switches the reader's encoding private void SwitchEncoding(Encoding newEncoding) { diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs index 7cd3a428d0..4dc66f8de5 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs @@ -992,21 +992,8 @@ namespace System.Xml } SetupEncoding(encoding); - // eat preamble - byte[] preamble = _ps.encoding.GetPreamble(); - int preambleLen = preamble.Length; - int i; - for (i = 0; i < preambleLen && i < _ps.bytesUsed; i++) - { - if (_ps.bytes[i] != preamble[i]) - { - break; - } - } - if (i == preambleLen) - { - _ps.bytePos = preambleLen; - } + // eat preamble + EatPreamble(); _documentStartBytePos = _ps.bytePos; diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriter.cs b/src/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriter.cs index 276b793cbe..f10d2efc56 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriter.cs +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriter.cs @@ -125,10 +125,10 @@ namespace System.Xml // Output UTF-8 byte order mark if Encoding object wants it if (!stream.CanSeek || stream.Position == 0) { - byte[] bom = encoding.GetPreamble(); + ReadOnlySpan<byte> bom = encoding.Preamble; if (bom.Length != 0) { - Buffer.BlockCopy(bom, 0, bufBytes, 1, bom.Length); + bom.CopyTo(new Span<byte>(bufBytes, 1)); bufPos += bom.Length; textPos += bom.Length; } |