前の月 / 次の月 / 最新

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 になっている。

2010-03-06 Sat

n進数的文字列を数値に変換 [D言語]

std.conv の to で、出来そうで出来ない。
to で出来るのは10進数の変換。

auto a = to!(int)("0xFF"); //NG
auto b = to!(int)("255"); //OK

std.c.stdlib の strto* を使う。

2010-03-05 Fri

std.regex での正規表現にはWYSIWYG文字列を使う [D言語]

std.regex での正規表現で普通の文字列を使うと、
regex 関数に渡す \ の数とマッチされる文字列の \ の数が、
一致しないので、しんどい。

auto r2 = regex("\"\\\""); //\"\\\" にマッチしないで \"\"にマッチ
auto r3 = regex("\"\\\\\""); //\"\\\" にマッチ

WYSIWYG文字列つかうとエスケープが楽になる。

auto r1 = regex(`"\\"`); //\"\\\" にマッチ
auto r2 = regex(r"`\\`"); //\`\\` にマッチ

この場合 " と ` が混在する \"\\` にマッチ正規表現が書けない。

そこで、" と ` が混在できるデリミタ指定文字列がおすすめ。

auto r1 = regex(q"{"\\`}"); //\"\\` にマッチ

過去ログ

2011 : 01 02 03 04 05 06 07 08 09 10 11 12
2010 : 01 02 03 04 05 06 07 08 09 10 11 12
2009 : 01 02 03 04 05 06 07 08 09 10 11 12
2008 : 01 02 03 04 05 06 07 08 09 10 11 12
2007 : 01 02 03 04 05 06 07 08 09 10 11 12