【问题标题】:C# human readable formating of a special syntax [closed]特殊语法的 C# 人类可读格式 [关闭]
【发布时间】:2012-11-07 05:50:47
【问题描述】:

大家好,我需要一些帮助来创建一个将普通语法转换为人类可读格式的函数:

语法如下:

[OPENTAG]
(type)name:value
[OPENTAG]
(type)name:value
(type)name:value
(type)name:value
[/CLOSETAG]
[/CLOSETAG]

想把它变成这样:

[开放标签] (类型)名称:值 [开放标签] (类型)名称:值 (类型)名称:值 (类型)名称:值 [/关闭标签] [/关闭标签]
    private string textFormater(string input)
    {
        string[] lines = Regex.Split(input, "\r\n");
        int tabs = 0;
        string newtext = "";
        foreach (string line in lines)
        {

            Match m = Regex.Match(line, "\\[.*\\]");
            bool isTop;
            bool isTopClose = false;
            string tabtext = "";

            if (m.Success)
            {
                if (line.Contains("/"))
                {
                    tabs--;
                    isTopClose = true;
                }
                else
                {
                    tabs++;
                }
                isTop = true;
            }
            else
            {
                isTop = false;
            }

            if (isTop && !isTopClose && tabs == 1)
            {
                newtext += line;
            }
            else if (isTop && !isTopClose)
            {
                for (int i = 1; i <= tabs - 1; i++)
                {
                    tabtext += "\t";
                }
                newtext += "\r\n" + tabtext + line;
            }
            else
            {
                for (int i = 1; i <= tabs; i++)
                {
                    tabtext += "\t";
                }
                newtext += "\r\n" + tabtext + line;
            }

        }
        return newtext;
    }

我有 atm 解决方案,但代码如此混乱和缓慢,在 2mb 的文件中需要很长时间 :) 感谢您的帮助!

干杯

【问题讨论】:

  • 你的意思是你只想缩进?您应该能够通过文件一次完成此操作。您能否发布您已有的解决方案并就您的代码提出更直接的问题?
  • 到底是什么问题?
  • 我想把这个语法解析成人类可读的形式。我发布了我当前的解决方案,但我知道它有点糟糕。我需要一个更快的解决方案,因为在 2mb 的文本文件中这个解决方案需要很长时间:7
  • 一开始为什么要用Regex.Split?它比 string.split() 快吗?另外,当 string.indexOf() 足够好时,为什么 Regex.Match() ?再次,它更快吗?

标签: c# syntax tags formatting human-readable


【解决方案1】:

尝试使用StringBuilder 输出文本而不是字符串,这样可以加快速度。

编辑:

这是你想要的吗?

private static string textFormater2(string input)
    {
        string[] lines = Regex.Split(input, "\r\n");
        int tabCount = 0;
        StringBuilder output = new StringBuilder();

        using (StringReader sr = new StringReader(input))
        {
            string l;
            while (!string.IsNullOrEmpty(l = sr.ReadLine()))
            {
                if (l.Substring(0, 1) == "[")
                    if (l.Contains('/'))
                        tabCount--;

                string tabs = string.Empty;
                for (int i = 0; i < tabCount; i++)
                    tabs += "\t";

                output.AppendLine(tabs + l);

                if (l.Substring(0, 1) == "[")
                    if (!l.Contains('/'))
                        tabCount++;
            }
        }

        return output.ToString();
    }

【讨论】:

  • 速度很快,但格式有点不对
  • 是的,抱歉,我刚刚注意到。我已经更正了,现在我得到了与您的示例相同的结果。你能再试一次吗?
  • 但我现在明白了,谢谢你的解决方案很棒!
  • 先生,您真的很棒!感谢您的帮助!!!! :D
  • 这比我制作的速度快近 100 倍 <.>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
相关资源
最近更新 更多