概要 †
- 文字列操作のための機能
定義 †
マクロ †
appendStringInfoCharMacro †
appendStringInfoCharMacro(str,ch)
- appendStringInfoChar()よりやや高速(マクロのインライン展開とある条件で領域拡張の処理のスキップ)
- バッファ領域長を超える場合はappendStringInfoChar()を呼ぶ(appendStringInfoChar()は,領域不足時に領域を拡張する),そうでない場合は文字配列を直接操作する
サンプル
StringInfoData strdata; initStringInfo(&strdata); appendStringInfoCharMacro(&strdata, 'A'); elog(NOTICE, "%s", strdata.data);
参考 appendStringInfoCharMacro - on doxygen.postgresql.org
列挙型 †
構造体 †
StringInfoData †
StringInfoは,StringInfoDataポインタの別名である
typedef StringInfoData *StringInfo;
番号 | データ型 | フィールド | 説明 |
---|---|---|---|
1 | char* | data | 文字列データのポインタ |
2 | int | len | 文字列の長さ(\0終端) |
3 | int | maxlen | バッファ割り当ての最大長 |
4 | int | cursor | 文字列バッファのカーソル,makeStringInfoやinitStringInfoで0初期化される。 stringinfo.cルーチンの中で都度更新されるフィールドでない |
関数 †
makeStringInfo †
// 引数1: extern StringInfo makeStringInfo(void);
- StringInfoDataを初期化してポインタを返す
サンプル
StringInfo *strinfo = makeStringInfo();
initStringInfo †
// 引数1:StringInfo構造体(StringInfoDataのポインタ) extern void initStringInfo(StringInfo str);
- StringInfoを初期化する,makeStringInfo()の中で呼ばれる
サンプル
StringInfo strinfo = (StringInfo) palloc(sizeof(StringInfoData)); initStringInfo(strinfo);
resetStringInfo †
// 引数1:StringInfo構造体(StringInfoDataのポインタ) extern void resetStringInfo(StringInfo str);
- バッファ領域はそのままで0初期化される
appendStringInfo †
// 引数1:StringInfo構造体(StringInfoDataのポインタ) // 引数2〜:strに追加する文字列のフォーマット指定子と追加する文字列 extern void appendStringInfo(StringInfo str, const char *fmt,...) pg_attribute_printf(2, 3);
- strバッファの末尾に文字列を追加する
- バッファ領域が不足している場合は拡張される
サンプル
StringInfo strinfo = makeStringInfo(); appendStringInfo(strinfo, "%s", "hoge"); elog(NOTICE, "%s", strinfo->data);
appendStringInfoVA †
// 引数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 †
// 引数1:StringInfo構造体(StringInfoDataのポインタ) // 引数2:追加する文字列 extern void appendStringInfoString(StringInfo str, const char *s);
- strのバッファ領域に文字列を追加する
- バッファ領域が不足している場合は拡張される
- memcpyでコピーする
appendStringInfoChar †
// 引数1:StringInfo構造体(StringInfoDataのポインタ) // 引数2:追加する文字(シングルバイト) extern void appendStringInfoChar(StringInfo str, char ch);
- strのバッファ領域に単一バイト文字を追加する
appendStringInfoSpaces †
// 引数1:StringInfo構造体(StringInfoDataのポインタ) // 引数2:追加するスペースサイズ extern void appendStringInfoSpaces(StringInfo str, int count);
- strのバッファ領域に指定された数分のスペースを追加する
appendBinaryStringInfo †
// 引数1:引数1:StringInfo構造体(StringInfoDataのポインタ) // 引数2:追加する文字列 // 引数3:文字列のサイズ extern void appendBinaryStringInfo(StringInfo str, const char *data, int datalen);
- strのバッファ領域にdataポインタで示される領域のdatalenサイズ分のデータをコピーする
- appendStringInfoString()関数内で呼ばれる
enlargeStringInfo †
// 引数1:引数1:StringInfo構造体(StringInfoDataのポインタ) // 引数2:拡張するサイズ(バイト) extern void enlargeStringInfo(StringInfo str, int needed);
- strのバッファ領域をneededバイト分拡張する
サンプルプログラム †
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); }
参考・関連 †
- stringinfo.h - on doxygen.postgresql.org
- stringinfo.c - on doxygen.postgresql.org
関連