Пример #1
0
        public static void Equals_NonOrdinal(string str1, string str2, StringComparison comparison, string culture, bool shouldCompareAsEqual)
        {
            Func <string, string, string, string, string, int> action = (str1, str2, comparison, culture, shouldCompareAsEqual) =>
            {
                using (new ThreadCultureChange(culture))
                {
                    using BoundedUtf8Span boundedSpan1 = new BoundedUtf8Span(str1);
                    using BoundedUtf8Span boundedSpan2 = new BoundedUtf8Span(str2);

                    Utf8Span span1 = boundedSpan1.Span;
                    Utf8Span span2 = boundedSpan2.Span;

                    StringComparison comparisonType = Enum.Parse <StringComparison>(comparison);
                    bool             expected       = bool.Parse(shouldCompareAsEqual);

                    Assert.Equal(expected, span1.Equals(span2, comparisonType));
                    Assert.Equal(expected, span2.Equals(span1, comparisonType));
                    Assert.Equal(expected, Utf8Span.Equals(span1, span2, comparisonType));
                    Assert.Equal(expected, Utf8Span.Equals(span2, span1, comparisonType));
                }

                return(RemoteExecutor.SuccessExitCode);
            };

            if (culture != null && PlatformDetection.IsUap) // need to apply a culture to the current thread
            {
                RemoteExecutor.Invoke(action, str1, str2, comparison.ToString(), culture, shouldCompareAsEqual.ToString()).Dispose();
            }
            else
            {
                action(str1, str2, comparison.ToString(), culture, shouldCompareAsEqual.ToString());
            }
        }
Пример #2
0
        public void ForEachBreakWhenFound(string name, ref StringComparison output)
        {
#if MCS2
            foreach (int value in Enum.GetValues(typeof(StringComparison)))
            {
                if (((StringComparison)value).ToString() == name)
                {
                    output = (StringComparison)value;
                    break;
                }
            }
#elif MCS5
            foreach (int value in Enum.GetValues(typeof(StringComparison)))
            {
                StringComparison stringComparison = (StringComparison)value;
                if (stringComparison.ToString() == name)
                {
                    output = (StringComparison)value;
                    break;
                }
            }
#else
            foreach (StringComparison value in Enum.GetValues(typeof(StringComparison)))
            {
                if (value.ToString() == name)
                {
                    output = value;
                    break;
                }
            }
#endif
        }
Пример #3
0
 public static MemberDeclarationSyntax StringIComparableTCompareToSyntax(this PrimitiveDescriptor descriptor, StringComparison stringComparison)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("CompareTo"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("other")).WithType(IdentifierName(descriptor.Name)))))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 PredefinedType(Token(SyntaxKind.StringKeyword)),
                 IdentifierName("Compare")))
         .WithArgumentList(
             ArgumentList(
                 SeparatedList <ArgumentSyntax>(
                     new SyntaxNodeOrToken[]
 {
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             ThisExpression(),
             IdentifierName(descriptor.InnerName))),
     Token(SyntaxKind.CommaToken),
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             IdentifierName("other"),
             IdentifierName(descriptor.InnerName))),
     Token(SyntaxKind.CommaToken),
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             IdentifierName("StringComparison"),
             IdentifierName(stringComparison.ToString())))
 })))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Пример #4
0
        public static Boolean Contains(this String source, String toCheck, StringComparison strComp)
        {
            Boolean retVal = false;

            try
            {
                if (toCheck != null)
                {
                    if (toCheck.Length > 0)
                    {
                        retVal = (source?.IndexOf(toCheck, strComp) >= 0);
                    }
                }
            }
            catch (Exception exUnhandled)
            {
                exUnhandled.Data.Add("source", source ?? "NULL");
                exUnhandled.Data.Add("toCheck", toCheck ?? "NULL");
                exUnhandled.Data.Add("strComp", strComp.ToString());

                throw;
            }

            return(retVal);
        }  // END public static Boolean Contains(this String source, String toCheck, StringComparison strComp)
Пример #5
0
 private static CodeAction CreateCodeAction(CodeFixContext context, InvocationExpressionSyntax invocation, StringComparison stringComparison)
 {
     return(CodeAction.Create(
                GetTitle(stringComparison),
                cancellationToken => UseStringComparisonRefactoring.RefactorAsync(context.Document, invocation, stringComparison, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.UseStringComparison, stringComparison.ToString())));
 }
Пример #6
0
        }  // END public static Boolean Contains(this String source, String toCheck, StringComparison strComp)

        public static string Replace(this String origString, String value2Replace, String newValue, StringComparison strComp)
        {
            StringBuilder sb = new StringBuilder();

            Int32 previousIndex = 0;

            Int32 index = 0;

            String retVal = "";

            try
            {
                if (value2Replace != null)
                {
                    if (newValue == null)
                    {
                        newValue = "";
                    }

                    index = origString.IndexOf(value2Replace, strComp);

                    while (index != -1)
                    {
                        sb.Append(origString.Substring(previousIndex, index - previousIndex));
                        sb.Append(newValue);
                        index += value2Replace.Length;

                        previousIndex = index;
                        index         = origString.IndexOf(value2Replace, index, strComp);
                    }

                    sb.Append(origString.Substring(previousIndex));

                    retVal = sb.ToString();
                } // END if (value2Replace != null)
            }
            catch (Exception exUnhandled)
            {
                exUnhandled.Data.Add("origString", origString ?? "NULL");
                exUnhandled.Data.Add("value2Replace", value2Replace ?? "NULL");
                exUnhandled.Data.Add("newValue", newValue ?? "NULL");
                exUnhandled.Data.Add("strComp", strComp.ToString());

                throw;
            }
            finally
            {
                if (sb != null)
                {
                    sb.Clear();

                    sb = null;
                }
            }

            return(retVal);
        }  // END public static string Replace(this String origString, String value2Replace, String newValue, StringComparison strComp)
        public async void FindsWarning_ForTrueInstanceEqualsCheck_WithSupportedStringComparison(StringComparison comparison)
        {
            var source =
                @"class TestClass { void TestMethod() {
    Xunit.Assert.True(""abc"".Equals(""a"", System.StringComparison." + comparison + @"));
} }";

            var expected = Verify.Diagnostic().WithSpan(2, 5, 2, 67 + comparison.ToString().Length).WithSeverity(DiagnosticSeverity.Warning).WithArguments($"Assert.True()");
            await Verify.VerifyAnalyzerAsync(source, expected);
        }
Пример #8
0
        public override string ToString()
        {
            var sb = new ValueStringBuilder(80);

            sb.Append(m_matchType.ToString());
            sb.Append(' ');
            sb.Append(m_pattern);
            sb.Append(' ');
            sb.Append(m_stringComparison.ToString());
            return(sb.ToString());
        }
Пример #9
0
        void EqualsTest(StringComparison compare, int count)
        {
            Console.WriteLine("\r\na.Equals(b, StringComparison.{0}):", compare.ToString());
            Stopwatch watch = new Stopwatch();

            watch.Start();
            for (int i = 0; i < count; i++)
            {
                bool re = a.Equals(b, compare);
            }
            watch.Stop();
            double time = watch.Elapsed.TotalSeconds;

            Console.Write(time);
        }
Пример #10
0
 public static MemberDeclarationSyntax StringGetHashCodeSyntax(this PrimitiveDescriptor descriptor, StringComparison stringComparison)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("GetHashCode"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)))
 .WithExpressionBody(
     ArrowExpressionClause(
         ConditionalExpression(
             BinaryExpression(
                 SyntaxKind.NotEqualsExpression,
                 IdentifierName(descriptor.InnerName),
                 LiteralExpression(SyntaxKind.NullLiteralExpression)),
             InvocationExpression(
                 MemberAccessExpression(
                     SyntaxKind.SimpleMemberAccessExpression,
                     MemberAccessExpression(
                         SyntaxKind.SimpleMemberAccessExpression,
                         IdentifierName("StringComparer"),
                         IdentifierName(stringComparison.ToString())),
                     IdentifierName("GetHashCode")))
             .WithArgumentList(
                 ArgumentList(
                     SingletonSeparatedList(Argument(IdentifierName(descriptor.InnerName))))),
             LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Пример #11
0
        public static Expression CreateStringCompare(this Expression leftExpression, StringTransform stringTransform, StringComparison stringComparison, Expression rightExpression)
        {
            var stringType            = typeof(string);
            var stringTransformMethod = stringType.GetMethod(stringTransform.ToString(), Type.EmptyTypes);

            var leftParam = leftExpression == null
                                ? stringTransform == StringTransform.None
                                        ? (Expression)Expression.Parameter(stringType)
                                        : Expression.Call(
                Expression.Parameter(stringType),
                stringTransformMethod)
                                : stringTransform == StringTransform.None
                                        ? leftExpression
                                        : Expression.Call(
                leftExpression,
                stringTransformMethod);

            var rightParam = stringTransform == StringTransform.None ? rightExpression : Expression.Call(rightExpression, stringTransformMethod);

            var stringCompareMethod = stringType.GetMethod(stringComparison.ToString(), new[] { stringType });

            return(Expression.Call(leftParam, stringCompareMethod, rightParam));
        }
        protected CodeExpression CreateStringEqualsExpression(StringComparison stringComparison, CodeExpression left, CodeExpression right)
        {
            if (left == null)
            {
                throw new ArgumentNullException("left");
            }
            if (right == null)
            {
                throw new ArgumentNullException("right");
            }

            return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(string)), "Equals", left, right,
                                                  new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(StringComparison)), stringComparison.ToString())));
        }
Пример #13
0
        public void DefaultConstructor_ThrowException_WhenNotOrdinalComparison(StringComparison comparisonType)
        {
            var paramBegin    = "begin";
            var paramContains = new List <string> {
                "1", "2", "three"
            };
            var paramEnd = "end";

            AssertExtensions.ThrowsContains <InvalidOperationException>(() => new WildcardPathSegment(paramBegin, paramContains, paramEnd, comparisonType), comparisonType.ToString());
        }
Пример #14
0
 public virtual object Test6(object x, StringComparison sc)
 {
     return("C# Test6" + x.ToString() + sc.ToString());
 }
Пример #15
0
 public virtual object Test5(StringComparison sc)
 {
     return("C# Test5" + sc.ToString());
 }
 public static void Str(this ExtensionMethodInvokerTest self, StringComparison cmp)
 {
     self.Text.Append(cmp.ToString());
 }
Пример #17
0
        StringCompareOp(StringComparison comparison)
        {
            switch (comparison)
            {
            case StringComparison.CurrentCulture:
                Comparer = StringComparer.CurrentCulture;
                Equals   = (locA, locB) => string.Equals(a: locA, b: locB, StringComparison.CurrentCulture);
                break;

            case StringComparison.CurrentCultureIgnoreCase:
                Comparer = StringComparer.CurrentCultureIgnoreCase;
                Equals   = (locA, locB) => string.Equals(a: locA, b: locB, StringComparison.CurrentCultureIgnoreCase);
                break;

            case StringComparison.InvariantCulture:
                Comparer = StringComparer.InvariantCulture;
                Equals   = (locA, locB) => string.Equals(a: locA, b: locB, StringComparison.InvariantCulture);
                break;

            case StringComparison.InvariantCultureIgnoreCase:
                Comparer = StringComparer.InvariantCultureIgnoreCase;
                Equals   = (locA, locB) => string.Equals(a: locA, b: locB, StringComparison.InvariantCultureIgnoreCase);
                break;

            case StringComparison.Ordinal:
                Comparer = StringComparer.Ordinal;
                Equals   = (locA, locB) => string.Equals(a: locA, b: locB, StringComparison.Ordinal);
                break;

            case StringComparison.OrdinalIgnoreCase:
                Comparer = StringComparer.OrdinalIgnoreCase;
                Equals   = (locA, locB) => string.Equals(a: locA, b: locB, StringComparison.OrdinalIgnoreCase);
                break;

            default:
                throw new ArgumentOutOfRangeException(paramName: nameof(comparison), message: $"Значение не поддерживается.{Environment.NewLine}\tЗначение:{comparison.ToString().FmtStr().GNLI2()}");
            }
            Type = comparison;
        }