示例#1
0
        internal bool ContainsPrefix(string prefix)
        {
            if (prefix is null)
            {
                throw new ArgumentNullException(nameof(prefix));
            }

            if (prefix.Length == 0)
            {
                return(_sortedValues.Length > 0); // only match empty string when we have some value
            }

            PrefixComparer prefixComparer = new PrefixComparer(prefix);
            bool           containsPrefix = Array.BinarySearch(_sortedValues, prefix, prefixComparer) > -1;

            if (!containsPrefix)
            {
                // If there's something in the search boundary that starts with the same name
                // as the collection prefix that we're trying to find, the binary search would actually fail.
                // For example, let's say we have foo.a, foo.bE and foo.b[0]. Calling Array.BinarySearch
                // will fail to find foo.b because it will land on foo.bE, then look at foo.a and finally
                // failing to find the prefix which is actually present in the container (foo.b[0]).
                // Here we're doing another pass looking specifically for collection prefix.
                containsPrefix = Array.BinarySearch(_sortedValues, prefix + "[", prefixComparer) > -1;
            }

            return(containsPrefix);
        }
示例#2
0

        
        /// <summary>
        /// 是否包含指定前缀
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public bool ContainsPrefix(string prefix)
        {
            if (prefix == null)
            {
                throw new ArgumentNullException(nameof(prefix));
            }
            if (prefix.Length == 0)
            {
                return(_sortedValues.Length > 0); // 空字符串表示任意的前缀
            }
            PrefixComparer prefixComparer = new PrefixComparer(prefix);

            // 搜索'xxx'前缀
            bool contaionsPrefix = Array.BinarySearch(_sortedValues, prefix, prefixComparer) > -1;

            if (!contaionsPrefix)
            {
                // 搜索'xxx['前缀
                contaionsPrefix = Array.BinarySearch(_sortedValues, prefix + "[", prefixComparer) > -1;
            }

            return(contaionsPrefix);
        }