D (Tango) 读取所有标准输入并将其分配给字符串

发布于 2024-08-24 16:07:25 字数 48 浏览 6 评论 0原文

在 D 语言中,我如何读取所有标准输入并将其分配给字符串(使用 Tango 库)?

In the D language how I can read all standard input and assign it to a string (with Tango library) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贩梦商人 2024-08-31 16:07:25

直接从 http://www.dsource.org/projects/tango/wiki/ChapterIoConsole 复制

import tango.text.stream.LineIterator;

foreach (line; new LineIterator!(char)(Cin.stream))
     // do something with each line

如果只需要 1 行,请使用

auto line = Cin.copyln();

Copied straight from http://www.dsource.org/projects/tango/wiki/ChapterIoConsole:

import tango.text.stream.LineIterator;

foreach (line; new LineIterator!(char)(Cin.stream))
     // do something with each line

If only 1 line is required, use

auto line = Cin.copyln();
李白 2024-08-31 16:07:25

另一种可能更有效的转储 Stdin 内容的方法是这样的:

module dumpstdin;

import tango.io.Console : Cin;
import tango.io.device.Array : Array;
import tango.io.model.IConduit : InputStream;

const BufferInitialSize = 4096u;
const BufferGrowingStep = 4096u;

ubyte[] dumpStream(InputStream ins)
{
    auto buffer = new Array(BufferInitialSize, BufferGrowingStep);
    buffer.copy(ins);
    return cast(ubyte[]) buffer.slice();
}

import tango.io.Stdout : Stdout;

void main()
{
    auto contentsOfStdin
        = cast(char[]) dumpStream(Cin.stream);

    Stdout
        ("Finished reading Stdin.").newline()
        ("Contents of Stdin was:").newline()
        ("<<")(contentsOfStdin)(">>").newline();
}

一些注意事项:

  • Array 的第二个参数是必要的;如果省略它,数组的大小将不会增长。
  • 我使用 4096,因为这通常是内存页的大小。
  • dumpStream 返回一个 ubyte[],因为 char[] 被定义为 UTF-8 字符串,而 Stdin 不一定需要如此。例如,如果有人通过管道将二进制文件传输到您的程序,您最终会得到一个无效的 char[],如果有任何东西检查它的有效性,它可能会引发异常。如果您只关心文本,那么将结果转换为 char[] 就可以了。
  • copyOutputStream 接口上的一种方法,可使其耗尽所提供的 InputStream 中的所有输入。

Another, probably more efficient way, of dumping the contents of Stdin would be something like this:

module dumpstdin;

import tango.io.Console : Cin;
import tango.io.device.Array : Array;
import tango.io.model.IConduit : InputStream;

const BufferInitialSize = 4096u;
const BufferGrowingStep = 4096u;

ubyte[] dumpStream(InputStream ins)
{
    auto buffer = new Array(BufferInitialSize, BufferGrowingStep);
    buffer.copy(ins);
    return cast(ubyte[]) buffer.slice();
}

import tango.io.Stdout : Stdout;

void main()
{
    auto contentsOfStdin
        = cast(char[]) dumpStream(Cin.stream);

    Stdout
        ("Finished reading Stdin.").newline()
        ("Contents of Stdin was:").newline()
        ("<<")(contentsOfStdin)(">>").newline();
}

Some notes:

  • The second parameter to Array is necessary; if you omit it, Array will not grow in size.
  • I used 4096 since that's generally the size of a page of memory.
  • dumpStream returns a ubyte[] because char[] is defined as a UTF-8 string, which Stdin doesn't necessarily need to be. For example, if someone piped a binary file to your program, you would end up with an invalid char[] that could throw an exception if anything checks it for validity. If you only care about text, then casting the result to a char[] is fine.
  • copy is a method on the OutputStream interface that causes it to drain the provided InputStream of all input.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文