前の日 / 次の日 / 最新 / 2010-03

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

2010-03-13 Sat

chunk をデコード [D言語]

2009-08-21-1 のコードを,
std.regex を使って書き直し。

import std.string;
import std.regex;
import std.c.stdlib;
import core.stdc.errno;
byte[] decodeChunked(byte[] src)
{
    string entityBody;
    char* endptr;
    auto reg = regex(q"{[\da-fA-F]+[^\r\n]*\r\n}"); //16進

    auto m = match(cast(char[])src,reg);

    errno = 0;
    auto chunkSize = strtol(m.hit.toStringz,&endptr,16);
    if (errno == ERANGE){}
    if (m.hit.toStringz == endptr) {}

    while(chunkSize > 0)
    {
        entityBody ~= m.post[0 .. chunkSize];
        m = match(m.post[chunkSize .. $],reg);
        errno = 0;
        chunkSize = strtol(m.hit.toStringz,&endptr,16);
        if (errno == ERANGE){}
        if (m.hit.toStringz == endptr) {}
    }

    return entityBody;
}

stdtol の戻り値は long じゃ無くて int [D言語]

http://www.kmonos.net/alang/d/2.0/phobos/std_c_stdlib.html

long strtol(in char*, char**, int);
uint strtoul(in char*, char**, int);

と記述されてた。

なんか strtol の戻り値が変。

core.stdc.stdlibを見ると、
strtol の戻り値が c_long 型になっていた。

core.stdc.configを見ると、
windows 上だと c_long 型は、
int の alias になっていた。

英語のドキュメントを見る。
http://www.digitalmars.com/d/2.0/phobos/std_c_stdlib.html

1.0のドキュメントを見る。
http://www.digitalmars.com/d/1.0/phobos/std_c_stdlib.html
long になっている。