public void Sum_Of_ColumnWidth_Is_Always_Equal_To_ConsoleWidth_And_RuneCount_Is_Always_Equal_To_String_Length() { const int start = 0x000000; const int end = 0x10ffff; for (int i = start; i <= end; i++) { Rune r = new Rune((uint)i); if (!r.IsValid) { continue; } ustring us = ustring.Make(r); string hex = i.ToString("x6"); int v = int.Parse(hex, System.Globalization.NumberStyles.HexNumber); string s = char.ConvertFromUtf32(v); if (!r.IsSurrogatePair) { Assert.AreEqual(r.ToString(), us); Assert.AreEqual(us, s); Assert.AreEqual(Rune.ColumnWidth(r), us.ConsoleWidth); Assert.AreEqual(s.Sum(c => Rune.ColumnWidth(c)), us.ConsoleWidth); Assert.AreEqual(us.RuneCount, s.Length); } else { Assert.AreEqual(r.ToString(), us.ToString()); Assert.AreEqual(us.ToString(), s); Assert.AreEqual(Rune.ColumnWidth(r), us.ConsoleWidth); Assert.AreEqual(s.Sum(c => Rune.ColumnWidth(c)), us.ConsoleWidth); Assert.AreEqual(us.RuneCount, s.Length); } } }
public void Test_Range(int start, int end) { for (int i = start; i <= end; i++) { Rune r = new Rune((uint)i); ustring us = ustring.Make(r); string hex = i.ToString("x6"); int v = int.Parse(hex, System.Globalization.NumberStyles.HexNumber); string s = char.ConvertFromUtf32(v); if (!r.IsSurrogatePair) { Assert.AreEqual(r.ToString(), us); Assert.AreEqual(us, s); Assert.AreEqual(Rune.ColumnWidth(r), us.ConsoleWidth); } else { Assert.AreEqual(r.ToString(), us.ToString()); Assert.AreEqual(us.ToString(), s); Assert.AreEqual(Rune.ColumnWidth(r), us.ConsoleWidth); } Assert.AreEqual(s.Sum(c => Rune.ColumnWidth(c)), us.ConsoleWidth); Assert.AreEqual(us.RuneCount, s.Length); } }
public void TestSliceRanges() { var str = "1234567890"; ustring a = ustring.Make(str); Assert.AreEqual(str, a.ToString()); var asbyte = new byte [] { (byte)'y', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'0', (byte)'z' }; SliceTests(a); SliceTests(ustring.Make("x" + str + "x") [1, 11]); var f = ustring.Make(asbyte, 1, 10); SliceTests(f); unsafe { fixed(byte *p = &asbyte[1]) { var ptrstr = ustring.Make((IntPtr)p, 10); SliceTests(ptrstr); } } }
static void DoMessage(Button button, ustring txt) { button.Clicked = () => { var btnText = button.Text.ToString(); MessageBox.Query(30, 7, "Message", $"Did you click {txt.ToString ()}?", "Yes", "No"); }; }
bool SetText(ustring text) { if (!DateTime.TryParseExact(text.ToString(), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) { return(false); } Text = text; return(true); }
void PrintTextElementCount(ustring us, string s, int consoleWidth, int runeCount, int stringCount, int txtElementCount) { Assert.AreEqual(us.ToString(), s); Assert.AreEqual(s, us.ToString()); Assert.AreEqual(consoleWidth, us.ConsoleWidth); Assert.AreEqual(runeCount, us.RuneCount); Assert.AreEqual(stringCount, s.Length); TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(s); int textElementCount = 0; while (enumerator.MoveNext()) { textElementCount++; // For versions prior to Net5.0 the StringInfo class might handle some grapheme clusters incorrectly. } Assert.AreEqual(txtElementCount, textElementCount); }
bool SetText(ustring text) { ustring [] vals = text.Split(ustring.Make(sepChar)); bool isValidTime = true; int hour = Int32.Parse(vals [0].ToString()); int minute = Int32.Parse(vals [1].ToString()); int second = isShort ? 0 : Int32.Parse(vals [2].ToString()); if (hour < 0) { isValidTime = false; hour = 0; vals [0] = "0"; } else if (hour > 23) { isValidTime = false; hour = 23; vals [0] = "23"; } if (minute < 0) { isValidTime = false; minute = 0; vals [1] = "0"; } else if (minute > 59) { isValidTime = false; minute = 59; vals [1] = "59"; } if (second < 0) { isValidTime = false; second = 0; vals [2] = "0"; } else if (second > 59) { isValidTime = false; second = 59; vals [2] = "59"; } string time = isShort ? $" {hour,2:00}{sepChar}{minute,2:00}" : $" {hour,2:00}{sepChar}{minute,2:00}{sepChar}{second,2:00}"; Text = time; if (!DateTime.TryParseExact(text.ToString(), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) || !isValidTime) { return(false); } return(true); }
public void Test_Surrogate_Pairs_Range() { for (uint h = 0xd800; h <= 0xdbff; h++) { for (uint l = 0xdc00; l <= 0xdfff; l++) { Rune r = new Rune(h, l); ustring us = ustring.Make(r); string hex = ((uint)r).ToString("x6"); int v = int.Parse(hex, System.Globalization.NumberStyles.HexNumber); string s = char.ConvertFromUtf32(v); Assert.True(v >= 0x10000 && v <= Rune.MaxRune); Assert.AreEqual(r.ToString(), us.ToString()); Assert.AreEqual(us.ToString(), s); Assert.AreEqual(Rune.ColumnWidth(r), us.ConsoleWidth); Assert.AreEqual(s.Sum(c => Rune.ColumnWidth(c)), us.ConsoleWidth); Assert.AreEqual(us.RuneCount, s.Length); } } }
static ustring ClipAndJustify(ustring str, int width, TextAlignment talign) { // Get rid of any '\r' added by Windows str = str.Replace("\r", ustring.Empty); int slen = str.RuneCount; if (slen > width) { var uints = str.ToRunes(width); var runes = new Rune [uints.Length]; for (int i = 0; i < uints.Length; i++) { runes [i] = uints [i]; } return(ustring.Make(runes)); } else { if (talign == TextAlignment.Justified) { // TODO: ustring needs this var words = str.ToString().Split(whitespace, StringSplitOptions.RemoveEmptyEntries); int textCount = words.Sum(arg => arg.Length); var spaces = (width - textCount) / (words.Length - 1); var extras = (width - textCount) % words.Length; var s = new System.Text.StringBuilder(); //s.Append ($"tc={textCount} sp={spaces},x={extras} - "); for (int w = 0; w < words.Length; w++) { var x = words [w]; s.Append(x); if (w + 1 < words.Length) { for (int i = 0; i < spaces; i++) { s.Append(' '); } } if (extras > 0) { //s.Append ('_'); extras--; } } return(ustring.Make(s.ToString())); } return(str); } }
public static ustring SetTitleText(ustring title, ustring shortcut) { var txt = title; var split = title.ToString().Split('~'); if (split.Length > 1) { txt = split [2].Trim();; } if (string.IsNullOrEmpty(shortcut.ToString())) { return(txt); } return($"~{shortcut}~ {txt}"); }
static ustring ClipAndJustify(ustring str, int width, TextAlignment talign) { int slen = str.Length; if (slen > width) { return(str [0, width]); } else { if (talign == TextAlignment.Justified) { // TODO: ustring needs this var words = str.ToString().Split(whitespace, StringSplitOptions.RemoveEmptyEntries); int textCount = words.Sum((arg) => arg.Length); var spaces = (width - textCount) / (words.Length - 1); var extras = (width - textCount) % words.Length; var s = new System.Text.StringBuilder(); //s.Append ($"tc={textCount} sp={spaces},x={extras} - "); for (int w = 0; w < words.Length; w++) { var x = words [w]; s.Append(x); if (w + 1 < words.Length) { for (int i = 0; i < spaces; i++) { s.Append(' '); } } if (extras > 0) { s.Append('_'); extras--; } } return(ustring.Make(s.ToString())); } return(str); } }
private void FilterField_Changed(object sender, ustring e) { // TODO: remove Apply button and code when this starts working FilterData(e.ToString()); _listView.SetSource(_itemList); }
private void FilterField_Changed(object sender, ustring e) { // TODO: remove Apply button and code when this starts working try { _filterErrorLabel.Text = " "; _filterErrorLabel.ColorScheme = Colors.Base; _filterErrorLabel.Redraw(_filterErrorLabel.Bounds); var itemList = GridViewHelpers.FilterData(_itemSource.GridViewRowList, e.ToString()); _listView.Source = new GridViewDataSource(itemList); } catch (Exception ex) { _filterErrorLabel.Text = ex.Message; _filterErrorLabel.ColorScheme = Colors.Error; _filterErrorLabel.Redraw(_filterErrorLabel.Bounds); _listView.Source = _itemSource; } }
public static bool PortCheck(ustring Port) { // Don't allow more than 4 digits and Match Digits only return(Port.Length > 5 || Regex.IsMatch(Port.ToString(), "[^0-9]+")); }