| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Buffers.Text; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | |
| | | 11 | | namespace System.Text |
| | | 12 | | { |
| | | 13 | | // ASCIIEncoding |
| | | 14 | | // |
| | | 15 | | // Note that ASCIIEncoding is optimized with no best fit and ? for fallback. |
| | | 16 | | // It doesn't come in other flavors. |
| | | 17 | | // |
| | | 18 | | // Note: ASCIIEncoding is the only encoding that doesn't do best fit (windows has best fit). |
| | | 19 | | // |
| | | 20 | | // Note: IsAlwaysNormalized remains false because 1/2 the code points are unassigned, so they'd |
| | | 21 | | // use fallbacks, and we cannot guarantee that fallbacks are normalized. |
| | | 22 | | |
| | | 23 | | public partial class ASCIIEncoding : Encoding |
| | | 24 | | { |
| | | 25 | | // This specialized sealed type has two benefits: |
| | | 26 | | // 1) it allows for devirtualization (see https://github.com/dotnet/coreclr/pull/9230), and |
| | | 27 | | // 2) it allows us to provide highly optimized implementations of certain routines because |
| | | 28 | | // we can make assumptions about the fallback mechanisms in use (in particular, always |
| | | 29 | | // replace with "?"). |
| | | 30 | | // |
| | | 31 | | // (We don't take advantage of #2 yet, but we can do so in the future because the implementation |
| | | 32 | | // of cloning below allows us to make assumptions about the behaviors of the sealed type.) |
| | | 33 | | internal sealed class ASCIIEncodingSealed : ASCIIEncoding |
| | | 34 | | { |
| | | 35 | | public override object Clone() |
| | | 36 | | { |
| | | 37 | | // The base implementation of Encoding.Clone calls object.MemberwiseClone and marks the new object mutab |
| | | 38 | | // We don't want to do this because it violates the invariants we have set for the sealed type. |
| | | 39 | | // Instead, we'll create a new instance of the base ASCIIEncoding type and mark it mutable. |
| | | 40 | | |
| | 0 | 41 | | return new ASCIIEncoding() |
| | 0 | 42 | | { |
| | 0 | 43 | | IsReadOnly = false |
| | 0 | 44 | | }; |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // Used by Encoding.ASCII for lazy initialization |
| | | 49 | | // The initialization code will not be run until a static member of the class is referenced |
| | 0 | 50 | | internal static readonly ASCIIEncodingSealed s_default = new ASCIIEncodingSealed(); |
| | | 51 | | |
| | 0 | 52 | | public ASCIIEncoding() : base(CodePageASCII) |
| | | 53 | | { |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | internal sealed override void SetDefaultFallbacks() |
| | | 57 | | { |
| | | 58 | | // For ASCIIEncoding we just use default replacement fallback |
| | 0 | 59 | | this.encoderFallback = EncoderFallback.ReplacementFallback; |
| | 0 | 60 | | this.decoderFallback = DecoderFallback.ReplacementFallback; |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | // WARNING: GetByteCount(string chars), GetBytes(string chars,...), and GetString(byte[] byteIndex...) |
| | | 64 | | // WARNING: have different variable names than EncodingNLS.cs, so this can't just be cut & pasted, |
| | | 65 | | // WARNING: or it'll break VB's way of calling these. |
| | | 66 | | // |
| | | 67 | | // The following methods are copied from EncodingNLS.cs. |
| | | 68 | | // Unfortunately EncodingNLS.cs is internal and we're public, so we have to re-implement them here. |
| | | 69 | | // These should be kept in sync for the following classes: |
| | | 70 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 71 | | |
| | | 72 | | // Returns the number of bytes required to encode a range of characters in |
| | | 73 | | // a character array. |
| | | 74 | | // |
| | | 75 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 76 | | // So if you fix this, fix the others. Currently those include: |
| | | 77 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 78 | | // parent method is safe |
| | | 79 | | |
| | | 80 | | public override unsafe int GetByteCount(char[] chars, int index, int count) |
| | | 81 | | { |
| | 0 | 82 | | if (chars is null) |
| | | 83 | | { |
| | 0 | 84 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chars, ExceptionResource.ArgumentNull_Array); |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | if ((index | count) < 0) |
| | | 88 | | { |
| | 0 | 89 | | ThrowHelper.ThrowArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.c |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | if (chars.Length - index < count) |
| | | 93 | | { |
| | 0 | 94 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRan |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | fixed (char* pChars = chars) |
| | | 98 | | { |
| | 0 | 99 | | return GetByteCountCommon(pChars + index, count); |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 104 | | // So if you fix this, fix the others. Currently those include: |
| | | 105 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 106 | | // parent method is safe |
| | | 107 | | |
| | | 108 | | public override unsafe int GetByteCount(string chars) |
| | | 109 | | { |
| | 0 | 110 | | if (chars is null) |
| | | 111 | | { |
| | 0 | 112 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chars); |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | fixed (char* pChars = chars) |
| | | 116 | | { |
| | 0 | 117 | | return GetByteCountCommon(pChars, chars.Length); |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 122 | | // So if you fix this, fix the others. Currently those include: |
| | | 123 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 124 | | |
| | | 125 | | [CLSCompliant(false)] |
| | | 126 | | public override unsafe int GetByteCount(char* chars, int count) |
| | | 127 | | { |
| | 0 | 128 | | if (chars is null) |
| | | 129 | | { |
| | 0 | 130 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chars); |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | if (count < 0) |
| | | 134 | | { |
| | 0 | 135 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRan |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | return GetByteCountCommon(chars, count); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | public override unsafe int GetByteCount(ReadOnlySpan<char> chars) |
| | 0 | 142 | | { |
| | | 143 | | // It's ok for us to pass null pointers down to the workhorse below. |
| | | 144 | | |
| | 0 | 145 | | fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) |
| | | 146 | | { |
| | 0 | 147 | | return GetByteCountCommon(charsPtr, chars.Length); |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 152 | | private unsafe int GetByteCountCommon(char* pChars, int charCount) |
| | | 153 | | { |
| | | 154 | | // Common helper method for all non-EncoderNLS entry points to GetByteCount. |
| | | 155 | | // A modification of this method should be copied in to each of the supported encodings: ASCII, UTF8, UTF16, |
| | | 156 | | |
| | 0 | 157 | | Debug.Assert(charCount >= 0, "Caller shouldn't specify negative length buffer."); |
| | 0 | 158 | | Debug.Assert(pChars is not null || charCount == 0, "Input pointer shouldn't be null if non-zero length speci |
| | | 159 | | |
| | | 160 | | // First call into the fast path. |
| | | 161 | | |
| | 0 | 162 | | int totalByteCount = GetByteCountFast(pChars, charCount, EncoderFallback, out int charsConsumed); |
| | | 163 | | |
| | 0 | 164 | | if (charsConsumed != charCount) |
| | | 165 | | { |
| | | 166 | | // If there's still data remaining in the source buffer, go down the fallback path. |
| | | 167 | | // We need to check for integer overflow since the fallback could change the required |
| | | 168 | | // output count in unexpected ways. |
| | | 169 | | |
| | 0 | 170 | | totalByteCount += GetByteCountWithFallback(pChars, charCount, charsConsumed); |
| | 0 | 171 | | if (totalByteCount < 0) |
| | | 172 | | { |
| | 0 | 173 | | ThrowConversionOverflow(); |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | return totalByteCount; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] // called directly by GetByteCountCommon |
| | | 181 | | private protected sealed override unsafe int GetByteCountFast(char* pChars, int charsLength, EncoderFallback? fa |
| | | 182 | | { |
| | | 183 | | // First: Can we short-circuit the entire calculation? |
| | | 184 | | // If an EncoderReplacementFallback is in use, all non-ASCII chars |
| | | 185 | | // (including surrogate halves) are replaced with the default string. |
| | | 186 | | // If the default string consists of a single ASCII value, then we |
| | | 187 | | // know there's a 1:1 char->byte transcoding in all cases. |
| | | 188 | | |
| | 0 | 189 | | int byteCount = charsLength; |
| | | 190 | | |
| | 0 | 191 | | if (!(fallback is EncoderReplacementFallback replacementFallback |
| | 0 | 192 | | && replacementFallback.MaxCharCount == 1 |
| | 0 | 193 | | && Ascii.IsValid(replacementFallback.DefaultString[0]))) |
| | | 194 | | { |
| | | 195 | | // Unrecognized fallback mechanism - count chars manually. |
| | | 196 | | |
| | 0 | 197 | | byteCount = (int)Ascii.GetIndexOfFirstNonAsciiChar(pChars, (uint)charsLength); |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | charsConsumed = byteCount; |
| | 0 | 201 | | return byteCount; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | // Parent method is safe. |
| | | 205 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 206 | | // So if you fix this, fix the others. Currently those include: |
| | | 207 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 208 | | |
| | | 209 | | public override unsafe int GetBytes(string chars, int charIndex, int charCount, |
| | | 210 | | byte[] bytes, int byteIndex) |
| | | 211 | | { |
| | 0 | 212 | | if (chars is null || bytes is null) |
| | | 213 | | { |
| | 0 | 214 | | ThrowHelper.ThrowArgumentNullException( |
| | 0 | 215 | | argument: (chars is null) ? ExceptionArgument.chars : ExceptionArgument.bytes, |
| | 0 | 216 | | resource: ExceptionResource.ArgumentNull_Array); |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | if ((charIndex | charCount) < 0) |
| | | 220 | | { |
| | 0 | 221 | | ThrowHelper.ThrowArgumentOutOfRangeException( |
| | 0 | 222 | | argument: (charIndex < 0) ? ExceptionArgument.charIndex : ExceptionArgument.charCount, |
| | 0 | 223 | | resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
| | | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | if (chars.Length - charIndex < charCount) |
| | | 227 | | { |
| | 0 | 228 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRan |
| | | 229 | | } |
| | | 230 | | |
| | 0 | 231 | | if ((uint)byteIndex > bytes.Length) |
| | | 232 | | { |
| | 0 | 233 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutO |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | fixed (char* pChars = chars) |
| | 0 | 237 | | fixed (byte* pBytes = bytes) |
| | | 238 | | { |
| | 0 | 239 | | return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); |
| | | 240 | | } |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | // Encodes a range of characters in a character array into a range of bytes |
| | | 244 | | // in a byte array. An exception occurs if the byte array is not large |
| | | 245 | | // enough to hold the complete encoding of the characters. The |
| | | 246 | | // GetByteCount method can be used to determine the exact number of |
| | | 247 | | // bytes that will be produced for a given range of characters. |
| | | 248 | | // Alternatively, the GetMaxByteCount method can be used to |
| | | 249 | | // determine the maximum number of bytes that will be produced for a given |
| | | 250 | | // number of characters, regardless of the actual character values. |
| | | 251 | | // |
| | | 252 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 253 | | // So if you fix this, fix the others. Currently those include: |
| | | 254 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 255 | | // parent method is safe |
| | | 256 | | |
| | | 257 | | public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, |
| | | 258 | | byte[] bytes, int byteIndex) |
| | | 259 | | { |
| | 0 | 260 | | if (chars is null || bytes is null) |
| | | 261 | | { |
| | 0 | 262 | | ThrowHelper.ThrowArgumentNullException( |
| | 0 | 263 | | argument: (chars is null) ? ExceptionArgument.chars : ExceptionArgument.bytes, |
| | 0 | 264 | | resource: ExceptionResource.ArgumentNull_Array); |
| | | 265 | | } |
| | | 266 | | |
| | 0 | 267 | | if ((charIndex | charCount) < 0) |
| | | 268 | | { |
| | 0 | 269 | | ThrowHelper.ThrowArgumentOutOfRangeException( |
| | 0 | 270 | | argument: (charIndex < 0) ? ExceptionArgument.charIndex : ExceptionArgument.charCount, |
| | 0 | 271 | | resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
| | | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | if (chars.Length - charIndex < charCount) |
| | | 275 | | { |
| | 0 | 276 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRan |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | if ((uint)byteIndex > bytes.Length) |
| | | 280 | | { |
| | 0 | 281 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutO |
| | | 282 | | } |
| | | 283 | | |
| | 0 | 284 | | fixed (char* pChars = chars) |
| | 0 | 285 | | fixed (byte* pBytes = bytes) |
| | | 286 | | { |
| | 0 | 287 | | return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); |
| | | 288 | | } |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 292 | | // So if you fix this, fix the others. Currently those include: |
| | | 293 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 294 | | |
| | | 295 | | [CLSCompliant(false)] |
| | | 296 | | public override unsafe int GetBytes(char* chars, int charCount, byte* bytes, int byteCount) |
| | | 297 | | { |
| | 0 | 298 | | if (chars is null || bytes is null) |
| | | 299 | | { |
| | 0 | 300 | | ThrowHelper.ThrowArgumentNullException( |
| | 0 | 301 | | argument: (chars is null) ? ExceptionArgument.chars : ExceptionArgument.bytes, |
| | 0 | 302 | | resource: ExceptionResource.ArgumentNull_Array); |
| | | 303 | | } |
| | | 304 | | |
| | 0 | 305 | | if ((charCount | byteCount) < 0) |
| | | 306 | | { |
| | 0 | 307 | | ThrowHelper.ThrowArgumentOutOfRangeException( |
| | 0 | 308 | | argument: (charCount < 0) ? ExceptionArgument.charCount : ExceptionArgument.byteCount, |
| | 0 | 309 | | resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | return GetBytesCommon(chars, charCount, bytes, byteCount); |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | public override unsafe int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes) |
| | 0 | 316 | | { |
| | | 317 | | // It's ok for us to operate on null / empty spans. |
| | | 318 | | |
| | 0 | 319 | | fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) |
| | 0 | 320 | | fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) |
| | | 321 | | { |
| | 0 | 322 | | return GetBytesCommon(charsPtr, chars.Length, bytesPtr, bytes.Length); |
| | | 323 | | } |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <inheritdoc/> |
| | | 327 | | public override unsafe bool TryGetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, out int bytesWritten) |
| | 0 | 328 | | { |
| | 0 | 329 | | fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) |
| | 0 | 330 | | fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) |
| | | 331 | | { |
| | 0 | 332 | | int written = GetBytesCommon(charsPtr, chars.Length, bytesPtr, bytes.Length, throwForDestinationOverflow |
| | 0 | 333 | | if (written >= 0) |
| | | 334 | | { |
| | 0 | 335 | | bytesWritten = written; |
| | 0 | 336 | | return true; |
| | | 337 | | } |
| | | 338 | | |
| | 0 | 339 | | bytesWritten = 0; |
| | 0 | 340 | | return false; |
| | | 341 | | } |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 345 | | private unsafe int GetBytesCommon(char* pChars, int charCount, byte* pBytes, int byteCount, bool throwForDestina |
| | | 346 | | { |
| | | 347 | | // Common helper method for all non-EncoderNLS entry points to GetBytes. |
| | | 348 | | // A modification of this method should be copied in to each of the supported encodings: ASCII, UTF8, UTF16, |
| | | 349 | | |
| | 0 | 350 | | Debug.Assert(charCount >= 0, "Caller shouldn't specify negative length buffer."); |
| | 0 | 351 | | Debug.Assert(pChars is not null || charCount == 0, "Input pointer shouldn't be null if non-zero length speci |
| | 0 | 352 | | Debug.Assert(byteCount >= 0, "Caller shouldn't specify negative length buffer."); |
| | 0 | 353 | | Debug.Assert(pBytes is not null || byteCount == 0, "Input pointer shouldn't be null if non-zero length speci |
| | | 354 | | |
| | | 355 | | // First call into the fast path. |
| | | 356 | | |
| | 0 | 357 | | int bytesWritten = GetBytesFast(pChars, charCount, pBytes, byteCount, out int charsConsumed); |
| | | 358 | | |
| | 0 | 359 | | if (charsConsumed == charCount) |
| | | 360 | | { |
| | | 361 | | // All elements converted - return immediately. |
| | | 362 | | |
| | 0 | 363 | | return bytesWritten; |
| | | 364 | | } |
| | | 365 | | else |
| | | 366 | | { |
| | | 367 | | // Simple narrowing conversion couldn't operate on entire buffer - invoke fallback. |
| | | 368 | | |
| | 0 | 369 | | return GetBytesWithFallback(pChars, charCount, pBytes, byteCount, charsConsumed, bytesWritten, throwForD |
| | | 370 | | } |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] // called directly by GetBytesCommon |
| | | 374 | | private protected sealed override unsafe int GetBytesFast(char* pChars, int charsLength, byte* pBytes, int bytes |
| | | 375 | | { |
| | 0 | 376 | | int bytesWritten = (int)Ascii.NarrowUtf16ToAscii(pChars, pBytes, (uint)Math.Min(charsLength, bytesLength)); |
| | | 377 | | |
| | 0 | 378 | | charsConsumed = bytesWritten; |
| | 0 | 379 | | return bytesWritten; |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | private protected sealed override int GetBytesWithFallback(ReadOnlySpan<char> chars, int originalCharsLength, Sp |
| | | 383 | | { |
| | | 384 | | // We special-case EncoderReplacementFallback if it's telling us to write a single ASCII char, |
| | | 385 | | // since we believe this to be relatively common and we can handle it more efficiently than |
| | | 386 | | // the base implementation. |
| | | 387 | | |
| | 0 | 388 | | if (((encoder is null) ? this.EncoderFallback : encoder.Fallback) is EncoderReplacementFallback replacementF |
| | 0 | 389 | | && replacementFallback.MaxCharCount == 1 |
| | 0 | 390 | | && Ascii.IsValid(replacementFallback.DefaultString[0])) |
| | | 391 | | { |
| | 0 | 392 | | byte replacementByte = (byte)replacementFallback.DefaultString[0]; |
| | | 393 | | |
| | 0 | 394 | | int numElementsToConvert = Math.Min(chars.Length, bytes.Length); |
| | 0 | 395 | | int idx = 0; |
| | | 396 | | |
| | | 397 | | // In a loop, replace the non-convertible data, then bulk-convert as much as we can. |
| | 0 | 398 | | while (idx < numElementsToConvert) |
| | | 399 | | { |
| | 0 | 400 | | bytes[idx++] = replacementByte; |
| | | 401 | | |
| | 0 | 402 | | if (idx < numElementsToConvert) |
| | | 403 | | { |
| | 0 | 404 | | Ascii.FromUtf16(chars.Slice(idx), bytes.Slice(idx), out int charsConsumed); |
| | | 405 | | |
| | 0 | 406 | | idx += charsConsumed; |
| | | 407 | | } |
| | | 408 | | |
| | 0 | 409 | | Debug.Assert(idx <= numElementsToConvert, "Somehow went beyond bounds of source or destination buffe |
| | | 410 | | } |
| | | 411 | | |
| | | 412 | | // Slice off how much we consumed / wrote. |
| | | 413 | | |
| | 0 | 414 | | chars = chars.Slice(numElementsToConvert); |
| | 0 | 415 | | bytes = bytes.Slice(numElementsToConvert); |
| | | 416 | | } |
| | | 417 | | |
| | | 418 | | // If we couldn't go through our fast fallback mechanism, or if we still have leftover |
| | | 419 | | // data because we couldn't consume everything in the loop above, we need to go down the |
| | | 420 | | // slow fallback path. |
| | | 421 | | |
| | 0 | 422 | | if (chars.IsEmpty) |
| | | 423 | | { |
| | 0 | 424 | | return originalBytesLength - bytes.Length; // total number of bytes written |
| | | 425 | | } |
| | | 426 | | else |
| | | 427 | | { |
| | 0 | 428 | | return base.GetBytesWithFallback(chars, originalCharsLength, bytes, originalBytesLength, encoder, throwF |
| | | 429 | | } |
| | | 430 | | } |
| | | 431 | | |
| | | 432 | | // Returns the number of characters produced by decoding a range of bytes |
| | | 433 | | // in a byte array. |
| | | 434 | | // |
| | | 435 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 436 | | // So if you fix this, fix the others. Currently those include: |
| | | 437 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 438 | | // parent method is safe |
| | | 439 | | |
| | | 440 | | public override unsafe int GetCharCount(byte[] bytes, int index, int count) |
| | | 441 | | { |
| | 0 | 442 | | if (bytes is null) |
| | | 443 | | { |
| | 0 | 444 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.bytes, ExceptionResource.ArgumentNull_Array); |
| | | 445 | | } |
| | | 446 | | |
| | 0 | 447 | | if ((index | count) < 0) |
| | | 448 | | { |
| | 0 | 449 | | ThrowHelper.ThrowArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.c |
| | | 450 | | } |
| | | 451 | | |
| | 0 | 452 | | if (bytes.Length - index < count) |
| | | 453 | | { |
| | 0 | 454 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRan |
| | | 455 | | } |
| | | 456 | | |
| | 0 | 457 | | fixed (byte* pBytes = bytes) |
| | | 458 | | { |
| | 0 | 459 | | return GetCharCountCommon(pBytes + index, count); |
| | | 460 | | } |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 464 | | // So if you fix this, fix the others. Currently those include: |
| | | 465 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 466 | | |
| | | 467 | | [CLSCompliant(false)] |
| | | 468 | | public override unsafe int GetCharCount(byte* bytes, int count) |
| | | 469 | | { |
| | 0 | 470 | | if (bytes is null) |
| | | 471 | | { |
| | 0 | 472 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.bytes, ExceptionResource.ArgumentNull_Array); |
| | | 473 | | } |
| | | 474 | | |
| | 0 | 475 | | if (count < 0) |
| | | 476 | | { |
| | 0 | 477 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRan |
| | | 478 | | } |
| | | 479 | | |
| | 0 | 480 | | return GetCharCountCommon(bytes, count); |
| | | 481 | | } |
| | | 482 | | |
| | | 483 | | public override unsafe int GetCharCount(ReadOnlySpan<byte> bytes) |
| | 0 | 484 | | { |
| | | 485 | | // It's ok for us to pass null pointers down to the workhorse routine. |
| | | 486 | | |
| | 0 | 487 | | fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) |
| | | 488 | | { |
| | 0 | 489 | | return GetCharCountCommon(bytesPtr, bytes.Length); |
| | | 490 | | } |
| | | 491 | | } |
| | | 492 | | |
| | | 493 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 494 | | private unsafe int GetCharCountCommon(byte* pBytes, int byteCount) |
| | | 495 | | { |
| | | 496 | | // Common helper method for all non-DecoderNLS entry points to GetCharCount. |
| | | 497 | | // A modification of this method should be copied in to each of the supported encodings: ASCII, UTF8, UTF16, |
| | | 498 | | |
| | 0 | 499 | | Debug.Assert(byteCount >= 0, "Caller shouldn't specify negative length buffer."); |
| | 0 | 500 | | Debug.Assert(pBytes is not null || byteCount == 0, "Input pointer shouldn't be null if non-zero length speci |
| | | 501 | | |
| | | 502 | | // First call into the fast path. |
| | | 503 | | |
| | 0 | 504 | | int totalCharCount = GetCharCountFast(pBytes, byteCount, DecoderFallback, out int bytesConsumed); |
| | | 505 | | |
| | 0 | 506 | | if (bytesConsumed != byteCount) |
| | | 507 | | { |
| | | 508 | | // If there's still data remaining in the source buffer, go down the fallback path. |
| | | 509 | | // We need to check for integer overflow since the fallback could change the required |
| | | 510 | | // output count in unexpected ways. |
| | | 511 | | |
| | 0 | 512 | | totalCharCount += GetCharCountWithFallback(pBytes, byteCount, bytesConsumed); |
| | 0 | 513 | | if (totalCharCount < 0) |
| | | 514 | | { |
| | 0 | 515 | | ThrowConversionOverflow(); |
| | | 516 | | } |
| | | 517 | | } |
| | | 518 | | |
| | 0 | 519 | | return totalCharCount; |
| | | 520 | | } |
| | | 521 | | |
| | | 522 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] // called directly by GetCharCountCommon |
| | | 523 | | private protected sealed override unsafe int GetCharCountFast(byte* pBytes, int bytesLength, DecoderFallback? fa |
| | | 524 | | { |
| | | 525 | | // First: Can we short-circuit the entire calculation? |
| | | 526 | | // If a DecoderReplacementFallback is in use, all non-ASCII bytes are replaced with |
| | | 527 | | // the default string. If the default string consists of a single BMP value, then we |
| | | 528 | | // know there's a 1:1 byte->char transcoding in all cases. |
| | | 529 | | |
| | 0 | 530 | | int charCount = bytesLength; |
| | | 531 | | |
| | 0 | 532 | | if (fallback is not DecoderReplacementFallback replacementFallback || replacementFallback.MaxCharCount != 1) |
| | | 533 | | { |
| | | 534 | | // Unrecognized fallback mechanism - count bytes manually. |
| | | 535 | | |
| | 0 | 536 | | charCount = (int)Ascii.GetIndexOfFirstNonAsciiByte(pBytes, (uint)bytesLength); |
| | | 537 | | } |
| | | 538 | | |
| | 0 | 539 | | bytesConsumed = charCount; |
| | 0 | 540 | | return charCount; |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 544 | | // So if you fix this, fix the others. Currently those include: |
| | | 545 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 546 | | // parent method is safe |
| | | 547 | | |
| | | 548 | | public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, |
| | | 549 | | char[] chars, int charIndex) |
| | | 550 | | { |
| | 0 | 551 | | if (bytes is null || chars is null) |
| | | 552 | | { |
| | 0 | 553 | | ThrowHelper.ThrowArgumentNullException( |
| | 0 | 554 | | argument: (bytes is null) ? ExceptionArgument.bytes : ExceptionArgument.chars, |
| | 0 | 555 | | resource: ExceptionResource.ArgumentNull_Array); |
| | | 556 | | } |
| | | 557 | | |
| | 0 | 558 | | if ((byteIndex | byteCount) < 0) |
| | | 559 | | { |
| | 0 | 560 | | ThrowHelper.ThrowArgumentOutOfRangeException( |
| | 0 | 561 | | argument: (byteIndex < 0) ? ExceptionArgument.byteIndex : ExceptionArgument.byteCount, |
| | 0 | 562 | | resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
| | | 563 | | } |
| | | 564 | | |
| | 0 | 565 | | if (bytes.Length - byteIndex < byteCount) |
| | | 566 | | { |
| | 0 | 567 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRan |
| | | 568 | | } |
| | | 569 | | |
| | 0 | 570 | | if ((uint)charIndex > (uint)chars.Length) |
| | | 571 | | { |
| | 0 | 572 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.charIndex, ExceptionResource.ArgumentOutO |
| | | 573 | | } |
| | | 574 | | |
| | 0 | 575 | | fixed (byte* pBytes = bytes) |
| | 0 | 576 | | fixed (char* pChars = chars) |
| | | 577 | | { |
| | 0 | 578 | | return GetCharsCommon(pBytes + byteIndex, byteCount, pChars + charIndex, chars.Length - charIndex); |
| | | 579 | | } |
| | | 580 | | } |
| | | 581 | | |
| | | 582 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 583 | | // So if you fix this, fix the others. Currently those include: |
| | | 584 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 585 | | |
| | | 586 | | [CLSCompliant(false)] |
| | | 587 | | public override unsafe int GetChars(byte* bytes, int byteCount, char* chars, int charCount) |
| | | 588 | | { |
| | 0 | 589 | | if (bytes is null || chars is null) |
| | | 590 | | { |
| | 0 | 591 | | ThrowHelper.ThrowArgumentNullException( |
| | 0 | 592 | | argument: (bytes is null) ? ExceptionArgument.bytes : ExceptionArgument.chars, |
| | 0 | 593 | | resource: ExceptionResource.ArgumentNull_Array); |
| | | 594 | | } |
| | | 595 | | |
| | 0 | 596 | | if ((byteCount | charCount) < 0) |
| | | 597 | | { |
| | 0 | 598 | | ThrowHelper.ThrowArgumentOutOfRangeException( |
| | 0 | 599 | | argument: (byteCount < 0) ? ExceptionArgument.byteCount : ExceptionArgument.charCount, |
| | 0 | 600 | | resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
| | | 601 | | } |
| | | 602 | | |
| | 0 | 603 | | return GetCharsCommon(bytes, byteCount, chars, charCount); |
| | | 604 | | } |
| | | 605 | | |
| | | 606 | | public override unsafe int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars) |
| | 0 | 607 | | { |
| | | 608 | | // It's ok for us to pass null pointers down to the workhorse below. |
| | | 609 | | |
| | 0 | 610 | | fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) |
| | 0 | 611 | | fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) |
| | | 612 | | { |
| | 0 | 613 | | return GetCharsCommon(bytesPtr, bytes.Length, charsPtr, chars.Length); |
| | | 614 | | } |
| | | 615 | | } |
| | | 616 | | |
| | | 617 | | /// <inheritdoc/> |
| | | 618 | | public override unsafe bool TryGetChars(ReadOnlySpan<byte> bytes, Span<char> chars, out int charsWritten) |
| | 0 | 619 | | { |
| | 0 | 620 | | fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) |
| | 0 | 621 | | fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) |
| | | 622 | | { |
| | 0 | 623 | | int written = GetCharsCommon(bytesPtr, bytes.Length, charsPtr, chars.Length, throwForDestinationOverflow |
| | 0 | 624 | | if (written >= 0) |
| | | 625 | | { |
| | 0 | 626 | | charsWritten = written; |
| | 0 | 627 | | return true; |
| | | 628 | | } |
| | | 629 | | |
| | 0 | 630 | | charsWritten = 0; |
| | 0 | 631 | | return false; |
| | | 632 | | } |
| | | 633 | | } |
| | | 634 | | |
| | | 635 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 636 | | private unsafe int GetCharsCommon(byte* pBytes, int byteCount, char* pChars, int charCount, bool throwForDestina |
| | | 637 | | { |
| | | 638 | | // Common helper method for all non-DecoderNLS entry points to GetChars. |
| | | 639 | | // A modification of this method should be copied in to each of the supported encodings: ASCII, UTF8, UTF16, |
| | | 640 | | |
| | 0 | 641 | | Debug.Assert(byteCount >= 0, "Caller shouldn't specify negative length buffer."); |
| | 0 | 642 | | Debug.Assert(pBytes is not null || byteCount == 0, "Input pointer shouldn't be null if non-zero length speci |
| | 0 | 643 | | Debug.Assert(charCount >= 0, "Caller shouldn't specify negative length buffer."); |
| | 0 | 644 | | Debug.Assert(pChars is not null || charCount == 0, "Input pointer shouldn't be null if non-zero length speci |
| | | 645 | | |
| | | 646 | | // First call into the fast path. |
| | | 647 | | |
| | 0 | 648 | | int charsWritten = GetCharsFast(pBytes, byteCount, pChars, charCount, out int bytesConsumed); |
| | | 649 | | |
| | 0 | 650 | | if (bytesConsumed == byteCount) |
| | | 651 | | { |
| | | 652 | | // All elements converted - return immediately. |
| | | 653 | | |
| | 0 | 654 | | return charsWritten; |
| | | 655 | | } |
| | | 656 | | else |
| | | 657 | | { |
| | | 658 | | // Simple narrowing conversion couldn't operate on entire buffer - invoke fallback. |
| | | 659 | | |
| | 0 | 660 | | return GetCharsWithFallback(pBytes, byteCount, pChars, charCount, bytesConsumed, charsWritten, throwForD |
| | | 661 | | } |
| | | 662 | | } |
| | | 663 | | |
| | | 664 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] // called directly by GetCharsCommon |
| | | 665 | | private protected sealed override unsafe int GetCharsFast(byte* pBytes, int bytesLength, char* pChars, int chars |
| | | 666 | | { |
| | 0 | 667 | | bytesConsumed = (int)Ascii.WidenAsciiToUtf16(pBytes, pChars, (uint)Math.Min(charsLength, bytesLength)); |
| | 0 | 668 | | return bytesConsumed; |
| | | 669 | | } |
| | | 670 | | |
| | | 671 | | private protected sealed override int GetCharsWithFallback(ReadOnlySpan<byte> bytes, int originalBytesLength, Sp |
| | | 672 | | { |
| | | 673 | | // We special-case DecoderReplacementFallback if it's telling us to write a single BMP char, |
| | | 674 | | // since we believe this to be relatively common and we can handle it more efficiently than |
| | | 675 | | // the base implementation. |
| | | 676 | | |
| | 0 | 677 | | if ((decoder is null ? DecoderFallback : decoder.Fallback) is DecoderReplacementFallback replacementFallback |
| | 0 | 678 | | && replacementFallback.MaxCharCount == 1) |
| | | 679 | | { |
| | 0 | 680 | | char replacementChar = replacementFallback.DefaultString[0]; |
| | | 681 | | |
| | 0 | 682 | | int numElementsToConvert = Math.Min(bytes.Length, chars.Length); |
| | 0 | 683 | | int idx = 0; |
| | | 684 | | |
| | | 685 | | // In a loop, replace the non-convertible data, then bulk-convert as much as we can. |
| | | 686 | | |
| | 0 | 687 | | while (idx < numElementsToConvert) |
| | | 688 | | { |
| | 0 | 689 | | chars[idx++] = replacementChar; |
| | | 690 | | |
| | 0 | 691 | | if (idx < numElementsToConvert) |
| | | 692 | | { |
| | 0 | 693 | | Ascii.ToUtf16(bytes.Slice(idx), chars.Slice(idx), out int bytesConsumed); |
| | 0 | 694 | | idx += bytesConsumed; |
| | | 695 | | } |
| | | 696 | | |
| | 0 | 697 | | Debug.Assert(idx <= numElementsToConvert, "Somehow went beyond bounds of source or destination buffe |
| | | 698 | | } |
| | | 699 | | |
| | | 700 | | // Slice off how much we consumed / wrote. |
| | | 701 | | |
| | 0 | 702 | | bytes = bytes.Slice(numElementsToConvert); |
| | 0 | 703 | | chars = chars.Slice(numElementsToConvert); |
| | | 704 | | } |
| | | 705 | | |
| | | 706 | | // If we couldn't go through our fast fallback mechanism, or if we still have leftover |
| | | 707 | | // data because we couldn't consume everything in the loop above, we need to go down the |
| | | 708 | | // slow fallback path. |
| | | 709 | | |
| | 0 | 710 | | if (bytes.IsEmpty) |
| | | 711 | | { |
| | 0 | 712 | | return originalCharsLength - chars.Length; // total number of chars written |
| | | 713 | | } |
| | | 714 | | else |
| | | 715 | | { |
| | 0 | 716 | | return base.GetCharsWithFallback(bytes, originalBytesLength, chars, originalCharsLength, decoder, throwF |
| | | 717 | | } |
| | | 718 | | } |
| | | 719 | | |
| | | 720 | | // Returns a string containing the decoded representation of a range of |
| | | 721 | | // bytes in a byte array. |
| | | 722 | | // |
| | | 723 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 724 | | // So if you fix this, fix the others. Currently those include: |
| | | 725 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 726 | | // parent method is safe |
| | | 727 | | |
| | | 728 | | public override unsafe string GetString(byte[] bytes, int byteIndex, int byteCount) |
| | | 729 | | { |
| | 0 | 730 | | if (bytes is null) |
| | | 731 | | { |
| | 0 | 732 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.bytes, ExceptionResource.ArgumentNull_Array); |
| | | 733 | | } |
| | | 734 | | |
| | 0 | 735 | | if ((byteIndex | byteCount) < 0) |
| | | 736 | | { |
| | 0 | 737 | | ThrowHelper.ThrowArgumentOutOfRangeException( |
| | 0 | 738 | | argument: (byteIndex < 0) ? ExceptionArgument.byteIndex : ExceptionArgument.byteCount, |
| | 0 | 739 | | resource: ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
| | | 740 | | } |
| | | 741 | | |
| | 0 | 742 | | if (bytes.Length - byteIndex < byteCount) |
| | | 743 | | { |
| | 0 | 744 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRan |
| | | 745 | | } |
| | | 746 | | |
| | | 747 | | // Avoid problems with empty input buffer |
| | 0 | 748 | | if (byteCount == 0) |
| | 0 | 749 | | return string.Empty; |
| | | 750 | | |
| | 0 | 751 | | fixed (byte* pBytes = bytes) |
| | | 752 | | { |
| | 0 | 753 | | return string.CreateStringFromEncoding(pBytes + byteIndex, byteCount, this); |
| | | 754 | | } |
| | | 755 | | } |
| | | 756 | | |
| | | 757 | | // |
| | | 758 | | // End of standard methods copied from EncodingNLS.cs |
| | | 759 | | // |
| | | 760 | | |
| | | 761 | | // |
| | | 762 | | // Beginning of methods used by shared fallback logic. |
| | | 763 | | // |
| | | 764 | | |
| | | 765 | | internal sealed override bool TryGetByteCount(Rune value, out int byteCount) |
| | | 766 | | { |
| | 0 | 767 | | if (value.IsAscii) |
| | | 768 | | { |
| | 0 | 769 | | byteCount = 1; |
| | 0 | 770 | | return true; |
| | | 771 | | } |
| | | 772 | | else |
| | | 773 | | { |
| | 0 | 774 | | byteCount = default; |
| | 0 | 775 | | return false; |
| | | 776 | | } |
| | | 777 | | } |
| | | 778 | | |
| | | 779 | | internal sealed override OperationStatus EncodeRune(Rune value, Span<byte> bytes, out int bytesWritten) |
| | | 780 | | { |
| | 0 | 781 | | if (value.IsAscii) |
| | | 782 | | { |
| | 0 | 783 | | if (!bytes.IsEmpty) |
| | | 784 | | { |
| | 0 | 785 | | bytes[0] = (byte)value.Value; |
| | 0 | 786 | | bytesWritten = 1; |
| | 0 | 787 | | return OperationStatus.Done; |
| | | 788 | | } |
| | | 789 | | else |
| | | 790 | | { |
| | 0 | 791 | | bytesWritten = 0; |
| | 0 | 792 | | return OperationStatus.DestinationTooSmall; |
| | | 793 | | } |
| | | 794 | | } |
| | | 795 | | else |
| | | 796 | | { |
| | 0 | 797 | | bytesWritten = 0; |
| | 0 | 798 | | return OperationStatus.InvalidData; |
| | | 799 | | } |
| | | 800 | | } |
| | | 801 | | |
| | | 802 | | internal sealed override OperationStatus DecodeFirstRune(ReadOnlySpan<byte> bytes, out Rune value, out int bytes |
| | | 803 | | { |
| | 0 | 804 | | if (!bytes.IsEmpty) |
| | | 805 | | { |
| | 0 | 806 | | byte b = bytes[0]; |
| | 0 | 807 | | if (Ascii.IsValid(b)) |
| | | 808 | | { |
| | | 809 | | // ASCII byte |
| | | 810 | | |
| | 0 | 811 | | value = new Rune(b); |
| | 0 | 812 | | bytesConsumed = 1; |
| | 0 | 813 | | return OperationStatus.Done; |
| | | 814 | | } |
| | | 815 | | else |
| | | 816 | | { |
| | | 817 | | // Non-ASCII byte |
| | | 818 | | |
| | 0 | 819 | | value = Rune.ReplacementChar; |
| | 0 | 820 | | bytesConsumed = 1; |
| | 0 | 821 | | return OperationStatus.InvalidData; |
| | | 822 | | } |
| | | 823 | | } |
| | | 824 | | else |
| | | 825 | | { |
| | | 826 | | // No data to decode |
| | | 827 | | |
| | 0 | 828 | | value = Rune.ReplacementChar; |
| | 0 | 829 | | bytesConsumed = 0; |
| | 0 | 830 | | return OperationStatus.NeedMoreData; |
| | | 831 | | } |
| | | 832 | | } |
| | | 833 | | |
| | | 834 | | // |
| | | 835 | | // End of methods used by shared fallback logic. |
| | | 836 | | // |
| | | 837 | | |
| | | 838 | | public override int GetMaxByteCount(int charCount) |
| | | 839 | | { |
| | 0 | 840 | | ArgumentOutOfRangeException.ThrowIfNegative(charCount); |
| | | 841 | | |
| | | 842 | | // Characters would be # of characters + 1 in case high surrogate is ? * max fallback |
| | 0 | 843 | | long byteCount = (long)charCount + 1; |
| | | 844 | | |
| | 0 | 845 | | if (EncoderFallback.MaxCharCount > 1) |
| | 0 | 846 | | byteCount *= EncoderFallback.MaxCharCount; |
| | | 847 | | |
| | | 848 | | // 1 to 1 for most characters. Only surrogates with fallbacks have less. |
| | | 849 | | |
| | 0 | 850 | | if (byteCount > 0x7fffffff) |
| | 0 | 851 | | throw new ArgumentOutOfRangeException(nameof(charCount), SR.ArgumentOutOfRange_GetByteCountOverflow); |
| | 0 | 852 | | return (int)byteCount; |
| | | 853 | | } |
| | | 854 | | |
| | | 855 | | |
| | | 856 | | public override int GetMaxCharCount(int byteCount) |
| | | 857 | | { |
| | 0 | 858 | | ArgumentOutOfRangeException.ThrowIfNegative(byteCount); |
| | | 859 | | |
| | | 860 | | // Just return length, SBCS stay the same length because they don't map to surrogate |
| | 0 | 861 | | long charCount = (long)byteCount; |
| | | 862 | | |
| | | 863 | | // 1 to 1 for most characters. Only surrogates with fallbacks have less, unknown fallbacks could be longer. |
| | 0 | 864 | | if (DecoderFallback.MaxCharCount > 1) |
| | 0 | 865 | | charCount *= DecoderFallback.MaxCharCount; |
| | | 866 | | |
| | 0 | 867 | | if (charCount > 0x7fffffff) |
| | 0 | 868 | | throw new ArgumentOutOfRangeException(nameof(byteCount), SR.ArgumentOutOfRange_GetCharCountOverflow); |
| | | 869 | | |
| | 0 | 870 | | return (int)charCount; |
| | | 871 | | } |
| | | 872 | | |
| | | 873 | | // True if and only if the encoding only uses single byte code points. (Ie, ASCII, 1252, etc) |
| | | 874 | | |
| | 0 | 875 | | public override bool IsSingleByte => true; |
| | | 876 | | |
| | 0 | 877 | | public override Decoder GetDecoder() => new DecoderNLS(this); |
| | | 878 | | |
| | | 879 | | |
| | 0 | 880 | | public override Encoder GetEncoder() => new EncoderNLS(this); |
| | | 881 | | } |
| | | 882 | | } |
| | | 883 | | |