public void GetTextElementEnumerator_Invalid() { AssertExtensions.Throws <ArgumentNullException>("str", () => StringInfo.GetTextElementEnumerator(null)); // Str is null AssertExtensions.Throws <ArgumentNullException>("str", () => StringInfo.GetTextElementEnumerator(null, 0)); // Str is null AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => StringInfo.GetTextElementEnumerator("abc", -1)); // Index < 0 AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => StringInfo.GetTextElementEnumerator("abc", 4)); // Index > str.Length }
public static IEnumerable <string> GetTextElementEnumerable(this string self) { var e = StringInfo.GetTextElementEnumerator(self); while (e.MoveNext()) { yield return((string)e.Current); } }
public static IEnumerable <string> ToTextElements(this string source) { var e = StringInfo.GetTextElementEnumerator(source); while (e.MoveNext()) { yield return(e.GetTextElement()); } }
static IEnumerable <string> GetTextElements(string s) { var text = StringInfo.GetTextElementEnumerator(s); while (text.MoveNext()) { yield return(text.GetTextElement()); } }
/// <summary> /// Separates a string into its grapheme clusters (or characters) /// </summary> /// <param name="value">The value to separate</param> private IEnumerable <string> GetGraphemeClusters(string value) { var enumerator = StringInfo.GetTextElementEnumerator(value); while (enumerator.MoveNext()) { yield return((string)enumerator.Current); } }
public void AccessingMembersBeforeEnumeration_ThrowsInvalidOperationException() { TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator("abc"); // Cannot access Current, ElementIndex or GetTextElement() before the enumerator has started Assert.Throws <InvalidOperationException>(() => enumerator.Current); Assert.Throws <InvalidOperationException>(() => enumerator.ElementIndex); Assert.Throws <InvalidOperationException>(() => enumerator.GetTextElement()); }
public void TestNullReference() { string str = null; Assert.Throws <ArgumentNullException>(() => { TextElementEnumerator TextElementEnumerator = StringInfo.GetTextElementEnumerator(str, 0); }); }
public static IEnumerable <String> AsTextElementEnumerable(this String s) { var enumerator = StringInfo.GetTextElementEnumerator(s); while (enumerator.MoveNext()) { yield return(enumerator.GetTextElement()); } }
public static IEnumerable <string> GetChars(this string s) { var enumerator = StringInfo.GetTextElementEnumerator(s); while (enumerator.MoveNext()) { yield return((string)enumerator.Current); } }
[TestCase("あえかァィ")] // 日文平假名和片假名 public void Should_IsCJK_Return_False_For_Non_CJK_Ideographs(string s) { var charEnum = StringInfo.GetTextElementEnumerator(s); while (charEnum.MoveNext()) { Assert.IsFalse(charEnum.GetTextElement().IsCJK()); } }
private static IEnumerable <string> EnumerateTextElements(this string s) { var enumerator = StringInfo.GetTextElementEnumerator(s); while (enumerator.MoveNext()) { yield return(enumerator.GetTextElement()); } }
private static IEnumerable <string> ExtractGraphemeClusters(string text) { var enumerator = StringInfo.GetTextElementEnumerator(text); while (enumerator.MoveNext()) { yield return((string)enumerator.Current); } }
public void TestNegativeIndex() { string str = "dur8&p!"; Assert.Throws <ArgumentOutOfRangeException>(() => { TextElementEnumerator TextElementEnumerator = StringInfo.GetTextElementEnumerator(str, -10); }); }
private static IEnumerable <string> GraphemeClusters(this string s) { TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(s); while (enumerator.MoveNext()) { yield return((string)enumerator.Current); } }
public static IEnumerable <string> Split(this string value, int desiredLength) { var characters = StringInfo.GetTextElementEnumerator(value); while (characters.MoveNext()) { yield return(String.Concat(Take(characters, desiredLength))); } }
public void Write(string code, bool allowSplit) { var characterEnum = StringInfo.GetTextElementEnumerator(code); while (characterEnum.MoveNext()) { string c = characterEnum.GetTextElement(); WriteUnicode(c, allowSplit); } }
public static void Main(String[] args) { var nfcSource = source.Normalize(NormalizationForm.FormC); var enumerator = StringInfo.GetTextElementEnumerator(nfcSource); var graphemes = nfcSource.AsTextElementEnumerable().ToArray(); foreach (var lexeme in GetLexemes(graphemes)) { } }
private static IEnumerable <string> TextElementCore(string input) { TextElementEnumerator elementEnumerator = StringInfo.GetTextElementEnumerator(input); while (elementEnumerator.MoveNext()) { string textElement = elementEnumerator.GetTextElement(); yield return(textElement); } }
public void TestSurrogatePair() { string str = "\uDBFF\uDFFF"; TextElementEnumerator TextElementEnumerator = StringInfo.GetTextElementEnumerator("s\uDBFF\uDFFF$", 1); TextElementEnumerator.MoveNext(); Assert.Equal(str, TextElementEnumerator.Current.ToString()); TextElementEnumerator.MoveNext(); Assert.Equal("$", TextElementEnumerator.Current.ToString()); }
public static IEnumerable <string> GetGraphemes(string text) { var enumerator = StringInfo.GetTextElementEnumerator(text); enumerator.Reset(); while (enumerator.MoveNext()) { yield return((string)enumerator.Current); } }
/// <summary> /// 文字列を文字単位(サロゲートペア等を考慮して)で列挙する /// </summary> /// <param name="s">this 文字列</param> /// <returns>1文字毎の列挙</returns> public static IEnumerable <string> TextElements(this string s) { var tee = StringInfo.GetTextElementEnumerator(s); tee.Reset(); while (tee.MoveNext()) { yield return(tee.GetTextElement()); } }
public static string UnicodeSafeSubstring(this string str, int startIndex, int length) { if (str == null) { throw new ArgumentNullException("str"); } if (startIndex < 0 || startIndex > str.Length) { throw new ArgumentOutOfRangeException("startIndex"); } if (length < 0) { throw new ArgumentOutOfRangeException("length"); } if (startIndex + length > str.Length) { throw new ArgumentOutOfRangeException("length"); } if (length == 0) { return(string.Empty); } var sb = new StringBuilder(length); int end = startIndex + length; var enumerator = StringInfo.GetTextElementEnumerator(str, startIndex); while (enumerator.MoveNext()) { string grapheme = enumerator.GetTextElement(); startIndex += grapheme.Length; if (startIndex > length) { break; } // Skip initial Low Surrogates/Combining Marks if (sb.Length == 0) { if (char.IsLowSurrogate(grapheme[0])) { continue; } UnicodeCategory cat = char.GetUnicodeCategory(grapheme, 0); if (cat == UnicodeCategory.NonSpacingMark || cat == UnicodeCategory.SpacingCombiningMark || cat == UnicodeCategory.EnclosingMark) { continue; } } sb.Append(grapheme); if (startIndex == length) { break; } } return(sb.ToString()); }
public static IEnumerable <string> Split(this string value, int desiredLength) { var characters = StringInfo.GetTextElementEnumerator(value); string v; do { v = string.Concat(characters.AsEnumerable <string>().Take(desiredLength)); yield return(v); }while (!string.IsNullOrEmpty(v)); }
private static string ReverseText(string text) { TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(text); List<string> elements = new List<string>(); while (enumerator.MoveNext()) { elements.Add(enumerator.GetTextElement()); } elements.Reverse(); return string.Concat(elements); }
private static string[] ExtractElements(string str) { List <string> elements = new List <string>(); TextElementEnumerator tEnum = StringInfo.GetTextElementEnumerator(str); while (tEnum.MoveNext()) { elements.Add(tEnum.GetTextElement()); } return(elements.Where(s => !CJKV.Contains(s)).ToArray()); }
static void EnumTextElements(string s) { string output = string.Empty; TextElementEnumerator charEnum = StringInfo.GetTextElementEnumerator(s); while (charEnum.MoveNext()) { output += string.Format("Character at index{0} is '{1}' {2}", charEnum.ElementIndex, charEnum.GetTextElement(), Environment.NewLine); } MessageBox.Show(output, "Result of GetTextElementEnumerator"); }
/// <summary> /// Utility method to convert plain string to Unicode elements. /// </summary> public static IList <string> ToUnicodeElements(string lyric) { var result = new List <string>(); var etor = StringInfo.GetTextElementEnumerator(lyric); while (etor.MoveNext()) { result.Add(etor.GetTextElement()); } return(result); }
public void TestCombiningCharacter() { string str = "a\u20D1"; TextElementEnumerator TextElementEnumerator = StringInfo.GetTextElementEnumerator("13229^a\u20D1a", 6); TextElementEnumerator.MoveNext(); Assert.Equal(str, TextElementEnumerator.Current.ToString()); TextElementEnumerator.MoveNext(); Assert.Equal("a", TextElementEnumerator.Current.ToString()); Assert.False(TextElementEnumerator.MoveNext()); }
public static string[] ToTextElements(string str) { List <string> elements = new List <string>(); TextElementEnumerator textEnum = StringInfo.GetTextElementEnumerator(str); while (textEnum.MoveNext()) { elements.Add(textEnum.GetTextElement()); } return(elements.ToArray()); }
public static string Random(this string str) { var chars = new List <string>(); var strElements = StringInfo.GetTextElementEnumerator(str); while (strElements.MoveNext()) { chars.Add(strElements.GetTextElement()); } return(chars[rand.Next(chars.Count)]); }