public static TryEncodeCodePoint ( UnicodeCodePoint codePoint, Span |
||
codePoint | UnicodeCodePoint | |
buffer | Span |
|
encodedBytes | int | |
Результат | bool |
public unsafe Utf8EncodedCodePoint(char highSurrogate, char lowSurrogate) : this() { UnicodeCodePoint codePoint = (UnicodeCodePoint)(uint)char.ConvertToUtf32(highSurrogate, lowSurrogate); fixed(byte *encodedData = &_byte0) { Span <byte> buffer = new Span <byte>(encodedData, 4); if (!Utf8Encoder.TryEncodeCodePoint(codePoint, buffer, out _length)) { // TODO: Change exception type throw new Exception("Internal error: this should never happen as codePoint should be within acceptable range"); } } }
// TODO: reevaluate implementation public Utf8String(IEnumerable <UnicodeCodePoint> codePoints) { int len = GetUtf8LengthInBytes(codePoints); var newSpan = new Span <byte>(new byte[len]); _buffer = newSpan; foreach (UnicodeCodePoint codePoint in codePoints) { int encodedBytes; if (!Utf8Encoder.TryEncodeCodePoint(codePoint, newSpan, out encodedBytes)) { throw new ArgumentException("Invalid code point", "codePoints"); } newSpan = newSpan.Slice(encodedBytes); } }
// TODO: reevaluate implementation public Utf8String(IEnumerable <uint> codePoints) { var len = GetUtf8LengthInBytes(codePoints); var buffer = new Span <byte>(new byte[len]); var index = 0; foreach (uint codePoint in codePoints) { int written; if (!Utf8Encoder.TryEncodeCodePoint(codePoint, buffer, index, out written)) { throw new ArgumentException("Invalid code point", "codePoints"); } index += written; } _buffer = buffer; }
public unsafe Utf8EncodedCodePoint(char character) : this() { if (char.IsSurrogate(character)) { throw new ArgumentOutOfRangeException("character", "Surrogate characters are not allowed"); } UnicodeCodePoint codePoint = (UnicodeCodePoint)(uint)character; fixed(byte *encodedData = &_byte0) { ByteSpan buffer = new ByteSpan(encodedData, 4); if (!Utf8Encoder.TryEncodeCodePoint(codePoint, buffer, out _length)) { // TODO: Change exception type throw new Exception("Internal error: this should never happen as codePoint is within acceptable range and is not surrogate"); } } }
// TODO: This should return Utf16CodeUnits which should wrap byte[]/Span<byte>, same for other encoders private static byte[] GetUtf8BytesFromString(string s) { int len = 0; for (int i = 0; i < s.Length; /* intentionally no increment */) { UnicodeCodePoint codePoint; int encodedChars; if (!Utf16LittleEndianEncoder.TryDecodeCodePointFromString(s, i, out codePoint, out encodedChars)) { throw new ArgumentException("s", "Invalid surrogate pair in the string."); } if (encodedChars <= 0) { // TODO: Fix exception type throw new Exception("internal error"); } int encodedBytes = Utf8Encoder.GetNumberOfEncodedBytes(codePoint); if (encodedBytes == 0) { // TODO: Fix exception type throw new Exception("Internal error: Utf16Decoder somehow got CodePoint out of range"); } len += encodedBytes; i += encodedChars; } byte[] bytes = new byte[len]; unsafe { fixed(byte *array_pinned = bytes) { Span <byte> p = new Span <byte>(array_pinned, len); for (int i = 0; i < s.Length; /* intentionally no increment */) { UnicodeCodePoint codePoint; int encodedChars; if (Utf16LittleEndianEncoder.TryDecodeCodePointFromString(s, i, out codePoint, out encodedChars)) { i += encodedChars; int encodedBytes; if (Utf8Encoder.TryEncodeCodePoint(codePoint, p, out encodedBytes)) { p = p.Slice(encodedBytes); } else { // TODO: Fix exception type throw new Exception("Internal error: Utf16Decoder somehow got CodePoint out of range or the buffer is too small"); } } else { // TODO: Fix exception type throw new Exception("Internal error: we did pre-validation of the string, nothing should go wrong"); } } } } return(bytes); }