public void TestDecodeRune() { foreach (var rm in runeMap) { var buffer = rm.Bytes; (var rune, var size) = Utf8.DecodeRune(buffer); Assert.AreEqual(rune, rm.Rune, "Decoding rune 0x{0:x} got 0x{1:x}", rm.Rune, rune); Assert.AreEqual(rm.Bytes.Length, size, "Decoding rune size for 0x{0:x} got 0x{1:x}", rm.Bytes.Length, size); (rune, size) = Utf8.DecodeRune(ustring.Make(rm.Bytes)); Assert.AreEqual(rune, rm.Rune, "Decoding rune from string 0x{0:x} got 0x{1:x}", rm.Rune, rune); Assert.AreEqual(rm.Bytes.Length, size, "Decoding rune size for 0x{0:x} got 0x{1:x}", rm.Bytes.Length, size); // Add trailing zero; var buffer2 = new byte [rm.Bytes.Length + 1]; Array.Copy(buffer, buffer2, rm.Bytes.Length); (rune, size) = Utf8.DecodeRune(buffer2); Assert.AreEqual(rune, rm.Rune, "Decoding rune 0x{0:x} got 0x{1:x}", rm.Rune, rune); Assert.AreEqual(rm.Bytes.Length, size, "Decoding rune size for 0x{0:x} got 0x{1:x}", rm.Bytes.Length, size); (rune, size) = Utf8.DecodeRune(ustring.Make(buffer2)); Assert.AreEqual(rune, rm.Rune, "Decoding rune from string 0x{0:x} got 0x{1:x}", rm.Rune, rune); Assert.AreEqual(rm.Bytes.Length, size, "Decoding rune size for 0x{0:x} got 0x{1:x}", rm.Bytes.Length, size); // Tru removing one byte var wantsize = 1; if (wantsize >= rm.Bytes.Length) { wantsize = 0; } var buffer3 = new byte [rm.Bytes.Length - 1]; Array.Copy(buffer, buffer3, buffer3.Length); (rune, size) = Utf8.DecodeRune(buffer3); Assert.AreEqual(rune, Utf8.RuneError, "Expected an error for short buffer in 0x{0:0}", rm.Rune); Assert.AreEqual(wantsize, size, "Expected {0}={1} for 0x{2}", wantsize, size, rm.Rune); // Make sure bad sequences fail var buffer4 = (byte [])rm.Bytes.Clone(); if (buffer4.Length == 1) { buffer4 [0] = 0x80; } else { buffer4 [buffer4.Length - 1] = 0x7f; } (rune, size) = Utf8.DecodeRune(buffer4); Assert.AreEqual(rune, Utf8.RuneError, "Expected malformed buffer to return an error for rune 0x{0:x}", rm.Rune); Assert.AreEqual(1, size, "Expected malformed buffer to return size 1"); (rune, size) = Utf8.DecodeRune(ustring.Make(buffer4)); Assert.AreEqual(rune, Utf8.RuneError, "Expected malformed buffer to return an error"); Assert.AreEqual(1, size, "Expected malformed buffer to return size 1"); } }
public void TestDecodeSurrogateRune() { foreach (var rm in surrogateMap) { (var rune, var size) = Utf8.DecodeRune(rm.Bytes); Assert.AreEqual(rune, Utf8.RuneError); Assert.AreEqual(1, size); (rune, size) = Utf8.DecodeRune(ustring.Make(rm.Bytes)); Assert.AreEqual(rune, Utf8.RuneError); Assert.AreEqual(1, size); } }
void RenderUstr(ustring ustr, int col, int line, int width) { int byteLen = ustr.Length; int used = 0; for (int i = 0; i < byteLen;) { (var rune, var size) = Utf8.DecodeRune(ustr, i, i - byteLen); var count = Rune.ColumnWidth(rune); if (used + count >= width) { break; } Driver.AddRune(rune); used += count; i += size; } for (; used < width; used++) { Driver.AddRune(' '); } }
// A slightly adapted method from gui.cs: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461 private void RenderUstr(ConsoleDriver driver, ustring ustr, int col, int line, int width) { int used = 0; int index = 0; while (index < ustr.Length) { (var rune, var size) = Utf8.DecodeRune(ustr, index, index - ustr.Length); var count = Rune.ColumnWidth(rune); if (used + count > width) { break; } driver.AddRune(rune); used += count; index += size; } while (used < width) { driver.AddRune(' '); used++; } }
void RenderLine(ConsoleDriver driver, IHasStats toRender, int col, int line, int width) { string suffix = " "; //Get relation to player if (toRender is IActor a) { var world = a.CurrentLocation.World; var relationship = world.Relationships.SumBetween(a, world.Player); if (relationship < -40) { suffix = "---"; } else if (relationship < -20) { suffix = "-- "; } else if (relationship < 0) { suffix = "- "; } else if (relationship > 40) { suffix = "+++"; } else if (relationship > 20) { suffix = "++ "; } else if (relationship > 0) { suffix = "+ "; } } //allow for the suffix var ustring = toRender.ToString(); ustring = ustring.Substring(0, Math.Min(ustring.Length, width - 3)) + suffix; ustring = ustring.PadRight(width); int byteLen = ustring.Length; int used = 0; for (int i = 0; i < byteLen;) { (var rune, var size) = Utf8.DecodeRune(ustring, i, i - byteLen); var count = Rune.ColumnWidth(rune); if (used + count >= width) { break; } if (rune == '-') { driver.SetAttribute(_red); } else if (rune == '+') { driver.SetAttribute(_green); } else { driver.SetAttribute(_normal); } driver.AddRune(rune); used += count; i += size; } for (; used < width; used++) { driver.AddRune(' '); } }