Skip to content

Commit

Permalink
Merge pull request dotnet#35683 from krwq/nullable-xml-2
Browse files Browse the repository at this point in the history
Nullable: System.Xml, part 2
  • Loading branch information
krwq authored May 12, 2020
2 parents 7f042fc + 833a7af commit 5f2398b
Show file tree
Hide file tree
Showing 28 changed files with 794 additions and 416 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
namespace System.Xml
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System;
using System.Xml.Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Diagnostics;

namespace System.Xml
Expand Down Expand Up @@ -38,7 +39,7 @@ internal override void Reset() { }
//
internal class IncrementalReadCharsDecoder : IncrementalReadDecoder
{
private char[] _buffer;
private char[]? _buffer;
private int _startIndex;
private int _curIndex;
private int _endIndex;
Expand All @@ -61,6 +62,7 @@ internal override bool IsFull

internal override int Decode(char[] chars, int startPos, int len)
{
Debug.Assert(_buffer != null);
Debug.Assert(chars != null);
Debug.Assert(len >= 0);
Debug.Assert(startPos >= 0);
Expand All @@ -73,6 +75,7 @@ internal override int Decode(char[] chars, int startPos, int len)
{
copyCount = len;
}

Buffer.BlockCopy(chars, startPos * 2, _buffer, _curIndex * 2, copyCount * 2);
_curIndex += copyCount;

Expand All @@ -81,6 +84,7 @@ internal override int Decode(char[] chars, int startPos, int len)

internal override int Decode(string str, int startPos, int len)
{
Debug.Assert(_buffer != null);
Debug.Assert(str != null);
Debug.Assert(len >= 0);
Debug.Assert(startPos >= 0);
Expand All @@ -93,6 +97,7 @@ internal override int Decode(string str, int startPos, int len)
{
copyCount = len;
}

str.CopyTo(startPos, _buffer, _curIndex, copyCount);
_curIndex += copyCount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Runtime.CompilerServices;

namespace System
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
namespace System.Xml
{
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
namespace System.Xml
{
using System;
Expand All @@ -24,9 +25,9 @@ internal class QueryOutputWriter : XmlRawWriter
{
private readonly XmlRawWriter _wrapped;
private bool _inCDataSection;
private readonly Dictionary<XmlQualifiedName, int> _lookupCDataElems;
private readonly BitStack _bitsCData;
private readonly XmlQualifiedName _qnameCData;
private readonly Dictionary<XmlQualifiedName, int>? _lookupCDataElems;
private readonly BitStack? _bitsCData;
private readonly XmlQualifiedName? _qnameCData;
private bool _outputDocType;
private readonly bool _checkWellFormedDoc;
private bool _hasDocElem;
Expand Down Expand Up @@ -86,7 +87,7 @@ public QueryOutputWriter(XmlRawWriter writer, XmlWriterSettings settings)
/// <summary>
/// Get and set the namespace resolver that's used by this RawWriter to resolve prefixes.
/// </summary>
internal override IXmlNamespaceResolver NamespaceResolver
internal override IXmlNamespaceResolver? NamespaceResolver
{
get
{
Expand Down Expand Up @@ -119,7 +120,7 @@ public override XmlWriterSettings Settings
{
get
{
XmlWriterSettings settings = _wrapped.Settings;
XmlWriterSettings settings = _wrapped.Settings!;

settings.ReadOnly = false;
settings.DocTypeSystem = _systemId;
Expand All @@ -133,7 +134,7 @@ public override XmlWriterSettings Settings
/// <summary>
/// Suppress this explicit call to WriteDocType if information was provided by XmlWriterSettings.
/// </summary>
public override void WriteDocType(string name, string pubid, string sysid, string subset)
public override void WriteDocType(string name, string? pubid, string? sysid, string? subset)
{
if (_publicId == null && _systemId == null)
{
Expand All @@ -146,7 +147,7 @@ public override void WriteDocType(string name, string pubid, string sysid, strin
/// Check well-formedness, possibly output doc-type-decl, and determine whether this element is a
/// CData section element.
/// </summary>
public override void WriteStartElement(string prefix, string localName, string ns)
public override void WriteStartElement(string? prefix, string localName, string? ns)
{
EndCDataSection();

Expand All @@ -164,7 +165,7 @@ public override void WriteStartElement(string prefix, string localName, string n
if (_outputDocType)
{
_wrapped.WriteDocType(
prefix.Length != 0 ? prefix + ":" + localName : localName,
string.IsNullOrEmpty(prefix) ? localName : prefix + ":" + localName,
_publicId,
_systemId,
null);
Expand All @@ -177,8 +178,8 @@ public override void WriteStartElement(string prefix, string localName, string n
if (_lookupCDataElems != null)
{
// Determine whether this element is a CData section element
_qnameCData.Init(localName, ns);
_bitsCData.PushBit(_lookupCDataElems.ContainsKey(_qnameCData));
_qnameCData!.Init(localName, ns);
_bitsCData!.PushBit(_lookupCDataElems.ContainsKey(_qnameCData));
}
}

Expand All @@ -192,7 +193,7 @@ internal override void WriteEndElement(string prefix, string localName, string n
_depth--;

if (_lookupCDataElems != null)
_bitsCData.PopBit();
_bitsCData!.PopBit();
}

internal override void WriteFullEndElement(string prefix, string localName, string ns)
Expand All @@ -205,15 +206,15 @@ internal override void WriteFullEndElement(string prefix, string localName, stri
_depth--;

if (_lookupCDataElems != null)
_bitsCData.PopBit();
_bitsCData!.PopBit();
}

internal override void StartElementContent()
{
_wrapped.StartElementContent();
}

public override void WriteStartAttribute(string prefix, string localName, string ns)
public override void WriteStartAttribute(string? prefix, string localName, string? ns)
{
_inAttr = true;
_wrapped.WriteStartAttribute(prefix, localName, ns);
Expand Down Expand Up @@ -248,7 +249,7 @@ internal override void WriteEndNamespaceDeclaration()
_wrapped.WriteEndNamespaceDeclaration();
}

public override void WriteCData(string text)
public override void WriteCData(string? text)
{
_wrapped.WriteCData(text);
}
Expand All @@ -273,7 +274,7 @@ public override void WriteWhitespace(string ws)
_wrapped.WriteWhitespace(ws);
}

public override void WriteString(string text)
public override void WriteString(string? text)
{
if (!_inAttr && (_inCDataSection || StartCDataSection()))
_wrapped.WriteCData(text);
Expand Down Expand Up @@ -351,7 +352,7 @@ public override void Flush()
private bool StartCDataSection()
{
Debug.Assert(!_inCDataSection);
if (_lookupCDataElems != null && _bitsCData.PeekBit())
if (_lookupCDataElems != null && _bitsCData!.PeekBit())
{
_inCDataSection = true;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System;
using System.IO;
using System.Diagnostics;
Expand All @@ -25,9 +26,9 @@ internal class QueryOutputWriterV1 : XmlWriter
{
private readonly XmlWriter _wrapped;
private bool _inCDataSection;
private readonly Dictionary<XmlQualifiedName, XmlQualifiedName> _lookupCDataElems;
private readonly BitStack _bitsCData;
private readonly XmlQualifiedName _qnameCData;
private readonly Dictionary<XmlQualifiedName, XmlQualifiedName?>? _lookupCDataElems;
private readonly BitStack? _bitsCData;
private readonly XmlQualifiedName? _qnameCData;
private bool _outputDocType, _inAttr;
private readonly string _systemId, _publicId;

Expand Down Expand Up @@ -71,7 +72,7 @@ public QueryOutputWriterV1(XmlWriter writer, XmlWriterSettings settings)
if (settings.CDataSectionElements != null && settings.CDataSectionElements.Count > 0)
{
_bitsCData = new BitStack();
_lookupCDataElems = new Dictionary<XmlQualifiedName, XmlQualifiedName>();
_lookupCDataElems = new Dictionary<XmlQualifiedName, XmlQualifiedName?>();
_qnameCData = new XmlQualifiedName();

// Add each element name to the lookup table
Expand Down Expand Up @@ -122,7 +123,7 @@ public override void WriteEndDocument()
/// <summary>
/// Suppress this explicit call to WriteDocType if information was provided by XmlWriterSettings.
/// </summary>
public override void WriteDocType(string name, string pubid, string sysid, string subset)
public override void WriteDocType(string name, string? pubid, string? sysid, string? subset)
{
if (_publicId == null && _systemId == null)
{
Expand All @@ -135,7 +136,7 @@ public override void WriteDocType(string name, string pubid, string sysid, strin
/// Output doc-type-decl on the first element, and determine whether this element is a
/// CData section element.
/// </summary>
public override void WriteStartElement(string prefix, string localName, string ns)
public override void WriteStartElement(string? prefix, string localName, string? ns)
{
EndCDataSection();

Expand All @@ -146,18 +147,22 @@ public override void WriteStartElement(string prefix, string localName, string n
if (ws == WriteState.Start || ws == WriteState.Prolog)
{
_wrapped.WriteDocType(
prefix.Length != 0 ? prefix + ":" + localName : localName,
string.IsNullOrEmpty(prefix) ? localName : prefix + ":" + localName,
_publicId,
_systemId,
null);
}

_outputDocType = false;
}

_wrapped.WriteStartElement(prefix, localName, ns);

if (_lookupCDataElems != null)
{
Debug.Assert(_qnameCData != null);
Debug.Assert(_bitsCData != null);

// Determine whether this element is a CData section element
_qnameCData.Init(localName, ns);
_bitsCData.PushBit(_lookupCDataElems.ContainsKey(_qnameCData));
Expand All @@ -171,7 +176,10 @@ public override void WriteEndElement()
_wrapped.WriteEndElement();

if (_lookupCDataElems != null)
{
Debug.Assert(_bitsCData != null);
_bitsCData.PopBit();
}
}

public override void WriteFullEndElement()
Expand All @@ -181,10 +189,13 @@ public override void WriteFullEndElement()
_wrapped.WriteFullEndElement();

if (_lookupCDataElems != null)
{
Debug.Assert(_bitsCData != null);
_bitsCData.PopBit();
}
}

public override void WriteStartAttribute(string prefix, string localName, string ns)
public override void WriteStartAttribute(string? prefix, string localName, string? ns)
{
_inAttr = true;
_wrapped.WriteStartAttribute(prefix, localName, ns);
Expand All @@ -196,7 +207,7 @@ public override void WriteEndAttribute()
_wrapped.WriteEndAttribute();
}

public override void WriteCData(string text)
public override void WriteCData(string? text)
{
_wrapped.WriteCData(text);
}
Expand All @@ -221,7 +232,7 @@ public override void WriteWhitespace(string ws)
_wrapped.WriteWhitespace(ws);
}

public override void WriteString(string text)
public override void WriteString(string? text)
{
if (!_inAttr && (_inCDataSection || StartCDataSection()))
_wrapped.WriteCData(text);
Expand Down Expand Up @@ -291,7 +302,7 @@ public override void Flush()
_wrapped.Flush();
}

public override string LookupPrefix(string ns)
public override string? LookupPrefix(string ns)
{
return _wrapped.LookupPrefix(ns);
}
Expand All @@ -308,11 +319,12 @@ public override string LookupPrefix(string ns)
private bool StartCDataSection()
{
Debug.Assert(!_inCDataSection);
if (_lookupCDataElems != null && _bitsCData.PeekBit())
if (_lookupCDataElems != null && _bitsCData!.PeekBit())
{
_inCDataSection = true;
return true;
}

return false;
}

Expand Down
Loading

0 comments on commit 5f2398b

Please sign in to comment.