Utf8Encoder

Namespace: SnowBank.Text · class

Methods

Equals

static bool Equals(ReadOnlySpan<byte> left, string right)

Tests if a buffer containing a UTF-8 string is equal to a UTF-16 string, by comparing each Rune

  • left — Buffer that points to a UTF-8 encoded string
  • right — String that contains a UTF-16 encoded string

Returns: true if they both contain the same sequences of runes (they are "equal"), or false if they are different, or left is an invalid UTF-8 sequence

This method does not normalize either of the arguments. The caller must ensure that they both are in the canonical form (by calling , or only using "safe" constants)

static bool Equals(Slice left, string right)

Tests if a buffer containing a UTF-8 string is equal to a UTF-16 string, by comparing each Rune

  • left — Buffer that points to a UTF-8 encoded string
  • right — String that contains a UTF-16 encoded string

Returns: true if they both contain the same sequences of runes (they are "equal"), or false if they are different, or left is an invalid UTF-8 sequence

This method does not normalize either of the arguments. The caller must ensure that they both are in the canonical form (by calling , or only using "safe" constants)

static bool Equals(ReadOnlySpan<byte> left, ReadOnlySpan<char> right)

Tests if a buffer containing a UTF-8 string is equal to a UTF-16 string, by comparing each Rune

  • left — Buffer that points to a UTF-8 encoded string
  • right — Buffer that points to a UTF-16 encoded string

Returns: true if they both contain the same sequences of runes (they are "equal"), or false if they are different, or left is an invalid UTF-8 sequence

This method does not normalize either of the arguments. The caller must ensure that they both are in the canonical form (by calling , or only using "safe" constants)

static bool Equals(Slice left, ReadOnlySpan<char> right)

GetBuffer

static Slice GetBuffer(string text)

GetByteCount

static int GetByteCount(string text)

Compute the size (in bytes) required to encode a string into UTF-8

  • text — String that we want to encode

Returns: Number of bytes required to encode the string in UTF-8

static int GetByteCount(Char* chars, int length)

Compute the size (in bytes) required to encode a string into UTF-8

  • chars — Pointer to the start of the string
  • length — Length (in characters) of the string

Returns: Number of bytes required to encode the string in UTF-8

static int GetByteCount(string text, int offset, int count)

Compute the size (in bytes) required to encode a section of a string into UTF-8

  • text — Complete string
  • offset — Offset of the first character in the string
  • count — Number of characters in the string

Returns: Number of bytes required to encode the substring in UTF-8

static int GetByteCount(char[] chars, int offset, int count)

Compute the size (in bytes) required to encode a section of a string into UTF-8

  • chars — Buffer containing the characters
  • offset — Offset of the first character in the string
  • count — Number of characters in the string

Returns: Number of bytes required to encode the substring in UTF-8

GetBytes

static byte[] GetBytes(string text)

static byte[] GetBytes(ReadOnlySpan<char> text)

GetMaxByteCount

static int GetMaxByteCount(int length)

Compute the maximum size (in bytes) required for a string of a specific length in the worst case scenario

TryDecodeCodePoint

static bool TryDecodeCodePoint(ReadOnlySpan<byte> buffer, out UnicodeCodePoint codePoint, out int length)

static bool TryDecodeCodePoint(Slice buffer, out UnicodeCodePoint codePoint, out int length)

static bool TryDecodeCodePoint(Byte* buffer, Byte* stop, out UnicodeCodePoint codePoint, out int length)

static bool TryDecodeCodePoint(byte[] buffer, int offset, int count, out UnicodeCodePoint codePoint, out int length)

TryEncodeCodePoint

static bool TryEncodeCodePoint(UnicodeCodePoint cp, ref byte buffer, int capacity, out int length)

static bool TryEncodeCodePoint(uint cp, ref byte buffer, int capacity, out int length)

TryGetCodePointLength

static bool TryGetCodePointLength(Byte* buffer, Byte* stop, out int length)

TryGetFirstByteCodePointValue

static bool TryGetFirstByteCodePointValue(Byte* buffer, Byte* stop, out UnicodeCodePoint cp, out int length)

static bool TryGetFirstByteCodePointValue(byte first, int count, out UnicodeCodePoint cp, out int length)

TryGetLength

static bool TryGetLength(Slice buffer, out int length)

Computes the length a UTF-8 encoded buffer

  • buffer — Bytes of the UTF-8 encoded string
  • length — Receives the length (in codepoints) of the string

Returns: True if the buffer contains a valid UTF-8 encoded string.

static bool TryGetLength(Byte* buffer, Byte* stop, out int length)

Computes the length a UTF-8 encoded buffer

  • buffer — Pointer to the start of the buffer
  • stop — Pointer to the next byte after the end of the buffer
  • length — Receives the length (in codepoints) of the string

Returns: True if the buffer contains a valid UTF-8 encoded string.

TryGetLengthAndHashCode

static bool TryGetLengthAndHashCode(Byte* buffer, Byte* stop, out int length, out int hashCode)

Computes the length and hashcode of a UTF-8 encoded buffer

  • buffer — Pointer to the start of the buffer
  • stop — Pointer to the next byte after the end of the buffer
  • length — Receives the length (in codepoints) of the string
  • hashCode — Receives the hashcode of the string

Returns: True if the buffer contains a valid UTF-8 encoded string.

TryValidateFirstByteCodePointValue

static bool TryValidateFirstByteCodePointValue(Byte* buffer, Byte* stop, out int length)

TryWriteCodePoint

static bool TryWriteCodePoint(ref SliceWriter writer, UnicodeCodePoint cp)

static bool TryWriteCodePoint(Span<byte> destination, UnicodeCodePoint cp, out int bytesWritten)

static bool TryWriteCodePoint(Span<byte> destination, char value, out int bytesWritten)

Fields

Encoding

static readonly UTF8Encoding Encoding