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