Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ GetBytes() [2/9]

unsafe override int System.Text.UTF32Encoding.GetBytes ( char * chars,
int charCount,
byte * bytes,
int byteCount,
EncoderNLS encoder )
inlinepackagevirtual

Reimplemented from System.Text.Encoding.

Definition at line 409 of file UTF32Encoding.cs.

410 {
411 char* ptr = chars;
412 char* ptr2 = chars + charCount;
413 byte* ptr3 = bytes;
414 byte* ptr4 = bytes + byteCount;
415 char c = '\0';
416 EncoderFallbackBuffer encoderFallbackBuffer = null;
417 if (encoder != null)
418 {
419 c = encoder._charLeftOver;
420 encoderFallbackBuffer = encoder.FallbackBuffer;
421 if (encoder._throwOnOverflow && encoderFallbackBuffer.Remaining > 0)
422 {
423 throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, EncodingName, encoder.Fallback?.GetType()));
424 }
425 }
426 else
427 {
428 encoderFallbackBuffer = encoderFallback.CreateFallbackBuffer();
429 }
430 encoderFallbackBuffer.InternalInitialize(ptr, ptr2, encoder, setEncoder: true);
431 while (true)
432 {
433 char c2;
434 char* chars2;
435 if ((c2 = encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < ptr2)
436 {
437 if (c2 == '\0')
438 {
439 c2 = *chars;
440 chars++;
441 }
442 if (c != 0)
443 {
444 if (!char.IsLowSurrogate(c2))
445 {
446 chars--;
447 chars2 = chars;
448 encoderFallbackBuffer.InternalFallback(c, ref chars2);
449 chars = chars2;
450 c = '\0';
451 continue;
452 }
453 uint surrogate = GetSurrogate(c, c2);
454 c = '\0';
455 if (bytes + 3 < ptr4)
456 {
457 if (_bigEndian)
458 {
459 *(bytes++) = 0;
460 *(bytes++) = (byte)(surrogate >> 16);
461 *(bytes++) = (byte)(surrogate >> 8);
462 *(bytes++) = (byte)surrogate;
463 }
464 else
465 {
466 *(bytes++) = (byte)surrogate;
467 *(bytes++) = (byte)(surrogate >> 8);
468 *(bytes++) = (byte)(surrogate >> 16);
469 *(bytes++) = 0;
470 }
471 continue;
472 }
473 if (encoderFallbackBuffer.bFallingBack)
474 {
475 encoderFallbackBuffer.MovePrevious();
476 encoderFallbackBuffer.MovePrevious();
477 }
478 else
479 {
480 chars -= 2;
481 }
482 ThrowBytesOverflow(encoder, bytes == ptr3);
483 c = '\0';
484 }
485 else
486 {
487 if (char.IsHighSurrogate(c2))
488 {
489 c = c2;
490 continue;
491 }
492 if (char.IsLowSurrogate(c2))
493 {
494 chars2 = chars;
495 encoderFallbackBuffer.InternalFallback(c2, ref chars2);
496 chars = chars2;
497 continue;
498 }
499 if (bytes + 3 < ptr4)
500 {
501 if (_bigEndian)
502 {
503 *(bytes++) = 0;
504 *(bytes++) = 0;
505 *(bytes++) = (byte)((uint)c2 >> 8);
506 *(bytes++) = (byte)c2;
507 }
508 else
509 {
510 *(bytes++) = (byte)c2;
511 *(bytes++) = (byte)((uint)c2 >> 8);
512 *(bytes++) = 0;
513 *(bytes++) = 0;
514 }
515 continue;
516 }
517 if (encoderFallbackBuffer.bFallingBack)
518 {
519 encoderFallbackBuffer.MovePrevious();
520 }
521 else
522 {
523 chars--;
524 }
525 ThrowBytesOverflow(encoder, bytes == ptr3);
526 }
527 }
528 if ((encoder != null && !encoder.MustFlush) || c <= '\0')
529 {
530 break;
531 }
532 chars2 = chars;
533 encoderFallbackBuffer.InternalFallback(c, ref chars2);
534 chars = chars2;
535 c = '\0';
536 }
537 if (encoder != null)
538 {
539 encoder._charLeftOver = c;
540 encoder._charsUsed = (int)(chars - ptr);
541 }
542 return (int)(bytes - ptr3);
543 }
unsafe void InternalInitialize(char *charStart, char *charEnd, EncoderNLS encoder, bool setEncoder)
EncoderFallbackBuffer CreateFallbackBuffer()
EncoderFallback encoderFallback
Definition Encoding.cs:341
virtual string EncodingName
Definition Encoding.cs:362
static uint GetSurrogate(char cHigh, char cLow)

References System.Text.UTF32Encoding._bigEndian, System.Text.EncoderNLS._charLeftOver, System.Text.EncoderNLS._throwOnOverflow, System.SR.Argument_EncoderFallbackNotEmpty, System.Text.EncoderFallbackBuffer.bFallingBack, System.byteCount, System.bytes, System.charCount, System.chars, System.Text.EncoderFallback.CreateFallbackBuffer(), System.Text.Encoding.encoderFallback, System.Text.Encoding.EncodingName, System.Text.EncoderNLS.Fallback, System.Text.EncoderNLS.FallbackBuffer, System.SR.Format(), System.Text.UTF32Encoding.GetSurrogate(), System.Text.EncoderFallbackBuffer.InternalFallback(), System.Text.EncoderFallbackBuffer.InternalGetNextChar(), System.Text.EncoderFallbackBuffer.InternalInitialize(), System.Text.EncoderFallbackBuffer.MovePrevious(), System.Text.EncoderNLS.MustFlush, System.Text.EncoderFallbackBuffer.Remaining, and System.Text.Encoding.ThrowBytesOverflow().