nanika/raw_string.h の提供するクラス nanika::raw_string は,NULL 文字を終端とする文字列処理を補助する程度の能力を持っています.文字列の開始アドレスのみをメンバとして持ち,メモリ管理もしないので,オブジェクトの作成や複製のコストを気にせず使うことができます.ただし,使い道はほとんどありません.
// find() が失敗したときの返り値です. enum { npos = -1 };
以下の関数オブジェクトを使用することで,アルファベットを小文字もしくは大文字に統一しつつ比較などがおこなえます.
// 文字をそのまま返します. struct as_is; // アルファベットの大文字を小文字に変換します. struct lower; // アルファベットの小文字を大文字に変換します. struct upper;
不要な文字を判定するデフォルトの関数オブジェクトです.
// 文字が空白かどうかを判定します. struct is_space;
// 空文字列を作成(定数文字列のアドレスを保持)します. raw_string(); // 受け取った文字列のアドレスを格納します. // ただし,NULL のときは空文字列を作成します. raw_string(const char *str); // 受け取った文字列のアドレスに index を加えて格納します. // ただし,NULL のときは空文字列を作成します. raw_string(const char *str, int index);
オペレータ <, <=, >, >=, ==, != を使用して,文字列同士の比較が可能です.
以下の関数が持つ引数 as に対して lower や upper を渡すことにより,文字列中のアルファベットを小文字もしくは大文字に統一しつつ処理できます.また,関数 trim を用いて文字列の先頭にある不要な文字を破棄するとき,引数 space に判定用の関数オブジェクトを指定することができます.
// 文字列を比較して大小関係を返します. // 引数の方が大きいときは負の値,引数の方が小さいときは正の値, // 引数と等しいときは 0 を返します. template <typename As> int compare(const raw_string &str, As as) const; template <typename As> int compare(const raw_string &str, int index, As as) const; template <typename As> int compare(const raw_string &str, int index, int n, As as) const; int compare(const raw_string &str) const; int compare(const raw_string &str, int index) const; int compare(const raw_string &str, int index, int n) const;
// 引数の文字列により始まるかどうかを調べます. template <typename As> bool starts_with(const raw_string &str, As as) const; bool starts_with(const raw_string &str) const;
// 新しい文字列を格納します. raw_string &assign(const raw_string &str); raw_string &assign(const raw_string &str, int index);
// 引数の文字列が最初に出現する位置を求めます. // 見つからなければ npos を返します. template <typename As> int find(const raw_string &str, As as) const; template <typename As> int find(char c, As as) const; int find(const raw_string &str) const; int find(char c) const;
// 引数の文字列に含まれる文字が最初に出現する位置を求めます. // 見つからなければ npos を返します. template <typename As> int find_first_of(const raw_string &set, As as) const; template <typename As> int find_first_of(char c, As as) const; int find_first_of(const raw_string &set) const; int find_first_of(char c) const;
// 引数の文字列に含まれない文字が最初に出現する位置を求めます. // 見つからなければ npos を返します. template <typename As> int find_first_not_of(const raw_string &set, As as) const; template <typename As> int find_first_not_of(char c, As as) const; int find_first_not_of(const raw_string &set) const; int find_first_not_of(char c) const;
// 先頭 n 文字をバッファに格納します. template <typename As> int copy(char *buf, int n, As as) const; int copy(char *buf, int n) const;
// 部分文字列(後半部分)を取り出します. raw_string substr(int index) const;
// 2 つの文字列を交換します. void swap(raw_string &str);
// 文字列の先頭にある空白を削除します. template <typename Space> raw_string trim_start(Space space) const; raw_string trim_start() const;
// 指定位置の文字を取り出します. char operator[](int index) const;
// 文字列の長さが 0 かどうかを返します. bool empty() const; // 文字列を走査して長さを返します. int size() const; int length() const;
// 格納されているポインタを返します. const char *data() const; const char *c_str() const;
オペレータ <, <=, >, >=, ==, != により,C 文字列と nanika::raw_string を文字列として比較できます.
また,出力ストリーム std::ostream に対するオペレータ << が定義されています.