Beispiel #1
0
        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
        }
Beispiel #2
0
        public static IEnumerable <string> GetTextElementEnumerable(this string self)
        {
            var e = StringInfo.GetTextElementEnumerator(self);

            while (e.MoveNext())
            {
                yield return((string)e.Current);
            }
        }
Beispiel #3
0
        public static IEnumerable <string> ToTextElements(this string source)
        {
            var e = StringInfo.GetTextElementEnumerator(source);

            while (e.MoveNext())
            {
                yield return(e.GetTextElement());
            }
        }
Beispiel #4
0
        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);
            });
        }
Beispiel #8
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());
            }
        }
Beispiel #12
0
        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);
            }
        }
Beispiel #15
0
        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);
            }
        }
Beispiel #17
0
        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());
        }
Beispiel #20
0
        public static IEnumerable <string> GetGraphemes(string text)
        {
            var enumerator = StringInfo.GetTextElementEnumerator(text);

            enumerator.Reset();
            while (enumerator.MoveNext())
            {
                yield return((string)enumerator.Current);
            }
        }
Beispiel #21
0
        /// <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());
            }
        }
Beispiel #22
0
    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());
    }
Beispiel #23
0
        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));
        }
Beispiel #24
0
 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());
        }
Beispiel #26
0
        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");
        }
Beispiel #27
0
        /// <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());
        }
Beispiel #29
0
        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());
        }
Beispiel #30
0
    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)]);
    }