super_string.hpp

00001 #ifndef _BASIC_SUPER_STRING_HPP__
00002 #define _BASIC_SUPER_STRING_HPP__
00003 
00004 
00005 /* Copyright 2006 CrystalClear Software, Inc.
00006  * Use, modification and distribution is subject to the 
00007  * Boost Software License, Version 1.0. (See accompanying
00008  * file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
00009  *
00010  * Author:  Jeff Garland 
00011  * Last Modified: $Date: 2006/07/03 02:43:02 $
00012  * Created: Sat Jun  29 14:02:41 2006 
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 //forward declare collection type
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   //iter_split will return entire string if no matches found
00339   return result.size()-1; //todo bug here is result not empty
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 /*  Generic function to append any type to the string
00728  *  This function will work with any type that is OutputStreamable.
00729  *
00730  @code
00731 
00732     super_string s;
00733     double dbl = 1.543;
00734     s.append(dbl);
00735     s += " - "; //std::string method
00736     //append a date to the string
00737     boost::gregorian::date d(2006, boost::gregorian::Jun, 29);
00738     s.append(d);
00739     //s == "1.543 - 2006-Jun-29"
00740 
00741  @endcode
00742  *@param value Value to be appended to the string.
00743  *@return Returns self-reference for chained operations
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 /*  Generic function to append any type to the string
00758  *  This is a faster version of the convert and append function.
00759  *  Use this version if doing a large number of conversions.  This
00760  *  version of the function also allows the user to control format 
00761  *  settings on the conversion by applying changes to the stringstream
00762  *  used to do the conversion.
00763  *  This function will work with any type that is OutputStreamable.
00764  *  
00765  *
00766  @code
00767 
00768     std::ostringstream ss;
00769     s.setprecision(
00770     super_string s;
00771     ss << std::setprecision(3);
00772     double dbl = 1.987654321;
00773     s.append(dbl, ss);
00774     //s == "1.99 - 2006-Jun-29"
00775     //ss -- has been modified
00776 
00777  @endcode
00778  *@param value Value to be appended to the string.
00779  *@param ss A stream to use in the conversion -- all data will be reset.
00780  *@return Returns self-reference for chained operations
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 /*  Generic function to append 2 values
00798  *  This function will work with any type that is OutputStreamable.
00799  *
00800 @code
00801 
00802     super_string s;
00803     double dbl = 1.543;
00804     s.append("double: ", dbl);
00805     //s == "double: 1.543"
00806 
00807 @endcode
00808  *@param value Value to be appended to the string.
00809  *@return Returns self-reference for chained operations
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 /*  Generic function to append any type to the string
00825  *  This is a faster version of the convert and append function.
00826  *  Use this version if doing a large number of conversions.  This
00827  *  version of the function also allows the user to control format 
00828  *  settings on the conversion by applying changes to the stringstream
00829  *  used to do the conversion.
00830  *  This function will work with any type that is OutputStreamable.
00831  *  
00832  *
00833  @code
00834 
00835     std::ostringstream ss;
00836     s.setprecision(
00837     super_string s;
00838     ss << std::setprecision(3);
00839     double dbl = 1.987654321;
00840     s.append(dbl, ss);
00841     //s == "1.99 - 2006-Jun-29"
00842     //ss -- has been modified
00843 
00844  @endcode
00845  *@param val1 First value to be appended to the string.
00846  *@param val2 Second value to be appended to the string.
00847  *@param ss A stream to use in the conversion -- all data will be reset.
00848  *@return Returns self-reference for chained operations
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 /*  Generic function to append 3 values
00865  *  This function will work with any type that is OutputStreamable.
00866  *
00867 @code
00868 
00869     super_string s;
00870     double dbl = 1.543;
00871     int i = 10;
00872     s.append("value is: ", dbl, i);
00873     //s == "double: 1.54310"
00874 
00875 @endcode
00876  *@param value Value to be appended to the string.
00877  *@return Returns self-reference for chained operations
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 /*  Generic function to append any type to the string
00895  *  This is a faster version of the convert and append function.
00896  *  Use this version if doing a large number of conversions.  This
00897  *  version of the function also allows the user to control format 
00898  *  settings on the conversion by applying changes to the stringstream
00899  *  used to do the conversion.
00900  *  This function will work with any type that is OutputStreamable.
00901  *  
00902  *
00903  @code
00904 
00905     std::ostringstream ss;
00906     super_string s;
00907     ss << std::setprecision(3);
00908     double dbl = 1.987654321;
00909     int i = 1000;
00910     s.append(dbl, i, " stuff", ss);
00911     //s == "1.99 1000 stuff"
00912     //ss -- has been modified
00913 
00914  @endcode
00915  *@param val1 First value to be appended to the string.
00916  *@param val2 Second value to be appended to the string.
00917  *@param val3 Third value to be appended to the string.
00918  *@param ss A stream to use in the conversion -- all data will be reset.
00919  *@return Returns self-reference for chained operations
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 /*  Generic function to append 4 values
00939  *  This function will work with any type that is OutputStreamable.
00940  *
00941 @code
00942 
00943     super_string s;
00944     double dbl = 1.543;
00945     int i = 10;
00946     s.append("value is: ", dbl, " int: ", i);
00947     //s == "double: 1.543 int: 10"
00948 
00949 @endcode
00950  *@param value Value to be appended to the string.
00951  *@return Returns self-reference for chained operations
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 /*  Generic function to append any type to the string
00970  *  This is a faster version of the convert and append function.
00971  *  Use this version if doing a large number of conversions.  This
00972  *  version of the function also allows the user to control format 
00973  *  settings on the conversion by applying changes to the stringstream
00974  *  used to do the conversion.
00975  *  This function will work with any type that is OutputStreamable.
00976  *  
00977  *
00978  @code
00979 
00980     std::ostringstream ss;
00981     s.setprecision(
00982     super_string s;
00983     ss << std::setprecision(3);
00984     double dbl = 1.987654321;
00985     int i = 1000;
00986     s.append(dbl, i, i, " stuff", ss);
00987     //s == "1.99 1000 1000 stuff"
00988     //ss -- has been modified
00989 
00990  @endcode
00991  *@param val1 First value to be appended to the string.
00992  *@param val2 Second value to be appended to the string.
00993  *@param val3 Third value to be appended to the string.
00994  *@param val4 Fourth value to be appended to the string.
00995  *@param ss A stream to use in the conversion -- all data will be reset.
00996  *@return Returns self-reference for chained operations
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 /* Generic function to append any type to the string including formatting
01095  * See: 
01096  <a href="http://www.boost.org/libs/format/doc/format.html#printf_directives">Boost Format specification syntax</a> 
01097    for details on the formmatting strings.
01098 
01099 @code
01100 
01101     super_string s;
01102     double dbl = 1.123456789;
01103     s.append_fmt(dbl, "%-7.2f");
01104     s += " - "; //std::string method
01105     //s == "1.543 - 2006-Jun-29"
01106 
01107 @endcode
01108 
01109   @value - The value to append to the end of the string.
01110   @format - Formatting string for the value.
01111   @return Returns self-reference for chained operations
01112  
01113   @throw Boost format exceptions: see <a href="http://www.boost.org/libs/format/doc/format.html#exceptions">Boost Format Exceptions.</a> for details.
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 /* Generic function to append 2 values of any to the string including formatting
01132  *
01133 @code
01134 
01135     super_string s;
01136     double dbl = 1.123456789;
01137     s.append_formatted(dbl, "some string", "%-7.2f %s" );
01138     //s == "1.12  some string"
01139 
01140 @endcode
01141  *
01142  *@value - The value to append to the end of the string.
01143  *@format - Formatting string for the value.
01144  *@return Returns self-reference for chained operations
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 /* Generic function to append 3 values of any to the string including formatting
01163  *
01164 @code
01165 
01166     super_string s;
01167     double dbl = 1.123456789;
01168     int i = 1000;
01169     s.append_formatted(dbl, dbl, i , "%-7.2f %-7.2f %-7d");
01170     //s == "1.23  1.23  1000   "
01171 
01172 @endcode
01173  *
01174  *@value - The value to append to the end of the string.
01175  *@format - Formatting string for the value.
01176  *@return Returns self-reference for chained operations
01177  *
01178  *JKG TODO: explain formatting strings and rules
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 /* Generic function to append 4 values of any to the string including formatting
01198  *
01199 @code
01200 
01201     super_string s;
01202     double dbl = 1.123456789;
01203     int i = 1000;
01204     s.append_formatted(dbl, i , dbl, i, "%-7.2f %-7d %-7.2f %-7d");
01205     //s == "1.12   1000  1.12   1000   "
01206 
01207 @endcode
01208  *
01209  *@value - The value to append to the end of the string.
01210  *@format - Formatting string for the value.
01211  *@return Returns self-reference for chained operations
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

Generated on Sun Jul 9 15:43:03 2006 for SuperString by  doxygen 1.4.6