Exemplo n.º 1
0
        /// <summary>
        /// Try parse a <see cref="LanguageVersion"/> from a string input, returning default if input was null.
        /// </summary>
        public static bool TryParse(string version, out LanguageVersion result)
        {
            if (version == null)
            {
                result = LanguageVersion.Default;
                return(true);
            }
            switch (CaseInsensitiveComparison.ToLower(version))
            {
            case "default":
                result = LanguageVersion.Default;
                return(true);

            case "latest":
                result = LanguageVersion.Latest;
                return(true);

            case "latestmajor":
                result = LanguageVersion.LatestMajor;
                return(true);

            case "preview":
                result = LanguageVersion.Preview;
                return(true);

            case "1":
            case "1.0":
                result = MetaLanguageVersion.Meta1;
                return(true);

            default:
                result = LanguageVersion.Default;
                return(false);
            }
        }
Exemplo n.º 2
0
            public Builder(IEnumerable <StringSlice> values)
            {
                // TODO(cyrusn): Properly handle unicode normalization here.
                var distinctValues = values.Where(v => v.Length > 0).Distinct(StringSliceComparer.OrdinalIgnoreCase).ToArray();
                var charCount      = values.Sum(v => v.Length);

                _concatenatedLowerCaseWords = new char[charCount];
                _wordSpans = new TextSpan[distinctValues.Length];

                var characterIndex = 0;

                for (int i = 0; i < distinctValues.Length; i++)
                {
                    var value = distinctValues[i];
                    _wordSpans[i] = new TextSpan(characterIndex, value.Length);

                    foreach (var ch in value)
                    {
                        _concatenatedLowerCaseWords[characterIndex] = CaseInsensitiveComparison.ToLower(ch);
                        characterIndex++;
                    }
                }

                // We will have one node for each string value that we are adding.
                _builderNodes = new BuilderNode[distinctValues.Length];
                _compactEdges = new Edge[distinctValues.Length * CompactEdgeAllocationSize];
            }
Exemplo n.º 3
0
        public IList <string> Find(string value, int?threshold = null)
        {
            if (_nodes.Length == 0)
            {
                return(SpecializedCollections.EmptyList <string>());
            }

            var lowerCaseCharacters = ArrayPool <char> .GetArray(value.Length);

            try
            {
                for (var i = 0; i < value.Length; i++)
                {
                    lowerCaseCharacters[i] = CaseInsensitiveComparison.ToLower(value[i]);
                }

                threshold ??= WordSimilarityChecker.GetThreshold(value);
                var result = new List <string>();
                Lookup(_nodes[0], lowerCaseCharacters, value.Length, threshold.Value, result, recursionCount: 0);
                return(result);
            }
            finally
            {
                ArrayPool <char> .ReleaseArray(lowerCaseCharacters);
            }
        }
Exemplo n.º 4
0
        public static bool TryParse(string version, out SarifVersion result)
        {
            if (version == null)
            {
                result = SarifVersion.Default;
                return(true);
            }

            switch (CaseInsensitiveComparison.ToLower(version))
            {
            case "default":
                result = SarifVersion.Default;
                return(true);

            case "latest":
                result = SarifVersion.Latest;
                return(true);

            case "1":
            case "1.0":
                result = SarifVersion.Sarif1;
                return(true);

            case "2":
            case "2.1":
                result = SarifVersion.Sarif2;
                return(true);

            default:
                result = SarifVersion.Default;
                return(false);
            }
        }
Exemplo n.º 5
0
        public static int LastIndexOf(this SourceText text, string value, int startIndex, bool caseSensitive)
        {
            var normalized = caseSensitive ? value : CaseInsensitiveComparison.ToLower(value);

            startIndex = startIndex + normalized.Length > text.Length
                ? text.Length - normalized.Length
                : startIndex;

            for (var i = startIndex; i >= 0; i--)
            {
                var match = true;
                for (var j = 0; j < normalized.Length; j++)
                {
                    // just use indexer of source text. perf of indexer depends on actual implementation of SourceText.
                    // * all of our implementation at editor layer should provide either O(1) or O(logn).
                    //
                    // only one implementation we have that could have bad indexer perf is CompositeText with heavily modified text
                    // at compiler layer but I believe that being used in find all reference will be very rare if not none.
                    if (!Match(normalized[j], text[i + j], caseSensitive))
                    {
                        match = false;
                        break;
                    }
                }

                if (match)
                {
                    return(i);
                }
            }

            return(-1);
        }
Exemplo n.º 6
0
 private static bool EqualsOrdinalIgnoreCase(char thisChar, char otherChar)
 {
     // Do a fast check first before converting to lowercase characters.
     return
         (thisChar == otherChar ||
          CaseInsensitiveComparison.ToLower(thisChar) == CaseInsensitiveComparison.ToLower(otherChar));
 }
Exemplo n.º 7
0
        private static EditorConfigFile Parse(IEnumerable <string> lines)
        {
            var parsedOptions = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var line in lines)
            {
                if (string.IsNullOrWhiteSpace(line) || IsComment(line))
                {
                    continue;
                }

                var propMatches = s_propertyMatcher.Matches(line);
                if (propMatches.Count > 0 && propMatches[0].Success)
                {
                    var key   = propMatches[0].Groups["key"].Value;
                    var value = propMatches[0].Groups["value"].Value;

                    Debug.Assert(!string.IsNullOrEmpty(key));
                    Debug.Assert(string.Equals(key, key.Trim(), StringComparison.Ordinal));
                    Debug.Assert(string.Equals(value, value.Trim(), StringComparison.Ordinal));

                    key = CaseInsensitiveComparison.ToLower(key);
                    if (s_reservedKeys.Contains(key) || s_reservedValues.Contains(value))
                    {
                        value = CaseInsensitiveComparison.ToLower(value);
                    }

                    parsedOptions[key] = value ?? "";
                    continue;
                }
            }

            return(new EditorConfigFile(parsedOptions));
        }
Exemplo n.º 8
0
        private static char[] ConvertToLowercaseArray(string text)
        {
            var array = ArrayPool<char>.GetArray(text.Length);
            for (int i = 0; i < text.Length; i++)
            {
                array[i] = CaseInsensitiveComparison.ToLower(text[i]);
            }

            return array;
        }
Exemplo n.º 9
0
        internal static int GetCaseInsensitiveFNVHashCode(string text, int start, int length)
        {
            int hashCode = Hash.FnvOffsetBias;
            int end      = start + length;

            for (int i = start; i < end; i++)
            {
                hashCode = unchecked ((hashCode ^ CaseInsensitiveComparison.ToLower(text[i])) * Hash.FnvPrime);
            }

            return(hashCode);
        }
Exemplo n.º 10
0
        internal int CompareToOrdinalIgnoreCase(StringSlice other)
        {
            var thisEnd  = this._span.End;
            var otherEnd = other._span.End;

            for (int i = this._span.Start, j = other._span.Start;
                 i < thisEnd && j < otherEnd;
                 i++, j++)
            {
                var diff =
                    CaseInsensitiveComparison.ToLower(this._underlyingString[i]) -
                    CaseInsensitiveComparison.ToLower(other._underlyingString[j]);
                if (diff != 0)
                {
                    return(diff);
                }
            }

            // Choose the one that is shorter if their prefixes match so far.
            return(this.Length - other.Length);
        }
Exemplo n.º 11
0
        public static CompilationCategorizedAnalyzerConfigOptions Parse(SourceText text)
        {
            var parsedOptions = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var textLine in text.Lines)
            {
                var line = textLine.ToString();

                if (string.IsNullOrWhiteSpace(line) || IsComment(line))
                {
                    continue;
                }

                var propMatches = s_propertyMatcher.Matches(line);
                if (propMatches.Count > 0 && propMatches[0].Groups.Count > 1)
                {
                    var key   = propMatches[0].Groups[1].Value;
                    var value = propMatches[0].Groups[2].Value;

                    Debug.Assert(!string.IsNullOrEmpty(key));
                    Debug.Assert(key == key.Trim());
                    Debug.Assert(value == value.Trim());

                    key = CaseInsensitiveComparison.ToLower(key);
                    if (s_reservedKeys.Contains(key) || s_reservedValues.Contains(value))
                    {
                        value = CaseInsensitiveComparison.ToLower(value);
                    }

                    parsedOptions[key] = value;
                    continue;
                }
            }

            return(CompilationCategorizedAnalyzerConfigOptions.Create(parsedOptions));
        }
Exemplo n.º 12
0
        public static bool TryParse(string?version, out LanguageVersion result)
        {
            if (version == null)
            {
                result = LanguageVersion.Default;
                return(true);
            }

            switch (CaseInsensitiveComparison.ToLower(version))
            {
            case "default":
                result = LanguageVersion.Default;
                return(true);

            case "latest":
                result = LanguageVersion.Latest;
                return(true);

            case "latestmajor":
                result = LanguageVersion.LatestMajor;
                return(true);

            case "preview":
                result = LanguageVersion.Preview;
                return(true);

            case "1":
            case "1.0":
            case "iso-1":
                result = LanguageVersion.CSharp1;
                return(true);

            case "2":
            case "2.0":
            case "iso-2":
                result = LanguageVersion.CSharp2;
                return(true);

            case "3":
            case "3.0":
                result = LanguageVersion.CSharp3;
                return(true);

            case "4":
            case "4.0":
                result = LanguageVersion.CSharp4;
                return(true);

            case "5":
            case "5.0":
                result = LanguageVersion.CSharp5;
                return(true);

            case "6":
            case "6.0":
                result = LanguageVersion.CSharp6;
                return(true);

            case "7":
            case "7.0":
                result = LanguageVersion.CSharp7;
                return(true);

            case "7.1":
                result = LanguageVersion.CSharp7_1;
                return(true);

            case "7.2":
                result = LanguageVersion.CSharp7_2;
                return(true);

            case "7.3":
                result = LanguageVersion.CSharp7_3;
                return(true);

            case "8":
            case "8.0":
                result = LanguageVersion.CSharp8;
                return(true);

            case "9":
            case "9.0":
                result = LanguageVersion.CSharp9;
                return(true);

            default:
                result = LanguageVersion.Default;
                return(false);
            }
        }
Exemplo n.º 13
0
 private static bool Match(char normalizedLeft, char right, bool caseSensitive)
 {
     return(caseSensitive ? normalizedLeft == right : normalizedLeft == CaseInsensitiveComparison.ToLower(right));
 }