/*! * bnf::String * * Copyright (c) 2007 tkuri {at} fat.coara.or.jp */ #ifndef BNF_STRING_HPP_ #define BNF_STRING_HPP_ /* * 文字型 char_t * * 文字列クラス String */ // 今回は,bnf::String の実体として std::basic_string を使っています. // プロジェクトの都合に合わせて,実装しなおしてください. #include #include "bnf.hpp" namespace bnf { /*! * String * * bnf::String クラスに必要なインタフェースを明示するため * あえてここでクラスを宣言していますが, * 実質的にはただの std::basic_string です。 * (size_type は int に代えていますが) * * プロジェクトの都合などで標準ライブラリが使えない場合は * このインタフェースを維持したまま,実装し直してください。 * * bnf::Map オブジェクトのキーになります. * bnf::Map クラスの実装に合わせて,必要な関数を追加してください. * たとえば std::map のキーにするには,比較演算子が必要なので * ここではその関数を実装しています. */ class String { typedef std::basic_string raw_t; typedef raw_t::size_type size_type; raw_t body_; public: /*! * コンストラクタ */ String() { } /*! * コンストラクタ */ String(char_t const * aRawStr) : body_(aRawStr) { } /*! * コピーコンストラクタ */ String(String const & another) : body_(another.body_) { } /*! * 代入 */ String& operator = (String const & another) { body_ = another.body_; return *this; } /*! * 文字追加 */ String& operator += (char_t aChar) { body_ += aChar; return *this; } /*! * 文字取得 */ char_t operator [] (int aIndex) const { return body_[aIndex]; } /*! * 長さ取得 */ int length() const { return static_cast(body_.length()); } /*! * 文字列比較 * aPos の位置から長さ aLen の部分文字列と,aStr を比較します. */ int compare(int aPos, int aLen, String const & aStr) const { return body_.compare( static_cast( aPos), static_cast( aLen), aStr.body_); } /*! * Cタイプの文字列表現 */ const char_t * c_str() const { return body_.c_str(); } // // 次の関数は IMatcher の str() でよく使いますが // str() はただのデバッグ用です。 // 不要といえば不要です。 // /*! * 文字列追加 */ String& operator += (String const & another) { body_ += another.body_; return *this; } // // 次の関数は,Map のキーにするために必要な比較演算子です. // bnf モジュール内で明示的に使っているわけではありません. // Map の実装によっては不要です. // /*! * 比較 */ bool operator < (String const & another) const { return body_ < another.body_; } /*! * 比較 */ bool operator == (String const & another) const { return body_ == another.body_; } }; } // end of namespace #endif // BNF_STRING_HPP_