public static int CharToUtf8(char character, ref FourBytes utf8) { var codepoint = (ushort)character; if (codepoint <= 0x7f) { utf8.B0 = (byte)codepoint; return 1; } if (codepoint <= 0x7ff) { utf8.B0 = (byte)(0xC0 | ((codepoint & 0x07C0) >> 6)); utf8.B1 = (byte)(0x80 | (codepoint & 0x3F)); return 2; } if (!char.IsSurrogate(character)) { utf8.B0 = (byte)(0xE0 | ((codepoint & 0xF000) >> 12)); utf8.B1 = (byte)(0x80 | ((codepoint & 0x0FC0) >> 6)); utf8.B2 = (byte)(0x80 | (codepoint & 0x003F)); return 3; } utf8.B3 = 0; throw new NotImplementedException(); }
public static int CharToUtf8(char c, ref FourBytes utf8) { var codepoint = (ushort)c; if (codepoint <= 0x7f) { utf8.B0 = (byte)codepoint; return(1); } if (codepoint <= 0x7ff) { utf8.B0 = (byte)(0xC0 | ((codepoint & 0x07C0) >> 6)); utf8.B1 = (byte)(0x80 | (codepoint & 0x3F)); return(2); } if (!char.IsSurrogate(c)) { utf8.B0 = (byte)(0xE0 | ((codepoint & 0xF000) >> 12)); utf8.B1 = (byte)(0x80 | ((codepoint & 0x0FC0) >> 6)); utf8.B2 = (byte)(0x80 | (codepoint & 0x003F)); return(3); } utf8.B3 = 0; throw new NotImplementedException(); }
public static bool TryFormat(this char value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten) { if (formattingData.IsUtf16) { if (buffer.Length < 2) { bytesWritten = 0; return(false); } buffer[0] = (byte)value; buffer[1] = (byte)(value >> 8); bytesWritten = 2; return(true); } if (buffer.Length < 1) { bytesWritten = 0; return(false); } // fast path for ASCII if (value <= 127) { buffer[0] = (byte)value; bytesWritten = 1; return(true); } var encoded = new FourBytes(); bytesWritten = Utf8Encoder.CharToUtf8(value, ref encoded); if (buffer.Length < bytesWritten) { bytesWritten = 0; return(false); } buffer[0] = encoded.B0; if (bytesWritten > 1) { buffer[1] = encoded.B1; } if (bytesWritten > 2) { buffer[2] = encoded.B2; } if (bytesWritten > 3) { buffer[3] = encoded.B3; } return(true); }
public static void TestReinterpret() { { TestBlah zero1 = new TestBlah() { a = 0, b = 0, c = 0 }; Vector3 zero2 = Unsafe.Reinterpret <TestBlah, Vector3>(zero1); zero2.ShouldBe <Vector3>(Vector3.zero); zero2.ShouldEqual(Vector3.zero); TestBlah yeet = new TestBlah() { a = 123, b = 456, c = 789 }; Vector3 yeah = Unsafe.Reinterpret <TestBlah, Vector3>(yeet); yeah.ShouldBe <Vector3>(new Vector3(123, 456, 789)); yeah.ShouldEqual(new Vector3(123, 456, 789)); } { int IEEEonePointOh = 0x3F80_0000; float onePointOhEff = 1.0f; int IEEEthreePointFive = 0x4060_0000; float threePointFiveEff = 3.5f; Unsafe.Reinterpret <int, float>(IEEEonePointOh).ShouldBe(1.0f); Unsafe.Reinterpret <int, float>(IEEEthreePointFive).ShouldBe(3.5f); Unsafe.Reinterpret <float, int>(onePointOhEff).ShouldBe(0x3F80_0000); Unsafe.Reinterpret <float, int>(threePointFiveEff).ShouldBe(0x4060_0000); } { int it = (unchecked ((int)0xDEADBEEF)); FourBytes f = Unsafe.Reinterpret <int, FourBytes>(it); if (!BitConverter.IsLittleEndian) { f.a.ShouldBe(0xDE); f.b.ShouldBe(0xAD); f.c.ShouldBe(0xBE); f.d.ShouldBe(0xEF); } else { f.d.ShouldBe(0xDE); f.c.ShouldBe(0xAD); f.b.ShouldBe(0xBE); f.a.ShouldBe(0xEF); } } }
public static char Utf8ToChar(ref FourBytes utf8) { throw new NotImplementedException(); }
public static bool TryFormat(this string value, Span <byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten) { if (formattingData.IsUtf16) { var valueBytes = value.Length << 1; if (valueBytes > buffer.Length) { bytesWritten = 0; return(false); } unsafe { fixed(char *pCharacters = value) { byte *pBytes = (byte *)pCharacters; buffer.Set(pBytes, valueBytes); } } bytesWritten = valueBytes; return(true); } GCHandle handle; var byteSpan = buffer.Pin(out handle); try { var avaliableBytes = byteSpan.Length; bytesWritten = 0; for (int i = 0; i < value.Length; i++) { var c = value[i]; var codepoint = (ushort)c; if (codepoint <= 0x7f) // this if block just optimizes for ascii { if (bytesWritten + 1 > avaliableBytes) { bytesWritten = 0; return(false); } byteSpan[bytesWritten++] = (byte)codepoint; } else { var encoded = new FourBytes(); var bytes = Utf8Encoder.CharToUtf8(c, ref encoded); if (bytesWritten + bytes > avaliableBytes) { bytesWritten = 0; return(false); } byteSpan[bytesWritten] = encoded.B0; if (bytes > 1) { byteSpan[bytesWritten + 1] = encoded.B1; if (bytes > 2) { byteSpan[bytesWritten + 2] = encoded.B2; if (bytes > 3) { byteSpan[bytesWritten + 3] = encoded.B3; } } } bytesWritten += bytes; } } return(true); } finally { handle.Free(); } }