Пример #1
0
        public static void EscapeThenUnescapeTemplate(string src, bool fullEscape, bool escapePercent)
        {
            string escaped   = StringEscaper.Escape(src, fullEscape, escapePercent);
            string unescaped = StringEscaper.Unescape(escaped, escapePercent);

            Assert.IsTrue(unescaped.Equals(src, StringComparison.Ordinal));
        }
Пример #2
0
        public void Unescape_6()
        {
            const string src  = "Incomplete#$";
            string       dest = StringEscaper.Unescape(src);

            Assert.IsTrue(dest.Equals(src, StringComparison.Ordinal));
        }
Пример #3
0
        public IntellisenseSuggest CycleValue(TextInputState input, bool backward, string language)
        {
            string query = input.Text;
            int    caret = input.Caret;

            var token = new MtgTolerantTokenizer(query).GetEditedToken(caret);

            if (token == null || token.Type.IsAny(TokenType.ModifierValue))
            {
                return(null);
            }

            string userField = token.ParentField ?? string.Empty;

            bool isFieldInvalid = !Adapter.IsAnyField(userField) && !Adapter.IsUserField(userField);

            if (isFieldInvalid)
            {
                return(null);
            }

            string currentValue;

            if (!Adapter.IsSuggestAnalyzedIn(userField, language))
            {
                token        = token.PhraseStart ?? token;
                currentValue = StringEscaper.Unescape(token.GetPhraseText(query));
            }
            else
            {
                currentValue = StringEscaper.Unescape(token.Value);
            }

            var snapshot  = State;
            var allValues = snapshot.GetValuesCache(userField, language);

            if (allValues.Count == 0)
            {
                return(null);
            }

            var currentIndex =
                allValues.BinarySearchLastIndexOf(str => Str.Comparer.Compare(str, currentValue) <= 0);

            int increment = backward ? -1 : 1;
            var nextIndex = currentIndex + increment;

            if (nextIndex == allValues.Count)
            {
                nextIndex = 0;
            }
            else if (nextIndex == -1)
            {
                nextIndex = allValues.Count - 1;
            }

            var nextValue = allValues[nextIndex];

            return(new IntellisenseSuggest(token, ReadOnlyList.From(nextValue), _allTokensAreValues));
        }
Пример #4
0
        public void Unescape_1()
        {
            string src  = "Comma [,]#$xPercent [%]#$xDoubleQuote [#$q]#$xSpace [ ]#$xTab [#$t]#$xSharp [##]#$xNewLine [#$x]";
            string dest = StringEscaper.Unescape(src, false);
            string comp = StringEscaperTests.SampleString;

            Assert.IsTrue(dest.Equals(comp, StringComparison.Ordinal));
        }
Пример #5
0
        public void Unescape_4()
        {
            string src  = "Comma#$s[#$c]#$xPercent#$s[#$p]#$xDoubleQuote#$s[#$q]#$xSpace#$s[#$s]#$xTab#$s[#$t]#$xSharp#$s[##]#$xNewLine#$s[#$x]";
            string dest = StringEscaper.Unescape(src, true);
            string comp = StringEscaperTests.SampleString;

            Assert.IsTrue(dest.Equals(comp, StringComparison.Ordinal));
        }
Пример #6
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is string escapedStr))
            {
                return(string.Empty);
            }

            return(StringEscaper.Unescape(escapedStr));
        }
Пример #7
0
        public static void UnescapeArrayTemplate(string[] srcs, bool escapePercent, string[] expects)
        {
            List <string> dests = StringEscaper.Unescape(srcs, escapePercent);

            for (int i = 0; i < dests.Count; i++)
            {
                Assert.IsTrue(dests[i].Equals(expects[i], StringComparison.Ordinal));
            }
        }
Пример #8
0
        public static void EscapeThenUnescapeArrayTemplate(string[] srcs, bool fullEscape, bool escapePercent)
        {
            List <string> escaped   = StringEscaper.Escape(srcs, fullEscape, escapePercent);
            List <string> unescaped = StringEscaper.Unescape(escaped, escapePercent);

            for (int i = 0; i < unescaped.Count; i++)
            {
                Assert.IsTrue(unescaped[i].Equals(srcs[i], StringComparison.Ordinal));
            }
        }
        public void CanUnesacapeMultiple()
        {
            Assert.AreEqual("test\t\ttest", StringEscaper.Unescape("test\\t\\ttest"));
            Assert.AreEqual("\t\ttesttest", StringEscaper.Unescape("\\t\\ttesttest"));
            Assert.AreEqual("testtest\t\t", StringEscaper.Unescape("testtest\\t\\t"));

            Assert.AreEqual("test\tt\test", StringEscaper.Unescape("test\\tt\\test"));
            Assert.AreEqual("\ttest\ttest", StringEscaper.Unescape("\\ttest\\ttest"));
            Assert.AreEqual("test\ttest\t", StringEscaper.Unescape("test\\ttest\\t"));
        }
Пример #10
0
        public void PercentSpecifier_Checks()
        {
            // Arrange
            Environment.SetEnvironmentVariable("SHARPINIT_UNIT_PATH", DirectoryName);
            UnitRegistry.DefaultScanDirectories.Clear();

            // Act
            UnitRegistry.ScanDefaultDirectories();
            var unit_unspecified = UnitRegistry.GetUnit <ServiceUnit>(TestUnitFilename);
            var unit_specified   = UnitRegistry.GetUnit <ServiceUnit>(TestUnitInstanceName);

            // Assert
            Assert.IsTrue(unit_unspecified.Descriptor.WorkingDirectory == "/" + StringEscaper.Unescape(unit_unspecified.Descriptor.DefaultInstance));
            Assert.IsTrue(unit_specified.Descriptor.WorkingDirectory == "/" + StringEscaper.Unescape(UnitRegistry.GetUnitParameter(TestUnitInstanceName)));
        }
Пример #11
0
        protected override Query GetWildcardQuery(string field, string escapedValue)
        {
            if (_adapter.IsAnyField(field) && !IsPass2ResolvingPhrases)
            {
                var result = new BooleanQuery(disableCoord: true);

                foreach (var userField in ExpandAnyField())
                {
                    if (_adapter.IsNumericField(userField))
                    {
                        continue;
                    }

                    var specificFieldQuery = base.GetWildcardQuery(userField, escapedValue);

                    if (specificFieldQuery == null)
                    {
                        continue;
                    }

                    result.Add(specificFieldQuery, Occur.SHOULD);
                }

                if (result.Clauses.Count == 0)
                {
                    return(_matchNothingQuery);
                }

                return(result);
            }

            var value = StringEscaper.Unescape(escapedValue);

            if (_adapter.IsFloatField(field) && !isValueFloat(value))
            {
                return(_matchNothingQuery);
            }

            if (_adapter.IsIntField(field) && !isValueInt(value))
            {
                return(_matchNothingQuery);
            }

            return(base.GetWildcardQuery(field, escapedValue));
        }
Пример #12
0
        private Query getMoreLikeQuery(string termImage, Token slopToken)
        {
            string unescaped = StringEscaper.Unescape(termImage);
            float  slop      = parseSlop(slopToken);

            if (string.IsNullOrEmpty(unescaped))
            {
                return(MatchNothingQuery);
            }

            if (!_repository.IsLoadingComplete.Signaled)
            {
                return(MatchNothingQuery);
            }

            string name   = unescaped.RemoveDiacritics();
            var    result = new DisjunctionMaxQuery(0.1f);

            addClauses(isToken: false);
            addClauses(isToken: true);

            return(result.Disjuncts.Count == 0
                                ? MatchNothingQuery
                                : result);

            void addClauses(bool isToken)
            {
                if (!_repository.MapByName(isToken).TryGetValue(name, out var list))
                {
                    return;
                }

                Card c = list[0];

                if (!string.IsNullOrEmpty(c.TextEn))
                {
                    result.Add(createMoreLikeThisQuery(slop, c.TextEn, nameof(Card.TextEn)));
                }

                if (!string.IsNullOrEmpty(c.GeneratedMana))
                {
                    result.Add(createMoreLikeThisQuery(slop, c.GeneratedMana, nameof(Card.GeneratedMana)));
                }
            }
        }
Пример #13
0
        private Query getMoreLikeQuery(string termImage, Token slopToken)
        {
            string unescaped = StringEscaper.Unescape(termImage);
            float  slop      = parseSlop(slopToken);

            if (string.IsNullOrEmpty(unescaped))
            {
                return(_matchNothingQuery);
            }

            if (!_repository.IsLoadingComplete)
            {
                return(_matchNothingQuery);
            }

            string cardName = unescaped.RemoveDiacritics();

            if (!_repository.CardsByName.TryGetValue(cardName, out var cards))
            {
                return(_matchNothingQuery);
            }

            var card = cards[0];

            var result = new DisjunctionMaxQuery(0.1f);

            if (!string.IsNullOrEmpty(card.TextEn))
            {
                result.Add(createMoreLikeThisQuery(slop, card.TextEn, nameof(card.TextEn)));
            }

            if (!string.IsNullOrEmpty(card.GeneratedMana))
            {
                result.Add(createMoreLikeThisQuery(slop, card.GeneratedMana, nameof(card.GeneratedMana)));
            }

            if (result.Disjuncts.Count == 0)
            {
                return(_matchNothingQuery);
            }

            return(result);
        }
Пример #14
0
        public static Unit CreateUnit(string name)
        {
            var pure_unit_name = GetUnitName(name);

            if (!UnitFiles.ContainsKey(pure_unit_name))
            {
                return(null);
            }

            var files = UnitFiles[pure_unit_name];
            var ext   = Path.GetExtension(name);

            if (!UnitTypes.ContainsKey(ext))
            {
                return(null);
            }

            var type       = UnitTypes[ext];
            var descriptor = GetUnitDescriptor(pure_unit_name);
            var context    = new UnitInstantiationContext();

            context.Substitutions["p"] = pure_unit_name;
            context.Substitutions["P"] = StringEscaper.Unescape(pure_unit_name);
            context.Substitutions["f"] = "/" + StringEscaper.Unescape(pure_unit_name);
            context.Substitutions["H"] = Environment.MachineName;

            var unit_parameter = GetUnitParameter(name);

            if (string.IsNullOrEmpty(unit_parameter))
            {
                unit_parameter = descriptor.DefaultInstance;
            }

            if (!string.IsNullOrEmpty(unit_parameter))
            {
                context.Substitutions["i"] = unit_parameter;
                context.Substitutions["I"] = StringEscaper.Unescape(unit_parameter);
                context.Substitutions["f"] = "/" + StringEscaper.Unescape(unit_parameter);
            }

            descriptor.InstantiateDescriptor(context);
            return((Unit)Activator.CreateInstance(type, name, descriptor));
        }
        public void Performance()
        {
            string[] tests = new[]
            {
                "test\\tt\\test",
                "test\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\test"
                ,
                "test\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\test"
                ,
                "test\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\testtest\\tt\\test"
            };


            const int itterations = 100;

            Stopwatch ours = Stopwatch.StartNew();

            for (int i = 0; i < itterations; i++)
            {
                StringEscaper.Unescape(tests[0]);
                StringEscaper.Unescape(tests[1]);
                StringEscaper.Unescape(tests[2]);
                StringEscaper.Unescape(tests[3]);
            }
            ours.Stop();

            Console.WriteLine("Ours executed in " + ours.ElapsedMilliseconds + "ms");

            Stopwatch reges = Stopwatch.StartNew();

            for (int i = 0; i < itterations; i++)
            {
                Regex.Unescape(tests[0]);
                Regex.Unescape(tests[1]);
                Regex.Unescape(tests[2]);
                Regex.Unescape(tests[3]);
            }
            reges.Stop();

            Console.WriteLine("REgex executed in " + reges.ElapsedMilliseconds + "ms");
        }
Пример #16
0
        public void Unescape_5()
        {
            string[] srcs = new string[]
            {
                "Comma#$s[#$c]",
                "Space#$s[#$s]",
                "DoubleQuote#$s[#$q]",
            };
            List <string> dests = StringEscaper.Unescape(srcs);

            string[] comps = new string[]
            {
                "Comma [,]",
                "Space [ ]",
                "DoubleQuote [\"]",
            };

            for (int i = 0; i < dests.Count; i++)
            {
                Assert.IsTrue(dests[i].Equals(comps[i], StringComparison.Ordinal));
            }
        }
Пример #17
0
        protected override Query HandleQuotedTerm(string field, Token termToken, Token slopToken)
        {
            string image = termToken.Image.Substring(1, termToken.Image.Length - 2);
            string value = StringEscaper.Unescape(image);

            (bool IsFloat, bool IsInt) numericTypeGetter() => getNumericTypes(value);

            Query queryFactory(string fld, bool analyzed)
            {
                if (analyzed)
                {
                    return(base.HandleQuotedTerm(fld, termToken, slopToken));
                }

                var notAnalyzedQuery = GetFieldQuery(fld, value, quoted: true);

                return(notAnalyzedQuery);
            }

            var result = resolveField(field, numericTypeGetter, queryFactory);

            return(result);
        }
Пример #18
0
        private IReadOnlyList <string> getAnalyzedTokens(Token t, string queryStr)
        {
            if (t.Type.IsAny(TokenType.RegexBody))
            {
                return(ReadOnlyList.From(t.Value));
            }

            if (!t.Type.IsAny(TokenType.FieldValue))
            {
                return(null);
            }

            string text = !t.IsPhrase || t.IsPhraseComplex || t.PhraseHasSlop
                                ? t.Value
                                : t.GetPhraseText(queryStr);

            var result = QueryParserAnalyzer
                         .GetTokens(t.ParentField, StringEscaper.Unescape(text))
                         .Select(_ => _.Term)
                         .ToReadOnlyList();

            return(result);
        }
Пример #19
0
        public IntellisenseSuggest CycleValue(TextInputState input, bool backward, string language)
        {
            string query = input.Text;
            int    caret = input.Caret;

            var token = new MtgTolerantTokenizer(query).GetEditedToken(caret);

            if (token == null || token.Type.IsAny(TokenType.ModifierValue))
            {
                return(null);
            }

            string userField      = Adapter.GetActualField(token.ParentField);
            bool   isFieldInvalid = !Adapter.IsAnyField(userField) && !Adapter.IsUserField(userField);

            if (isFieldInvalid)
            {
                return(null);
            }

            string currentValue;

            if (!Adapter.IsSuggestAnalyzedIn(userField, language))
            {
                token        = token.PhraseStart ?? token;
                currentValue = StringEscaper.Unescape(token.GetPhraseText(query));
            }
            else
            {
                currentValue = StringEscaper.Unescape(token.Value);
            }

            var snapshot  = State;
            var allValues = snapshot.GetValuesCache(userField, language);

            if (allValues.Count == 0)
            {
                return(null);
            }

            int currentIndex;

            if (Adapter.IsFloatField(userField))
            {
                float currentParsed = float.Parse(currentValue, NumberStyles.Float, Str.Culture);
                currentIndex = allValues.BinarySearchLastIndex(str =>
                                                               float.Parse(str, NumberStyles.Float, Str.Culture) - currentParsed <= 0);
            }
            else if (Adapter.IsIntField(userField))
            {
                float currentParsed = int.Parse(currentValue, NumberStyles.Integer, Str.Culture);
                currentIndex = allValues.BinarySearchLastIndex(str =>
                                                               int.Parse(str, NumberStyles.Integer, Str.Culture) - currentParsed <= 0);
            }
            else
            {
                currentIndex = allValues.BinarySearchLastIndex(str =>
                                                               Str.Comparer.Compare(str, currentValue) <= 0);
            }

            int increment = backward ? -1 : 1;
            var nextIndex = currentIndex + increment;

            if (nextIndex == allValues.Count)
            {
                nextIndex = 0;
            }
            else if (nextIndex == -1)
            {
                nextIndex = allValues.Count - 1;
            }

            var nextValue = allValues[nextIndex];

            return(new IntellisenseSuggest(token, new[] { nextValue }, _allTokensAreValues));
        }
 public void CanUnescapeUnicodeEntity()
 {
     Assert.AreEqual("the word is \u1ab9", StringEscaper.Unescape("the word is \\u1ab9"));
 }
 public void CanUnescapeRegular()
 {
     Assert.AreEqual("test", StringEscaper.Unescape("test"));
     Assert.AreEqual("", StringEscaper.Unescape(""));
     Assert.AreEqual("asdf asdf asdf ", StringEscaper.Unescape("asdf asdf asdf "));
 }
Пример #22
0
        public IntellisenseSuggest Suggest(string language, TextInputState input)
        {
            string query = input.Text;
            int    caret = input.Caret;

            var token = new MtgTolerantTokenizer(query).GetEditedToken(caret);

            if (token == null || token.Type.IsAny(TokenType.ModifierValue))
            {
                return(LuceneSpellcheckerConstants.EmptySuggest);
            }

            string userField = token.ParentField ?? string.Empty;

            bool isFieldInvalid = !_adapter.IsAnyField(userField) && !_adapter.IsUserField(userField);

            if (!_adapter.IsSuggestAnalyzedIn(userField, language))
            {
                token = token.PhraseStart ?? token;
            }

            string valuePart =
                StringEscaper.Unescape(query.Substring(token.Position, caret - token.Position));

            if (token.Type.IsAny(TokenType.FieldValue | TokenType.Wildcard))
            {
                IReadOnlyList <string> valueSuggest;

                if (isFieldInvalid || string.IsNullOrEmpty(userField) && string.IsNullOrEmpty(valuePart))
                {
                    valueSuggest = ReadOnlyList.Empty <string>();
                }
                else if (_adapter.IsAnyField(userField))
                {
                    valueSuggest = suggestAllFieldValues(valuePart, language);
                }
                else
                {
                    valueSuggest = suggestValues(userField, language, valuePart);
                }

                if (!string.IsNullOrEmpty(userField))
                {
                    return(new IntellisenseSuggest(token, valueSuggest, _allTokensAreValues));
                }

                var fieldSuggest = suggestFields(fieldPart: valuePart);

                var values = fieldSuggest.Concat(valueSuggest).ToReadOnlyList();

                var types = fieldSuggest.Select(_ => TokenType.Field)
                            .Concat(valueSuggest.Select(_ => TokenType.FieldValue))
                            .ToReadOnlyList();

                return(new IntellisenseSuggest(token, values, types));
            }

            if (token.Type.IsAny(TokenType.Field))
            {
                return(new IntellisenseSuggest(token, suggestFields(fieldPart: valuePart), _allTokensAreField));
            }

            if (token.Type.IsAny(TokenType.Boolean))
            {
                return(new IntellisenseSuggest(token, LuceneSpellcheckerConstants.BooleanOperators, LuceneSpellcheckerConstants.AllTokensAreBoolean));
            }

            return(LuceneSpellcheckerConstants.EmptySuggest);
        }
Пример #23
0
        public static void UnescapeTemplate(string src, bool escapePercent, string expected)
        {
            string dest = StringEscaper.Unescape(src, escapePercent);

            Assert.IsTrue(dest.Equals(expected, StringComparison.Ordinal));
        }