【问题标题】:Why does StringReader read '\uffff'?为什么 StringReader 读取 '\uffff'?
【发布时间】:2015-11-22 00:56:14
【问题描述】:

我正在编写我自己的语言,称为 SPL。它有一个输入命令,它从ISplRuntime.Input 属性(它是一个TextReader)读取输入。所有其他命令都在这个界面上运行,因为这样我可以只用一个库编写不同的应用程序!

然后我编写了另一个控制台应用程序来测试我的语言。这是我对ISplRuntime 的实现。关注Input 和构造函数:

public class MyRuntime : ISplRuntime {
    protected TextReader reader;
    protected bool stopped;

    public object Current {
        get;
        set;
    }

    public virtual TextReader Input {
        get {
            return reader;
        }
    }

    public object[] Memory {
        get;
        protected set;
    }

    public TextWriter Output {
        get {
            return Console.Out;
        }
    }

    public bool Stopped {
        get {
            return stopped;
        }

        set {
            stopped = value;
            if (value) {
                Console.WriteLine ();
                Console.WriteLine ("Program has finished");
            }
        }
    }

    public void ShowErrorMessage (string error) {
        Console.WriteLine (error);
    }

    public MyRuntime () {
        string s = Console.ReadLine ();
        reader = new StringReader (s);
        stopped = false;
        Memory = new object[20];
    }
}

构建运行时时,它会要求输入。并使用该输入创建一个StringReader 并在Input 属性中返回它。所以每次输入只会是一条线。

然后我用 SPL 编写一个程序来输出输入。这就是问题所在!当我输入 1 1 1 1 时,它会打印 1 1 1 并抛出 FormatException。这就是我读取数字输入的方式:

private bool ReadFromInput (ISplRuntime runtime, out int i) {
    char stuffRead = (char)runtime.Input.Peek ();
    if (stuffRead == ' ') {
        i = 0;
        runtime.Input.Read ();
        return true;
    }

    if (char.IsNumber (stuffRead)) {
        string numberString = "";
        while (char.IsNumber (stuffRead)) {
            stuffRead = (char)runtime.Input.Read ();
            numberString += stuffRead;
        }

        i = Convert.ToInt32 (numberString); //This is where the exception occured! (Obviously, because there is no other methods that would throw it)
        return true;
    } else {
        i = 0;
        return false;
    }
}

参数runtime就是刚才看到的运行时。如果成功读取数字,则返回 true。而那个数字就是输出参数i

在 Visual Studio 中使用“Watch”窗口后,我发现抛出异常时数字字符串为"1\uffff"。这就是它抛出它的原因!我知道(认为)'\uffff' 是行尾字符。但为什么它会出现在我的输入中?我知道(认为)按 Ctrl + Z 会结束行,但我没有!然后我在监视窗口中检查了runtime.Input。结果如下:

我看到有一个名为_s 的字段,我认为这是我告诉它读取的字符串。看? _s'\uffff' 都没有,怎么读出来的?

附:我已经知道解决方案了。我只需要稍微改变while 循环就可以了。但我想知道为什么它会读取行尾。

【问题讨论】:

  • _pos = 7, _length = 7。您不必按 Ctrl+Z 来获取 StringReader 上的文件结束条件,它本身就会用完字符。
  • @HansPassant 所以被读取的最后一个字符总是'\uffff'?
  • 如果你读到一个字符并且没​​有更多字符,那么就没有最后一个字符。你会得到-1。

标签: c# input console end-of-line


【解决方案1】:

这里没有谜团 - \uffff 是由您的代码生成的。您只需阅读文档并了解您调用的方法返回什么。

TextReader.Peek Method

返回值
类型:System.Int32
表示要读取的下一个字符的整数,如果没有更多字符可用或读取器不支持查找,则为 -1

TextReader.Read Method

返回值
类型:System.Int32
文本阅读器中的下一个字符,如果没有更多可用字符,则为 -1

希望您能看到-1 (0xffffffff) 和\uffff 之间的关系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2022-01-02
    相关资源
    最近更新 更多