Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

String.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * 文字列ヘッダ
00022  * @author Junpee
00023  */
00024 
00025 #ifndef STRING_H_
00026 #define STRING_H_
00027 
00028 namespace Lamp{
00029 
00030 //------------------------------------------------------------------------------
00031 /**
00032  * 文字列
00033  *
00034  * このクラスは継承しないで下さい。
00035  */
00036 class String{
00037 public:
00038     //--------------------------------------------------------------------------
00039     // 文字列の構築、破棄
00040     //--------------------------------------------------------------------------
00041     /**
00042      * コンストラクタ
00043      */
00044     String();
00045 
00046     /**
00047      * コンストラクタ
00048      *
00049      * 文字列による初期化
00050      * @param initString 初期化文字列
00051      */
00052     String(const char* initString);
00053 
00054     /**
00055      * コピーコンストラクタ
00056      * @param copy コピー元
00057      */
00058     String(const String& copy);
00059 
00060     /**
00061      * 代入演算子
00062      * @param copy 代入元
00063      */
00064     String& operator =(const String& copy);
00065 
00066     /**
00067      * 代入演算子
00068      * @param copy 代入元
00069      */
00070     String& operator =(const char* copy);
00071 
00072     /**
00073      * デストラクタ
00074      */
00075     ~String();
00076 
00077     //--------------------------------------------------------------------------
00078     // 文字列の情報を取得する
00079     //--------------------------------------------------------------------------
00080     /**
00081      * 文字列のバイト長を返す
00082      *
00083      * NULL終端を含まない文字列のバイト長を返します。
00084      * @return 文字列のバイト長
00085      */
00086     // 文字数と混同しないようにする為にlengthでは無くsizeにした。
00087     int getSize() const{ return size_; }
00088 
00089     /**
00090      * 文字数を返す
00091      * @return 文字列に含まれる文字の数
00092      */
00093     // 使用頻度が低く、誤用を防ぐためにlengthでは無くCharacterCountにした。
00094     int getCharacterCount() const;
00095 
00096     /**
00097      * 空文字列かどうか
00098      * @return 空文字列ならtrueを返す。
00099      */
00100     bool isEmpty() const{ return (size_ == 0); }
00101 
00102     /**
00103      * バイトシーケンスの取得
00104      *
00105      * 取得したバイトシーケンスは文字列に変更が加えられた時に
00106      * 失われる可能性があります。
00107      * 取得したポインタのデータを直接書き換えるとStringが正しく動作しません。
00108      * @return バイトシーケンス
00109      */
00110     const char* getBytes() const{
00111         if(string_ != NULL){ return string_; }
00112         return &nullString;
00113     }
00114 
00115     /**
00116      * 文字の取得
00117      * @param index 取得する文字のインデックス
00118      * @return インデックス位置にある文字
00119      */
00120     char charAt(int index) const{
00121         Assert(index >= 0);
00122         Assert(index <= size_);
00123         if(string_ != NULL){ return string_[index]; }
00124         return nullString;
00125     }
00126 
00127     /**
00128      * 部分文字列の取得
00129      * @param beginIndex 部分文字列の開始位置
00130      */
00131     String getSubstring(int beginIndex) const;
00132 
00133     /**
00134      * 部分文字列の取得
00135      * @param beginIndex 部分文字列の開始位置
00136      * @param endIndex 部分文字列の終了位置(終了文字のインデックス + 1)
00137      */
00138     String getSubstring(int beginIndex, int endIndex) const;
00139 
00140     /**
00141      * 大文字化した文字列の取得
00142      * @return 大文字化した文字列
00143      */
00144     String getUpperCase() const;
00145 
00146     /**
00147      * 小文字化した文字列の取得
00148      * @return 小文字化した文字列
00149      */
00150     String getLowerCase() const;
00151 
00152     /**
00153      * ハッシュコードの取得
00154      * @return ハッシュコード
00155      */
00156     u_int getHashCode() const;
00157 
00158     //--------------------------------------------------------------------------
00159     // 文字列をパースする
00160     //--------------------------------------------------------------------------
00161     /**
00162      * charへのパース
00163      * @param value [out] char化された値が代入されます
00164      * @return trueならパース成功
00165      */
00166     bool parseChar(char* value) const;
00167 
00168     /**
00169      * u_charへのパース
00170      * @param value [out] u_char化された値が代入されます
00171      * @return trueならパース成功
00172      */
00173     bool parseUChar(u_char* value) const;
00174 
00175     /**
00176      * shortへのパース
00177      * @param value [out] short化された値が代入されます
00178      * @return trueならパース成功
00179      */
00180     bool parseShort(short* value) const;
00181 
00182     /**
00183      * u_shortへのパース
00184      * @param value [out] u_short化された値が代入されます
00185      * @return trueならパース成功
00186      */
00187     bool parseUShort(u_short* value) const;
00188 
00189     /**
00190      * intへのパース
00191      * @param value [out] int化された値が代入されます
00192      * @return trueならパース成功
00193      */
00194     bool parseInt(int* value) const;
00195 
00196     /**
00197      * u_intへのパース
00198      * @param value [out] u_int化された値が代入されます
00199      * @return trueならパース成功
00200      */
00201     bool parseUInt(u_int* value) const;
00202 
00203     /**
00204      * floatへのパース
00205      * @param value [out] float化された値が代入されます
00206      * @return trueならパース成功
00207      */
00208     bool parseFloat(float* value) const;
00209 
00210     /**
00211      * doubleへのパース
00212      * @param value [out] double化された値が代入されます
00213      * @return trueならパース成功
00214      */
00215     bool parseDouble(double* value) const;
00216 
00217     //--------------------------------------------------------------------------
00218     // 文字列を変更する
00219     //--------------------------------------------------------------------------
00220     /**
00221      * 文字列の追加
00222      * @param appendString 連結する文字列
00223      * @return 変更済み文字列
00224      */
00225     String& append(const String& appendString);
00226 
00227     /**
00228      * 文字列の追加
00229      * @param appendString 連結する文字列
00230      * @return 変更済み文字列
00231      */
00232     String& append(const char* appendString);
00233 
00234     /**
00235      * 追加演算子
00236      * @param appendString 追加文字列
00237      * @return 変更済み文字列
00238      */
00239     String& operator +=(const String& appendString){
00240         return append(appendString);
00241     }
00242 
00243     /**
00244      * 追加演算子
00245      * @param appendString 追加文字列
00246      * @return 変更済み文字列
00247      */
00248     String& operator +=(const char* appendString){
00249         return append(appendString);
00250     }
00251 
00252     /**
00253      * フォーマット
00254      *
00255      * printfと同じ書式でフォーマットした文字列を設定する。
00256      * %sでStringを入れる場合はgetBytes()が必要
00257      * @param formatString フォーマット書式
00258      * @param ... 可変長引数
00259      * @return 変更済み文字列
00260      */
00261     String& format(const char* formatString, ...);
00262 
00263     //--------------------------------------------------------------------------
00264     // 文字列を比較する
00265     //--------------------------------------------------------------------------
00266     /**
00267      * 文字列の辞書式比較
00268      * @param compareString 比較する文字列
00269      */
00270     int compareTo(const String& compareString) const;
00271 
00272     /**
00273      * 文字列の辞書式比較
00274      * @param compareString 比較する文字列
00275      */
00276     int compareTo(const char* compareString) const;
00277 
00278     /**
00279      * 大文字、小文字を無視した文字列の辞書式比較
00280      * @param compareString 比較する文字列
00281      */
00282     int compareToIgnoreCase(const String& compareString) const;
00283 
00284     /**
00285      * 文字列の比較
00286      * @param compareString 比較する文字列
00287      * @return 文字列が同じであればtrue
00288      */
00289     bool equals(const String& compareString) const;
00290 
00291     /**
00292      * 文字列の比較
00293      * @param compareString 比較する文字列
00294      * @return 文字列が同じであればtrue
00295      */
00296     bool equals(const char* compareString) const;
00297 
00298     /**
00299      * 文字列の比較
00300      * @param compareString 比較する文字列
00301      * @return 同じ値であればtrueを返す
00302      */
00303     inline bool operator ==(const String& compareString) const{
00304         return equals(compareString);
00305     }
00306 
00307     /**
00308      * 文字列の比較
00309      * @param compareString 比較する文字列
00310      * @return 同じ値でなければtrueを返す
00311      */
00312     inline bool operator !=(const String& compareString) const{
00313         return !equals(compareString);
00314     }
00315 
00316     /**
00317      * 大文字、小文字を無視した文字列の比較
00318      * @param compareString 比較する文字列
00319      * @return 文字列が同じであればtrue
00320      */
00321     bool equalsIsIgnoreCase(const String& compareString) const;
00322 
00323     /**
00324      * 指定した文字列で始まるかどうか
00325      * @param prefix 比較する文字列
00326      * @return 指定した文字列で始まっていればtrue
00327      */
00328     bool startsWith(const String& prefix) const;
00329 
00330     /**
00331      * 指定した文字列で始まるかどうか
00332      * @param prefix 比較する文字列
00333      * @return 指定した文字列で始まっていればtrue
00334      */
00335     bool startsWith(const char* prefix) const;
00336 
00337     /**
00338      * 指定した文字列で終わるかどうか
00339      * @param suffix 比較する文字列
00340      * @return 指定した文字列で終わっていればtrue
00341      */
00342     bool endsWith(const String& suffix) const;
00343 
00344     /**
00345      * 指定した文字列で終わるかどうか
00346      * @param suffix 比較する文字列
00347      * @return 指定した文字列で終わっていればtrue
00348      */
00349     bool endsWith(const char* suffix) const;
00350 
00351     /**
00352      * 指定された文字が最初に出現する位置のインデックスを取得
00353      * @param searchChar 検索文字
00354      * @return 最初に出現した位置のインデックス。無ければ-1を返す。
00355      */
00356     int getIndexOf(const char searchChar) const;
00357 
00358     /**
00359      * 指定された文字列が最初に出現する位置のインデックスを取得
00360      * @param searchString 検索文字列
00361      * @return 最初に出現した位置のインデックス。無ければ-1を返す。
00362      */
00363     int getIndexOf(const char* searchString) const;
00364 
00365     /**
00366      * 指定された文字列が最初に出現する位置のインデックスを取得
00367      * @param searchString 検索文字列
00368      * @return 最初に出現した位置のインデックス。無ければ-1を返す。
00369      */
00370     int getIndexOf(const String& searchString) const;
00371 
00372     /**
00373      * 指定された文字が最後に出現する位置のインデックスを取得
00374      * @param searchChar 検索文字
00375      * @return 最後に出現した位置のインデックス。無ければ-1を返す。
00376      */
00377     int getLastIndexOf(const char searchChar) const;
00378 
00379     /**
00380      * 指定された文字列が最後に出現する位置のインデックスを取得
00381      * @param searchString 検索文字列
00382      * @return 最後に出現した位置のインデックス。無ければ-1を返す。
00383      */
00384     int getLastIndexOf(const char* searchString) const;
00385 
00386     /**
00387      * 指定された文字列が最後に出現する位置のインデックスを取得
00388      * @param searchString 検索文字列
00389      * @return 最後に出現した位置のインデックス。無ければ-1を返す。
00390      */
00391     int getLastIndexOf(const String& searchString) const;
00392 
00393 #ifdef _DEBUG
00394     /**
00395      * デバッグ用文字列情報出力
00396      */
00397     void debugPrint();
00398 
00399 #endif// End of _DEBUG
00400 
00401 private:
00402     /**
00403      * 内部使用コンストラクタ
00404      *
00405      * 文字列と長さによる初期化。空文字列を渡してはいけない。
00406      * リファレンスカウンタをインクリメントしておく必要がある。
00407      * @param initString 初期化文字列
00408      * @param initLength 初期化文字列長
00409      */
00410     String(char* initString, int initLength);
00411 
00412     // 文字列
00413     char* string_;
00414     // 文字列サイズ
00415     int size_;
00416     // ヌル文字列
00417     static const char nullString = '\0';
00418     // フォーマットデフォルト長
00419     static const int formatDefaultLength = 64;
00420     // フォーマットスケール
00421     static const int formatScale = 2;
00422 
00423 };
00424 
00425 //------------------------------------------------------------------------------
00426 } // End of namespace Lamp
00427 //------------------------------------------------------------------------------
00428 /**
00429  * 文字列の連結
00430  * @param leftString 演算子の左側の文字列
00431  * @param rightString 演算子の右側の文字列
00432  * @return 連結された文字列
00433  */
00434 const Lamp::String operator+(
00435     const Lamp::String& leftString, const Lamp::String& rightString);
00436 
00437 //------------------------------------------------------------------------------
00438 #endif // End of STRING_H_
00439 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:36 2005 for Lamp by doxygen 1.3.2