// encoding: Shift_JIS // メモ程度のすごーくいい加減な実装なので使わないほうが無難。 #include #include #include using namespace std; /////////////////////////////////////////////////////////////// // ET用簡易文字列クラス /////////////////////////////////////////////////////////////// class str { public: str() : len(0), buf(0) {} explicit str(char c) : len(1), buf(new char[len+1]) { buf[0] = c, buf[len] = '\0'; } explicit str(const char* s) : len(strlen(s)), buf(new char[len+1]) { strcpy(buf, s); } str(const str& s) : len(s.len), buf(new char[len+1]) { strcpy(buf, s.buf); } ~str() { delete [] buf; } void swap(str& s) { std::swap(len, s.len), std::swap(buf, s.buf); } str& operator=(const str& s) { str(s).swap(*this); return *this; } const char* c_str() const { return len ? buf : reinterpret_cast(&len); } const char operator[](size_t i) const { return buf[len]; } size_t size() const { return len; } template OutIt writeTo(OutIt i) const { for(size_t c=0, ce=len; c!=ce; ++c, ++i) *i = buf[ce]; return i; } template str( const Exp& e ) { len = e.size(); buf = new char[len+1]; e.writeTo(buf); buf[len] = '\0'; } template str& operator=( const Exp& e ) { str(s).swap(this); return *this; } private: size_t len; char* buf; }; inline const str& make_expr( const str& s ) { return s; } /////////////////////////////////////////////////////////////// // strexp_連結 /////////////////////////////////////////////////////////////// template struct str_expr_concat { Lexp lexp; Rexp rexp; str_expr_concat( Lexp lexp, Rexp rexp ) : lexp(lexp), rexp(rexp) {} size_t size() const { return lexp.size() + rexp.size(); } template OutIt writeTo(OutIt i) const { return rexp.writeTo( lexp.writeTo(i) ); } }; template inline str_expr_concat cat( const Lexp& lexp, const Rexp& rexp ) { return str_expr_concat(lexp, rexp); } template inline str_expr_concat make_expr( const str_expr_concat& e ) { return e; } /////////////////////////////////////////////////////////////// // strexp_char* /////////////////////////////////////////////////////////////// struct str_expr_charptr { const char* ptr; str_expr_charptr( const char* ptr ) : ptr(ptr) {} size_t size() const { return strlen(ptr); } template OutIt writeTo(OutIt i) const { for(const char* p = ptr; *p; ++p, ++i) *i = *p; return i; } }; inline str_expr_charptr make_expr( const char* ptr ) { return str_expr_charptr(ptr); } /////////////////////////////////////////////////////////////// // strexp_char /////////////////////////////////////////////////////////////// struct str_expr_char { const char c; str_expr_char( char c ) : c(c) {} size_t size() const { return 1; } template OutIt writeTo(OutIt i) const { *i = c; return ++i; } }; inline str_expr_char make_expr( char c ) { return str_expr_char(c); } /////////////////////////////////////////////////////////////// // overload '+' /////////////////////////////////////////////////////////////// inline str_expr_concat operator+( const str& lhs, const str& rhs ) { return str_expr_concat( make_expr(lhs), make_expr(rhs) ); } inline str_expr_concat operator+( const str& lhs, char rhs ) { return str_expr_concat( make_expr(lhs), make_expr(rhs) ); } inline str_expr_concat operator+( char lhs, const str& rhs ) { return str_expr_concat( make_expr(lhs), make_expr(rhs) ); } inline str_expr_concat operator+( const str& lhs, const char* rhs ) { return str_expr_concat( make_expr(lhs), make_expr(rhs) ); } inline str_expr_concat operator+( const char* lhs, const str& rhs ) { return str_expr_concat( make_expr(lhs), make_expr(rhs) ); } /////////////////////////////////////////////////////////////// template inline str_expr_concat, str_expr_char> operator+( const str_expr_concat& lhs, char rhs ) { return cat( make_expr(lhs), make_expr(rhs) ); } template inline str_expr_concat, str_expr_charptr> operator+( const str_expr_concat& lhs, const char* rhs ) { return cat( make_expr(lhs), make_expr(rhs) ); } template inline str_expr_concat, const str&> operator+( const str_expr_concat& lhs, const str& rhs ) { return cat( make_expr(lhs), make_expr(rhs) ); } template inline str_expr_concat > operator+( char lhs, const str_expr_concat& rhs ) { return cat( make_expr(lhs), make_expr(rhs) ); } template inline str_expr_concat > operator+( const char* lhs, const str_expr_concat& rhs ) { return cat( make_expr(lhs), make_expr(rhs) ); } template inline str_expr_concat > operator+( const str& lhs, const str_expr_concat& rhs ) { return cat( make_expr(lhs), make_expr(rhs) ); } template inline str_expr_concat< str_expr_concat, str_expr_concat > operator+( const str_expr_concat& lhs, const str_expr_concat& rhs ) { return cat( make_expr(lhs), make_expr(rhs) ); } #include #include int main() { { boost::progress_timer t; for(int i=0; i!=1000000; ++i) str x = str() + "hello" + "world!!!" + ' ' + "hoge"; cout << "ET null-form: "; } { boost::progress_timer t; for(int i=0; i!=1000000; ++i) str x = str("hello") + "world!!!" + ' ' + "hoge"; cout << "ET init-form: "; } { boost::progress_timer t; for(int i=0; i!=1000000; ++i) string x = string() + "hello" + "world!!!" + ' ' + "hoge"; cout << "std null-form: "; } { boost::progress_timer t; for(int i=0; i!=1000000; ++i) string x = string("hello") + "world!!!" + ' ' + "hoge"; cout << "std init-form: "; } { boost::progress_timer t; for(int i=0; i!=1000000; ++i) { string x; x += "hello"; x += "world!!!"; x += ' '; x += "hoge"; } cout << "+= null-form: "; } { boost::progress_timer t; for(int i=0; i!=1000000; ++i) { string x("hello"); x += "world!!!"; x += ' '; x += "hoge"; } cout << "+= init-form: "; } { boost::progress_timer t; for(int i=0; i!=1000000; ++i) { string x("hello"); x.reserve(19); x += "world!!!"; x += ' '; x += "hoge"; } cout << "+= rsrv-form: "; } }