if (nMaxDigits > 0)
{
+ Debug.Assert(sDecimal != null);
sb.Append(sDecimal);
if ((digPos < 0) && (nMaxDigits > 0))
{
// 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.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
ThrowHelper.ThrowArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
- if (chars.Length - index < count)
+ if (chars!.Length - index < count)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer);
}
fixed (char* pChars = chars)
{
- return GetByteCountCommon(pChars, chars.Length);
+ return GetByteCountCommon(pChars, chars!.Length);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] // called directly by GetByteCountCommon
- private protected sealed override unsafe int GetByteCountFast(char* pChars, int charsLength, EncoderFallback fallback, out int charsConsumed)
+ private protected sealed override unsafe int GetByteCountFast(char* pChars, int charsLength, EncoderFallback? fallback, out int charsConsumed)
{
// First: Can we short-circuit the entire calculation?
// If an EncoderReplacementFallback is in use, all non-ASCII chars
resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
- if (chars.Length - charIndex < charCount)
+ if (chars!.Length - charIndex < charCount)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCount);
}
- if ((uint)byteIndex > bytes.Length)
+ if ((uint)byteIndex > bytes!.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_Index);
}
resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
- if (chars.Length - charIndex < charCount)
+ if (chars!.Length - charIndex < charCount)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCount);
}
- if ((uint)byteIndex > bytes.Length)
+ if ((uint)byteIndex > bytes!.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_Index);
}
return bytesWritten;
}
- private protected sealed override unsafe int GetBytesWithFallback(ReadOnlySpan<char> chars, int originalCharsLength, Span<byte> bytes, int originalBytesLength, EncoderNLS encoder)
+ private protected sealed override unsafe int GetBytesWithFallback(ReadOnlySpan<char> chars, int originalCharsLength, Span<byte> bytes, int originalBytesLength, EncoderNLS? encoder)
{
// We special-case EncoderReplacementFallback if it's telling us to write a single ASCII char,
// since we believe this to be relatively common and we can handle it more efficiently than
ThrowHelper.ThrowArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
- if (bytes.Length - index < count)
+ if (bytes!.Length - index < count)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] // called directly by GetCharCountCommon
- private protected sealed override unsafe int GetCharCountFast(byte* pBytes, int bytesLength, DecoderFallback fallback, out int bytesConsumed)
+ private protected sealed override unsafe int GetCharCountFast(byte* pBytes, int bytesLength, DecoderFallback? fallback, out int bytesConsumed)
{
// First: Can we short-circuit the entire calculation?
// If a DecoderReplacementFallback is in use, all non-ASCII bytes are replaced with
resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
- if (bytes.Length - byteIndex < byteCount)
+ if (bytes!.Length - byteIndex < byteCount)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer);
}
- if ((uint)charIndex > (uint)chars.Length)
+ if ((uint)charIndex > (uint)chars!.Length)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.charIndex, ExceptionResource.ArgumentOutOfRange_Index);
}
return charsWritten;
}
- private protected sealed override unsafe int GetCharsWithFallback(ReadOnlySpan<byte> bytes, int originalBytesLength, Span<char> chars, int originalCharsLength, DecoderNLS decoder)
+ private protected sealed override unsafe int GetCharsWithFallback(ReadOnlySpan<byte> bytes, int originalBytesLength, Span<char> chars, int originalCharsLength, DecoderNLS? decoder)
{
// We special-case DecoderReplacementFallback if it's telling us to write a single BMP char,
// since we believe this to be relatively common and we can handle it more efficiently than
resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
- if (bytes.Length - byteIndex < byteCount)
+ if (bytes!.Length - byteIndex < byteCount)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer);
}
// 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;
using System.Numerics;
using System.Runtime.CompilerServices;
// 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.Text
{
internal class CodePageDataItem
{
public int CodePage { get; }
public int UIFamilyCodePage { get; }
- public string WebName { get; }
- public string HeaderName { get; }
- public string BodyName { get; }
- public string DisplayName { get; }
+ public string? WebName { get; }
+ public string? HeaderName { get; }
+ public string? BodyName { get; }
+ public string? DisplayName { get; }
public uint Flags { get; }
internal CodePageDataItem(
int codePage,
int uiFamilyCodePage,
- string webName,
- string headerName,
- string bodyName,
- string displayName,
+ string? webName,
+ string? headerName,
+ string? bodyName,
+ string? displayName,
uint flags)
{
CodePage = codePage;
// 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.Text;
using System;
using System.Diagnostics;
//
public abstract class Decoder
{
- internal DecoderFallback _fallback = null;
+ internal DecoderFallback? _fallback = null;
- internal DecoderFallbackBuffer _fallbackBuffer = null;
+ internal DecoderFallbackBuffer? _fallbackBuffer = null;
protected Decoder()
{
// We don't call default reset because default reset probably isn't good if we aren't initialized.
}
- public DecoderFallback Fallback
+ public DecoderFallback? Fallback
{
get
{
// 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
//
// This is used internally to create best fit behavior as per the original windows best fit behavior.
//
internal sealed class InternalDecoderBestFitFallback : DecoderFallback
{
// Our variables
- internal Encoding _encoding = null;
- internal char[] _arrayBestFit = null;
+ internal Encoding _encoding;
+ internal char[]? _arrayBestFit = null;
internal char _cReplacement = '?';
internal InternalDecoderBestFitFallback(Encoding encoding)
}
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is InternalDecoderBestFitFallback that)
{
- return (_encoding.CodePage == that._encoding.CodePage);
+ return _encoding.CodePage == that._encoding.CodePage;
}
- return (false);
+ return false;
}
public override int GetHashCode()
private InternalDecoderBestFitFallback _oFallback;
// Private object for locking instead of locking on a public type for SQL reliability work.
- private static object s_InternalSyncObject;
+ private static object? s_InternalSyncObject;
private static object InternalSyncObject
{
get
if (s_InternalSyncObject == null)
{
object o = new object();
- Interlocked.CompareExchange<object>(ref s_InternalSyncObject, o, null);
+ Interlocked.CompareExchange<object?>(ref s_InternalSyncObject, o, null);
}
- return s_InternalSyncObject;
+ return s_InternalSyncObject!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
}
}
{
// Need to figure out our best fit character, low is beginning of array, high is 1 AFTER end of array
int lowBound = 0;
+ Debug.Assert(_oFallback._arrayBestFit != null);
int highBound = _oFallback._arrayBestFit.Length;
int index;
char cCheck;
// 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.Globalization;
using System.Runtime.Serialization;
}
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is DecoderExceptionFallback that)
{
private void Throw(byte[] bytesUnknown, int index)
{
+ bytesUnknown = bytesUnknown ?? Array.Empty<byte>();
+
// Create a string representation of our bytes.
StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 3);
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public sealed class DecoderFallbackException : ArgumentException
{
- private byte[] _bytesUnknown = null;
+ private byte[]? _bytesUnknown = null;
private int _index = 0;
public DecoderFallbackException()
HResult = HResults.COR_E_ARGUMENT;
}
- public DecoderFallbackException(string message)
+ public DecoderFallbackException(string? message)
: base(message)
{
HResult = HResults.COR_E_ARGUMENT;
}
- public DecoderFallbackException(string message, Exception innerException)
+ public DecoderFallbackException(string? message, Exception? innerException)
: base(message, innerException)
{
HResult = HResults.COR_E_ARGUMENT;
}
- public DecoderFallbackException(string message, byte[] bytesUnknown, int index)
+ public DecoderFallbackException(string? message, byte[]? bytesUnknown, int index)
: base(message)
{
_bytesUnknown = bytesUnknown;
{
}
- public byte[] BytesUnknown
+ public byte[]? BytesUnknown
{
get
{
- return (_bytesUnknown);
+ return _bytesUnknown;
}
}
// 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;
using System.Globalization;
using System.Threading;
{
public abstract class DecoderFallback
{
- private static DecoderFallback s_replacementFallback; // Default fallback, uses no best fit & "?"
- private static DecoderFallback s_exceptionFallback;
+ private static DecoderFallback? s_replacementFallback; // Default fallback, uses no best fit & "?"
+ private static DecoderFallback? s_exceptionFallback;
public static DecoderFallback ReplacementFallback =>
- s_replacementFallback ?? Interlocked.CompareExchange(ref s_replacementFallback, new DecoderReplacementFallback(), null) ?? s_replacementFallback;
+ s_replacementFallback ?? Interlocked.CompareExchange(ref s_replacementFallback, new DecoderReplacementFallback(), null) ?? s_replacementFallback!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
public static DecoderFallback ExceptionFallback =>
- s_exceptionFallback ?? Interlocked.CompareExchange<DecoderFallback>(ref s_exceptionFallback, new DecoderExceptionFallback(), null) ?? s_exceptionFallback;
+ s_exceptionFallback ?? Interlocked.CompareExchange<DecoderFallback?>(ref s_exceptionFallback, new DecoderExceptionFallback(), null) ?? s_exceptionFallback!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
// Fallback
//
internal unsafe byte* byteStart;
internal unsafe char* charEnd;
- internal Encoding _encoding;
- internal DecoderNLS _decoder;
+ internal Encoding? _encoding;
+ internal DecoderNLS? _decoder;
private int _originalByteCount;
// Internal Reset
this.charEnd = charEnd;
}
- internal static DecoderFallbackBuffer CreateAndInitialize(Encoding encoding, DecoderNLS decoder, int originalByteCount)
+ internal static DecoderFallbackBuffer CreateAndInitialize(Encoding encoding, DecoderNLS? decoder, int originalByteCount)
{
// The original byte count is only used for keeping track of what 'index' value needs
// to be passed to the abstract Fallback method. The index value is calculated by subtracting
// private helper methods
internal void ThrowLastBytesRecursive(byte[] bytesUnknown)
{
+ bytesUnknown = bytesUnknown ?? Array.Empty<byte>();
+
// Create a string representation of our bytes.
StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 3);
int i;
// 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.Buffers;
using System.Diagnostics;
using System.Runtime.InteropServices;
this.Reset();
}
- // This is used by our child deserializers
- internal DecoderNLS()
- {
- _encoding = null;
- this.Reset();
- }
-
public override void Reset()
{
ClearLeftoverData();
_throwOnOverflow = true;
// By default just call the encoding version, no flush by default
+ Debug.Assert(_encoding != null);
return _encoding.GetCharCount(bytes, count, this);
}
_throwOnOverflow = true;
// By default just call the encodings version
+ Debug.Assert(_encoding != null);
return _encoding.GetChars(bytes, byteCount, chars, charCount, this);
}
_bytesUsed = 0;
// Do conversion
+ Debug.Assert(_encoding != null);
charsUsed = _encoding.GetChars(bytes, byteCount, chars, charCount, this);
bytesUsed = _bytesUsed;
combinedBuffer = combinedBuffer.Slice(0, ConcatInto(GetLeftoverData(), bytes, combinedBuffer));
int charCount = 0;
+ Debug.Assert(_encoding != null);
switch (_encoding.DecodeFirstRune(combinedBuffer, out Rune value, out int combinedBufferBytesConsumed))
{
case OperationStatus.Done:
if (FallbackBuffer.Fallback(combinedBuffer.Slice(0, combinedBufferBytesConsumed).ToArray(), index: 0))
{
- charCount = _fallbackBuffer.DrainRemainingDataForGetCharCount();
+ charCount = _fallbackBuffer!.DrainRemainingDataForGetCharCount();
Debug.Assert(charCount >= 0, "Fallback buffer shouldn't have returned a negative char count.");
}
bool persistNewCombinedBuffer = false;
+ Debug.Assert(_encoding != null);
switch (_encoding.DecodeFirstRune(combinedBuffer, out Rune value, out int combinedBufferBytesConsumed))
{
case OperationStatus.Done:
// Couldn't decode the buffer. Fallback the buffer instead.
if (FallbackBuffer.Fallback(combinedBuffer.Slice(0, combinedBufferBytesConsumed).ToArray(), index: 0)
- && !_fallbackBuffer.TryDrainRemainingDataForGetChars(chars, out charsWritten))
+ && !_fallbackBuffer!.TryDrainRemainingDataForGetChars(chars, out charsWritten))
{
goto DestinationTooSmall;
}
// opportunity for any code before us to make forward progress, so we must fail immediately.
_encoding.ThrowCharsOverflow(this, nothingDecoded: true);
- throw null; // will never reach this point
+ // TODO-NULLABLE: https://github.com/dotnet/csharplang/issues/538
+ throw null!; // will never reach this point
}
/// <summary>
// 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.Text
}
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is DecoderReplacementFallback that)
{
- return (_strDefault == that._strDefault);
+ return _strDefault == that._strDefault;
}
- return (false);
+ return false;
}
public override int GetHashCode()
// Construction
public DecoderReplacementFallbackBuffer(DecoderReplacementFallback fallback)
{
+ // TODO-NULLABLE: NullReferenceException (fallback)
_strDefault = fallback.DefaultString;
}
// 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.Text;
using System;
using System.Diagnostics;
//
public abstract class Encoder
{
- internal EncoderFallback _fallback = null;
+ internal EncoderFallback? _fallback = null;
- internal EncoderFallbackBuffer _fallbackBuffer = null;
+ internal EncoderFallbackBuffer? _fallbackBuffer = null;
protected Encoder()
{
// We don't call default reset because default reset probably isn't good if we aren't initialized.
}
- public EncoderFallback Fallback
+ public EncoderFallback? Fallback
{
get
{
// 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
//
// This is used internally to create best fit behavior as per the original windows best fit behavior.
//
internal class InternalEncoderBestFitFallback : EncoderFallback
{
// Our variables
- internal Encoding _encoding = null;
- internal char[] _arrayBestFit = null;
+ internal Encoding _encoding;
+ internal char[]? _arrayBestFit = null;
internal InternalEncoderBestFitFallback(Encoding encoding)
{
}
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is InternalEncoderBestFitFallback that)
{
- return (_encoding.CodePage == that._encoding.CodePage);
+ return _encoding.CodePage == that._encoding.CodePage;
}
- return (false);
+ return false;
}
public override int GetHashCode()
private int _iSize;
// Private object for locking instead of locking on a public type for SQL reliability work.
- private static object s_InternalSyncObject;
+ private static object? s_InternalSyncObject;
private static object InternalSyncObject
{
get
if (s_InternalSyncObject == null)
{
object o = new object();
- Interlocked.CompareExchange<object>(ref s_InternalSyncObject, o, null);
+ Interlocked.CompareExchange<object?>(ref s_InternalSyncObject, o, null);
}
- return s_InternalSyncObject;
+ return s_InternalSyncObject!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
}
}
{
// Need to figure out our best fit character, low is beginning of array, high is 1 AFTER end of array
int lowBound = 0;
+ Debug.Assert(_oFallback._arrayBestFit != null);
int highBound = _oFallback._arrayBestFit.Length;
int index;
// 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.Runtime.Serialization;
}
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is EncoderExceptionFallback that)
{
- return (true);
+ return true;
}
- return (false);
+ return false;
}
public override int GetHashCode()
HResult = HResults.COR_E_ARGUMENT;
}
- public EncoderFallbackException(string message)
+ public EncoderFallbackException(string? message)
: base(message)
{
HResult = HResults.COR_E_ARGUMENT;
}
- public EncoderFallbackException(string message, Exception innerException)
+ public EncoderFallbackException(string? message, Exception? innerException)
: base(message, innerException)
{
HResult = HResults.COR_E_ARGUMENT;
}
internal EncoderFallbackException(
- string message, char charUnknown, int index) : base(message)
+ string? message, char charUnknown, int index) : base(message)
{
_charUnknown = charUnknown;
_index = index;
{
get
{
- return (_charUnknown);
+ return _charUnknown;
}
}
{
get
{
- return (_charUnknownHigh);
+ return _charUnknownHigh;
}
}
{
get
{
- return (_charUnknownLow);
+ return _charUnknownLow;
}
}
// Return true if the unknown character is a surrogate pair.
public bool IsUnknownSurrogate()
{
- return (_charUnknownHigh != '\0');
+ return _charUnknownHigh != '\0';
}
}
}
// 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.Buffers;
using System.Diagnostics;
using System.Threading;
{
public abstract class EncoderFallback
{
- private static EncoderFallback s_replacementFallback; // Default fallback, uses no best fit & "?"
- private static EncoderFallback s_exceptionFallback;
+ private static EncoderFallback? s_replacementFallback; // Default fallback, uses no best fit & "?"
+ private static EncoderFallback? s_exceptionFallback;
// Get each of our generic fallbacks.
get
{
if (s_replacementFallback == null)
- Interlocked.CompareExchange<EncoderFallback>(ref s_replacementFallback, new EncoderReplacementFallback(), null);
+ Interlocked.CompareExchange<EncoderFallback?>(ref s_replacementFallback, new EncoderReplacementFallback(), null);
- return s_replacementFallback;
+ return s_replacementFallback!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
}
}
get
{
if (s_exceptionFallback == null)
- Interlocked.CompareExchange<EncoderFallback>(ref s_exceptionFallback, new EncoderExceptionFallback(), null);
+ Interlocked.CompareExchange<EncoderFallback?>(ref s_exceptionFallback, new EncoderExceptionFallback(), null);
- return s_exceptionFallback;
+ return s_exceptionFallback!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
}
}
// These help us with our performance and messages internally
internal unsafe char* charStart;
internal unsafe char* charEnd;
- internal EncoderNLS encoder; // TODO: MAKE ME PRIVATE
+ internal EncoderNLS? encoder; // TODO: MAKE ME PRIVATE
internal bool setEncoder;
internal bool bUsedEncoder;
internal bool bFallingBack = false;
internal int iRecursionCount = 0;
private const int iMaxRecursion = 250;
- private Encoding encoding;
+ private Encoding? encoding;
private int originalCharCount;
// Internal Reset
// Set the above values
// This can't be part of the constructor because EncoderFallbacks would have to know how to implement these.
- internal unsafe void InternalInitialize(char* charStart, char* charEnd, EncoderNLS encoder, bool setEncoder)
+ internal unsafe void InternalInitialize(char* charStart, char* charEnd, EncoderNLS? encoder, bool setEncoder)
{
this.charStart = charStart;
this.charEnd = charEnd;
this.iRecursionCount = 0;
}
- internal static EncoderFallbackBuffer CreateAndInitialize(Encoding encoding, EncoderNLS encoder, int originalCharCount)
+ internal static EncoderFallbackBuffer CreateAndInitialize(Encoding encoding, EncoderNLS? encoder, int originalCharCount)
{
// The original char count is only used for keeping track of what 'index' value needs
// to be passed to the abstract Fallback method. The index value is calculated by subtracting
{
int originalBytesLength = bytes.Length;
+ Debug.Assert(encoding != null);
Rune thisRune;
while ((thisRune = GetNextRune()).Value != 0)
{
{
int totalByteCount = 0;
+ Debug.Assert(encoding != null);
Rune thisRune;
while ((thisRune = GetNextRune()).Value != 0)
{
// 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.Buffers;
using System.Diagnostics;
using System.Runtime.InteropServices;
this.Reset();
}
- internal EncoderNLS()
- {
- _encoding = null;
- this.Reset();
- }
-
public override void Reset()
{
_charLeftOver = (char)0;
_mustFlush = flush;
_throwOnOverflow = true;
+ Debug.Assert(_encoding != null);
return _encoding.GetByteCount(chars, count, this);
}
_mustFlush = flush;
_throwOnOverflow = true;
+ Debug.Assert(_encoding != null);
return _encoding.GetBytes(chars, charCount, bytes, byteCount, this);
}
_charsUsed = 0;
// Do conversion
+ Debug.Assert(_encoding != null);
bytesUsed = _encoding.GetBytes(chars, charCount, bytes, byteCount, this);
charsUsed = _charsUsed;
{
get
{
+ Debug.Assert(_encoding != null);
return _encoding;
}
}
{
charsConsumed = 1; // consumed the leftover high surrogate + the first char in the input buffer
+ Debug.Assert(_encoding != null);
if (_encoding.TryGetByteCount(rune, out int byteCount))
{
Debug.Assert(byteCount >= 0, "Encoding shouldn't have returned a negative byte count.");
}
// Now tally the number of bytes that would've been emitted as part of fallback.
-
+ Debug.Assert(_fallbackBuffer != null);
return _fallbackBuffer.DrainRemainingDataForGetByteCount();
}
}
if (Rune.TryCreate(_charLeftOver, secondChar, out Rune rune))
{
charsConsumed = 1; // at the very least, we consumed 1 char from the input
+ Debug.Assert(_encoding != null);
switch (_encoding.EncodeRune(rune, bytes, out bytesWritten))
{
case OperationStatus.Done:
// 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.Runtime;
using System.Diagnostics;
}
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is EncoderReplacementFallback that)
{
- return (_strDefault == that._strDefault);
+ return _strDefault == that._strDefault;
}
- return (false);
+ return false;
}
public override int GetHashCode()
}
}
-
-
public sealed class EncoderReplacementFallbackBuffer : EncoderFallbackBuffer
{
// Store our default string
// Construction
public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback)
{
+ // TODO-NULLABLE: NullReferenceException
// 2X in case we're a surrogate pair
_strDefault = fallback.DefaultString + fallback.DefaultString;
}
// 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.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
/// <summary>
/// Entry point from <see cref="EncoderNLS.GetByteCount"/>.
/// </summary>
- internal virtual unsafe int GetByteCount(char* pChars, int charCount, EncoderNLS encoder)
+ internal virtual unsafe int GetByteCount(char* pChars, int charCount, EncoderNLS? encoder)
{
Debug.Assert(encoder != null, "This code path should only be called from EncoderNLS.");
Debug.Assert(charCount >= 0, "Caller should've checked this condition.");
/// The implementation should not attempt to perform any sort of fallback behavior.
/// If custom fallback behavior is necessary, override <see cref="GetByteCountWithFallback"/>.
/// </remarks>
- private protected virtual unsafe int GetByteCountFast(char* pChars, int charsLength, EncoderFallback fallback, out int charsConsumed)
+ private protected virtual unsafe int GetByteCountFast(char* pChars, int charsLength, EncoderFallback? fallback, out int charsConsumed)
{
// Any production-quality type would override this method and provide a real
// implementation, so we won't provide a base implementation. However, a
/// If the resulting byte count is greater than <see cref="int.MaxValue"/>.
/// (Implementation should call <see cref="ThrowConversionOverflow"/>.)
/// </exception>
- private protected virtual unsafe int GetByteCountWithFallback(ReadOnlySpan<char> chars, int originalCharsLength, EncoderNLS encoder)
+ private protected virtual unsafe int GetByteCountWithFallback(ReadOnlySpan<char> chars, int originalCharsLength, EncoderNLS? encoder)
{
Debug.Assert(!chars.IsEmpty, "Caller shouldn't invoke this method with an empty input buffer.");
Debug.Assert(originalCharsLength >= 0, "Caller provided invalid parameter.");
/// <summary>
/// Entry point from <see cref="EncoderNLS.GetBytes"/> and <see cref="EncoderNLS.Convert"/>.
/// </summary>
- internal virtual unsafe int GetBytes(char* pChars, int charCount, byte* pBytes, int byteCount, EncoderNLS encoder)
+ internal virtual unsafe int GetBytes(char* pChars, int charCount, byte* pBytes, int byteCount, EncoderNLS? encoder)
{
Debug.Assert(encoder != null, "This code path should only be called from EncoderNLS.");
Debug.Assert(charCount >= 0, "Caller should've checked this condition.");
/// implementation, deferring to the base implementation if needed. This method calls <see cref="ThrowBytesOverflow"/>
/// if necessary.
/// </remarks>
- private protected virtual unsafe int GetBytesWithFallback(ReadOnlySpan<char> chars, int originalCharsLength, Span<byte> bytes, int originalBytesLength, EncoderNLS encoder)
+ private protected virtual unsafe int GetBytesWithFallback(ReadOnlySpan<char> chars, int originalCharsLength, Span<byte> bytes, int originalBytesLength, EncoderNLS? encoder)
{
Debug.Assert(!chars.IsEmpty, "Caller shouldn't invoke this method with an empty input buffer.");
Debug.Assert(originalCharsLength >= 0, "Caller provided invalid parameter.");
/// <summary>
/// Entry point from <see cref="DecoderNLS.GetCharCount"/>.
/// </summary>
- internal virtual unsafe int GetCharCount(byte* pBytes, int byteCount, DecoderNLS decoder)
+ internal virtual unsafe int GetCharCount(byte* pBytes, int byteCount, DecoderNLS? decoder)
{
Debug.Assert(decoder != null, "This code path should only be called from DecoderNLS.");
Debug.Assert(byteCount >= 0, "Caller should've checked this condition.");
/// The implementation should not attempt to perform any sort of fallback behavior.
/// If custom fallback behavior is necessary, override <see cref="GetCharCountWithFallback"/>.
/// </remarks>
- private protected virtual unsafe int GetCharCountFast(byte* pBytes, int bytesLength, DecoderFallback fallback, out int bytesConsumed)
+ private protected virtual unsafe int GetCharCountFast(byte* pBytes, int bytesLength, DecoderFallback? fallback, out int bytesConsumed)
{
// Any production-quality type would override this method and provide a real
// implementation, so we won't provide a base implementation. However, a
/// If the resulting char count is greater than <see cref="int.MaxValue"/>.
/// (Implementation should call <see cref="ThrowConversionOverflow"/>.)
/// </exception>
- private unsafe int GetCharCountWithFallback(ReadOnlySpan<byte> bytes, int originalBytesLength, DecoderNLS decoder)
+ private unsafe int GetCharCountWithFallback(ReadOnlySpan<byte> bytes, int originalBytesLength, DecoderNLS? decoder)
{
Debug.Assert(!bytes.IsEmpty, "Caller shouldn't invoke this method with an empty input buffer.");
Debug.Assert(originalBytesLength >= 0, "Caller provided invalid parameter.");
/// <summary>
/// Entry point from <see cref="DecoderNLS.GetChars"/> and <see cref="DecoderNLS.Convert"/>.
/// </summary>
- internal virtual unsafe int GetChars(byte* pBytes, int byteCount, char* pChars, int charCount, DecoderNLS decoder)
+ internal virtual unsafe int GetChars(byte* pBytes, int byteCount, char* pChars, int charCount, DecoderNLS? decoder)
{
Debug.Assert(decoder != null, "This code path should only be called from DecoderNLS.");
Debug.Assert(byteCount >= 0, "Caller should've checked this condition.");
/// implementation, deferring to the base implementation if needed. This method calls <see cref="ThrowCharsOverflow"/>
/// if necessary.
/// </remarks>
- private protected virtual unsafe int GetCharsWithFallback(ReadOnlySpan<byte> bytes, int originalBytesLength, Span<char> chars, int originalCharsLength, DecoderNLS decoder)
+ private protected virtual unsafe int GetCharsWithFallback(ReadOnlySpan<byte> bytes, int originalBytesLength, Span<char> chars, int originalCharsLength, DecoderNLS? decoder)
{
Debug.Assert(!bytes.IsEmpty, "Caller shouldn't invoke this method with an empty input buffer.");
Debug.Assert(originalBytesLength >= 0, "Caller provided invalid parameter.");
// 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;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
internal int _codePage = 0;
- internal CodePageDataItem _dataItem = null;
+ internal CodePageDataItem? _dataItem = null;
// Because of encoders we may be read only
[OptionalField(VersionAdded = 2)]
private bool _isReadOnly = true;
// Encoding (encoder) fallback
- internal EncoderFallback encoderFallback = null;
- internal DecoderFallback decoderFallback = null;
+ internal EncoderFallback encoderFallback = null!;
+ internal DecoderFallback decoderFallback = null!;
protected Encoding() : this(0)
{
// This constructor is needed to allow any sub-classing implementation to provide encoder/decoder fallback objects
// because the encoding object is always created as read-only object and don't allow setting encoder/decoder fallback
// after the creation is done.
- protected Encoding(int codePage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
+ protected Encoding(int codePage, EncoderFallback? encoderFallback, DecoderFallback? decoderFallback)
{
// Validate code page
if (codePage < 0)
public static Encoding GetEncoding(int codepage)
{
- Encoding result = EncodingProvider.GetEncodingFromProvider(codepage);
+ Encoding? result = EncodingProvider.GetEncodingFromProvider(codepage);
if (result != null)
return result;
public static Encoding GetEncoding(int codepage,
EncoderFallback encoderFallback, DecoderFallback decoderFallback)
{
- Encoding baseEncoding = EncodingProvider.GetEncodingFromProvider(codepage, encoderFallback, decoderFallback);
+ Encoding? baseEncoding = EncodingProvider.GetEncodingFromProvider(codepage, encoderFallback, decoderFallback);
if (baseEncoding != null)
return baseEncoding;
//
public static Encoding GetEncoding(string name)
{
- Encoding baseEncoding = EncodingProvider.GetEncodingFromProvider(name);
+ Encoding? baseEncoding = EncodingProvider.GetEncodingFromProvider(name);
if (baseEncoding != null)
return baseEncoding;
public static Encoding GetEncoding(string name,
EncoderFallback encoderFallback, DecoderFallback decoderFallback)
{
- Encoding baseEncoding = EncodingProvider.GetEncodingFromProvider(name, encoderFallback, decoderFallback);
+ Encoding? baseEncoding = EncodingProvider.GetEncodingFromProvider(name, encoderFallback, decoderFallback);
if (baseEncoding != null)
return baseEncoding;
// Otherwise, the code below will throw exception when trying to call
// EncodingTable.GetCodePageFromName().
//
- return (GetEncoding(EncodingTable.GetCodePageFromName(name), encoderFallback, decoderFallback));
+ return GetEncoding(EncodingTable.GetCodePageFromName(name), encoderFallback, decoderFallback);
}
// Return a list of all EncodingInfo objects describing all of our encodings
// Returns the name for this encoding that can be used with mail agent body tags.
// If the encoding may not be used, the string is empty.
- public virtual string BodyName
+ public virtual string? BodyName
{
get
{
{
GetDataItem();
}
- return (_dataItem.BodyName);
+ return _dataItem!.BodyName;
}
}
// Returns the human-readable description of the encoding ( e.g. Hebrew (DOS)).
- public virtual string EncodingName
+ public virtual string? EncodingName
{
get
{
GetDataItem();
}
- return _dataItem.DisplayName;
+ return _dataItem!.DisplayName;
}
}
// Returns the name for this encoding that can be used with mail agent header
// tags. If the encoding may not be used, the string is empty.
- public virtual string HeaderName
+ public virtual string? HeaderName
{
get
{
{
GetDataItem();
}
- return (_dataItem.HeaderName);
+ return _dataItem!.HeaderName;
}
}
// Returns the IANA preferred name for this encoding.
- public virtual string WebName
+ public virtual string? WebName
{
get
{
{
GetDataItem();
}
- return (_dataItem.WebName);
+ return _dataItem!.WebName;
}
}
{
GetDataItem();
}
- return (_dataItem.UIFamilyCodePage);
+ return _dataItem!.UIFamilyCodePage;
}
}
{
GetDataItem();
}
- return ((_dataItem.Flags & MIMECONTF_BROWSER) != 0);
+ return (_dataItem!.Flags & MIMECONTF_BROWSER) != 0;
}
}
{
GetDataItem();
}
- return ((_dataItem.Flags & MIMECONTF_SAVABLE_BROWSER) != 0);
+ return (_dataItem!.Flags & MIMECONTF_SAVABLE_BROWSER) != 0;
}
}
{
GetDataItem();
}
- return ((_dataItem.Flags & MIMECONTF_MAILNEWS) != 0);
+ return (_dataItem!.Flags & MIMECONTF_MAILNEWS) != 0;
}
}
{
GetDataItem();
}
- return ((_dataItem.Flags & MIMECONTF_SAVABLE_MAILNEWS) != 0);
+ return (_dataItem!.Flags & MIMECONTF_SAVABLE_MAILNEWS) != 0;
}
}
private static Encoding BigEndianUTF32 => UTF32Encoding.s_bigEndianDefault;
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is Encoding that)
return (_codePage == that._codePage) &&
SR.Format(SR.Argument_EncodingConversionOverflowBytes, EncodingName, EncoderFallback.GetType()), "bytes");
}
- internal void ThrowBytesOverflow(EncoderNLS encoder, bool nothingEncoded)
+ internal void ThrowBytesOverflow(EncoderNLS? encoder, bool nothingEncoded)
{
if (encoder == null || encoder._throwOnOverflow || nothingEncoded)
{
}
// If we didn't throw, we are in convert and have to remember our flushing
- encoder.ClearMustFlush();
+ encoder!.ClearMustFlush();
}
[StackTraceHidden]
SR.Format(SR.Argument_EncodingConversionOverflowChars, EncodingName, DecoderFallback.GetType()), "chars");
}
- internal void ThrowCharsOverflow(DecoderNLS decoder, bool nothingDecoded)
+ internal void ThrowCharsOverflow(DecoderNLS? decoder, bool nothingDecoded)
{
if (decoder == null || decoder._throwOnOverflow || nothingDecoded)
{
}
// If we didn't throw, we are in convert and have to remember our flushing
- decoder.ClearMustFlush();
+ decoder!.ClearMustFlush();
}
internal sealed class DefaultEncoder : Encoder, IObjectReference
private unsafe char* _charEnd;
private int _charCountResult = 0;
private Encoding _enc;
- private DecoderNLS _decoder;
+ private DecoderNLS? _decoder;
private unsafe byte* _byteStart;
private unsafe byte* _byteEnd;
private unsafe byte* _bytes;
private DecoderFallbackBuffer _fallbackBuffer;
- internal unsafe EncodingCharBuffer(Encoding enc, DecoderNLS decoder, char* charStart, int charCount,
+ internal unsafe EncodingCharBuffer(Encoding enc, DecoderNLS? decoder, char* charStart, int charCount,
byte* byteStart, int byteCount)
{
_enc = enc;
private unsafe char* _charEnd;
private int _byteCountResult = 0;
private Encoding _enc;
- private EncoderNLS _encoder;
+ private EncoderNLS? _encoder;
internal EncoderFallbackBuffer fallbackBuffer;
- internal unsafe EncodingByteBuffer(Encoding inEncoding, EncoderNLS inEncoder,
+ internal unsafe EncodingByteBuffer(Encoding inEncoding, EncoderNLS? inEncoder,
byte* inByteStart, int inByteCount, char* inCharStart, int inCharCount)
{
_enc = inEncoding;
if (_encoder._throwOnOverflow && _encoder.InternalHasFallbackBuffer &&
this.fallbackBuffer.Remaining > 0)
throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty,
- _encoder.Encoding.EncodingName, _encoder.Fallback.GetType()));
+ _encoder.Encoding.EncodingName, _encoder.Fallback!.GetType()));
}
fallbackBuffer.InternalInitialize(_chars, _charEnd, _encoder, _bytes != null);
}
internal bool AddByte(byte b1)
{
- return (AddByte(b1, 0));
+ return AddByte(b1, 0);
}
internal bool AddByte(byte b1, byte b2)
{
- return (AddByte(b1, b2, 0));
+ return AddByte(b1, b2, 0);
}
internal bool AddByte(byte b1, byte b2, int moreBytesExpected)
{
- return (AddByte(b1, 1 + moreBytesExpected) && AddByte(b2, moreBytesExpected));
+ return AddByte(b1, 1 + moreBytesExpected) && AddByte(b2, moreBytesExpected);
}
internal bool AddByte(byte b1, byte b2, byte b3)
internal bool AddByte(byte b1, byte b2, byte b3, int moreBytesExpected)
{
- return (AddByte(b1, 2 + moreBytesExpected) &&
+ return AddByte(b1, 2 + moreBytesExpected) &&
AddByte(b2, 1 + moreBytesExpected) &&
- AddByte(b3, moreBytesExpected));
+ AddByte(b3, moreBytesExpected);
}
internal bool AddByte(byte b1, byte b2, byte b3, byte b4)
{
- return (AddByte(b1, 3) &&
+ return AddByte(b1, 3) &&
AddByte(b2, 2) &&
AddByte(b3, 1) &&
- AddByte(b4, 0));
+ AddByte(b4, 0);
}
internal unsafe void MovePrevious(bool bThrow)
get
{
// See if fallbackBuffer is not empty or if there's data left in chars buffer.
- return ((fallbackBuffer.Remaining > 0) || (_chars < _charEnd));
+ return (fallbackBuffer.Remaining > 0) || (_chars < _charEnd);
}
}
// https://github.com/dotnet/buildtools/blob/6736870b84e06b75e7df32bb84d442db1b2afa10/src/Microsoft.DotNet.Build.Tasks/PackageFiles/encoding.targets
//
+// TODO-NULLABLE: We should edit original source instead: https://github.com/dotnet/buildtools/blob/master/src/Microsoft.DotNet.Build.Tasks/GenerateEncodingTable.cs#L235
+#nullable enable
namespace System.Text
{
internal static partial class EncodingTable
// 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.Text
{
public sealed class EncodingInfo
return Encoding.GetEncoding(CodePage);
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is EncodingInfo that)
{
// 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.Collections;
using System.Diagnostics;
public override unsafe int GetByteCount(string s)
{
// Validate input
- if (s==null)
+ if (s == null)
throw new ArgumentNullException(nameof(s));
fixed (char* pChars = s)
// 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.Collections;
using System.Collections.Generic;
public abstract class EncodingProvider
{
public EncodingProvider() { }
- public abstract Encoding GetEncoding(string name);
- public abstract Encoding GetEncoding(int codepage);
+ public abstract Encoding? GetEncoding(string name);
+ public abstract Encoding? GetEncoding(int codepage);
// GetEncoding should return either valid encoding or null. shouldn't throw any exception except on null name
- public virtual Encoding GetEncoding(string name, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
+ public virtual Encoding? GetEncoding(string name, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
{
- Encoding enc = GetEncoding(name);
+ Encoding? enc = GetEncoding(name);
if (enc != null)
{
- enc = (Encoding)GetEncoding(name).Clone();
+ enc = (Encoding)enc.Clone();
enc.EncoderFallback = encoderFallback;
enc.DecoderFallback = decoderFallback;
}
return enc;
}
- public virtual Encoding GetEncoding(int codepage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
+ public virtual Encoding? GetEncoding(int codepage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
{
- Encoding enc = GetEncoding(codepage);
+ Encoding? enc = GetEncoding(codepage);
if (enc != null)
{
- enc = (Encoding)GetEncoding(codepage).Clone();
+ enc = (Encoding)enc.Clone();
enc.EncoderFallback = encoderFallback;
enc.DecoderFallback = decoderFallback;
}
}
}
- internal static Encoding GetEncodingFromProvider(int codepage)
+ internal static Encoding? GetEncodingFromProvider(int codepage)
{
if (s_providers == null)
return null;
EncodingProvider[] providers = s_providers;
foreach (EncodingProvider provider in providers)
{
- Encoding enc = provider.GetEncoding(codepage);
+ Encoding? enc = provider.GetEncoding(codepage);
if (enc != null)
return enc;
}
return null;
}
- internal static Encoding GetEncodingFromProvider(string encodingName)
+ internal static Encoding? GetEncodingFromProvider(string encodingName)
{
if (s_providers == null)
return null;
EncodingProvider[] providers = s_providers;
foreach (EncodingProvider provider in providers)
{
- Encoding enc = provider.GetEncoding(encodingName);
+ Encoding? enc = provider.GetEncoding(encodingName);
if (enc != null)
return enc;
}
return null;
}
- internal static Encoding GetEncodingFromProvider(int codepage, EncoderFallback enc, DecoderFallback dec)
+ internal static Encoding? GetEncodingFromProvider(int codepage, EncoderFallback enc, DecoderFallback dec)
{
if (s_providers == null)
return null;
EncodingProvider[] providers = s_providers;
foreach (EncodingProvider provider in providers)
{
- Encoding encing = provider.GetEncoding(codepage, enc, dec);
- if (encing != null)
- return encing;
+ Encoding? encoding = provider.GetEncoding(codepage, enc, dec);
+ if (encoding != null)
+ return encoding;
}
return null;
}
- internal static Encoding GetEncodingFromProvider(string encodingName, EncoderFallback enc, DecoderFallback dec)
+ internal static Encoding? GetEncodingFromProvider(string encodingName, EncoderFallback enc, DecoderFallback dec)
{
if (s_providers == null)
return null;
EncodingProvider[] providers = s_providers;
foreach (EncodingProvider provider in providers)
{
- Encoding encoding = provider.GetEncoding(encodingName, enc, dec);
+ Encoding? encoding = provider.GetEncoding(encodingName, enc, dec);
if (encoding != null)
return encoding;
}
}
private static object s_InternalSyncObject = new object();
- private static volatile EncodingProvider[] s_providers;
+ private static volatile EncodingProvider[]? s_providers;
}
}
// 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.Collections;
using System.Diagnostics;
using System.Threading;
internal static partial class EncodingTable
{
private static readonly Hashtable s_nameToCodePage = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));
- private static CodePageDataItem[] s_codePageToCodePageData;
+ private static CodePageDataItem?[]? s_codePageToCodePageData;
/*=================================GetCodePageFromName==========================
**Action: Given a encoding name, return the correct code page number for this encoding.
return arrayEncodingInfo;
}
- internal static CodePageDataItem GetCodePageDataItem(int codePage)
+ internal static CodePageDataItem? GetCodePageDataItem(int codePage)
{
if (s_codePageToCodePageData == null)
{
- Interlocked.CompareExchange(ref s_codePageToCodePageData, new CodePageDataItem[s_mappedCodePages.Length], null);
+ Interlocked.CompareExchange<CodePageDataItem?[]?>(ref s_codePageToCodePageData, new CodePageDataItem[s_mappedCodePages.Length], null);
}
// Keep in sync with s_mappedCodePages
return null;
}
- CodePageDataItem data = s_codePageToCodePageData[index];
+ CodePageDataItem? data = s_codePageToCodePageData![index]; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
if (data == null)
{
- Interlocked.CompareExchange(ref s_codePageToCodePageData[index], InternalGetCodePageDataItem(codePage, index), null);
+ Interlocked.CompareExchange<CodePageDataItem?>(ref s_codePageToCodePageData[index], InternalGetCodePageDataItem(codePage, index), null);
data = s_codePageToCodePageData[index];
}
// 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.Diagnostics;
// GetByteCount
// Note: We start by assuming that the output will be the same as count. Having
// an encoder or fallback may change that assumption
- internal override unsafe int GetByteCount(char* chars, int charCount, EncoderNLS encoder)
+ internal override unsafe int GetByteCount(char* chars, int charCount, EncoderNLS? encoder)
{
// Just need to ASSERT, this is called by something else internal that checked parameters already
Debug.Assert(charCount >= 0, "[Latin1Encoding.GetByteCount]count is negative");
// If we have an encoder AND we aren't using default fallback,
// then we may have a complicated count.
- EncoderReplacementFallback fallback;
+ EncoderReplacementFallback? fallback;
if (encoder != null)
{
charLeftOver = encoder._charLeftOver;
char* charEnd = chars + charCount;
// For fallback we may need a fallback buffer, we know we aren't default fallback.
- EncoderFallbackBuffer fallbackBuffer = null;
+ EncoderFallbackBuffer? fallbackBuffer = null;
char* charsForFallback;
// We may have a left over character from last time, try and process it.
}
internal override unsafe int GetBytes(char* chars, int charCount,
- byte* bytes, int byteCount, EncoderNLS encoder)
+ byte* bytes, int byteCount, EncoderNLS? encoder)
{
// Just need to ASSERT, this is called by something else internal that checked parameters already
Debug.Assert(bytes != null, "[Latin1Encoding.GetBytes]bytes is null");
// Get any left over characters & check fast or slower fallback type
char charLeftOver = (char)0;
- EncoderReplacementFallback fallback = null;
+ EncoderReplacementFallback? fallback = null;
if (encoder != null)
{
charLeftOver = encoder._charLeftOver;
byte* byteEnd = bytes + byteCount;
// For fallback we may need a fallback buffer, we know we aren't default fallback, create & init it
- EncoderFallbackBuffer fallbackBuffer = null;
+ EncoderFallbackBuffer? fallbackBuffer = null;
char* charsForFallback;
// We may have a left over character from last time, try and process it.
}
// This is internal and called by something else,
- internal override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS decoder)
+ internal override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS? decoder)
{
// Just assert, we're called internally so these should be safe, checked already
Debug.Assert(bytes != null, "[Latin1Encoding.GetCharCount]bytes is null");
}
internal override unsafe int GetChars(byte* bytes, int byteCount,
- char* chars, int charCount, DecoderNLS decoder)
+ char* chars, int charCount, DecoderNLS? decoder)
{
// Just need to ASSERT, this is called by something else internal that checked parameters already
Debug.Assert(bytes != null, "[Latin1Encoding.GetChars]bytes is null");
// 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.Buffers;
using System.Diagnostics;
using System.Globalization;
return bytesWritten;
}
- public override bool Equals(object obj) => (obj is Rune other) && this.Equals(other);
+ public override bool Equals(object? obj) => (obj is Rune other) && this.Equals(other);
public bool Equals(Rune other) => (this == other);
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input);
}
- if ((uint)index >= (uint)input.Length)
+ if ((uint)index >= (uint)input!.Length)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
}
}
else
{
- return (GetUnicodeCategoryNonAscii(value) == UnicodeCategory.DecimalDigitNumber);
+ return GetUnicodeCategoryNonAscii(value) == UnicodeCategory.DecimalDigitNumber;
}
}
}
else
{
- return (GetUnicodeCategoryNonAscii(value) == UnicodeCategory.LowercaseLetter);
+ return GetUnicodeCategoryNonAscii(value) == UnicodeCategory.LowercaseLetter;
}
}
}
else
{
- return (GetUnicodeCategoryNonAscii(value) == UnicodeCategory.UppercaseLetter);
+ return GetUnicodeCategoryNonAscii(value) == UnicodeCategory.UppercaseLetter;
}
}
return ToLowerInvariant(value);
}
- return ChangeCaseCultureAware(value, culture.TextInfo, toUpper: false);
+ return ChangeCaseCultureAware(value, culture!.TextInfo, toUpper: false);
}
public static Rune ToLowerInvariant(Rune value)
return ToUpperInvariant(value);
}
- return ChangeCaseCultureAware(value, culture.TextInfo, toUpper: true);
+ return ChangeCaseCultureAware(value, culture!.TextInfo, toUpper: true);
}
public static Rune ToUpperInvariant(Rune value)
// 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.Text
{
// An enumerator for retrieving System.Text.Rune instances from a ROS<char>.
// 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.Text
private void ShowChunks(int maxChunksToShow = 10)
{
int count = 0;
- StringBuilder head = this, current = this;
+ StringBuilder head = this;
+ StringBuilder? current = this;
+
while (current != null)
{
if (count < maxChunksToShow)
}
else
{
+ Debug.Assert(head.m_ChunkPrevious != null);
head = head.m_ChunkPrevious;
}
current = current.m_ChunkPrevious;
}
+
current = head;
string[] chunks = new string[count];
for (int i = count; i > 0; i--)
{
chunks[i - 1] = new string(current.m_ChunkChars).Replace('\0', '.');
+ Debug.Assert(current.m_ChunkPrevious != null);
current = current.m_ChunkPrevious;
}
+
Debug.WriteLine('|' + string.Join('|', chunks) + '|');
}
}
// 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.Text;
using System.Runtime;
using System.Runtime.Serialization;
/// <summary>
/// The chunk that logically precedes this chunk.
/// </summary>
- internal StringBuilder m_ChunkPrevious;
+ internal StringBuilder? m_ChunkPrevious;
/// <summary>
/// The number of characters in this chunk.
/// Initializes a new instance of the <see cref="StringBuilder"/> class.
/// </summary>
/// <param name="value">The initial contents of this builder.</param>
- public StringBuilder(string value)
+ public StringBuilder(string? value)
: this(value, DefaultCapacity)
{
}
/// </summary>
/// <param name="value">The initial contents of this builder.</param>
/// <param name="capacity">The initial capacity of this builder.</param>
- public StringBuilder(string value, int capacity)
+ public StringBuilder(string? value, int capacity)
: this(value, 0, value?.Length ?? 0, capacity)
{
}
/// <param name="startIndex">The index to start in <paramref name="value"/>.</param>
/// <param name="length">The number of characters to read in <paramref name="value"/>.</param>
/// <param name="capacity">The initial capacity of this builder.</param>
- public StringBuilder(string value, int startIndex, int length, int capacity)
+ public StringBuilder(string? value, int startIndex, int length, int capacity)
{
if (capacity < 0)
{
}
int persistedCapacity = 0;
- string persistedString = null;
+ string? persistedString = null;
int persistedMaxCapacity = int.MaxValue;
bool capacityPresent = false;
Debug.Assert(currentBlock.m_ChunkLength >= 0);
Debug.Assert(currentBlock.m_ChunkOffset >= 0);
- StringBuilder prevBlock = currentBlock.m_ChunkPrevious;
+ StringBuilder? prevBlock = currentBlock.m_ChunkPrevious;
if (prevBlock == null)
{
Debug.Assert(currentBlock.m_ChunkOffset == 0);
}
string result = string.FastAllocateString(Length);
- StringBuilder chunk = this;
+ StringBuilder? chunk = this;
unsafe
{
fixed (char* destinationPtr = result)
{
get
{
- StringBuilder chunk = this;
+ StringBuilder? chunk = this;
for (;;)
{
int indexInBlock = index - chunk.m_ChunkOffset;
}
set
{
- StringBuilder chunk = this;
+ StringBuilder? chunk = this;
for (;;)
{
int indexInBlock = index - chunk.m_ChunkOffset;
public struct ChunkEnumerator
{
private readonly StringBuilder _firstChunk; // The first Stringbuilder chunk (which is the end of the logical string)
- private StringBuilder _currentChunk; // The chunk that this enumerator is currently returning (Current).
- private readonly ManyChunkInfo _manyChunks; // Only used for long string builders with many chunks (see constructor)
+ private StringBuilder? _currentChunk; // The chunk that this enumerator is currently returning (Current).
+ private readonly ManyChunkInfo? _manyChunks; // Only used for long string builders with many chunks (see constructor)
/// <summary>
/// Implement IEnumerable.GetEnumerator() to return 'this' as the IEnumerator
StringBuilder next = _firstChunk;
while (next.m_ChunkPrevious != _currentChunk)
+ {
+ Debug.Assert(next.m_ChunkPrevious != null);
next = next.m_ChunkPrevious;
+ }
_currentChunk = next;
return true;
}
/// <summary>
/// Implements the IEnumerator pattern.
/// </summary>
- public ReadOnlyMemory<char> Current => new ReadOnlyMemory<char>(_currentChunk.m_ChunkChars, 0, _currentChunk.m_ChunkLength);
+ public ReadOnlyMemory<char> Current => new ReadOnlyMemory<char>(_currentChunk!.m_ChunkChars, 0, _currentChunk.m_ChunkLength); // TODO-NULLABLE: NullReferenceException if called before calling MoveNext
#region private
internal ChunkEnumerator(StringBuilder stringBuilder)
_manyChunks = new ManyChunkInfo(stringBuilder, chunkCount);
}
- private static int ChunkCount(StringBuilder stringBuilder)
+ private static int ChunkCount(StringBuilder? stringBuilder)
{
int ret = 0;
while (stringBuilder != null)
private readonly StringBuilder[] _chunks; // These are in normal order (first chunk first)
private int _chunkPos;
- public bool MoveNext(ref StringBuilder current)
+ public bool MoveNext(ref StringBuilder? current)
{
int pos = ++_chunkPos;
if (_chunks.Length <= pos)
return true;
}
- public ManyChunkInfo(StringBuilder stringBuilder, int chunkCount)
+ public ManyChunkInfo(StringBuilder? stringBuilder, int chunkCount)
{
_chunks = new StringBuilder[chunkCount];
while (0 <= --chunkCount)
/// <param name="value">The characters to append.</param>
/// <param name="startIndex">The index to start in <paramref name="value"/>.</param>
/// <param name="charCount">The number of characters to read in <paramref name="value"/>.</param>
- public StringBuilder Append(char[] value, int startIndex, int charCount)
+ public StringBuilder Append(char[]? value, int startIndex, int charCount)
{
if (startIndex < 0)
{
/// Appends a string to the end of this builder.
/// </summary>
/// <param name="value">The string to append.</param>
- public StringBuilder Append(string value)
+ public StringBuilder Append(string? value)
{
if (value != null)
{
/// <param name="value">The string to append.</param>
/// <param name="startIndex">The index to start in <paramref name="value"/>.</param>
/// <param name="count">The number of characters to read in <paramref name="value"/>.</param>
- public StringBuilder Append(string value, int startIndex, int count)
+ public StringBuilder Append(string? value, int startIndex, int count)
{
if (startIndex < 0)
{
}
}
- public StringBuilder Append(StringBuilder value)
+ public StringBuilder Append(StringBuilder? value)
{
if (value != null && value.Length != 0)
{
return this;
}
- public StringBuilder Append(StringBuilder value, int startIndex, int count)
+ public StringBuilder Append(StringBuilder? value, int startIndex, int count)
{
if (startIndex < 0)
{
public StringBuilder AppendLine() => Append(Environment.NewLine);
- public StringBuilder AppendLine(string value)
+ public StringBuilder AppendLine(string? value)
{
Append(value);
return Append(Environment.NewLine);
AssertInvariants();
- StringBuilder chunk = this;
+ StringBuilder? chunk = this;
int sourceEndIndex = sourceIndex + count;
int curDestIndex = count;
while (count > 0)
{
+ Debug.Assert(chunk != null);
int chunkEndIndex = sourceEndIndex - chunk.m_ChunkOffset;
if (chunkEndIndex >= 0)
{
/// <param name="index">The index to insert in this builder.</param>
/// <param name="value">The string to insert.</param>
/// <param name="count">The number of times to insert the string.</param>
- public StringBuilder Insert(int index, string value, int count)
+ public StringBuilder Insert(int index, string? value, int count)
{
if (count < 0)
{
return Append(value.ToString());
}
- internal StringBuilder AppendSpanFormattable<T>(T value, string format, IFormatProvider provider) where T : ISpanFormattable, IFormattable
+ internal StringBuilder AppendSpanFormattable<T>(T value, string? format, IFormatProvider? provider) where T : ISpanFormattable, IFormattable
{
if (value.TryFormat(RemainingCurrentChunk, out int charsWritten, format, provider))
{
return Append(value.ToString(format, provider));
}
- public StringBuilder Append(object value) => (value == null) ? this : Append(value.ToString());
+ public StringBuilder Append(object? value) => (value == null) ? this : Append(value.ToString());
- public StringBuilder Append(char[] value)
+ public StringBuilder Append(char[]? value)
{
if (value?.Length > 0)
{
#region AppendJoin
- public unsafe StringBuilder AppendJoin(string separator, params object[] values)
+ public unsafe StringBuilder AppendJoin(string? separator, params object?[] values)
{
separator = separator ?? string.Empty;
fixed (char* pSeparator = separator)
}
}
- public unsafe StringBuilder AppendJoin<T>(string separator, IEnumerable<T> values)
+ public unsafe StringBuilder AppendJoin<T>(string? separator, IEnumerable<T> values)
{
separator = separator ?? string.Empty;
fixed (char* pSeparator = separator)
}
}
- public unsafe StringBuilder AppendJoin(string separator, params string[] values)
+ public unsafe StringBuilder AppendJoin(string? separator, params string?[] values)
{
separator = separator ?? string.Empty;
fixed (char* pSeparator = separator)
}
}
- public unsafe StringBuilder AppendJoin(char separator, params object[] values)
+ public unsafe StringBuilder AppendJoin(char separator, params object?[] values)
{
return AppendJoinCore(&separator, 1, values);
}
return AppendJoinCore(&separator, 1, values);
}
- public unsafe StringBuilder AppendJoin(char separator, params string[] values)
+ public unsafe StringBuilder AppendJoin(char separator, params string?[] values)
{
return AppendJoinCore(&separator, 1, values);
}
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.values);
}
+ Debug.Assert(values != null);
using (IEnumerator<T> en = values.GetEnumerator())
{
if (!en.MoveNext())
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.values);
}
+ Debug.Assert(values != null);
if (values.Length == 0)
{
return this;
if (values[0] != null)
{
- Append(values[0].ToString());
+ Append(values[0]!.ToString()); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/34644
}
for (int i = 1; i < values.Length; i++)
Append(separator, separatorLength);
if (values[i] != null)
{
- Append(values[i].ToString());
+ Append(values[i]!.ToString()); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/34644
}
}
return this;
#endregion
- public StringBuilder Insert(int index, string value)
+ public StringBuilder Insert(int index, string? value)
{
if ((uint)index > (uint)Length)
{
return this;
}
- public StringBuilder Insert(int index, char[] value)
+ public StringBuilder Insert(int index, char[]? value)
{
if ((uint)index > (uint)Length)
{
return this;
}
- public StringBuilder Insert(int index, char[] value, int startIndex, int charCount)
+ public StringBuilder Insert(int index, char[]? value, int startIndex, int charCount)
{
int currentLength = Length;
if ((uint)index > (uint)currentLength)
[CLSCompliant(false)]
public StringBuilder Insert(int index, ulong value) => Insert(index, value.ToString(), 1);
- public StringBuilder Insert(int index, object value) => (value == null) ? this : Insert(index, value.ToString(), 1);
+ public StringBuilder Insert(int index, object? value) => (value == null) ? this : Insert(index, value.ToString(), 1);
public StringBuilder Insert(int index, ReadOnlySpan<char> value)
{
return this;
}
- public StringBuilder AppendFormat(string format, object arg0) => AppendFormatHelper(null, format, new ParamsArray(arg0));
+ public StringBuilder AppendFormat(string format, object? arg0) => AppendFormatHelper(null, format, new ParamsArray(arg0));
- public StringBuilder AppendFormat(string format, object arg0, object arg1) => AppendFormatHelper(null, format, new ParamsArray(arg0, arg1));
+ public StringBuilder AppendFormat(string format, object? arg0, object? arg1) => AppendFormatHelper(null, format, new ParamsArray(arg0, arg1));
- public StringBuilder AppendFormat(string format, object arg0, object arg1, object arg2) => AppendFormatHelper(null, format, new ParamsArray(arg0, arg1, arg2));
+ public StringBuilder AppendFormat(string format, object? arg0, object? arg1, object? arg2) => AppendFormatHelper(null, format, new ParamsArray(arg0, arg1, arg2));
- public StringBuilder AppendFormat(string format, params object[] args)
+ public StringBuilder AppendFormat(string format, params object?[] args)
{
if (args == null)
{
return AppendFormatHelper(null, format, new ParamsArray(args));
}
- public StringBuilder AppendFormat(IFormatProvider provider, string format, object arg0) => AppendFormatHelper(provider, format, new ParamsArray(arg0));
+ public StringBuilder AppendFormat(IFormatProvider provider, string format, object? arg0) => AppendFormatHelper(provider, format, new ParamsArray(arg0));
- public StringBuilder AppendFormat(IFormatProvider provider, string format, object arg0, object arg1) => AppendFormatHelper(provider, format, new ParamsArray(arg0, arg1));
+ public StringBuilder AppendFormat(IFormatProvider provider, string format, object? arg0, object? arg1) => AppendFormatHelper(provider, format, new ParamsArray(arg0, arg1));
- public StringBuilder AppendFormat(IFormatProvider provider, string format, object arg0, object arg1, object arg2) => AppendFormatHelper(provider, format, new ParamsArray(arg0, arg1, arg2));
+ public StringBuilder AppendFormat(IFormatProvider provider, string format, object? arg0, object? arg1, object? arg2) => AppendFormatHelper(provider, format, new ParamsArray(arg0, arg1, arg2));
- public StringBuilder AppendFormat(IFormatProvider provider, string format, params object[] args)
+ public StringBuilder AppendFormat(IFormatProvider provider, string format, params object?[] args)
{
if (args == null)
{
private const int IndexLimit = 1000000; // Note: 0 <= ArgIndex < IndexLimit
private const int WidthLimit = 1000000; // Note: -WidthLimit < ArgAlign < WidthLimit
- internal StringBuilder AppendFormatHelper(IFormatProvider provider, string format, ParamsArray args)
+ internal StringBuilder AppendFormatHelper(IFormatProvider? provider, string format, ParamsArray args)
{
if (format == null)
{
int pos = 0;
int len = format.Length;
char ch = '\x0';
- StringBuilder unescapedItemFormat = null;
+ StringBuilder? unescapedItemFormat = null;
- ICustomFormatter cf = null;
+ ICustomFormatter? cf = null;
if (provider != null)
{
cf = (ICustomFormatter)provider.GetFormat(typeof(ICustomFormatter));
// Start of parsing of optional formatting parameter.
//
object arg = args[index];
- string itemFormat = null;
+ string? itemFormat = null;
ReadOnlySpan<char> itemFormatSpan = default; // used if itemFormat is null
// Is current character a colon? which indicates start of formatting parameter.
if (ch == ':')
if (ch != '}') FormatError();
// Construct the output for this arg hole.
pos++;
- string s = null;
+ string? s = null;
if (cf != null)
{
if (itemFormatSpan.Length != 0 && itemFormat == null)
/// If <paramref name="newValue"/> is <c>null</c>, instances of <paramref name="oldValue"/>
/// are removed from this builder.
/// </remarks>
- public StringBuilder Replace(string oldValue, string newValue) => Replace(oldValue, newValue, 0, Length);
+ public StringBuilder Replace(string oldValue, string? newValue) => Replace(oldValue, newValue, 0, Length);
/// <summary>
/// Determines if the contents of this builder are equal to the contents of another builder.
/// </summary>
/// <param name="sb">The other builder.</param>
- public bool Equals(StringBuilder sb)
+ public bool Equals(StringBuilder? sb)
{
if (sb == null)
return false;
if (sb == this)
return true;
- StringBuilder thisChunk = this;
+ StringBuilder? thisChunk = this;
int thisChunkIndex = thisChunk.m_ChunkLength;
- StringBuilder sbChunk = sb;
+ StringBuilder? sbChunk = sb;
int sbChunkIndex = sbChunk.m_ChunkLength;
for (;;)
{
return sbChunkIndex < 0;
if (sbChunkIndex < 0)
return false;
+
+ Debug.Assert(thisChunk != null && sbChunk != null);
if (thisChunk.m_ChunkChars[thisChunkIndex] != sbChunk.m_ChunkChars[sbChunkIndex])
return false;
}
if (!chunk.EqualsOrdinal(span.Slice(span.Length - offset, chunk_length)))
return false;
+ Debug.Assert(sbChunk.m_ChunkPrevious != null);
sbChunk = sbChunk.m_ChunkPrevious;
} while (sbChunk != null);
/// If <paramref name="newValue"/> is <c>null</c>, instances of <paramref name="oldValue"/>
/// are removed from this builder.
/// </remarks>
- public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count)
+ public StringBuilder Replace(string oldValue, string? newValue, int startIndex, int count)
{
int currentLength = Length;
if ((uint)startIndex > (uint)currentLength)
int deltaLength = newValue.Length - oldValue.Length;
- int[] replacements = null; // A list of replacement positions in a chunk to apply
+ int[]? replacements = null; // A list of replacement positions in a chunk to apply
int replacementsCount = 0;
// Find the chunk, indexInChunk for the starting point
int indexInChunk = startIndex - chunk.m_ChunkOffset;
while (count > 0)
{
+ Debug.Assert(chunk != null);
// Look for a match in the chunk,indexInChunk pointer
if (StartsWith(chunk, indexInChunk, count, oldValue))
{
int indexBeforeAdjustment = index;
// See if we accumulated any replacements, if so apply them.
+ Debug.Assert(replacements != null);
ReplaceAllInChunk(replacements, replacementsCount, chunk, oldValue.Length, newValue);
// The replacement has affected the logical index. Adjust it.
index += ((newValue.Length - oldValue.Length) * replacementsCount);
}
if (startIndexInChunk >= 0)
break;
+
+ Debug.Assert(chunk.m_ChunkPrevious != null);
chunk = chunk.m_ChunkPrevious;
}
if (indexInChunk >= chunk.m_ChunkLength)
{
- chunk = Next(chunk);
+ chunk = Next(chunk)!;
if (chunk == null)
return false;
indexInChunk = 0;
indexInChunk += lengthToCopy;
if (indexInChunk >= chunk.m_ChunkLength)
{
- chunk = Next(chunk);
+ StringBuilder? nextChunk = Next(chunk);
+ Debug.Assert(nextChunk != null, "we should never get past last chunk because range should already be validated before calling");
+ chunk = nextChunk;
indexInChunk = 0;
}
count -= lengthToCopy;
StringBuilder result = this;
while (result.m_ChunkOffset > index)
{
+ Debug.Assert(result.m_ChunkPrevious != null);
result = result.m_ChunkPrevious;
}
StringBuilder result = this;
while (result.m_ChunkOffset * sizeof(char) > byteIndex)
{
+ Debug.Assert(result.m_ChunkPrevious != null);
result = result.m_ChunkPrevious;
}
/// way down until it finds the specified chunk (which is O(n)). Thus, it is more expensive than
/// a field fetch.
/// </remarks>
- private StringBuilder Next(StringBuilder chunk) => chunk == this ? null : FindChunkForIndex(chunk.m_ChunkOffset + chunk.m_ChunkLength);
+ private StringBuilder? Next(StringBuilder chunk) => chunk == this ? null : FindChunkForIndex(chunk.m_ChunkOffset + chunk.m_ChunkLength);
/// <summary>
/// Transfers the character buffer from this chunk to a new chunk, and allocates a new buffer with a minimum size for this chunk.
while (chunk.m_ChunkOffset > index)
{
chunk.m_ChunkOffset += count;
+ Debug.Assert(chunk.m_ChunkPrevious != null);
chunk = chunk.m_ChunkPrevious;
}
indexInChunk = index - chunk.m_ChunkOffset;
/// <param name="size">The size of the character buffer for this chunk.</param>
/// <param name="maxCapacity">The maximum capacity, to be stored in this chunk.</param>
/// <param name="previousBlock">The predecessor of this chunk.</param>
- private StringBuilder(int size, int maxCapacity, StringBuilder previousBlock)
+ private StringBuilder(int size, int maxCapacity, StringBuilder? previousBlock)
{
Debug.Assert(size > 0);
Debug.Assert(maxCapacity > 0);
// Find the chunks for the start and end of the block to delete.
chunk = this;
- StringBuilder endChunk = null;
+ StringBuilder? endChunk = null;
int endIndexInChunk = 0;
for (;;)
{
{
chunk.m_ChunkOffset -= count;
}
+
+ Debug.Assert(chunk.m_ChunkPrevious != null);
chunk = chunk.m_ChunkPrevious;
}
Debug.Assert(chunk != null, "We fell off the beginning of the string!");
// 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.Text
{
/// <summary>Provide a cached reusable instance of stringbuilder per thread.</summary>
// Please do not change the type, the name, or the semantic usage of this member without understanding the implication for tools.
// Get in touch with the diagnostics team if you have questions.
[ThreadStatic]
- private static StringBuilder t_cachedInstance;
+ private static StringBuilder? t_cachedInstance;
/// <summary>Get a StringBuilder for the specified capacity.</summary>
/// <remarks>If a StringBuilder of an appropriate size is cached, it will be returned and the cache emptied.</remarks>
{
if (capacity <= MaxBuilderSize)
{
- StringBuilder sb = t_cachedInstance;
+ StringBuilder? sb = t_cachedInstance;
if (sb != null)
{
// Avoid stringbuilder block fragmentation by getting a new StringBuilder
}
}
}
+
return new StringBuilder(capacity);
}
// 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.Collections;
using System.Collections.Generic;
// 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
//
// Don't override IsAlwaysNormalized because it is just a Unicode Transformation and could be confused.
//
public override unsafe int GetByteCount(string s)
{
// Validate input
- if (s==null)
+ if (s == null)
throw new ArgumentNullException(nameof(s));
fixed (char* pChars = s)
//
// End of standard methods copied from EncodingNLS.cs
//
-
- internal override unsafe int GetByteCount(char* chars, int count, EncoderNLS encoder)
+ internal override unsafe int GetByteCount(char* chars, int count, EncoderNLS? encoder)
{
Debug.Assert(chars != null, "[UTF32Encoding.GetByteCount]chars!=null");
Debug.Assert(count >= 0, "[UTF32Encoding.GetByteCount]count >=0");
char highSurrogate = '\0';
// For fallback we may need a fallback buffer
- EncoderFallbackBuffer fallbackBuffer = null;
+ EncoderFallbackBuffer? fallbackBuffer = null;
char* charsForFallback;
if (encoder != null)
// We mustn't have left over fallback data when counting
if (fallbackBuffer.Remaining > 0)
- throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType()));
+ throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback?.GetType().ToString() ?? string.Empty));
}
else
{
}
internal override unsafe int GetBytes(char* chars, int charCount,
- byte* bytes, int byteCount, EncoderNLS encoder)
+ byte* bytes, int byteCount, EncoderNLS? encoder)
{
Debug.Assert(chars != null, "[UTF32Encoding.GetBytes]chars!=null");
Debug.Assert(bytes != null, "[UTF32Encoding.GetBytes]bytes!=null");
char highSurrogate = '\0';
// For fallback we may need a fallback buffer
- EncoderFallbackBuffer fallbackBuffer = null;
+ EncoderFallbackBuffer? fallbackBuffer = null;
char* charsForFallback;
if (encoder != null)
// We mustn't have left over fallback data when not converting
if (encoder._throwOnOverflow && fallbackBuffer.Remaining > 0)
- throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType()));
+ throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback!.GetType())); // TODO-NULLABLE: NullReferenceException
}
else
{
return (int)(bytes - byteStart);
}
- internal override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder)
+ internal override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS? baseDecoder)
{
Debug.Assert(bytes != null, "[UTF32Encoding.GetCharCount]bytes!=null");
Debug.Assert(count >= 0, "[UTF32Encoding.GetCharCount]count >=0");
- UTF32Decoder decoder = (UTF32Decoder)baseDecoder;
+ UTF32Decoder? decoder = (UTF32Decoder?)baseDecoder;
// None so far!
int charCount = 0;
uint iChar = 0;
// For fallback we may need a fallback buffer
- DecoderFallbackBuffer fallbackBuffer = null;
+ DecoderFallbackBuffer? fallbackBuffer = null;
// See if there's anything in our decoder
if (decoder != null)
}
internal override unsafe int GetChars(byte* bytes, int byteCount,
- char* chars, int charCount, DecoderNLS baseDecoder)
+ char* chars, int charCount, DecoderNLS? baseDecoder)
{
Debug.Assert(chars != null, "[UTF32Encoding.GetChars]chars!=null");
Debug.Assert(bytes != null, "[UTF32Encoding.GetChars]bytes!=null");
Debug.Assert(byteCount >= 0, "[UTF32Encoding.GetChars]byteCount >=0");
Debug.Assert(charCount >= 0, "[UTF32Encoding.GetChars]charCount >=0");
- UTF32Decoder decoder = (UTF32Decoder)baseDecoder;
+ UTF32Decoder? decoder = (UTF32Decoder?)baseDecoder;
// None so far!
char* charStart = chars;
uint iChar = 0;
// For fallback we may need a fallback buffer
- DecoderFallbackBuffer fallbackBuffer = null;
+ DecoderFallbackBuffer? fallbackBuffer = null;
char* charsForFallback;
// See if there's anything in our decoder
{
readCount = decoder.readByteCount;
iChar = (uint)decoder.iChar;
+ Debug.Assert(baseDecoder != null);
fallbackBuffer = baseDecoder.FallbackBuffer;
// Shouldn't have anything in fallback buffer for GetChars
_bigEndian ? (ReadOnlySpan<byte>)new byte[4] { 0x00, 0x00, 0xFE, 0xFF } : // uses C# compiler's optimization for static byte[] data
(ReadOnlySpan<byte>)new byte[4] { 0xFF, 0xFE, 0x00, 0x00 };
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is UTF32Encoding that)
{
(EncoderFallback.Equals(that.EncoderFallback)) &&
(DecoderFallback.Equals(that.DecoderFallback));
}
- return (false);
+
+ return false;
}
// 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
//
// Don't override IsAlwaysNormalized because it is just a Unicode Transformation and could be confused.
//
internal static readonly UTF7Encoding s_default = new UTF7Encoding();
// The set of base 64 characters.
- private byte[] _base64Bytes;
+ private byte[] _base64Bytes = null!;
// The decoded bits for every base64 values. This array has a size of 128 elements.
// The index is the code point value of the base 64 characters. The value is -1 if
// the code point is not a valid base 64 character. Otherwise, the value is a value
// from 0 ~ 63.
- private sbyte[] _base64Values;
+ private sbyte[] _base64Values = null!;
// The array to decide if a Unicode code point below 0x80 can be directly encoded in UTF7.
// This array has a size of 128.
- private bool[] _directEncode;
+ private bool[] _directEncode = null!;
private bool _allowOptionals;
this.decoderFallback = new DecoderUTF7Fallback();
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is UTF7Encoding that)
{
(EncoderFallback.Equals(that.EncoderFallback)) &&
(DecoderFallback.Equals(that.DecoderFallback));
}
- return (false);
+ return false;
}
// Compared to all the other encodings, variations of UTF7 are unlikely
public override unsafe int GetByteCount(string s)
{
// Validate input
- if (s==null)
+ if (s == null)
throw new ArgumentNullException(nameof(s));
fixed (char* pChars = s)
//
// End of standard methods copied from EncodingNLS.cs
//
-
- internal sealed override unsafe int GetByteCount(char* chars, int count, EncoderNLS baseEncoder)
+ internal sealed override unsafe int GetByteCount(char* chars, int count, EncoderNLS? baseEncoder)
{
Debug.Assert(chars != null, "[UTF7Encoding.GetByteCount]chars!=null");
Debug.Assert(count >= 0, "[UTF7Encoding.GetByteCount]count >=0");
}
internal sealed override unsafe int GetBytes(
- char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS baseEncoder)
+ char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS? baseEncoder)
{
Debug.Assert(byteCount >= 0, "[UTF7Encoding.GetBytes]byteCount >=0");
Debug.Assert(chars != null, "[UTF7Encoding.GetBytes]chars!=null");
Debug.Assert(charCount >= 0, "[UTF7Encoding.GetBytes]charCount >=0");
// Get encoder info
- UTF7Encoding.Encoder encoder = (UTF7Encoding.Encoder)baseEncoder;
+ UTF7Encoding.Encoder? encoder = (UTF7Encoding.Encoder?)baseEncoder;
// Default bits & count
int bits = 0;
return buffer.Count;
}
- internal sealed override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder)
+ internal sealed override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS? baseDecoder)
{
Debug.Assert(count >= 0, "[UTF7Encoding.GetCharCount]count >=0");
Debug.Assert(bytes != null, "[UTF7Encoding.GetCharCount]bytes!=null");
}
internal sealed override unsafe int GetChars(
- byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS baseDecoder)
+ byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS? baseDecoder)
{
Debug.Assert(byteCount >= 0, "[UTF7Encoding.GetChars]byteCount >=0");
Debug.Assert(bytes != null, "[UTF7Encoding.GetChars]bytes!=null");
Debug.Assert(charCount >= 0, "[UTF7Encoding.GetChars]charCount >=0");
// Might use a decoder
- UTF7Encoding.Decoder decoder = (UTF7Encoding.Decoder)baseDecoder;
+ UTF7Encoding.Decoder? decoder = (UTF7Encoding.Decoder?)baseDecoder;
// Get our output buffer info.
Encoding.EncodingCharBuffer buffer = new Encoding.EncodingCharBuffer(
{
get
{
- return (this.bits != 0 || this.bitCount != -1);
+ return this.bits != 0 || this.bitCount != -1;
}
}
}
}
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
- DecoderUTF7Fallback that = value as DecoderUTF7Fallback;
+ DecoderUTF7Fallback? that = value as DecoderUTF7Fallback;
if (that != null)
{
return true;
}
- return (false);
+ return false;
}
public override int GetHashCode()
}
// return true if we were allowed to do this
- return (iCount >= 0 && iCount <= iSize);
+ return iCount >= 0 && iCount <= iSize;
}
// Return # of chars left in this fallback
// 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
// The worker functions in this file was optimized for performance. If you make changes
// you should use care to consider all of the interesting cases.
public override unsafe int GetByteCount(string chars)
{
// Validate input
- if (chars==null)
- throw new ArgumentNullException("s");
+ if (chars == null)
+ throw new ArgumentNullException(nameof(chars));
fixed (char* pChars = chars)
return GetByteCount(pChars, chars.Length, null);
// To simplify maintenance, the structure of GetByteCount and GetBytes should be
// kept the same as much as possible
- internal sealed override unsafe int GetByteCount(char* chars, int count, EncoderNLS baseEncoder)
+ internal sealed override unsafe int GetByteCount(char* chars, int count, EncoderNLS? baseEncoder)
{
// For fallback we may need a fallback buffer.
// We wait to initialize it though in case we don't have any broken input unicode
- EncoderFallbackBuffer fallbackBuffer = null;
+ EncoderFallbackBuffer? fallbackBuffer = null;
char* pSrcForFallback;
char* pSrc = chars;
{
fallbackBuffer = encoder.FallbackBuffer;
if (fallbackBuffer.Remaining > 0)
- throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType()));
+ throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback!.GetType())); // TODO-NULLABLE: NullReferenceException
// Set our internal fallback interesting things.
fallbackBuffer.InternalInitialize(chars, pEnd, encoder, false);
// Our workhorse
// Note: We ignore mismatched surrogates, unless the exception flag is set in which case we throw
internal sealed override unsafe int GetBytes(
- char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS baseEncoder)
+ char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS? baseEncoder)
{
Debug.Assert(chars != null, "[UTF8Encoding.GetBytes]chars!=null");
Debug.Assert(byteCount >= 0, "[UTF8Encoding.GetBytes]byteCount >=0");
Debug.Assert(charCount >= 0, "[UTF8Encoding.GetBytes]charCount >=0");
Debug.Assert(bytes != null, "[UTF8Encoding.GetBytes]bytes!=null");
- UTF8Encoder encoder = null;
+ UTF8Encoder? encoder = null;
// For fallback we may need a fallback buffer.
// We wait to initialize it though in case we don't have any broken input unicode
- EncoderFallbackBuffer fallbackBuffer = null;
+ EncoderFallbackBuffer? fallbackBuffer = null;
char* pSrcForFallback;
char* pSrc = chars;
// We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary
fallbackBuffer = encoder.FallbackBuffer;
if (fallbackBuffer.Remaining > 0 && encoder._throwOnOverflow)
- throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType()));
+ throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback!.GetType())); // TODO-NULLABLE: NullReferenceException
// Set our internal fallback interesting things.
fallbackBuffer.InternalInitialize(chars, pEnd, encoder, true);
//
// To simplify maintenance, the structure of GetCharCount and GetChars should be
// kept the same as much as possible
- internal sealed override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder)
+ internal sealed override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS? baseDecoder)
{
Debug.Assert(count >= 0, "[UTF8Encoding.GetCharCount]count >=0");
Debug.Assert(bytes != null, "[UTF8Encoding.GetCharCount]bytes!=null");
// for the character being decoded
int charCount = count;
int ch = 0;
- DecoderFallbackBuffer fallback = null;
+ DecoderFallbackBuffer? fallback = null;
if (baseDecoder != null)
{
// To simplify maintenance, the structure of GetCharCount and GetChars should be
// kept the same as much as possible
internal sealed override unsafe int GetChars(
- byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS baseDecoder)
+ byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS? baseDecoder)
{
Debug.Assert(chars != null, "[UTF8Encoding.GetChars]chars!=null");
Debug.Assert(byteCount >= 0, "[UTF8Encoding.GetChars]count >=0");
int ch = 0;
- DecoderFallbackBuffer fallback = null;
+ DecoderFallbackBuffer? fallback = null;
byte* pSrcForFallback;
char* pTargetForFallback;
if (baseDecoder != null)
private unsafe byte[] GetBytesUnknown(ref byte* pSrc, int ch)
{
// Get our byte[]
- byte[] bytesUnknown = null;
+ byte[] bytesUnknown;
// See if it was a plain char
// (have to check >= 0 because we have all sorts of wierd bit flags)
_emitUTF8Identifier ? PreambleSpan :
default;
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is UTF8Encoding that)
{
(EncoderFallback.Equals(that.EncoderFallback)) &&
(DecoderFallback.Equals(that.DecoderFallback));
}
- return (false);
+ return false;
}
{
get
{
- return (this.surrogateChar != 0);
+ return this.surrogateChar != 0;
}
}
}
{
get
{
- return (this.bits != 0);
+ return this.bits != 0;
}
}
}
// 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.Buffers;
using System.Diagnostics;
// 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.Buffers;
using System.Diagnostics;
using System.IO;
/// <paramref name="value"/> but where all invalid UTF-8 sequences have been replaced
/// with U+FFD.
/// </summary>
- public static Utf8String ValidateAndFixupUtf8String(Utf8String value)
+ public static Utf8String? ValidateAndFixupUtf8String(Utf8String? value)
{
if (Utf8String.IsNullOrEmpty(value))
{
// 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;
using System.Runtime.InteropServices;
// 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
//
// Don't override IsAlwaysNormalized because it is just a Unicode Transformation and could be confused.
//
public override unsafe int GetByteCount(string s)
{
// Validate input
- if (s==null)
+ if (s == null)
throw new ArgumentNullException(nameof(s));
fixed (char* pChars = s)
byte[] bytes, int byteIndex)
{
if (s == null || bytes == null)
- throw new ArgumentNullException((s == null ? nameof(s) : nameof(bytes)), SR.ArgumentNull_Array);
+ throw new ArgumentNullException(s == null ? nameof(s) : nameof(bytes), SR.ArgumentNull_Array);
if (charIndex < 0 || charCount < 0)
- throw new ArgumentOutOfRangeException((charIndex < 0 ? nameof(charIndex) : nameof(charCount)), SR.ArgumentOutOfRange_NeedNonNegNum);
+ throw new ArgumentOutOfRangeException(charIndex < 0 ? nameof(charIndex) : nameof(charCount), SR.ArgumentOutOfRange_NeedNonNegNum);
if (s.Length - charIndex < charCount)
throw new ArgumentOutOfRangeException(nameof(s), SR.ArgumentOutOfRange_IndexCount);
//
// End of standard methods copied from EncodingNLS.cs
//
-
- internal sealed override unsafe int GetByteCount(char* chars, int count, EncoderNLS encoder)
+ internal sealed override unsafe int GetByteCount(char* chars, int count, EncoderNLS? encoder)
{
Debug.Assert(chars != null, "[UnicodeEncoding.GetByteCount]chars!=null");
Debug.Assert(count >= 0, "[UnicodeEncoding.GetByteCount]count >=0");
bool wasHereBefore = false;
// For fallback we may need a fallback buffer
- EncoderFallbackBuffer fallbackBuffer = null;
+ EncoderFallbackBuffer? fallbackBuffer = null;
char* charsForFallback;
if (encoder != null)
{
fallbackBuffer = encoder.FallbackBuffer;
if (fallbackBuffer.Remaining > 0)
- throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType()));
+ throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback!.GetType())); // TODO-NULLABLE: NullReferenceException
// Set our internal fallback interesting things.
fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, false);
}
internal sealed override unsafe int GetBytes(
- char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS encoder)
+ char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS? encoder)
{
Debug.Assert(chars != null, "[UnicodeEncoding.GetBytes]chars!=null");
Debug.Assert(byteCount >= 0, "[UnicodeEncoding.GetBytes]byteCount >=0");
char* charStart = chars;
// For fallback we may need a fallback buffer
- EncoderFallbackBuffer fallbackBuffer = null;
+ EncoderFallbackBuffer? fallbackBuffer = null;
char* charsForFallback;
// Get our encoder, but don't clear it yet.
// We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary
fallbackBuffer = encoder.FallbackBuffer;
if (fallbackBuffer.Remaining > 0 && encoder._throwOnOverflow)
- throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType()));
+ throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback!.GetType())); // TODO-NULLABLE: NullReferenceException
// Set our internal fallback interesting things.
fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, false);
return (int)(bytes - byteStart);
}
- internal sealed override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder)
+ internal sealed override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS? baseDecoder)
{
Debug.Assert(bytes != null, "[UnicodeEncoding.GetCharCount]bytes!=null");
Debug.Assert(count >= 0, "[UnicodeEncoding.GetCharCount]count >=0");
- UnicodeEncoding.Decoder decoder = (UnicodeEncoding.Decoder)baseDecoder;
+ UnicodeEncoding.Decoder? decoder = (UnicodeEncoding.Decoder?)baseDecoder;
byte* byteEnd = bytes + count;
byte* byteStart = bytes;
int charCount = count >> 1;
// For fallback we may need a fallback buffer
- DecoderFallbackBuffer fallbackBuffer = null;
+ DecoderFallbackBuffer? fallbackBuffer = null;
if (decoder != null)
{
// Get fallback for previous high surrogate
// Note we have to reconstruct bytes because some may have been in decoder
- byte[] byteBuffer = null;
+ byte[]? byteBuffer = null;
if (bigEndian)
{
byteBuffer = new byte[]
// Get fallback for this low surrogate
// Note we have to reconstruct bytes because some may have been in decoder
- byte[] byteBuffer = null;
+ byte[]? byteBuffer = null;
if (bigEndian)
{
byteBuffer = new byte[]
charCount--;
// fall back the high surrogate.
- byte[] byteBuffer = null;
+ byte[]? byteBuffer = null;
if (bigEndian)
{
byteBuffer = new byte[]
{
// No hanging high surrogates allowed, do fallback and remove count for it
charCount--;
- byte[] byteBuffer = null;
+ byte[]? byteBuffer = null;
if (bigEndian)
{
byteBuffer = new byte[]
}
internal sealed override unsafe int GetChars(
- byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS baseDecoder)
+ byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS? baseDecoder)
{
Debug.Assert(chars != null, "[UnicodeEncoding.GetChars]chars!=null");
Debug.Assert(byteCount >= 0, "[UnicodeEncoding.GetChars]byteCount >=0");
Debug.Assert(charCount >= 0, "[UnicodeEncoding.GetChars]charCount >=0");
Debug.Assert(bytes != null, "[UnicodeEncoding.GetChars]bytes!=null");
- UnicodeEncoding.Decoder decoder = (UnicodeEncoding.Decoder)baseDecoder;
+ UnicodeEncoding.Decoder? decoder = (UnicodeEncoding.Decoder?)baseDecoder;
// Need last vars
int lastByte = -1;
}
// For fallback we may need a fallback buffer
- DecoderFallbackBuffer fallbackBuffer = null;
+ DecoderFallbackBuffer? fallbackBuffer = null;
char* charsForFallback;
byte* byteEnd = bytes + byteCount;
{
// Get fallback for previous high surrogate
// Note we have to reconstruct bytes because some may have been in decoder
- byte[] byteBuffer = null;
+ byte[]? byteBuffer = null;
if (bigEndian)
{
byteBuffer = new byte[]
// Expected a previous high surrogate
// Get fallback for this low surrogate
// Note we have to reconstruct bytes because some may have been in decoder
- byte[] byteBuffer = null;
+ byte[]? byteBuffer = null;
if (bigEndian)
{
byteBuffer = new byte[]
else if (lastChar > 0)
{
// Had a high surrogate, expected a low surrogate, fall back the high surrogate.
- byte[] byteBuffer = null;
+ byte[]? byteBuffer = null;
if (bigEndian)
{
byteBuffer = new byte[]
if (lastChar > 0)
{
// No hanging high surrogates allowed, do fallback and remove count for it
- byte[] byteBuffer = null;
+ byte[]? byteBuffer = null;
if (bigEndian)
{
byteBuffer = new byte[]
}
- public override bool Equals(object value)
+ public override bool Equals(object? value)
{
if (value is UnicodeEncoding that)
{
// 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.Text
// 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;
using System.Diagnostics;
// 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.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
{
internal ref partial struct ValueStringBuilder
{
- private char[] _arrayToReturnToPool;
+ private char[]? _arrayToReturnToPool;
private Span<char> _chars;
private int _pos;
_chars.CopyTo(poolArray);
- char[] toReturn = _arrayToReturnToPool;
+ char[]? toReturn = _arrayToReturnToPool;
_chars = _arrayToReturnToPool = poolArray;
if (toReturn != null)
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
- char[] toReturn = _arrayToReturnToPool;
+ char[]? toReturn = _arrayToReturnToPool;
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
if (toReturn != null)
{