00001 #ifndef _BASIC_SUPER_STRING_HPP__
00002 #define _BASIC_SUPER_STRING_HPP__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <boost/algorithm/string.hpp>
00017 #include <boost/algorithm/string/regex.hpp>
00018 #include <boost/regex.hpp>
00019 #include <boost/format.hpp>
00020 #include <string>
00021 #include <vector>
00022 #include <sstream>
00023 #include <fstream>
00024
00025
00026 template<typename char_type> class basic_super_string_vector;
00027
00036 template<class char_type >
00037 class basic_super_string : public std::basic_string<char_type> {
00038 public:
00039 typedef std::basic_string<char_type> ss_base_string_type;
00040 typedef std::basic_string<char_type> base_string_type;
00041 typedef typename base_string_type::const_iterator iterator_type;
00042 typedef typename base_string_type::size_type size_type;
00043 typedef std::basic_ostringstream<char_type> string_stream_type;
00044 typedef std::vector<basic_super_string<char_type> > string_vector;
00045
00051 basic_super_string()
00052 {}
00053
00055 basic_super_string(const char_type* const s) :
00056 base_string_type(s)
00057 {}
00058
00060 basic_super_string(const base_string_type& s) :
00061 base_string_type(s)
00062 {}
00063
00065 basic_super_string(iterator_type beg, iterator_type end) :
00066 base_string_type(beg, end)
00067 {}
00069
00074 bool contains (const base_string_type& s) const;
00075 bool contains_regex (const base_string_type& s) const;
00076 bool starts_with (const base_string_type& s, size_type offset=0) const;
00077 bool ends_with (const base_string_type& s) const;
00079
00084 bool icontains (const base_string_type& s) const;
00085 bool istarts_with(const base_string_type& s, size_type offset=0) const;
00086 bool iends_with (const base_string_type& s) const;
00088
00093
00094 unsigned int split(const base_string_type& predicate,
00095 string_vector& result) const;
00096
00097 unsigned int split_regex(const base_string_type& predicate_regex,
00098 string_vector& result) const;
00100
00101
00106 basic_super_string& append_file(const base_string_type& filepath);
00107
00109
00110
00115
00116 basic_super_string& replace_all_regex (const base_string_type& match_regex,
00117 const base_string_type& replace_regex);
00118
00119 basic_super_string& replace_first(const base_string_type& match_string,
00120 const base_string_type& replace_string);
00121 basic_super_string& replace_last (const base_string_type& match_string,
00122 const base_string_type& replace_string);
00123 basic_super_string& replace_nth (const base_string_type& match_string,
00124 const base_string_type& replace_string,
00125 size_type n);
00126 basic_super_string& replace_all (const base_string_type& match_string,
00127 const base_string_type& replace_string);
00129
00134 basic_super_string& ireplace_first(const base_string_type& match_string,
00135 const base_string_type& replace_string);
00136 basic_super_string& ireplace_last (const base_string_type& match_string,
00137 const base_string_type& replace_string);
00138 basic_super_string& ireplace_nth (const base_string_type& match_string,
00139 const base_string_type& replace_string,
00140 size_type n);
00141 basic_super_string& ireplace_all (const base_string_type& match_string,
00142 const base_string_type& replace_string);
00143
00145
00150 basic_super_string& trim();
00151 basic_super_string& trim_left();
00152 basic_super_string& trim_right();
00154
00155
00160 basic_super_string& to_upper();
00161 basic_super_string& to_lower();
00163
00164
00165 template<typename T>
00166 basic_super_string<char_type>& append (const T& value);
00167
00168 template<typename T1, typename T2>
00169 basic_super_string<char_type>& append (const T1& val1,
00170 const T2& val2);
00171
00172 template<typename T1, typename T2, typename T3>
00173 basic_super_string<char_type>& append (const T1& val1,
00174 const T2& val2,
00175 const T3& val3);
00176
00177
00178 template<typename T1, typename T2, typename T3, typename T4>
00179 basic_super_string<char_type>& append (const T1& val1,
00180 const T2& val2,
00181 const T3& val3,
00182 const T4& val4);
00183
00184
00190 template<typename T1, typename T2, typename T3, typename T4, typename T5>
00191 basic_super_string<char_type>& append (const T1& val1,
00192 const T2& val2,
00193 const T3& val3,
00194 const T4& val4,
00195 const T5& val5);
00196 template<class T>
00197 basic_super_string<char_type>& prepend (const T& value);
00198
00199 template<class T>
00200 basic_super_string<char_type>& insert_at (size_type pos, const T& value);
00202
00203
00204 template<typename T>
00205 basic_super_string<char_type>& append (const T& value,
00206 string_stream_type& ss);
00207
00208 template<typename T1, typename T2>
00209 basic_super_string<char_type>& append (const T1& val1,
00210 const T2& val2,
00211 string_stream_type& ss);
00212
00213 template<typename T1, typename T2, typename T3>
00214 basic_super_string<char_type>& append (const T1& val1,
00215 const T2& val2,
00216 const T3& val3,
00217 string_stream_type& ss);
00218
00219 template<typename T1, typename T2, typename T3, typename T4>
00220 basic_super_string<char_type>& append (const T1& val1,
00221 const T2& val2,
00222 const T3& val3,
00223 const T4& val4,
00224 string_stream_type& ss);
00225
00230
00231 template<typename T1, typename T2, typename T3, typename T4, typename T5>
00232 basic_super_string<char_type>& append (const T1& val1,
00233 const T2& val2,
00234 const T3& val3,
00235 const T4& val4,
00236 const T5& val5,
00237 string_stream_type& ss);
00239
00240
00241 template<typename T>
00242 basic_super_string<char_type>& append_formatted (const T& value,
00243 const base_string_type& fmt);
00244
00245 template<typename T1, typename T2>
00246 basic_super_string<char_type>& append_formatted (const T1& val1,
00247 const T2& val2,
00248 const base_string_type& fmt);
00249 template<typename T1, typename T2, typename T3>
00250 basic_super_string<char_type>& append_formatted (const T1& val1,
00251 const T2& val2,
00252 const T3& val3,
00253 const base_string_type& fmt);
00254 template<typename T1, typename T2, typename T3, typename T4>
00255 basic_super_string<char_type>& append_formatted (const T1& val1,
00256 const T2& val2,
00257 const T3& val3,
00258 const T4& val4,
00259 const base_string_type& fmt);
00260
00265
00266 template<typename T1, typename T2, typename T3, typename T4, typename T5>
00267 basic_super_string<char_type>& append_formatted (const T1& val1,
00268 const T2& val2,
00269 const T3& val3,
00270 const T4& val4,
00271 const T5& val5,
00272 const base_string_type& fmt);
00273
00275
00276
00277 private:
00278
00279 };
00280
00281
00293 template<class char_type>
00294 inline
00295 basic_super_string<char_type>&
00296 basic_super_string<char_type>::append_file(const base_string_type& filepath)
00297 {
00298 std::ifstream infile(filepath.c_str());
00299 if (infile) {
00300 std::istreambuf_iterator<char> itr(infile);
00301 std::istreambuf_iterator<char> file_end;
00302 base_string_type data(itr, file_end);
00303 this->append(data);
00304 }
00305 return *this;
00306 }
00307
00308
00326 template<class char_type>
00327 inline
00328 unsigned int
00329 basic_super_string<char_type>::split(const base_string_type& predicate,
00330 string_vector& result) const
00331 {
00332
00333 namespace alg = boost::algorithm;
00334
00335 alg::iter_split(result, *this,
00336 alg::first_finder(predicate, alg::is_equal()));
00337
00338
00339 return result.size()-1;
00340 }
00341
00361 template<class char_type>
00362 inline
00363 unsigned int
00364 basic_super_string<char_type>::split_regex(const base_string_type& predicate_regex,
00365 string_vector& result) const
00366 {
00367
00368 boost::basic_regex<char_type> re(predicate_regex);
00369 boost::regex_token_iterator<iterator_type> i(this->begin(), this->end(), re, -1);
00370 boost::regex_token_iterator<iterator_type> j;
00371
00372 unsigned int count = 0;
00373 while(i != j) {
00374 base_string_type s(*i);
00375 result.push_back(s);
00376 count++;
00377 i++;
00378 }
00379 return count;
00380
00381 }
00382
00383
00399 template<class char_type>
00400 inline
00401 bool
00402 basic_super_string<char_type>::contains_regex(const base_string_type& predicate_regex) const
00403 {
00404 boost::regex::basic_regex<char_type> pred(predicate_regex.begin(),
00405 predicate_regex.end());
00406 return boost::regex_search(this->begin(),
00407 this->end(),
00408 pred,
00409 boost::match_default);
00410 }
00411
00412
00429 template<class char_type>
00430 inline
00431 basic_super_string<char_type>&
00432 basic_super_string<char_type>::replace_all_regex(const base_string_type& match_regex,
00433 const base_string_type& replace_format)
00434 {
00435 boost::regex::basic_regex<char_type> pred(match_regex);
00436 boost::algorithm::replace_all_regex(*this, pred, replace_format);
00437 return *this;
00438 }
00439
00440
00452 template<class char_type>
00453 inline
00454 basic_super_string<char_type>&
00455 basic_super_string<char_type>::replace_all(const base_string_type& match_string,
00456 const base_string_type& replace_string)
00457 {
00458 boost::algorithm::replace_all(*this, match_string, replace_string);
00459 return *this;
00460 }
00461
00462
00475 template<class char_type>
00476 inline
00477 basic_super_string<char_type>&
00478 basic_super_string<char_type>::replace_nth(const base_string_type& match_string,
00479 const base_string_type& replace_string,
00480 size_type n)
00481 {
00482 boost::algorithm::replace_nth(*this, match_string, n, replace_string);
00483 return *this;
00484 }
00485
00486
00498 template<class char_type>
00499 inline
00500 basic_super_string<char_type>&
00501 basic_super_string<char_type>::replace_first(const base_string_type& match_string,
00502 const base_string_type& replace_string)
00503 {
00504 this->replace_nth(match_string, replace_string, 0);
00505 return *this;
00506 }
00507
00508
00520 template<class char_type>
00521 inline
00522 basic_super_string<char_type>&
00523 basic_super_string<char_type>::replace_last(const base_string_type& match_string,
00524 const base_string_type& replace_string)
00525 {
00526 boost::algorithm::replace_last(*this, match_string, replace_string);
00527 return *this;
00528 }
00529
00541 template<class char_type>
00542 inline
00543 basic_super_string<char_type>&
00544 basic_super_string<char_type>::ireplace_all(const base_string_type& match_string,
00545 const base_string_type& replace_string)
00546 {
00547 boost::algorithm::ireplace_all(*this, match_string, replace_string);
00548 return *this;
00549 }
00550
00551
00564 template<class char_type>
00565 inline
00566 basic_super_string<char_type>&
00567 basic_super_string<char_type>::ireplace_nth(const base_string_type& match_string,
00568 const base_string_type& replace_string,
00569 size_type n)
00570 {
00571 boost::algorithm::ireplace_nth(*this, match_string, n, replace_string);
00572 return *this;
00573 }
00574
00575
00587 template<class char_type>
00588 inline
00589 basic_super_string<char_type>&
00590 basic_super_string<char_type>::ireplace_first(const base_string_type& match_string,
00591 const base_string_type& replace_string)
00592 {
00593 this->ireplace_nth(match_string, replace_string, 0);
00594 return *this;
00595 }
00596
00597
00609 template<class char_type>
00610 inline
00611 basic_super_string<char_type>&
00612 basic_super_string<char_type>::ireplace_last(const base_string_type& match_string,
00613 const base_string_type& replace_string)
00614 {
00615 boost::algorithm::ireplace_last(*this, match_string, replace_string);
00616 return *this;
00617 }
00618
00619
00629 template<class char_type>
00630 inline
00631 bool
00632 basic_super_string<char_type>::contains(const base_string_type& predicate_string) const
00633 {
00634 return boost::algorithm::contains(*this, predicate_string);
00635 }
00636
00647 template<class char_type>
00648 inline
00649 bool
00650 basic_super_string<char_type>::icontains(const base_string_type& predicate_string) const
00651 {
00652 return boost::algorithm::icontains(*this, predicate_string);
00653 }
00654
00655
00665 template<class char_type>
00666 inline
00667 bool
00668 basic_super_string<char_type>::ends_with(const base_string_type& predicate_string) const
00669 {
00670 return boost::algorithm::ends_with(*this, predicate_string);
00671 }
00672
00683 template<class char_type>
00684 inline
00685 bool
00686 basic_super_string<char_type>::iends_with(const base_string_type& predicate_string) const
00687 {
00688 return boost::algorithm::iends_with(*this, predicate_string);
00689 }
00690
00691
00697 template<class char_type>
00698 inline
00699 bool
00700 basic_super_string<char_type>::starts_with(const base_string_type& predicate_string,
00701 size_type offset) const
00702 {
00703 return boost::algorithm::starts_with(this->substr(offset), predicate_string);
00704 }
00705
00717 template<class char_type>
00718 inline
00719 bool
00720 basic_super_string<char_type>::istarts_with(const base_string_type& predicate_string,
00721 size_type offset) const
00722 {
00723 return boost::algorithm::istarts_with(this->substr(offset), predicate_string);
00724 }
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745 template<class char_type>
00746 template<typename T>
00747 inline
00748 basic_super_string<char_type>&
00749 basic_super_string<char_type>::append(const T& value)
00750 {
00751 string_stream_type ss;
00752 ss << value;
00753 *this += ss.str();
00754 return *this;
00755 }
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782 template<class char_type>
00783 template<typename T>
00784 inline
00785 basic_super_string<char_type>&
00786 basic_super_string<char_type>::append(const T& value,
00787 string_stream_type& ss)
00788 {
00789 ss.str("");
00790 ss << value;
00791 *this += ss.str();
00792 return *this;
00793 }
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 template<class char_type>
00812 template<typename T1, typename T2>
00813 inline
00814 basic_super_string<char_type>&
00815 basic_super_string<char_type>::append(const T1& val1, const T2& val2)
00816 {
00817 string_stream_type ss;
00818 ss << val1 << val2;
00819 *this += ss.str();
00820 return *this;
00821 }
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850 template<class char_type>
00851 template<typename T1, typename T2>
00852 inline
00853 basic_super_string<char_type>&
00854 basic_super_string<char_type>::append(const T1& val1,
00855 const T2& val2,
00856 string_stream_type& ss)
00857 {
00858 ss.str("");
00859 ss << val1 << val2;
00860 *this += ss.str();
00861 return *this;
00862 }
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879 template<class char_type>
00880 template<typename T1, typename T2, typename T3>
00881 inline
00882 basic_super_string<char_type>&
00883 basic_super_string<char_type>::append(const T1& val1,
00884 const T2& val2,
00885 const T3& val3)
00886 {
00887 string_stream_type ss;
00888 ss << val1 << val2 << val3;
00889 *this += ss.str();
00890 return *this;
00891 }
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922 template<class char_type>
00923 template<typename T1, typename T2, typename T3>
00924 inline
00925 basic_super_string<char_type>&
00926 basic_super_string<char_type>::append(const T1& val1,
00927 const T2& val2,
00928 const T3& val3,
00929 string_stream_type& ss)
00930 {
00931 ss.str("");
00932 ss << val1 << val2 << val3;
00933 *this += ss.str();
00934 return *this;
00935 }
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953 template<class char_type>
00954 template<typename T1, typename T2, typename T3, typename T4>
00955 inline
00956 basic_super_string<char_type>&
00957 basic_super_string<char_type>::append(const T1& val1,
00958 const T2& val2,
00959 const T3& val3,
00960 const T4& val4)
00961 {
00962 string_stream_type ss;
00963 ss << val1 << val2 << val3 << val4;
00964 *this += ss.str();
00965 return *this;
00966 }
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998 template<class char_type>
00999 template<typename T1, typename T2, typename T3, typename T4>
01000 inline
01001 basic_super_string<char_type>&
01002 basic_super_string<char_type>::append(const T1& val1,
01003 const T2& val2,
01004 const T3& val3,
01005 const T4& val4,
01006 string_stream_type& ss)
01007 {
01008 ss.str("");
01009 ss << val1 << val2 << val3 << val4;
01010 *this += ss.str();
01011 return *this;
01012 }
01013
01014
01030 template<class char_type>
01031 template<typename T1, typename T2, typename T3, typename T4, typename T5>
01032 inline
01033 basic_super_string<char_type>&
01034 basic_super_string<char_type>::append(const T1& val1,
01035 const T2& val2,
01036 const T3& val3,
01037 const T4& val4,
01038 const T5& val5)
01039 {
01040 string_stream_type ss;
01041 ss << val1 << val2 << val3 << val4 << val5;
01042 *this += ss.str();
01043 return *this;
01044 }
01045
01046
01076 template<class char_type>
01077 template<typename T1, typename T2, typename T3, typename T4, typename T5>
01078 inline
01079 basic_super_string<char_type>&
01080 basic_super_string<char_type>::append(const T1& val1,
01081 const T2& val2,
01082 const T3& val3,
01083 const T4& val4,
01084 const T5& val5,
01085 string_stream_type& ss)
01086 {
01087 ss.str("");
01088 ss << val1 << val2 << val3 << val4 << val5;
01089 *this += ss.str();
01090 return *this;
01091 }
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116 template<class char_type>
01117 template<typename T>
01118 inline
01119 basic_super_string<char_type>&
01120 basic_super_string<char_type>::append_formatted(const T& value,
01121 const base_string_type& format)
01122 {
01123 boost::format f(format);
01124 string_stream_type ss;
01125 ss << f % value;
01126 *this += ss.str();
01127 return *this;
01128 }
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147 template<class char_type>
01148 template<typename T1, typename T2>
01149 inline
01150 basic_super_string<char_type>&
01151 basic_super_string<char_type>::append_formatted(const T1& val1,
01152 const T2& val2,
01153 const base_string_type& format)
01154 {
01155 boost::format f(format);
01156 string_stream_type ss;
01157 ss << f % val1 % val2;
01158 *this += ss.str();
01159 return *this;
01160 }
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180 template<class char_type>
01181 template<typename T1, typename T2, typename T3>
01182 inline
01183 basic_super_string<char_type>&
01184 basic_super_string<char_type>::append_formatted(const T1& val1,
01185 const T2& val2,
01186 const T3& val3,
01187 const base_string_type& format)
01188 {
01189 boost::format f(format);
01190 string_stream_type ss;
01191 ss << f % val1 % val2 % val3;
01192 *this += ss.str();
01193 return *this;
01194 }
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214 template<class char_type>
01215 template<typename T1, typename T2, typename T3, typename T4>
01216 inline
01217 basic_super_string<char_type>&
01218 basic_super_string<char_type>::append_formatted(const T1& val1,
01219 const T2& val2,
01220 const T3& val3,
01221 const T4& val4,
01222 const base_string_type& format)
01223 {
01224 boost::format f(format);
01225 string_stream_type ss;
01226 ss << f % val1 % val2 % val3 % val4;
01227 *this += ss.str();
01228 return *this;
01229 }
01230
01257 template<class char_type>
01258 template<typename T1, typename T2, typename T3, typename T4, typename T5>
01259 inline
01260 basic_super_string<char_type>&
01261 basic_super_string<char_type>::append_formatted(const T1& val1,
01262 const T2& val2,
01263 const T3& val3,
01264 const T4& val4,
01265 const T5& val5,
01266 const base_string_type& format)
01267 {
01268 boost::format f(format);
01269 string_stream_type ss;
01270 ss << f % val1 % val2 % val3 % val4 % val5;
01271 *this += ss.str();
01272 return *this;
01273 }
01274
01275
01291 template<class char_type>
01292 template<class T>
01293 inline
01294 basic_super_string<char_type>&
01295 basic_super_string<char_type>::prepend(const T& value)
01296 {
01297 string_stream_type ss;
01298 ss << value;
01299 this->insert(0, ss.str());
01300 return *this;
01301 }
01302
01317 template<class char_type>
01318 template<class T>
01319 inline
01320 basic_super_string<char_type>&
01321 basic_super_string<char_type>::insert_at(size_type position, const T& value)
01322 {
01323 string_stream_type ss;
01324 ss << value;
01325 this->insert(position, ss.str());
01326 return *this;
01327 }
01328
01329
01330
01343 template<class char_type>
01344 inline
01345 basic_super_string<char_type>&
01346 basic_super_string<char_type>::trim()
01347 {
01348 boost::algorithm::trim(*this);
01349 return *this;
01350 }
01351
01352
01353
01354
01359 template<class char_type>
01360 inline
01361 basic_super_string<char_type>&
01362 basic_super_string<char_type>::trim_left()
01363 {
01364 boost::algorithm::trim_left(*this);
01365 return *this;
01366 }
01367
01368
01369
01375 template<class char_type>
01376 inline
01377 basic_super_string<char_type>&
01378 basic_super_string<char_type>::trim_right()
01379 {
01380 boost::algorithm::trim_right(*this);
01381 return *this;
01382 }
01383
01384
01385
01400 template<class char_type>
01401 inline
01402 basic_super_string<char_type>&
01403 basic_super_string<char_type>::to_upper()
01404 {
01405 boost::algorithm::to_upper(*this);
01406 return *this;
01407 }
01408
01409
01422 template<class char_type>
01423 inline
01424 basic_super_string<char_type>&
01425 basic_super_string<char_type>::to_lower()
01426 {
01427 boost::algorithm::to_lower(*this);
01428 return *this;
01429 }
01430
01431
01433 typedef basic_super_string<char> super_string;
01434
01436 typedef basic_super_string<wchar_t> wsuper_string;
01437
01438
01439 #endif