public static bool ContainsEmoji(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(false);
            }
            TextElementEnumerator elementEnumerator = StringInfo.GetTextElementEnumerator(str);
            bool flag1 = elementEnumerator.MoveNext();

            while (flag1)
            {
                string textElement1 = elementEnumerator.GetTextElement();
                string hexString1   = BrowserNavigationService.ConvertToHexString(Encoding.BigEndianUnicode.GetBytes(textElement1));
                if (hexString1 == "")
                {
                    flag1 = elementEnumerator.MoveNext();
                }
                else
                {
                    bool flag2        = true;
                    bool flag3        = BrowserNavigationService._flagsPrefixes.Contains(hexString1);
                    int  elementIndex = elementEnumerator.ElementIndex;
                    if ((flag3 || BrowserNavigationService._modificatableSmiles.Contains(hexString1)) && elementEnumerator.MoveNext())
                    {
                        string textElement2 = elementEnumerator.GetTextElement();
                        string hexString2   = BrowserNavigationService.ConvertToHexString(Encoding.BigEndianUnicode.GetBytes(textElement2));
                        if (hexString2 == "" && elementEnumerator.MoveNext())
                        {
                            textElement2 = elementEnumerator.GetTextElement();
                            hexString2   = BrowserNavigationService.ConvertToHexString(Encoding.BigEndianUnicode.GetBytes(textElement2));
                        }
                        if (hexString2 != "" && (flag3 || BrowserNavigationService._smilesModificators.Contains(hexString2)))
                        {
                            flag2         = false;
                            hexString1   += hexString2;
                            textElement1 += textElement2;
                        }
                        else
                        {
                            elementEnumerator.Reset();
                            elementEnumerator.MoveNext();
                            while (elementEnumerator.ElementIndex != elementIndex)
                            {
                                elementEnumerator.MoveNext();
                            }
                        }
                    }
                    if (flag2)
                    {
                        BrowserNavigationService.CheckRelationsSmiles(ref hexString1, ref elementEnumerator, ref textElement1);
                    }
                    if (Emoji.Dict.ContainsKey(hexString1))
                    {
                        return(true);
                    }
                    flag1 = elementEnumerator.MoveNext();
                }
            }
            return(false);
        }
Example #2
0
    public static string replaceextendedchars(String s, Hashtable asciilookup)
    {
        // This StringBuilder holds the output results.
        StringBuilder sb = new StringBuilder();

        // Use the enumerator returned from GetTextElementEnumerator
        // method to examine each real character.
        TextElementEnumerator charEnum = StringInfo.GetTextElementEnumerator(s);

        while (charEnum.MoveNext())
        {
            //sb.Append(char.ConvertToUtf32(charEnum.GetTextElement(), 0).ToString());
            //sb.Append(", ");
            if (asciilookup.ContainsKey(char.ConvertToUtf32(charEnum.GetTextElement(), 0)))
            {
                sb.Append(asciilookup[char.ConvertToUtf32(charEnum.GetTextElement(), 0)]);
            }
            else
            {
                sb.Append(charEnum.GetTextElement());
            }
        }

        return(sb.ToString());
    }
Example #3
0
        /// <summary>
        /// Reads a text-element from the input and moves forward. This also
        /// converts '\r\n' to '\n' and changes the Position and Line.
        /// </summary>
        /// <returns>The text-element that was read or null if at the end.</returns>
        protected virtual string ReadElement()
        {
            string ret = null;

            try
            {
                ret = input.GetTextElement();
                input.MoveNext();
            }
            catch (Exception) { }

            if (ret == null)
            {
                return(null);
            }

            Position += ret.Length;
            if (ret == "\r" || ret == "\n")
            {
                if (ret == "\r")
                {
                    string temp = PeekElement();
                    if (temp == "\n")
                    {
                        input.MoveNext();
                    }
                    ret = "\n";
                }
                Line++;
                Position = 1;
            }
            return(ret);
        }
    public bool NegTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("NegTest2: The enumerator is positioned  after the last text element.");
        try
        {
            String myString = "\uD800\uDC00\u0061\u0300\u00C6";

            // Creates and initializes a TextElementEnumerator for myString.
            TextElementEnumerator myTEE = StringInfo.GetTextElementEnumerator(myString);
            myTEE.Reset();
            string acutalValue = null;
            while (myTEE.MoveNext())
            {
                acutalValue = myTEE.GetTextElement();
            }
            acutalValue = myTEE.GetTextElement();
            TestLibrary.TestFramework.LogError("102.1", " InvalidOperationException should be caught.");
            retVal = false;
        }
        catch (InvalidOperationException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("102.0", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
Example #5
0
        private static List <string> GenerateIndividualMessages(string content)
        {
            List <string>         allMessages   = new();
            int                   numBytes      = 0;
            StringBuilder         stringBuilder = new();
            StringBuilder         wordBuilder   = new();
            TextElementEnumerator te            = StringInfo.GetTextElementEnumerator(content);

            while (te.MoveNext())
            {
                string teString   = te.GetTextElement();
                int    teNumBytes = teString.EnumerateRunes().Sum(x => x.Utf8SequenceLength);
                numBytes += teNumBytes;
                if (numBytes >= 2000)
                {
                    allMessages.Add(stringBuilder.ToString());
                    stringBuilder.Clear();
                    numBytes = teNumBytes;
                }
                wordBuilder.Append(teString);
                if (string.IsNullOrWhiteSpace(teString))
                {
                    stringBuilder.Append(wordBuilder);
                    wordBuilder.Clear();
                }
            }

            allMessages.Add(stringBuilder.Append(wordBuilder).ToString());
            return(allMessages);
        }
Example #6
0
        public static SortedList <int, byte> ToCharVector(this string word)
        {
            var vec = new SortedList <int, byte>();
            TextElementEnumerator charEnum = StringInfo.GetTextElementEnumerator(word);

            while (charEnum.MoveNext())
            {
                var element   = charEnum.GetTextElement().ToCharArray();
                int codePoint = 0;

                foreach (var c in element)
                {
                    codePoint += c;
                }

                if (vec.ContainsKey(codePoint))
                {
                    vec[codePoint] += 1;
                }
                else
                {
                    vec[codePoint] = 1;
                }
            }
            return(vec);
        }
Example #7
0
        private void TestMultiReadingWithString()
        {
            // 讀取多音字資料檔.
            string multiReadingChars = File.ReadAllText(lblMultiReadingFileName.Text);

            DateTime startTime         = DateTime.Now;
            int      multiReadingCount = 0;

            using (StreamReader sr = new StreamReader(txtInFileName.Text))
            {
                string line = sr.ReadLine();
                while (line != null)
                {
                    TextElementEnumerator itor = StringInfo.GetTextElementEnumerator(line);
                    while (itor.MoveNext())
                    {
                        string hanChar = itor.GetTextElement();
                        if (multiReadingChars.IndexOf(hanChar) >= 0)
                        {
                            multiReadingCount++;
                        }
                    }
                    line = sr.ReadLine();
                }
            }
            DateTime endTime = DateTime.Now;

            txtMsg.AppendText("\r\n判斷多音字-使用 String 類別:");
            txtMsg.AppendText("\r\n  共發現 " + multiReadingCount + " 個多音字。");
            txtMsg.AppendText("\r\n  時間: " + (endTime - startTime).ToString());
        }
Example #8
0
 public static IEnumerable <string> ToEnumerable(this TextElementEnumerator enumerator)
 {
     while (enumerator.MoveNext())
     {
         yield return(enumerator.GetTextElement());
     }
 }
Example #9
0
        private void TestMultiReadingWithPhoneticData()
        {
            // 讀取注音資料檔.
            ChinesePhoneticData phoneData = new ChinesePhoneticData();

            phoneData.LoadFromFile(lblPhoneFileName.Text);

            DateTime startTime         = DateTime.Now;
            int      multiReadingCount = 0;

            using (StreamReader sr = new StreamReader(txtInFileName.Text))
            {
                string line = sr.ReadLine();
                while (line != null)
                {
                    TextElementEnumerator itor = StringInfo.GetTextElementEnumerator(line);
                    while (itor.MoveNext())
                    {
                        string hanChar = itor.GetTextElement();
                        if (phoneData.IsMultiReading(hanChar))
                        {
                            multiReadingCount++;
                        }
                    }
                    line = sr.ReadLine();
                }
            }
            DateTime endTime = DateTime.Now;

            txtMsg.AppendText("\r\n判斷多音字-使用 Huanlin.TextServices.ChinesePhoneticData 類別:");
            txtMsg.AppendText("\r\n  共發現 " + multiReadingCount + " 個多音字。");
            txtMsg.AppendText("\r\n  時間: " + (endTime - startTime).ToString());
        }
Example #10
0
        private void WrapWord(Graphics g, Font font, float width, int index)
        {
            Word word = FWords[index];
            TextElementEnumerator charEnum = StringInfo.GetTextElementEnumerator(word.Text);
            string text = "";

            while (charEnum.MoveNext())
            {
                string textElement = charEnum.GetTextElement();
                float  textWidth   = g.MeasureString(text + textElement, font).Width;

                if (textWidth > width)
                {
                    if (text == "")
                    {
                        text = textElement;
                    }
                    Word newWord = new Word(word.Text.Substring(text.Length), word.OriginalTextIndex + text.Length);
                    word.Text = text;
                    FWords.Insert(index + 1, newWord);
                    return;
                }
                else
                {
                    text += textElement;
                }
            }
        }
        public void NegTest2()
        {
            String myString = "\uD800\uDC00\u0061\u0300\u00C6";

            // Creates and initializes a TextElementEnumerator for myString.
            TextElementEnumerator myTEE = StringInfo.GetTextElementEnumerator(myString);

            myTEE.Reset();
            string actualValue = null;

            while (myTEE.MoveNext())
            {
                actualValue = myTEE.GetTextElement();
            }
            Assert.Throws <InvalidOperationException>(() =>
            {
                actualValue = myTEE.GetTextElement();
            });
        }
        private static IEnumerable <string> TextElementCore(string input)
        {
            TextElementEnumerator elementEnumerator = StringInfo.GetTextElementEnumerator(input);

            while (elementEnumerator.MoveNext())
            {
                string textElement = elementEnumerator.GetTextElement();
                yield return(textElement);
            }
        }
Example #13
0
        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());
        }
Example #14
0
/*
 *  public static String Reverse(String str) {
 *      char[] buf = str.ToCharArray();
 *      Array.Reverse(buf);
 *      return new String(buf);
 *  }
 */

        public static String Reverse(String str)
        {
            List <String>         list       = new List <String>();
            TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(str);

            while (enumerator.MoveNext())
            {
                list.Add(enumerator.GetTextElement());
            }
            list.Reverse();
            return(String.Join("", list));
        }
Example #15
0
        /// <summary>
        /// Encodes a string into a byte array.
        /// </summary>
        /// <param name="s">The string to encode</param>
        /// <returns>The array of bytes representing the string encoding.</returns>
        /// <exception cref="ArgumentNullException">System.ArgumentNullException</exception>
        /// <exception cref="ArgumentException">System.ArgumentException</exception>
        public override byte[] GetBytes(string s)
        {
            var bytes = new List <byte>();
            TextElementEnumerator textEnumerator = StringInfo.GetTextElementEnumerator(s);

            while (textEnumerator.MoveNext())
            {
                var elem = textEnumerator.GetTextElement();
                bytes.AddRange(GetCharBytes(elem));
            }
            return(bytes.ToArray());
        }
        /// <summary>
        ///     ToTextElements
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static IEnumerable <string> ToTextElements(this string val)
        {
            if (val == null)
            {
                throw new ArgumentNullException("val");
            }
            TextElementEnumerator elementEnumerator = StringInfo.GetTextElementEnumerator(val);

            while (elementEnumerator.MoveNext())
            {
                string textElement = elementEnumerator.GetTextElement();
                yield return(textElement);
            }
        }
Example #17
0
    private static void EnumTextElements(String s)
    {
        String output = String.Empty;

        TextElementEnumerator charEnum =
            StringInfo.GetTextElementEnumerator(s);

        while (charEnum.MoveNext())
        {
            output += String.Format(
                "Text element at index {0} is '{1}'{2}",
                charEnum.ElementIndex, charEnum.GetTextElement(),
                Environment.NewLine);
        }
        MessageBox.Show(output, "Result of GetTextElementEnumerator");
    }
Example #18
0
        /// <summary> Returns the reverse of the specified string. </summary>
        /// <param name="s"> The string to reverse. </param>
        public static string Reverse(this string s)
        {
            Contract.Requires(s != null);

            var cache = new List <string>(s.Length);
            TextElementEnumerator e = StringInfo.GetTextElementEnumerator(s);

            while (e.MoveNext())
            {
                cache.Add(e.GetTextElement());
            }

            cache.Reverse();
            string result = string.Concat(cache);

            return(result);
        }
Example #19
0
    public static void EnumerateTextElements(string str)
    {
        // Get the Enumerator.
        TextElementEnumerator teEnum = null;

        // Parse the string using the ParseCombiningCharacters method.
        Console.WriteLine("\nParsing with ParseCombiningCharacters:");
        int[] teIndices = StringInfo.ParseCombiningCharacters(str);

        for (int i = 0; i < teIndices.Length; i++)
        {
            if (i < teIndices.Length - 1)
            {
                Console.WriteLine("Text Element {0} ({1}..{2})= {3}", i,
                                  teIndices[i], teIndices[i + 1] - 1,
                                  ShowHexValues(str.Substring(teIndices[i], teIndices[i + 1] -
                                                              teIndices[i])));
            }
            else
            {
                Console.WriteLine("Text Element {0} ({1}..{2})= {3}", i,
                                  teIndices[i], str.Length - 1,
                                  ShowHexValues(str.Substring(teIndices[i])));
            }
        }
        Console.WriteLine();

        // Parse the string with the GetTextElementEnumerator method.
        Console.WriteLine("Parsing with TextElementEnumerator:");
        teEnum = StringInfo.GetTextElementEnumerator(str);

        int teCount = -1;

        while (teEnum.MoveNext())
        {
            // Displays the current element.
            // Both GetTextElement() and Current retrieve the current
            // text element. The latter returns it as an Object.
            teCount++;
            Console.WriteLine("Text Element {0} ({1}..{2})= {3}", teCount,
                              teEnum.ElementIndex, teEnum.ElementIndex +
                              teEnum.GetTextElement().Length - 1, ShowHexValues((string)(teEnum.Current)));
        }
    }