#author("2017-05-16T00:08:52+09:00","default:haikikyou","haikikyou")
#author("2017-05-17T21:19:49+09:00","default:haikikyou","haikikyou")
[[PostgreSQL/開発]]

#contents

* 概要 [#x3212ce9]

- 文字列操作のための機能

* 定義 [#lfd0bcfa]

** マクロ [#wc26469d]

*** appendStringInfoCharMacro [#v7ac47f3]

#geshi(c){{
appendStringInfoCharMacro(str,ch)
}}

- appendStringInfoChar()よりやや高速(マクロのインライン展開とある条件で領域拡張の処理のスキップ)
- バッファ領域長を超える場合はappendStringInfoChar()を呼ぶ(appendStringInfoChar()は,領域不足時に領域を拡張する),そうでない場合は文字配列を直接操作する

&size(12){&color(white,#00afcc){ サンプル };};

#geshi(c){{
StringInfoData strdata;
initStringInfo(&strdata);
appendStringInfoCharMacro(&strdata, 'A');
elog(NOTICE, "%s", strdata.data);
}}

&size(12){&color(white,orange){ 参考 };}; [[appendStringInfoCharMacro >https://doxygen.postgresql.org/stringinfo_8h.html#aaac10cade3f97706962c52bbb5c8c6e9]] - on doxygen.postgresql.org
** 列挙型 [#zda8d716]

** 構造体 [#b3dbc701]

*** StringInfoData [#fdb4943b]

StringInfoは,StringInfoDataポインタの別名である

#geshi(c){{
typedef StringInfoData *StringInfo;
}}

|~番号|~データ型|~フィールド|~説明|h
|1|char*|data|文字列データのポインタ|
|2|int|len|文字列の長さ(\0終端)|
|3|int|maxlen|バッファ割り当ての最大長|
|4|int|cursor|文字列バッファのカーソル,makeStringInfoやinitStringInfoで0初期化される。&br;stringinfo.cルーチンの中で都度更新されるフィールドでない|
** 関数 [#jb5f6354]

*** makeStringInfo [#w4a8173c]

#geshi(c){{
// 引数1:
extern StringInfo makeStringInfo(void);
}}

- StringInfoDataを初期化してポインタを返す

&size(12){&color(white,#00afcc){ サンプル };};
#geshi(c){{
StringInfo *strinfo = makeStringInfo();
}}
*** initStringInfo [#o7d1b4fb]

#geshi(c){{
// 引数1:StringInfo構造体(StringInfoDataのポインタ)
extern void initStringInfo(StringInfo str);
}}

- StringInfoを初期化する,makeStringInfo()の中で呼ばれる

&size(12){&color(white,#00afcc){ サンプル };};
#geshi(c){{
StringInfo strinfo = (StringInfo) palloc(sizeof(StringInfoData));
initStringInfo(strinfo);
}}
*** resetStringInfo [#q3bfc3ec]

#geshi(c){{
// 引数1:StringInfo構造体(StringInfoDataのポインタ)
extern void resetStringInfo(StringInfo str);
}}

- バッファ領域はそのままで0初期化される
*** appendStringInfo [#hf190f75]

#geshi(c){{
// 引数1:StringInfo構造体(StringInfoDataのポインタ)
// 引数2〜:strに追加する文字列のフォーマット指定子と追加する文字列
extern void appendStringInfo(StringInfo str, const char *fmt,...) pg_attribute_printf(2, 3);
}}

- strバッファの末尾に文字列を追加する
- バッファ領域が不足している場合は拡張される

&size(12){&color(white,#00afcc){ サンプル };};
#geshi(c){{
StringInfo strinfo = makeStringInfo();
appendStringInfo(strinfo, "%s", "hoge");
elog(NOTICE, "%s", strinfo->data);
}}
*** appendStringInfoVA [#l2f2d632]

#geshi(c){{
// 引数1:StringInfo構造体(StringInfoDataのポインタ)
// 引数2〜:strに追加する文字列のフォーマット指定子と追加する文字列
// 引数3:追加する文字列の可変長引数
// 戻り値:=0:書き込み成功,≠0 追加に必要とされる領域サイズ
extern int appendStringInfoVA(StringInfo str, const char *fmt, va_list args) pg_attribute_printf(2, 0);
}}

- strのバッファ領域に文字列を追加する,
- 戻り値が0でない場合は,既存の文字列はそのままで追加に必要な見積もりサイズを返す

*** appendStringInfoString [#t6e76aff]

#geshi(c){{
// 引数1:StringInfo構造体(StringInfoDataのポインタ)
// 引数2:追加する文字列
extern void appendStringInfoString(StringInfo str, const char *s);
}}

- strのバッファ領域に文字列を追加する
- バッファ領域が不足している場合は拡張される
- memcpyでコピーする

*** appendStringInfoChar [#z8601226]

#geshi(c){{
// 引数1:StringInfo構造体(StringInfoDataのポインタ)
// 引数2:追加する文字(シングルバイト)
extern void appendStringInfoChar(StringInfo str, char ch);
}}

- strのバッファ領域に単一バイト文字を追加する

*** appendStringInfoSpaces [#o7f2de12]

#geshi(c){{
// 引数1:StringInfo構造体(StringInfoDataのポインタ)
// 引数2:追加するスペースサイズ
extern void appendStringInfoSpaces(StringInfo str, int count);
}}

- strのバッファ領域に指定された数分のスペースを追加する

*** appendBinaryStringInfo [#i6c08919]

#geshi(c){{
// 引数1:引数1:StringInfo構造体(StringInfoDataのポインタ)
// 引数2:追加する文字列
// 引数3:文字列のサイズ
extern void appendBinaryStringInfo(StringInfo str, const char *data, int datalen);
}}

- strのバッファ領域にdataポインタで示される領域のdatalenサイズ分のデータをコピーする
- appendStringInfoString()関数内で呼ばれる

*** enlargeStringInfo [#q1721071]

#geshi(c){{
// 引数1:引数1:StringInfo構造体(StringInfoDataのポインタ)
// 引数2:拡張するサイズ(バイト)
extern void enlargeStringInfo(StringInfo str, int needed);
}}

- strのバッファ領域をneededバイト分拡張する

* サンプルプログラム [#kf97c68f]

#geshi(c){{
void stringinfo_sample()
{
    // makeStringInfo()から初期化
    StringInfo strinfo = makeStringInfo();
    appendStringInfo(strinfo, "%s", "hoge");
    elog(NOTICE, "%s", strinfo->data);
    pfree(strinfo->data); // バッファ領域を解放
    pfree(strinfo); // 構造体の領域を解放

    // StringInfoData構造体を使う
    StringInfoData strdata;
    const char* string = "hello world";
    initStringInfo(&strdata); // バッファ領域初期化
    appendBinaryStringInfo(&strdata, string, strlen(string));
    elog(NOTICE, "%s", strdata.data);

    resetStringInfo(&strdata); // リセット,バッファ領域はそのまま
    appendStringInfoChar(&strdata, 'A');
    elog(NOTICE, "%s", strdata.data);
    pfree(strdata.data);
}
}}

* 参考・関連 [#b306106b]

- [[stringinfo.h>https://doxygen.postgresql.org/stringinfo_8h_source.html]] - on doxygen.postgresql.org
- [[stringinfo.c>https://doxygen.postgresql.org/stringinfo_8c_source.html]] - on doxygen.postgresql.org

* コメント [#q3200529]
&size(12){&color(white,orange){ 関連 };};

#comment
#related



トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP