예제 #1
0
        public void TestCombiningCharacter()
        {
            string str    = "a\u20D1";
            string result = StringInfo.GetNextTextElement("13229^a\u20D1abcde", 6);

            Assert.Equal(str, result);
        }
        public static object CoerceKeyTip(ISyncKeyTipAndContent syncElement,
                                          object baseValue,
                                          DependencyProperty contentProperty)
        {
            DependencyObject element = syncElement as DependencyObject;

            Debug.Assert(element != null);
            if (syncElement.KeepKeyTipAndContentInSync &&
                !syncElement.IsKeyTipSyncSource)
            {
                syncElement.KeepKeyTipAndContentInSync = false;
                if (string.IsNullOrEmpty((string)baseValue))
                {
                    string stringContent = element.GetValue(contentProperty) as string;
                    if (stringContent != null)
                    {
                        int accessIndex = RibbonHelper.FindAccessKeyMarker(stringContent);
                        if (accessIndex >= 0 && accessIndex < stringContent.Length - 1)
                        {
                            syncElement.KeepKeyTipAndContentInSync = true;
                            return(StringInfo.GetNextTextElement(stringContent, accessIndex + 1));
                        }
                    }
                }
            }
            return(baseValue);
        }
예제 #3
0
        private bool CheckOperatorName(string name)
        {
            if (String.IsNullOrEmpty(name) || Char.IsDigit(name, 0))
            {
                return(false);
            }

            string InvalidChars = "()[]{}\'\"\u0009\u000a\u000d";
            string elem;

            int n = name.Length;
            int i = 0;
            int m;

            while (i < n)
            {
                elem = StringInfo.GetNextTextElement(name, i);
                m    = elem.Length;
                if (InvalidChars.IndexOf(elem[0]) >= 0 || (Char.IsWhiteSpace(elem, 0) && elem[0] != ' ') ||
                    (elem[0] == ' ' && (m != 1 || (i + m < n && name[i + m] == ' '))))
                {
                    return(false);
                }
                i += m;
            }

            i++;
            return(true);
        }
    public bool NegTest3()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("NegTest3: The index is a negative number");

        try
        {
            string str    = "df8%^dk";
            string result = StringInfo.GetNextTextElement(str, -1);
            TestLibrary.TestFramework.LogError("105", "The ArgumentOutOfRangeException was not thrown as expected");
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("106", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return(retVal);
    }
    public bool NegTest1()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("NegTest1: The string is a null reference");

        try
        {
            string str    = null;
            string result = StringInfo.GetNextTextElement(str, 0);
            TestLibrary.TestFramework.LogError("101", "The ArgumentNullException was not thrown as expected");
            retVal = false;
        }
        catch (ArgumentNullException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("102", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return(retVal);
    }
    public bool PosTest4()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest4: The element is a combination of base character and several combining characters");

        try
        {
            string str    = "z\uFE22\u20D1\u20EB";
            string result = StringInfo.GetNextTextElement("az\uFE22\u20D1\u20EBabcde", 1);
            if (result.Length != 4)
            {
                TestLibrary.TestFramework.LogError("008", "The result is not the value as expected,length is: " + result.Length);
                retVal = false;
            }
            if (result != str)
            {
                TestLibrary.TestFramework.LogError("009", "The result is not the value as expected,the result is: " + result);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("010", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
    public bool PosTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest2: The first element is a surrogate pair");

        try
        {
            string str    = "\uDBFF\uDFFF";
            string result = StringInfo.GetNextTextElement("ef45-;\uDBFF\uDFFFabcde", 6);
            if (result.Length != 2)
            {
                TestLibrary.TestFramework.LogError("003", "The result is not the value as expected");
                retVal = false;
            }
            if (result != str)
            {
                TestLibrary.TestFramework.LogError("004", "The result is not the value as expected,the result is: " + result);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("005", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
        public void TestNonSpacingMark()
        {
            string str    = "a\u20D1";
            string result = StringInfo.GetNextTextElement("a\u20D1abcde");

            Assert.Equal(str, result);
        }
예제 #9
0
        public override bool Capitalize()
        {
            int startPosition = _startPoint.CurrentPosition;

            if (IsEmpty)
            {
                int       endPosition   = _endPoint.CurrentPosition;
                TextRange currentWord   = _startPoint.GetCurrentWord();
                string    nextCharacter = _startPoint.GetNextCharacter();
                if (_startPoint.CurrentPosition == currentWord.GetStartPoint().CurrentPosition)
                {
                    nextCharacter = nextCharacter.ToUpper(CultureInfo.CurrentCulture);
                }
                else
                {
                    nextCharacter = nextCharacter.ToLower(CultureInfo.CurrentCulture);
                }
                if (!PrimitivesUtilities.Replace(TextBuffer.AdvancedTextBuffer, new Span(_startPoint.CurrentPosition, nextCharacter.Length), nextCharacter))
                {
                    return(false);
                }
                _endPoint.MoveTo(endPosition);
            }
            else
            {
                using (ITextEdit edit = TextBuffer.AdvancedTextBuffer.CreateEdit())
                {
                    TextRange currentWord = _startPoint.GetCurrentWord();

                    // If the current word extends past this range, go to the next word
                    if (currentWord.GetStartPoint().CurrentPosition < _startPoint.CurrentPosition)
                    {
                        currentWord = currentWord.GetEndPoint().GetNextWord();
                    }

                    while (currentWord.GetStartPoint().CurrentPosition < _endPoint.CurrentPosition)
                    {
                        string wordText     = currentWord.GetText();
                        string startElement = StringInfo.GetNextTextElement(wordText);
                        wordText = startElement.ToUpper(CultureInfo.CurrentCulture) + wordText.Substring(startElement.Length).ToLower(CultureInfo.CurrentCulture);
                        if (!edit.Replace(currentWord.AdvancedTextRange.Span, wordText))
                        {
                            edit.Cancel();
                            return(false);
                        }

                        currentWord = currentWord.GetEndPoint().GetNextWord();
                    }

                    edit.Apply();

                    if (edit.Canceled)
                    {
                        return(false);
                    }
                }
            }
            _startPoint.MoveTo(startPosition);
            return(true);
        }
        public override string GetPreviousCharacter()
        {
            ITextSnapshot snapshot        = _textBuffer.AdvancedTextBuffer.CurrentSnapshot;
            int           currentPosition = CurrentPosition;

            if (currentPosition == 0)
            {
                return(string.Empty);
            }

            int    index         = currentPosition - 1;
            string characterText = char.ToString(snapshot[index]);

            while (--index >= 0)
            {
                string newText = snapshot[index] + characterText;
                if (StringInfo.GetNextTextElement(newText).Length <= characterText.Length)
                {
                    break;
                }

                characterText = newText;
            }

            return(characterText);
        }
예제 #11
0
        private static GraceObject mAt(
            EvaluationContext ctx,
            GraceString self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceNumber>();

            if (oth == null)
            {
                return(GraceString.Create("bad index"));
            }
            int idx = oth.GetInt() - 1;

            if (idx >= self.graphemeIndices.Length || idx < 0)
            {
                ErrorReporting.RaiseError(ctx, "R2013",
                                          new Dictionary <string, string> {
                    { "index", "" + (idx + 1) },
                    { "valid", self.graphemeIndices.Length > 0 ?
                      "1 .. " + self.graphemeIndices.Length
                                : "none (empty)" }
                }, "Index must be a number");
            }
            int start = self.graphemeIndices[idx];

            return(GraceString.Create(
                       StringInfo.GetNextTextElement(self.Value, start)));
        }
    public bool PosTest1()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest1: Get the text element from a random index in a string");

        try
        {
            string str    = TestLibrary.Generator.GetString(-55, true, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            int    index  = this.GetInt32(8, str.Length);
            string result = StringInfo.GetNextTextElement(str, index);
            if (result != str[index].ToString())
            {
                TestLibrary.TestFramework.LogError("001", "The result is not the value as expected,the result is: " + result);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
    public bool NegTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("NegTest2: The index is out of the range of the string");

        try
        {
            string str    = "abc";
            string result = StringInfo.GetNextTextElement(str, -4);
            TestLibrary.TestFramework.LogError("103", "The ArgumentOutOfRangeException was not thrown as expected");
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("104", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return(retVal);
    }
예제 #14
0
        public void TestSurrogatePair()
        {
            string str    = "\uDBFF\uDFFF";
            string result = StringInfo.GetNextTextElement("ef45-;\uDBFF\uDFFFabcde", 6);

            Assert.Equal(2, result.Length);
            Assert.Equal(str, result);
        }
예제 #15
0
        public void GetNextTextElement_Invalid()
        {
            AssertExtensions.Throws <ArgumentNullException>("str", () => StringInfo.GetNextTextElement(null));              // Str is null
            AssertExtensions.Throws <ArgumentNullException>("str", () => StringInfo.GetNextTextElement(null, 0));           // Str is null

            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => StringInfo.GetNextTextElement("abc", -1)); // Index < 0
            AssertExtensions.Throws <ArgumentOutOfRangeException>("index", () => StringInfo.GetNextTextElement("abc", 4));  // Index > str.Length
        }
예제 #16
0
 public void GetNextTextElement(string str, int index, string expected)
 {
     if (index == 0)
     {
         Assert.Equal(expected, StringInfo.GetNextTextElement(str));
     }
     Assert.Equal(expected, StringInfo.GetNextTextElement(str, index));
 }
예제 #17
0
        public void TestRandomIndex()
        {
            string str    = _generator.GetString(-55, true, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);
            int    index  = this.GetInt32(8, str.Length);
            string result = StringInfo.GetNextTextElement(str, index);

            Assert.Equal(str[index].ToString(), result);
        }
예제 #18
0
        public void TestCharacterCombination()
        {
            string str    = "z\uFE22\u20D1\u20EB";
            string result = StringInfo.GetNextTextElement("az\uFE22\u20D1\u20EBabcde", 1);

            Assert.Equal(4, result.Length);
            Assert.Equal(str, result);
        }
예제 #19
0
파일: Lexer.cs 프로젝트: mwh/kernan
        private Token lexCloseBracket()
        {
            int start = index;

            advanceIndex();
            UnicodeCategory cat  = validateChar();
            char            c    = code[index];
            string          cStr = StringInfo.GetNextTextElement(code, index);

            while (UnicodeLookup.CloseBrackets.Contains(cStr) ||
                   isOperatorCharacter(c, cat))
            {
                advanceIndex();
                cat  = validateChar();
                c    = code[index];
                cStr = StringInfo.GetNextTextElement(code, index);
                if (code.Substring(start, index - start) == "]]")
                {
                    return(new RGenericToken(moduleName, line, column));
                }
            }
            string bracket = code.Substring(start, index - start);

            int[]  indices   = StringInfo.ParseCombiningCharacters(bracket);
            int    lastIndex = indices[indices.Length - 1];
            string l         = StringInfo.GetNextTextElement(bracket, lastIndex);
            // For ease, any ) characters at the end of a closing bracket
            // are removed from the token.
            int sub            = 0;
            int blen           = bracket.Length;
            int graphemeOffset = indices.Length - 1;

            while (l == ")" || l == ".")
            {
                sub++;
                graphemeOffset--;
                lastIndex = indices[graphemeOffset];
                l         = StringInfo.GetNextTextElement(bracket, lastIndex);
            }
            if (sub > 0)
            {
                // Reset index to before the )s so that they will be
                // lexed as the following tokens.
                index  -= sub;
                bracket = bracket.Substring(0, bracket.Length - sub);
            }
            if (!UnicodeLookup.CloseBrackets.Contains(l))
            {
                reportError("L0009",
                            new Dictionary <string, string>
                {
                    { "char", l }
                },
                            "Invalid character at end of bracket sequence");
            }
            return(new CloseBracketToken(moduleName, line, column, bracket));
        }
예제 #20
0
        public void TestNegativeIndex()
        {
            string str = "df8%^dk";

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                string result = StringInfo.GetNextTextElement(str, -1);
            });
        }
예제 #21
0
        public void TestOutOfRangeIndex()
        {
            string str = "abc";

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                string result = StringInfo.GetNextTextElement(str, -4);
            });
        }
예제 #22
0
        public void TestNullReference()
        {
            string str = null;

            Assert.Throws <ArgumentNullException>(() =>
            {
                string result = StringInfo.GetNextTextElement(str, 0);
            });
        }
예제 #23
0
        public void GetNextTextElement()
        {
            Assert.AreEqual("A", StringInfo.GetNextTextElement("ABC", 0), "#1");
            Assert.AreEqual("C", StringInfo.GetNextTextElement("ABC", 2), "#2");
            Assert.AreEqual("A\u0330", StringInfo.GetNextTextElement("A\u0330BC", 0), "#3");
            Assert.AreEqual("B", StringInfo.GetNextTextElement("A\u0330BC", 2), "#4");

            // hmm ...
            Assert.AreEqual(String.Empty, StringInfo.GetNextTextElement("A\u0330BC", 4), "#4");
        }
예제 #24
0
        public static string SubStringByTextElements(this string str, int startIndex, int length)
        {
            string resultStr = "";

            for (int i = startIndex; i < startIndex + length; i++)
            {
                resultStr += StringInfo.GetNextTextElement(str, i);
            }
            return(resultStr);
        }
        public void GetNextTextElement()
        {
            Assert.AreEqual("A", StringInfo.GetNextTextElement("ABC", 0), "#1");
            Assert.AreEqual("C", StringInfo.GetNextTextElement("ABC", 2), "#2");
            Assert.AreEqual("A\u0330", StringInfo.GetNextTextElement("A\u0330BC", 0), "#3");
            Assert.AreEqual("B", StringInfo.GetNextTextElement("A\u0330BC", 2), "#4");

            // hmm ...
#if NET_2_0 // it causes ArgumentOutOfRangeException in 1.x, not worthy to test it anymore
            Assert.AreEqual(String.Empty, StringInfo.GetNextTextElement("A\u0330BC", 4), "#4");
#endif
        }
예제 #26
0
        /// <summary>
        /// Converts suits to ints
        /// </summary>
        /// <param name="input">stdin</param>
        /// <returns>
        /// A list of ints.
        /// </returns>
        private void ConvertToSuit(string input)
        {
            string firstChar = StringInfo.GetNextTextElement(input, 1);

            try
            {
                Suit = (Suit)Int16.Parse(firstChar);
            }
            catch (Exception e)
            {
                Suit = (Suit)Enum.Parse(typeof(Suit), firstChar.ToUpper());
            }
        }
예제 #27
0
        /// <summary>
        /// Convert face cards to ints.
        /// </summary>
        /// <param name="input">stdin</param>
        /// <returns>
        /// a list of ints
        /// </returns>
        private void ConvertToRank(string input)
        {
            string firstChar = StringInfo.GetNextTextElement(input, 0);

            try
            {
                Rank = (Rank)Int16.Parse(firstChar);
            }
            catch (Exception e)
            {
                Rank = (Rank)Enum.Parse(typeof(Rank), firstChar.ToUpper());
            }
        }
        /// <summary>Converts a G4 identifier name to a C# name.</summary>
        /// <param name="g4Name">The G4 identifier name.</param>
        /// <returns><paramref name="g4Name"/> converted to an ideomatic C# name, that is, with the first
        /// character in uppercase.</returns>
        public static string ToCSharpName(string g4Name)
        {
            string firstTextElement = StringInfo.GetNextTextElement(g4Name);
            string toUpper          = firstTextElement.ToUpper(CultureInfo.InvariantCulture);

            if (toUpper == firstTextElement)
            {
                // Avoid allocating...
                return(g4Name);
            }
            else
            {
                return(String.Concat(toUpper, g4Name.Remove(0, firstTextElement.Length)));
            }
        }
예제 #29
0
        /// <summary>
        /// Returns StringInfo.GetNextTextElement(key).ToUpperInvariant() throwing exceptions for null
        /// and multi-char strings.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string NormalizeKey(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            string firstCharacter = StringInfo.GetNextTextElement(key);

            if (key != firstCharacter)
            {
                throw new ArgumentException(SR.Get(SRID.AccessKeyManager_NotAUnicodeCharacter, "key"));
            }

            return(firstCharacter.ToUpperInvariant());
        }
        public void TestLetter()
        {
            char   ch  = _generator.GetCharLetter(-55);
            string str = _generator.GetString(-55, false, c_MINI_STRING_LENGTH, c_MAX_STRING_LENGTH);

            // If the random string's first letter is a NonSpacingMark the returned result will be ch + string's first letter (expected)
            // That scenario is hit in PosTest3. In order to avoid random failures, if the first character happens to be a NonSpacingMark
            // then append a random char letter on to the string
            if (CharUnicodeInfo.GetUnicodeCategory(str.ToCharArray()[0]) == UnicodeCategory.NonSpacingMark)
            {
                str = _generator.GetCharLetter(-55).ToString() + str;
            }
            string result = StringInfo.GetNextTextElement(ch.ToString() + str);

            Assert.Equal(ch.ToString(), result);
        }