3

问题是:在运行时设置编码时如何解析文件?

编码可以是:utf-8utf-16latin1其他

目标是将 ubyte[] 从所选编码转换为字符串。因为当您使用 std.stdio.File.byChunk 或 std.mmFile.MmFile 时,您将 ubyte[] 作为数据。

4

4 回答 4

1

您是否正在尝试将文本文件转换为 utf-8?如果答案是“是”,Phobos 有专门的功能:@trusted string toUTF8(in char[] s). 有关详细信息,请参阅http://dlang.org/phobos/std_utf.html

对不起,如果它不是你需要的。

于 2012-03-11T03:17:45.417 回答
0

我找到了一种方法,也许使用 std.algorithm.reduce 应该更好

import std.string;
import std.stdio;
import std.encoding;
import std.algorithm;

void main( string[] args ){
    File f = File( "pathToAfFile.txt", "r" );
    size_t i;
    auto e = EncodingScheme.create("utf-8");
    foreach( const(ubyte)[] buffer; f.byChunk( 4096 ) ){
        size_t step = 0;
        if( step == 0 ) step = e.firstSequence( buffer );
        for( size_t start; start + step < buffer.length; start = start + step )
            write( e.decode( buffer[start..start + step] ) );
    }
}
于 2012-03-10T21:09:56.313 回答
0

File.byChunk 返回一个范围,该范围通过前面返回一个 ubyte[]。

快速的 Google 搜索似乎表明 UTF-8 使用 1 到 6 个字节对数据进行编码,因此只需确保您始终拥有 6 个字节的数据,并且可以使用 std.encoding 的解码将其转换为 dchar 字符。然后,您可以使用 std.utf 的 toUFT8 转换为常规字符串而不是 dstring。

下面的转换函数将任何无符号数组范围转换为字符串。

import std.encoding, std.stdio, std.traits, std.utf;

void main()
{
    File input = File("test.txt");

    string data = convert(input.byChunk(512));

    writeln("Data: ", data);
}

string convert(R)(R chunkRange) 
in
{
    assert(isArray!(typeof(chunkRange.front)) && isUnsigned!(typeof(chunkRange.front[0])));
} 
body
{
    ubyte[] inbuffer;
    dchar[] outbuffer;

    while(inbuffer.length > 0 || !chunkRange.empty)
    {
        while((inbuffer.length < 6) && !chunkRange.empty)// Max UTF-8 byte length is 6
        {
            inbuffer ~= chunkRange.front;
            chunkRange.popFront();
        }

        outbuffer ~= decode(inbuffer);
    }

    return toUTF8(outbuffer); // Convert to string instead of dstring
}
于 2012-09-25T12:21:59.430 回答
0

D 字符串已经是 UTF-8。无需转码。您可以使用validatefromstd.utf检查文件是否包含有效的 UTF-8。如果您使用readTextfrom std.file,它将为您进行验证。

于 2012-03-11T13:21:54.773 回答