<?xml version="1.0" encoding="utf-8" ?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" 
			xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" 
			xmlns:cc="http://web.resource.org/cc/" xml:lang="ja">
<channel rdf:about="http://libc.blog47.fc2.com/?xml">
<title>標準Cライブラリの実装</title>
<link>http://libc.blog47.fc2.com/</link>
<description>C99に対応した標準Cライブラリの実装レポートを行っていきます。</description>
<dc:language>ja</dc:language>
<items>
<rdf:Seq>
<rdf:li rdf:resource="http://libc.blog47.fc2.com/blog-entry-80.html" />
<rdf:li rdf:resource="http://libc.blog47.fc2.com/blog-entry-79.html" />
<rdf:li rdf:resource="http://libc.blog47.fc2.com/blog-entry-78.html" />
<rdf:li rdf:resource="http://libc.blog47.fc2.com/blog-entry-77.html" />
<rdf:li rdf:resource="http://libc.blog47.fc2.com/blog-entry-76.html" />
</rdf:Seq>
</items>
</channel>
<item rdf:about="http://libc.blog47.fc2.com/blog-entry-80.html">
<link>http://libc.blog47.fc2.com/blog-entry-80.html</link>
<title>ソースコードのライセンスについて</title>
<description> しばらく忙しくて放置していますが、また春頃になれば、それなりに注力できる可能性があります。もっとも、そのころになれば、JSPカーネルではなく、ASPカーネルに対応することを考えるような気もしますが...。さて、このたび、このブログで公開しているソースコードのライセンスに関して質問を受けました。このブログは、あくまでも実装レポートで、私が実装中に考えたことや、判断に迷ったことなどを交えながら、記録を残して行
 </description>
<content:encoded>
<![CDATA[ しばらく忙しくて放置していますが、また春頃になれば、それなりに注力できる可能性があります。もっとも、そのころになれば、JSPカーネルではなく、ASPカーネルに対応することを考えるような気もしますが...。<br /><br />さて、このたび、このブログで公開しているソースコードのライセンスに関して質問を受けました。このブログは、あくまでも実装レポートで、私が実装中に考えたことや、判断に迷ったことなどを交えながら、記録を残して行けたらと思って始めたものです。現在公開されている内容も、ざっと動作は見ていますが、いろいろバグも指摘されていますし、私自身も過去の記事のバグを見つけています。<br /><br />そんな感じで、まだ現状では非常に完成度は低いと考えてください。それでもよければ、あくまでも自己責任の下で、自由に利用していただいてかまいません。ただし、（合法・非合法にかかわらず）反社会的または反人道的な用途への利用だけはご遠慮ください。報告も著作権表示も対価も不要ですが、何らかの形でご一報くださると管理人は喜びます。<br /><br />なお、ブログの本文に関しては、リンクを貼るか、引用元を記載していただければ、常識の範囲で引用していただいてかまいません。<br /><br />何か不明の点がありましたら、コメントなどでご質問ください。<br /> ]]>
</content:encoded>
<dc:subject>未分類</dc:subject>
<dc:date>2006-11-26T00:02:22+09:00</dc:date>
<dc:creator>高木信尚</dc:creator>
<dc:publisher>FC2-BLOG</dc:publisher>
</item>
<item rdf:about="http://libc.blog47.fc2.com/blog-entry-79.html">
<link>http://libc.blog47.fc2.com/blog-entry-79.html</link>
<title>atof関数</title>
<description> 順番からすると今回はatof関数を取り上げる必要がありそうです。この関数も、atol関数などと同様、エラー処理ができないので推奨できないもののひとつです。今回の実装でも、規格との互換性のために申し訳程度の実装しか行いません。static __inline__ double atof(const char *__s){&amp;nbsp; return strtod(__s, NULL);}このように、atol関数と同様、&amp;lt;stdlib.h&amp;gt;ヘッダ内ではインライン関数として定義し、同じ内容のものを外部
 </description>
<content:encoded>
<![CDATA[ 順番からすると今回はatof関数を取り上げる必要がありそうです。この関数も、atol関数などと同様、エラー処理ができないので推奨できないもののひとつです。今回の実装でも、規格との互換性のために申し訳程度の実装しか行いません。<br /><code><br />static __inline__ double atof(const char *__s)<br />{<br />&nbsp; return strtod(__s, NULL);<br />}<br /></code><br />このように、atol関数と同様、&lt;stdlib.h&gt;ヘッダ内ではインライン関数として定義し、同じ内容のものを外部関数としても定義します。内部的にstrtod関数を呼び出しますので、効率面からもatof関数を使用するメリットは何もありません。<br /><br />ところで、strtod関数もそうなのですが、atof関数はC99から機能が拡張されています。というのも、C99では16進浮動小数点定数というものが導入されたこともあり、atof関数やstrtod関数でも16進浮動小数点数を解釈できるようになっています。詳しくはstrtod関数の回に解説したいと思います。<br /> ]]>
</content:encoded>
<dc:subject>一般ユーティリティ</dc:subject>
<dc:date>2006-07-28T01:54:44+09:00</dc:date>
<dc:creator>高木信尚</dc:creator>
<dc:publisher>FC2-BLOG</dc:publisher>
</item>
<item rdf:about="http://libc.blog47.fc2.com/blog-entry-78.html">
<link>http://libc.blog47.fc2.com/blog-entry-78.html</link>
<title>strtoul関数とstrtoull関数</title>
<description> strtol関数はlong型を返しましたが、strtoul関数はunsigned long型を返します。基本的な違いはそれだけです。細かな部分では、符号無し整数しか扱いませんので、'-'符号が現れるとそこで処理を終えますし、オーバーフローの判定はLONG_MAXではなくULONG_MAXで行います。strtoull関数はunsigned long long型を返す関数ですが、strtoul関数とほとんど内容は同じですので、具体的な解説は割愛します。strtoul関数の実装のうち、unsign
 </description>
<content:encoded>
<![CDATA[ strtol関数はlong型を返しましたが、strtoul関数はunsigned long型を返します。基本的な違いはそれだけです。細かな部分では、符号無し整数しか扱いませんので、'-'符号が現れるとそこで処理を終えますし、オーバーフローの判定はLONG_MAXではなくULONG_MAXで行います。<br /><br />strtoull関数はunsigned long long型を返す関数ですが、strtoul関数とほとんど内容は同じですので、具体的な解説は割愛します。strtoul関数の実装のうち、unsigned longの部分をunsigned long longに、ULONG_MAXの部分をULLONG_MAXに読み替えれば、strtoull関数になると思います。<br /><br />それでは、strtoul関数の実装です。<br /><code><br />#include &lt;limits.h&gt;<br />#include &lt;ctype.h&gt;<br />#include &lt;errno.h&gt;<br /><br />int _space_sign(const char *s, const char **endptr);<br /><br />unsigned long strtoul(const char * __restrict__ s, char ** __restrict__ endptr, int radix)<br />{<br />&nbsp; unsigned long result;<br /><br />&nbsp; if (_space_sign(s, (const char**)&amp;s) != 0)<br />&nbsp; &nbsp; --s;&nbsp; // '-'の位置まで戻す<br /><br />&nbsp; if (s[0] == '0')<br />&nbsp; {<br />&nbsp; &nbsp; ++s;<br />&nbsp; &nbsp; if ((s[1] | 0x20) == 'x')<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; if (radix == 0 || radix == 16)<br />&nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; ++s;<br />&nbsp; &nbsp; &nbsp; &nbsp; radix = 16;<br />&nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; else if (radix == 0)<br />&nbsp; &nbsp; &nbsp; radix = 8;<br />&nbsp; }<br />&nbsp; else if (radix == 0)<br />&nbsp; &nbsp; radix = 10;<br /><br />&nbsp; int c;<br />&nbsp; for (result = 0; c = tolower((unsigned char)*s), isdigit(c) || ('a' &lt;= c &amp;&amp; c &lt;= 'z'); s++)<br />&nbsp; {<br />&nbsp; &nbsp; int d = isdigit(c) ? c - '0' : c - 'a' + 10;<br />&nbsp; &nbsp; if (d >= radix)<br />&nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; if (result &gt; (ULONG_MAX - d) / radix)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; errno = ERANGE;<br />&nbsp; &nbsp; &nbsp; result = ULONG_MAX;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; else<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; result = result * radix + d;<br />&nbsp; &nbsp; }<br />&nbsp; }<br /><br />&nbsp; if (endptr != NULL)<br />&nbsp; &nbsp; *endptr = (char*)s;<br /><br />&nbsp; return result;<br />}<br /></code><br />細部を除けばstrtol関数と違いはありませんので、strtol関数と異なる部分だけ解説したいと思います。<br /><br />まず、strtol関数では_space_sign関数の返却値をsignフラグに格納していましたが、strtoul関数では_space_sign関数が非0（＝負）を返した場合は、ポインタを1つ戻し、'-'文字を指すようにしています。<br /><br />次に、LONG_MAXを用いてオーバーロード判定を行っていた箇所は、ULONG_MAXを用いて判定しています。その際、signフラグによる補正もなくなっています。また、オーバーフロー発生時は常にULONG_MAXを返すようにしています。<br /><br />最後に、signフラグがないため、結果はresultをそのまま返すようにしています。<br /><br /> ]]>
</content:encoded>
<dc:subject>一般ユーティリティ</dc:subject>
<dc:date>2006-07-19T10:26:53+09:00</dc:date>
<dc:creator>高木信尚</dc:creator>
<dc:publisher>FC2-BLOG</dc:publisher>
</item>
<item rdf:about="http://libc.blog47.fc2.com/blog-entry-77.html">
<link>http://libc.blog47.fc2.com/blog-entry-77.html</link>
<title>テンプレートを変更しました。</title>
<description> このブログにとっては初めてのテンプレート変更です。ソースコードを書くには横幅がある程度必要ですので、今回の変更に踏み切りました。これで多少は本文が見やすくなったかと思います。
 </description>
<content:encoded>
<![CDATA[ このブログにとっては初めてのテンプレート変更です。ソースコードを書くには横幅がある程度必要ですので、今回の変更に踏み切りました。これで多少は本文が見やすくなったかと思います。 ]]>
</content:encoded>
<dc:subject>未分類</dc:subject>
<dc:date>2006-07-17T20:40:56+09:00</dc:date>
<dc:creator>高木信尚</dc:creator>
<dc:publisher>FC2-BLOG</dc:publisher>
</item>
<item rdf:about="http://libc.blog47.fc2.com/blog-entry-76.html">
<link>http://libc.blog47.fc2.com/blog-entry-76.html</link>
<title>strtol関数とstrtoll関数</title>
<description> 久々の更新になりますが、今回はstrtol関数とstrtoll関数です。これらの関数は、返却値の型がlong型かlong long型かの違いだけですので、ここではstrtol関数に絞って書くことにします。strtoll関数はlong型のところをlong long型に、LONG_MAXのところをLLONG_MAXに読み替えればよいだけです。それでは早速実装を見てみましょう。#include &amp;lt;limits.h&amp;gt;#include &amp;lt;ctype.h&amp;gt;#include &amp;lt;errno.h&amp;gt;int _space_sign(const 
 </description>
<content:encoded>
<![CDATA[ 久々の更新になりますが、今回はstrtol関数とstrtoll関数です。これらの関数は、返却値の型がlong型かlong long型かの違いだけですので、ここではstrtol関数に絞って書くことにします。strtoll関数はlong型のところをlong long型に、LONG_MAXのところをLLONG_MAXに読み替えればよいだけです。<br /><br />それでは早速実装を見てみましょう。<br /><code><br />#include &lt;limits.h&gt;<br />#include &lt;ctype.h&gt;<br />#include &lt;errno.h&gt;<br /><br />int _space_sign(const char *s, const char **endptr);<br /><br />long strtol(const char * __restrict__ s, char ** __restrict__ endptr, int radix)<br />{<br />&nbsp; int sign = _space_sign(s, (const char**)&amp;s);<br />&nbsp; long result;<br /><br />&nbsp; if (s[0] == '0')<br />&nbsp; {<br />&nbsp; &nbsp; ++s;<br />&nbsp; &nbsp; if ((s[1] | 0x20) == 'x')<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; if (radix == 0 || radix == 16)<br />&nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; ++s;<br />&nbsp; &nbsp; &nbsp; &nbsp; radix = 16;<br />&nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; else if (radix == 0)<br />&nbsp; &nbsp; &nbsp; radix = 8;<br />&nbsp; }<br />&nbsp; else if (radix == 0)<br />&nbsp; &nbsp; radix = 10;<br /><br />&nbsp; int c;<br />&nbsp; for (result = 0; c = tolower((unsigned char)*s), isdigit(c) || ('a' &lt;= c &amp;&amp; c &lt;= 'z'); s++)<br />&nbsp; {<br />&nbsp; &nbsp; int d = isdigit(c) ? c - '0' : c - 'a' + 10;<br />&nbsp; &nbsp; if (d >= radix)<br />&nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; if (result &gt; (LONG_MAX - d - sign) / radix)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; errno = ERANGE;<br />&nbsp; &nbsp; &nbsp; result = sign ? LONG_MIN : LONG_MAX;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; else<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; result = result * radix + d;<br />&nbsp; &nbsp; }<br />&nbsp; }<br /><br />&nbsp; if (endptr != NULL)<br />&nbsp; &nbsp; *endptr = (char*)s;<br /><br />&nbsp; if (sign != 0)<br />&nbsp; &nbsp; result = -result;<br />&nbsp; return result;<br />}<br /></code><br />このあたりの関数になってくると、ソースコードもそれなりに複雑になってきますが、ポイントだけをかいつまんで解説することにします。そろそろブログ上でもソースにまともなコメントを入れた方がよさそうな気がしてきましたが、とりあえずこのままで突き進みます。<br /><br />まず、最初に<a href="http://libc.blog47.fc2.com/blog-entry-74.html">atoi関数</a>の回に作成した_space_sign関数を呼び出して、空白類文字の読み飛ばしと符号の判定を行っています。この際、仮引数 s の__restrict__修飾子が原因で警告が出るので、const char**で &s をキャストしています。あまり望ましくない回避方法かもしれませんが、実害はないのでこうしています。<br /><br />次に、16進または8進の接頭辞である 0x および 0 の処理を行っています。0x の処理では、文字を強制的に小文字化するために、s[1] | 0x20という手法を採っています。この手法は移植性はありませんが、まともに'x'と'X'の両方を判定したり、tolower関数を使うより高速です。<br /><br />その次に現れるfor文はatoi関数を拡張したようなものです。扱う数字が'0'から'9'だけでなく、'a'から'z'を10から35に解釈しなければならないこともあり、またオーバーフローの判定が必要なこともあって、かなり複雑になっています。<br /><br />先ほどの小文字化の強制のときもそうであったように、ここでも英字の判定は処理系に依存します。標準Cでは、英字の連続性は保証されませんが、今回は対象とする処理系が固定ですので、敢えて規格厳密合致プログラムを捨てて効率を優先しています。<br /><br />ちなみにisalnum関数を使用していないのは、将来"C"ロケール以外に対応したときのことを考慮してです。"C"ロケール以外では、isalnum関数がどんな文字に対して真を返すか分からないので、正確に'A'から'Z'および'a'から'z'だけを検出したい場合には使えないのです。<br /><br />result > (LONG_MAX - d - sign) / radix という条件式はオーバーフローの判定ですが、かなり難解になってしまいました。というのも、値が負の場合の境界はLONG_MINであり、これは -LONG_MAX-1 であるため、絶対値が異なるからです。そこで負であることを表すフラグ sign を用いて補正しています。後は見ての通りの内容ですので、説明は不要かと思います。<br /><br />なお、strtol関数やstrtoll関数は、"C"ロケール以外ではもっと別の形式を解釈できてもよいことになっています。例えば、3桁ごとの区切り文字をlconv構造体のthouosands_sepフィールドを元に解釈してもよいですし、極端な話、漢数字を解釈してもよいわけですが、多くの場合、そこまでする必要性もないですし、仮にそのような用途があっても、別の専用関数を用意した方がよいでしょう。<br /><br /><font color="#FF0000">2006/07/19 バグがいくつか見つかったので修正しました。</font> ]]>
</content:encoded>
<dc:subject>一般ユーティリティ</dc:subject>
<dc:date>2006-07-17T20:16:07+09:00</dc:date>
<dc:creator>高木信尚</dc:creator>
<dc:publisher>FC2-BLOG</dc:publisher>
</item>
</rdf:RDF>