当前位置: 首页>>代码示例>>C#>>正文


C# Slice.GroupBy方法代码示例

本文整理汇总了C#中Slice.GroupBy方法的典型用法代码示例。如果您正苦于以下问题:C# Slice.GroupBy方法的具体用法?C# Slice.GroupBy怎么用?C# Slice.GroupBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Slice的用法示例。


在下文中一共展示了Slice.GroupBy方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OptimizedRegexInner

        private static string OptimizedRegexInner(Slice<string> strings, string openParen)
        {
            var closeParen = openParen.PythonAnd(")").PythonOr("");
            if (strings.Length == 0)
                return "";

            var first = strings[0];
            if (strings.Length == 1)
                return openParen + Escape(first) + closeParen;

            if (string.IsNullOrEmpty(first))
            {
                //first string empty
                return openParen + OptimizedRegexInner(strings.SubSlice(1), "(?:") +
                       "?" + closeParen;
            }

            if (first.Length == 1)
            {
                //multiple one-char strings? make a charset
                StringBuilder oneletter = new StringBuilder();
                List<string> rest = new List<string>();

                foreach (var s in strings)
                {
                    if (s.Length == 1)
                        oneletter.Append(s);
                    else
                    {
                        rest.Add(s);
                    }
                }

                if (oneletter.Length > 1) // do we have more than one oneletter string?
                {
                    if (rest.Count > 0)
                    {
                        // 1-character + rest
                        return openParen + OptimizedRegexInner(rest.ToArray(), "") +
                               "|" + MakeCharset(oneletter.ToString()) + closeParen;
                    }
                    return MakeCharset(oneletter.ToString());
                }
            }

            var prefix = CommonPrefix.Of(strings);
            if (prefix.Length > 0)
            {
                var plen = prefix.Length;
                // we have a prefix for all strings
                //  print '-> prefix:', prefix
                return openParen + Escape(prefix)
                       + OptimizedRegexInner(strings.Select(s => s.Substring(plen)).ToArray(), "(?:")
                       + closeParen;
            }

            // is there a suffix?
            var stringsReversed = strings.Select(s => s.Backwards()).ToArray();
            var suffix = CommonPrefix.Of(stringsReversed);
            if (suffix.Length > 0)
            {
                var slen = suffix.Length;
                return openParen
                    + OptimizedRegexInner(strings.Select(s => s.Substring(s.Length - slen)).ToArray(), "(?:")
                    + Escape(suffix.Backwards()) + closeParen;
            }

            //# recurse on common 1-string prefixes
            //# print '-> last resort'

            var groups = strings.GroupBy(s => s[0] == first[0]);


            return openParen +
                string.Join("|", groups.Select(g => OptimizedRegexInner(g.ToArray(), ""))) +
                closeParen;
        }
开发者ID:akatakritos,项目名称:PygmentSharp,代码行数:77,代码来源:RegexUtil.cs


注:本文中的Slice.GroupBy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。