library of assembled shared sources

http://lass.cocamware.com

extended_string.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #include <locale>
00044 
00045 namespace lass
00046 {
00047 namespace stde
00048 {
00049 namespace impl
00050 {
00051     template <typename StringType> 
00052     StringType whitespace(const StringType&) 
00053     { 
00054         return StringType(" \t\n"); 
00055     }
00056 }
00057 
00058 /** @ingroup extended_string
00059  *  convert std::basic_string to lower case by using user locale
00060  */
00061 template <typename Char, typename Traits, typename Alloc>
00062 std::basic_string<Char, Traits, Alloc>
00063 tolower(const std::basic_string<Char, Traits, Alloc>& input,
00064         const std::locale& locale)
00065 {
00066     std::basic_string<Char, Traits, Alloc> result(input);
00067     typedef typename std::basic_string<Char, Traits, Alloc>::iterator iterator;
00068     for (iterator i = result.begin(); i != result.end(); ++i)
00069     {
00070         *i = std::tolower<Char>(*i, locale);
00071     }
00072     return result;
00073 }
00074 
00075 
00076 
00077 /** @ingroup extended_string
00078  *  convert std::basic_string to upper case by using user locale
00079  */
00080 template <typename Char, typename Traits, typename Alloc>
00081 std::basic_string<Char, Traits, Alloc>
00082 toupper(const std::basic_string<Char, Traits, Alloc>& input,
00083         const std::locale& locale)
00084 {
00085     std::basic_string<Char, Traits, Alloc> result(input);
00086     typedef typename std::basic_string<Char, Traits, Alloc>::iterator iterator;
00087     for (iterator i = result.begin(); i != result.end(); ++i)
00088     {
00089         *i = std::toupper<Char>(*i, locale);
00090     }
00091     return result;
00092 }
00093 
00094 /** @ingroup extended_string
00095  *  replace all instances of @a to_be_replaced in @a input by @a replacement.
00096  */
00097 template <typename Char, typename Traits, typename Alloc>
00098 std::basic_string<Char, Traits, Alloc>
00099 replace_all(const std::basic_string<Char, Traits, Alloc>& input,
00100             const std::basic_string<Char, Traits, Alloc>& to_be_replaced,
00101             const std::basic_string<Char, Traits, Alloc>& replacement)
00102 {
00103     typedef std::basic_string<Char, Traits, Alloc> string_type;
00104     typename string_type::size_type size_to_be_replaced = to_be_replaced.size();
00105     typename string_type::size_type size_replacement = replacement.size();
00106     string_type result(input);
00107 
00108     typename string_type::size_type i = result.find(to_be_replaced);
00109     while (i != string_type::npos)
00110     {
00111         result.replace(i, size_to_be_replaced, replacement);
00112         i = result.find(to_be_replaced, i + size_replacement);
00113     }
00114     return result;
00115 }
00116 
00117 /** @ingroup extended_string
00118  *  replace all instances of @a to_be_replaced in @a input by @a replacement.
00119  */
00120 template <typename Char, typename Traits, typename Alloc>
00121 std::basic_string<Char, Traits, Alloc>
00122 replace_all(const std::basic_string<Char, Traits, Alloc>& input,
00123     const Char* to_be_replaced,
00124     const Char* replacement)
00125 {
00126     return replace_all(input, std::string(to_be_replaced), std::string(replacement));
00127 }
00128 
00129 
00130 
00131 /** @ingroup extended_string
00132  *  returns true if @a input begins with the input @a prefix
00133  */
00134 template <typename Char, typename Traits, typename Alloc>
00135 bool begins_with(const std::basic_string<Char, Traits, Alloc>& input,
00136     const std::basic_string<Char, Traits, Alloc>& prefix)
00137 {
00138     return prefix.length() <= input.length() && std::equal(prefix.begin(), prefix.end(), input.begin());
00139 }
00140 
00141 /** @ingroup extended_string
00142  *  returns true if @a input begins with the input @a prefix
00143  */
00144 template <typename Char, typename Traits, typename Alloc>
00145 bool begins_with(const std::basic_string<Char, Traits, Alloc>& input,
00146     const Char* prefix)
00147 {
00148     return begins_with(input, std::string(prefix));
00149 }
00150 
00151 
00152 
00153 /** @ingroup extended_string
00154  *  returns true if @a input ends with the input @a suffix
00155  */
00156 template <typename Char, typename Traits, typename Alloc>
00157 bool ends_with(const std::basic_string<Char, Traits, Alloc>& input,
00158     const std::basic_string<Char, Traits, Alloc>& suffix)
00159 {
00160     return suffix.length() <= input.length() && 
00161         std::equal(suffix.begin(), suffix.end(), input.begin() + input.length() - suffix.length());
00162 }
00163 
00164 /** @ingroup extended_string
00165  *  returns true if @a input ends with the input @a suffix
00166  */
00167 template <typename Char, typename Traits, typename Alloc>
00168 bool ends_with(const std::basic_string<Char, Traits, Alloc>& input,
00169     const Char* suffix)
00170 {
00171     return ends_with(input, std::string(suffix));
00172 }
00173 
00174 
00175 
00176 /** @ingroup extended_string
00177  *  Reflects the Python function @c split without seperator argument
00178  *
00179  *  Return a vector of the words of the string @a to_be_split.  The words are separated by arbitrary
00180  *  strings of whitespace characters (space, tab, newline, return, formfeed). 
00181  *
00182  *  If @a to_be_split is empty, then the result will have an empty vector.
00183  */
00184 template <typename Char, typename Traits, typename Alloc>
00185 std::vector< std::basic_string<Char, Traits, Alloc> >
00186 split(const std::basic_string<Char, Traits, Alloc>& to_be_split)
00187 {
00188     typedef std::basic_string<Char, Traits, Alloc> string_type;
00189     typedef typename string_type::size_type size_type;
00190 
00191     const string_type seperators = impl::whitespace(to_be_split);
00192     std::vector< std::basic_string<Char, Traits, Alloc> > result;
00193 
00194     if (to_be_split.empty())
00195     {
00196         return result;
00197     }
00198 
00199     size_type begin = 0;
00200     size_type end = to_be_split.find_first_of(seperators);
00201     while (end != string_type::npos)
00202     {
00203         result.push_back(to_be_split.substr(begin, end - begin));
00204         begin = to_be_split.find_first_not_of(seperators, end);
00205         if (begin == string_type::npos)
00206         {
00207             result.push_back(string_type());
00208             return result;
00209         }
00210         end = to_be_split.find_first_of(seperators, begin);
00211     }
00212 
00213     result.push_back(to_be_split.substr(begin));
00214     return result;
00215 }
00216 
00217 
00218 
00219 /** @ingroup extended_string
00220  *  Reflects the Python function @c split without seperator argument
00221  *
00222  *  Return a vector of the words of the string @a to_be_split.  The second argument @a seperator
00223  *  specifies a string to be used as the word separator. The returned vector will then have one
00224  *  more item than the number of non-overlapping occurrences of the separator in the string.
00225  *
00226  *  The optional third argument @a max_split defaults to 0.  If it is nonzero, at most
00227  *  @a max_split number of splits occur, and the remainder of the string is returned as the final
00228  *  element of the list (thus, the list will have at most @a max_split + 1 elements).
00229  *
00230  *  If @a to_be_split is empty, then the result will have an empty string as only element.
00231  */
00232 template <typename Char, typename Traits, typename Alloc>
00233 std::vector< std::basic_string<Char, Traits, Alloc> >
00234 split(const std::basic_string<Char, Traits, Alloc>& to_be_split,
00235       const std::basic_string<Char, Traits, Alloc>& seperator,
00236       size_t max_split)
00237 {
00238     typedef std::basic_string<Char, Traits, Alloc> string_type;
00239     typedef typename string_type::size_type size_type;
00240 
00241     std::vector< std::basic_string<Char, Traits, Alloc> > result;
00242     if (max_split == 0)
00243     {
00244         max_split = result.max_size() - 1;
00245     }
00246 
00247     const size_type seperator_size = seperator.size();
00248 
00249     size_type begin = 0;
00250     while (result.size() < max_split)
00251     {
00252         const size_type end = to_be_split.find(seperator, begin);
00253         if (end == string_type::npos)
00254         {
00255             break;
00256         }
00257 
00258         result.push_back(to_be_split.substr(begin, end - begin));
00259         begin = end + seperator_size;
00260     }
00261 
00262     result.push_back(to_be_split.substr(begin));
00263     return result;
00264 }
00265 
00266 /** @ingroup extended_string
00267  *  Reflects the Python function @c split without seperator argument
00268  */
00269 template <typename Char, typename Traits, typename Alloc>
00270 std::vector< std::basic_string<Char, Traits, Alloc> >
00271 split(const std::basic_string<Char, Traits, Alloc>& to_be_split,
00272     const Char* seperator,
00273     size_t max_split)
00274 {
00275     return split(to_be_split, std::string(seperator), max_split);
00276 }
00277 
00278 
00279 
00280 /** @ingroup extended_string
00281  */
00282 template <typename Char, typename Traits, typename Alloc, typename InputIterator>
00283 std::basic_string<Char, Traits, Alloc>
00284 join(const std::basic_string<Char, Traits, Alloc>& joiner, InputIterator first, InputIterator last)
00285 {
00286     std::basic_ostringstream<Char, Traits, Alloc> buffer;
00287     if (first != last)
00288     {
00289         buffer << *first++;
00290     }
00291     while (first != last)
00292     {
00293         buffer << joiner << *first++;
00294     }
00295     return buffer.str();
00296 }
00297 
00298 
00299 
00300 template <typename Char, typename Traits, typename Alloc, typename InputRange> inline
00301 std::basic_string<Char, Traits, Alloc>
00302 join_r(const std::basic_string<Char, Traits, Alloc>& joiner, const InputRange& range)
00303 {
00304     return join(joiner, range.begin(), range.end());
00305 }
00306 
00307 
00308 
00309 /** @ingroup extended_string
00310  *  Return a copy of the string @a to_be_stripped with leading characters removed.
00311  *
00312  *  The characters in the string @a to_be_removed will be stripped from the beginning of the string
00313  *  @a to_be_stripped. 
00314  */
00315 template <typename Char, typename Traits, typename Alloc>
00316 std::basic_string<Char, Traits, Alloc>
00317 lstrip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped,
00318        const std::basic_string<Char, Traits, Alloc>& to_be_removed)
00319 {
00320     typedef std::basic_string<Char, Traits, Alloc> string_type;
00321     const typename string_type::size_type begin = to_be_stripped.find_first_not_of(to_be_removed);
00322     return begin == string_type::npos ? string_type() : to_be_stripped.substr(begin);
00323 }
00324 
00325 
00326 
00327 /** @ingroup extended_string
00328  *  Return a copy of the string @a to_be_stripped with leading whitespace removed.
00329  */
00330 template <typename Char, typename Traits, typename Alloc> inline
00331 std::basic_string<Char, Traits, Alloc>
00332 lstrip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped)
00333 {
00334     return lstrip(to_be_stripped, impl::whitespace(to_be_stripped));
00335 }
00336 
00337 
00338 
00339 /** @ingroup extended_string
00340  *  Return a copy of the string @a to_be_stripped with trailing characters removed.
00341  *
00342  *  The characters in the string @a to_be_removed will be stripped from the ending of the string
00343  *  @a to_be_stripped. 
00344  */
00345 template <typename Char, typename Traits, typename Alloc>
00346 std::basic_string<Char, Traits, Alloc>
00347 rstrip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped,
00348        const std::basic_string<Char, Traits, Alloc>& to_be_removed)
00349 {
00350     typedef std::basic_string<Char, Traits, Alloc> string_type;
00351     const typename string_type::size_type end = to_be_stripped.find_last_not_of(to_be_removed);
00352     return end == string_type::npos ? string_type() : to_be_stripped.substr(0, end + 1);
00353 }
00354 
00355 
00356 
00357 /** @ingroup extended_string
00358  *  Return a copy of the string @a to_be_stripped with trailing whitespace removed.
00359  */
00360 template <typename Char, typename Traits, typename Alloc> inline
00361 std::basic_string<Char, Traits, Alloc>
00362 rstrip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped)
00363 {
00364     return rstrip(to_be_stripped, impl::whitespace(to_be_stripped));
00365 }
00366 
00367 
00368 
00369 /** @ingroup extended_string
00370  *  Return a copy of the string @a to_be_stripped with both leading and trailing characters removed.
00371  *
00372  *  The characters in the string @a to_be_removed will be stripped from both the beginning and the 
00373  *  ending of the string @a to_be_stripped. 
00374  */
00375 template <typename Char, typename Traits, typename Alloc>
00376 std::basic_string<Char, Traits, Alloc>
00377 strip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped,
00378       const std::basic_string<Char, Traits, Alloc>& to_be_removed)
00379 {
00380     typedef std::basic_string<Char, Traits, Alloc> string_type;
00381     const typename string_type::size_type begin = to_be_stripped.find_first_not_of(to_be_removed);
00382     const typename string_type::size_type end = to_be_stripped.find_last_not_of(to_be_removed);
00383     return begin == string_type::npos ? string_type() : to_be_stripped.substr(begin, end - begin + 1);
00384 }
00385 
00386 
00387 
00388 /** @ingroup extended_string
00389  *  Return a copy of the string @a to_be_stripped with leading and trailing whitespace removed.
00390  */
00391 template <typename Char, typename Traits, typename Alloc> inline
00392 std::basic_string<Char, Traits, Alloc>
00393 strip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped)
00394 {
00395     return strip(to_be_stripped, impl::whitespace(to_be_stripped));
00396 }
00397 
00398 
00399 
00400 }
00401 
00402 }
00403 
00404 // EOF

Generated on Mon Nov 10 14:20:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo