mirror of
https://github.com/boostorg/algorithm.git
synced 2025-06-25 12:01:39 +02:00
Compare commits
9 Commits
boost-1.42
...
boost-1.46
Author | SHA1 | Date | |
---|---|---|---|
9535d80b31 | |||
01492a93c6 | |||
50703b8c97 | |||
0f8d556130 | |||
bbd3220a1e | |||
9068069106 | |||
a37af3c81e | |||
f5885c6fb0 | |||
d45bb3545e |
@ -31,7 +31,7 @@ namespace boost {
|
||||
struct to_lowerF : public std::unary_function<CharT, CharT>
|
||||
{
|
||||
// Constructor
|
||||
to_lowerF( const std::locale& Loc ) : m_Loc( Loc ) {}
|
||||
to_lowerF( const std::locale& Loc ) : m_Loc( &Loc ) {}
|
||||
|
||||
// Operation
|
||||
CharT operator ()( CharT Ch ) const
|
||||
@ -39,11 +39,11 @@ namespace boost {
|
||||
#if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x564) && !defined(_USE_OLD_RW_STL)
|
||||
return std::tolower( Ch);
|
||||
#else
|
||||
return std::tolower<CharT>( Ch, m_Loc );
|
||||
return std::tolower<CharT>( Ch, *m_Loc );
|
||||
#endif
|
||||
}
|
||||
private:
|
||||
const std::locale& m_Loc;
|
||||
const std::locale* m_Loc;
|
||||
};
|
||||
|
||||
// a toupper functor
|
||||
@ -51,7 +51,7 @@ namespace boost {
|
||||
struct to_upperF : public std::unary_function<CharT, CharT>
|
||||
{
|
||||
// Constructor
|
||||
to_upperF( const std::locale& Loc ) : m_Loc( Loc ) {}
|
||||
to_upperF( const std::locale& Loc ) : m_Loc( &Loc ) {}
|
||||
|
||||
// Operation
|
||||
CharT operator ()( CharT Ch ) const
|
||||
@ -59,11 +59,11 @@ namespace boost {
|
||||
#if defined(__BORLANDC__) && (__BORLANDC__ >= 0x560) && (__BORLANDC__ <= 0x564) && !defined(_USE_OLD_RW_STL)
|
||||
return std::toupper( Ch);
|
||||
#else
|
||||
return std::toupper<CharT>( Ch, m_Loc );
|
||||
return std::toupper<CharT>( Ch, *m_Loc );
|
||||
#endif
|
||||
}
|
||||
private:
|
||||
const std::locale& m_Loc;
|
||||
const std::locale* m_Loc;
|
||||
};
|
||||
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
|
||||
|
@ -32,8 +32,8 @@ namespace boost {
|
||||
struct is_classifiedF :
|
||||
public predicate_facade<is_classifiedF>
|
||||
{
|
||||
// Boost.Lambda support
|
||||
template <class Args> struct sig { typedef bool type; };
|
||||
// Boost.ResultOf support
|
||||
typedef bool result_type;
|
||||
|
||||
// Constructor from a locale
|
||||
is_classifiedF(std::ctype_base::mask Type, std::locale const & Loc = std::locale()) :
|
||||
@ -72,8 +72,8 @@ namespace boost {
|
||||
typedef typename ::boost::remove_const<CharT>::type set_value_type;
|
||||
|
||||
public:
|
||||
// Boost.Lambda support
|
||||
template <class Args> struct sig { typedef bool type; };
|
||||
// Boost.ResultOf support
|
||||
typedef bool result_type;
|
||||
|
||||
// Constructor
|
||||
template<typename RangeT>
|
||||
@ -253,8 +253,8 @@ namespace boost {
|
||||
struct is_from_rangeF :
|
||||
public predicate_facade< is_from_rangeF<CharT> >
|
||||
{
|
||||
// Boost.Lambda support
|
||||
template <class Args> struct sig { typedef bool type; };
|
||||
// Boost.ResultOf support
|
||||
typedef bool result_type;
|
||||
|
||||
// Constructor
|
||||
is_from_rangeF( CharT From, CharT To ) : m_From(From), m_To(To) {}
|
||||
@ -278,8 +278,8 @@ namespace boost {
|
||||
{
|
||||
public:
|
||||
|
||||
// Boost.Lambda support
|
||||
template <class Args> struct sig { typedef bool type; };
|
||||
// Boost.ResultOf support
|
||||
typedef bool result_type;
|
||||
|
||||
// Constructor
|
||||
pred_andF( Pred1T Pred1, Pred2T Pred2 ) :
|
||||
@ -303,8 +303,8 @@ namespace boost {
|
||||
public predicate_facade< pred_orF<Pred1T,Pred2T> >
|
||||
{
|
||||
public:
|
||||
// Boost.Lambda support
|
||||
template <class Args> struct sig { typedef bool type; };
|
||||
// Boost.ResultOf support
|
||||
typedef bool result_type;
|
||||
|
||||
// Constructor
|
||||
pred_orF( Pred1T Pred1, Pred2T Pred2 ) :
|
||||
@ -328,8 +328,8 @@ namespace boost {
|
||||
public predicate_facade< pred_notF<PredT> >
|
||||
{
|
||||
public:
|
||||
// Boost.Lambda support
|
||||
template <class Args> struct sig { typedef bool type; };
|
||||
// Boost.ResultOf support
|
||||
typedef bool result_type;
|
||||
|
||||
// Constructor
|
||||
pred_notF( PredT Pred ) : m_Pred(Pred) {}
|
||||
|
@ -49,17 +49,17 @@ namespace boost {
|
||||
if ( !M )
|
||||
{
|
||||
// Match not found - return original sequence
|
||||
std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
|
||||
Output = std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
|
||||
return Output;
|
||||
}
|
||||
|
||||
// Copy the beginning of the sequence
|
||||
std::copy( ::boost::begin(Input), ::boost::begin(M), Output );
|
||||
Output = std::copy( ::boost::begin(Input), ::boost::begin(M), Output );
|
||||
// Format find result
|
||||
// Copy formated result
|
||||
std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
|
||||
Output = std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
|
||||
// Copy the rest of the sequence
|
||||
std::copy( M.end(), ::boost::end(Input), Output );
|
||||
Output = std::copy( M.end(), ::boost::end(Input), Output );
|
||||
|
||||
return Output;
|
||||
}
|
||||
@ -74,13 +74,17 @@ namespace boost {
|
||||
const InputT& Input,
|
||||
FormatterT Formatter,
|
||||
const FindResultT& FindResult )
|
||||
{
|
||||
return ::boost::algorithm::detail::find_format_copy_impl2(
|
||||
Output,
|
||||
Input,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
{
|
||||
if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
|
||||
return ::boost::algorithm::detail::find_format_copy_impl2(
|
||||
Output,
|
||||
Input,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
} else {
|
||||
return std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -132,11 +136,15 @@ namespace boost {
|
||||
FormatterT Formatter,
|
||||
const FindResultT& FindResult)
|
||||
{
|
||||
return ::boost::algorithm::detail::find_format_copy_impl2(
|
||||
Input,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
|
||||
return ::boost::algorithm::detail::find_format_copy_impl2(
|
||||
Input,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
} else {
|
||||
return Input;
|
||||
}
|
||||
}
|
||||
|
||||
// replace implementation ----------------------------------------------------//
|
||||
@ -180,11 +188,13 @@ namespace boost {
|
||||
FormatterT Formatter,
|
||||
const FindResultT& FindResult)
|
||||
{
|
||||
::boost::algorithm::detail::find_format_impl2(
|
||||
Input,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
|
||||
::boost::algorithm::detail::find_format_impl2(
|
||||
Input,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
@ -57,9 +57,9 @@ namespace boost {
|
||||
while( M )
|
||||
{
|
||||
// Copy the beginning of the sequence
|
||||
std::copy( LastMatch, M.begin(), Output );
|
||||
Output = std::copy( LastMatch, M.begin(), Output );
|
||||
// Copy formated result
|
||||
std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
|
||||
Output = std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
|
||||
|
||||
// Proceed to the next match
|
||||
LastMatch=M.end();
|
||||
@ -67,7 +67,7 @@ namespace boost {
|
||||
}
|
||||
|
||||
// Copy the rest of the sequence
|
||||
std::copy( LastMatch, ::boost::end(Input), Output );
|
||||
Output = std::copy( LastMatch, ::boost::end(Input), Output );
|
||||
|
||||
return Output;
|
||||
}
|
||||
@ -84,14 +84,18 @@ namespace boost {
|
||||
FinderT Finder,
|
||||
FormatterT Formatter,
|
||||
const FindResultT& FindResult )
|
||||
{
|
||||
return ::boost::algorithm::detail::find_format_all_copy_impl2(
|
||||
Output,
|
||||
Input,
|
||||
Finder,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
{
|
||||
if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
|
||||
return ::boost::algorithm::detail::find_format_all_copy_impl2(
|
||||
Output,
|
||||
Input,
|
||||
Finder,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
} else {
|
||||
return std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
|
||||
}
|
||||
}
|
||||
|
||||
// find_format_all_copy implementation ----------------------------------------------//
|
||||
@ -156,12 +160,16 @@ namespace boost {
|
||||
FormatterT Formatter,
|
||||
const FindResultT& FindResult)
|
||||
{
|
||||
return ::boost::algorithm::detail::find_format_all_copy_impl2(
|
||||
Input,
|
||||
Finder,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
|
||||
return ::boost::algorithm::detail::find_format_all_copy_impl2(
|
||||
Input,
|
||||
Finder,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
} else {
|
||||
return Input;
|
||||
}
|
||||
}
|
||||
|
||||
// find_format_all implementation ------------------------------------------------//
|
||||
@ -248,12 +256,14 @@ namespace boost {
|
||||
FormatterT Formatter,
|
||||
FindResultT FindResult)
|
||||
{
|
||||
::boost::algorithm::detail::find_format_all_impl2(
|
||||
Input,
|
||||
Finder,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
|
||||
::boost::algorithm::detail::find_format_all_impl2(
|
||||
Input,
|
||||
Finder,
|
||||
Formatter,
|
||||
FindResult,
|
||||
Formatter(FindResult) );
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
@ -52,7 +52,9 @@ namespace boost {
|
||||
find_format_store& operator=( FindResultT FindResult )
|
||||
{
|
||||
iterator_range<ForwardIteratorT>::operator=(FindResult);
|
||||
m_FormatResult=m_Formatter(FindResult);
|
||||
if( !this->empty() ) {
|
||||
m_FormatResult=m_Formatter(FindResult);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
@ -68,6 +70,15 @@ namespace boost {
|
||||
const formatter_type& m_Formatter;
|
||||
};
|
||||
|
||||
template<typename InputT, typename FindResultT>
|
||||
bool check_find_result(InputT&, FindResultT& FindResult)
|
||||
{
|
||||
typedef BOOST_STRING_TYPENAME
|
||||
range_const_iterator<InputT>::type input_iterator_type;
|
||||
iterator_range<input_iterator_type> ResultRange(FindResult);
|
||||
return !ResultRange.empty();
|
||||
}
|
||||
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
@ -752,7 +752,7 @@ namespace boost {
|
||||
|
||||
\param Output An output iterator to which the result will be copied
|
||||
\param Input An input string
|
||||
\param N Length of the head.
|
||||
\param N Length of the tail.
|
||||
For N>=0, at most N characters are extracted.
|
||||
For N<0, size(Input)-|N| characters are extracted.
|
||||
\return An output iterator pointing just after the last inserted character or
|
||||
@ -797,7 +797,7 @@ namespace boost {
|
||||
considered to be the tail. The input sequence is modified in-place.
|
||||
|
||||
\param Input An input string
|
||||
\param N Length of the head
|
||||
\param N Length of the tail
|
||||
For N>=0, at most N characters are extracted.
|
||||
For N<0, size(Input)-|N| characters are extracted.
|
||||
*/
|
||||
|
@ -257,7 +257,7 @@ namespace boost {
|
||||
|
||||
//! Find tail algorithm
|
||||
/*!
|
||||
Get the head of the input. Head is a suffix of the string of the
|
||||
Get the tail of the input. Tail is a suffix of the string of the
|
||||
given size. If the input is shorter then required, whole input if considered
|
||||
to be the tail.
|
||||
|
||||
|
@ -240,7 +240,7 @@ namespace boost {
|
||||
m_Match(Other.m_Match),
|
||||
m_Next(Other.m_Next),
|
||||
m_End(Other.m_End),
|
||||
m_bEof(false)
|
||||
m_bEof(Other.m_bEof)
|
||||
{}
|
||||
|
||||
//! Constructor
|
||||
@ -259,7 +259,11 @@ namespace boost {
|
||||
m_End(End),
|
||||
m_bEof(false)
|
||||
{
|
||||
increment();
|
||||
// force the correct behavior for empty sequences and yield at least one token
|
||||
if(Begin!=End)
|
||||
{
|
||||
increment();
|
||||
}
|
||||
}
|
||||
//! Constructor
|
||||
/*!
|
||||
@ -278,7 +282,11 @@ namespace boost {
|
||||
m_Next=::boost::begin(lit_col);
|
||||
m_End=::boost::end(lit_col);
|
||||
|
||||
increment();
|
||||
// force the correct behavior for empty sequences and yield at least one token
|
||||
if(m_Next!=m_End)
|
||||
{
|
||||
increment();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -92,11 +92,11 @@ Synopsis of <tt><boost/algorithm/minmax.hpp></tt></h3>
|
||||
namespace boost {
|
||||
|
||||
template <class T>
|
||||
tuple<T const&, T const&> >
|
||||
tuple<T const&, T const&>
|
||||
minmax(const T& a, const T& b);
|
||||
|
||||
template <class T, class <a href="http://www.sgi.com/tech/stl/BinaryPredicate.html">BinaryPredicate</a>>
|
||||
tuple<T const&, T const&> >
|
||||
tuple<T const&, T const&>
|
||||
minmax(const T& a, const T& b, BinaryPredicate comp);
|
||||
|
||||
}
|
||||
@ -243,7 +243,7 @@ range
|
||||
|
||||
<a name="complexity">
|
||||
<h3>
|
||||
<a NAME="Complexity"></a>Complexity</h3>
|
||||
Complexity</h3>
|
||||
Minmax performs a single comparison and is otherwise of constant complexity.
|
||||
The use of <tt>boost::tuple<T const&></tt> prevents copy
|
||||
constructors in case the arguments are passed by reference.
|
||||
@ -438,7 +438,7 @@ comparisons).</p>
|
||||
slower than
|
||||
<tt>first_min_element</tt> alone, still much less than <tt>first_min_element</tt>
|
||||
and
|
||||
<tt>last_max_element</tt> called separately. <a href="#Performance">[2]</a>
|
||||
<tt>last_max_element</tt> called separately. <a href="#Note2">[2]</a>
|
||||
|
||||
<h4><b>Why algorithms and not accumulators?</b></h4>
|
||||
<p>The minmax algorithms are useful in computing the extent of a range.
|
||||
|
@ -12,6 +12,7 @@ toolset.using doxygen ;
|
||||
|
||||
boostbook string_algo : string_algo.xml autodoc
|
||||
:
|
||||
<xsl:param>boost.root=../../../../..
|
||||
<format>pdf:<xsl:param>boost.url.prefix=http://www.boost.org/doc/libs/release/doc/html
|
||||
;
|
||||
|
||||
|
@ -25,7 +25,7 @@
|
||||
</para>
|
||||
<para>
|
||||
<emphasis role="bold">Definition:</emphasis> A string is a
|
||||
<ulink url="../../libs/range/doc/range.html">range</ulink> of characters accessible in sequential
|
||||
<ulink url="../../libs/range/index.html">range</ulink> of characters accessible in sequential
|
||||
ordered fashion. Character is any value type with "cheap" copying and assignment.
|
||||
</para>
|
||||
<para>
|
||||
|
@ -32,7 +32,9 @@
|
||||
free-standing functions and type-generators exists:</p><code>void foo( const T&, int ); <br>
|
||||
int bar( T& ); <br>
|
||||
foo_type_of< T >::type;</code> <br> <br><hr size="1" ><h3 >Literature</h3><ul ><li > <a href="http://www.boost.org/more/generic_programming.html#type_generator" target="_self" >Type Generators</a> </li><li > <a href="http://www.boost.org/more/generic_programming.html#concept" target="_self" >Concepts</a> </li><li > <a href="http://www.sgi.com/tech/stl/stl_introduction.html" target="_self" >Concepts and SGI STL</a> </li></ul><hr size="1" ><p >© Thorsten Ottosen 2003-2004 (nesotto_AT_cs.auc.dk).
|
||||
Permission to copy, use, modify, sell and distribute this software is granted provided this copyright notice appears
|
||||
in all copies. This software is provided "as is" without express or implied warranty, and with no
|
||||
claim as to its suitability for any purpose.</p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></body></html>
|
||||
<!-- Copyright Dezide Aps 2003-2004 -->
|
||||
<br>Use, modification and distribution is subject to the Boost
|
||||
Software License, Version 1.0. (See accompanying file
|
||||
<code class="filename">LICENSE_1_0.txt</code> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
|
||||
</br>
|
||||
</p>
|
||||
<!-- Copyright Dezide Aps 2003-2004 -->
|
||||
|
@ -33,7 +33,7 @@
|
||||
<librarypurpose>
|
||||
A set of generic string-related algorithms and utilities
|
||||
</librarypurpose>
|
||||
<librarycategory name="category:algoritms"/>
|
||||
<librarycategory name="category:algorithms"/>
|
||||
<librarycategory name="category:string-text"/>
|
||||
</libraryinfo>
|
||||
|
||||
|
@ -57,7 +57,7 @@
|
||||
The magic of <ulink url="../../libs/range/index.html">Boost.Range</ulink>
|
||||
provides a uniform way of handling different string types.
|
||||
If there is a need to pass a pair of iterators,
|
||||
<ulink url="../../libs/range/doc/utility_class.html"><code>boost::iterator_range</code></ulink>
|
||||
<ulink url="../../libs/range/doc/html/range/reference/utilities/iterator_range.html"><code>boost::iterator_range</code></ulink>
|
||||
can be used to package iterators into a structure with a compatible interface.
|
||||
</para>
|
||||
</listitem>
|
||||
@ -169,7 +169,7 @@
|
||||
<programlisting>
|
||||
string str1=" hello world! ";
|
||||
string str2=trim_left_copy(str1); // str2 == "hello world! "
|
||||
string str3=trim_right_copy(str2); // str3 == " hello world!"
|
||||
string str3=trim_right_copy(str1); // str3 == " hello world!"
|
||||
trim(str1); // str1 == "hello world!"
|
||||
|
||||
string phone="00423333444";
|
||||
@ -208,7 +208,7 @@
|
||||
</programlisting>
|
||||
<para>
|
||||
We have used <functionname>find_last()</functionname> to search the <code>text</code> for "ll".
|
||||
The result is given in the <ulink url="../../libs/range/doc/utility_class.html"><code>boost::iterator_range</code></ulink>.
|
||||
The result is given in the <ulink url="../../libs/range/doc/html/range/reference/utilities/iterator_range.html"><code>boost::iterator_range</code></ulink>.
|
||||
This range delimits the
|
||||
part of the input which satisfies the find criteria. In our example it is the last occurrence of "ll".
|
||||
|
||||
@ -217,7 +217,7 @@
|
||||
<ulink url="../../libs/range/index.html">Boost.Range</ulink>.
|
||||
|
||||
The following lines transform the result. Notice that
|
||||
<ulink url="../../libs/range/doc/utility_class.html"><code>boost::iterator_range</code></ulink> has familiar
|
||||
<ulink url="../../libs/range/doc/html/range/reference/utilities/iterator_range.html"><code>boost::iterator_range</code></ulink> has familiar
|
||||
<code>begin()</code> and <code>end()</code> methods, so it can be used like any other STL container.
|
||||
Also it is convertible to bool therefore it is easy to use find algorithms for a simple containment checking.
|
||||
</para>
|
||||
@ -264,7 +264,7 @@
|
||||
the find iterator allows us to iterate over the substrings matching the specified criteria.
|
||||
This facility is using the <link linkend="string_algo.finder_concept">Finder</link> to incrementally
|
||||
search the string.
|
||||
Dereferencing a find iterator yields an <ulink url="../../libs/range/doc/utility_class.html"><code>boost::iterator_range</code></ulink>
|
||||
Dereferencing a find iterator yields an <ulink url="../../libs/range/doc/html/range/reference/utilities/iterator_range.html"><code>boost::iterator_range</code></ulink>
|
||||
object, that delimits the current match.
|
||||
</para>
|
||||
<para>
|
||||
@ -339,7 +339,7 @@
|
||||
typedef vector< string > split_vector_type;
|
||||
|
||||
split_vector_type SplitVec; // #2: Search for tokens
|
||||
split( SplitVec, str1, is_any_of("-*") ); // SplitVec == { "hello abc","ABC","aBc goodbye" }
|
||||
split( SplitVec, str1, is_any_of("-*"), token_compress_on ); // SplitVec == { "hello abc","ABC","aBc goodbye" }
|
||||
</programlisting>
|
||||
<para>
|
||||
<code>[hello]</code> designates an <code>iterator_range</code> delimiting this substring.
|
||||
|
@ -59,5 +59,11 @@ test-suite algorithm/string
|
||||
:
|
||||
: regex
|
||||
]
|
||||
[ run
|
||||
find_format_test.cpp
|
||||
: :
|
||||
:
|
||||
: find_format
|
||||
]
|
||||
;
|
||||
|
||||
|
163
string/test/find_format_test.cpp
Normal file
163
string/test/find_format_test.cpp
Normal file
@ -0,0 +1,163 @@
|
||||
// Boost string_algo library find_format_test.cpp file ------------------//
|
||||
|
||||
// Copyright (c) 2009 Steven Watanabe
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
#include <boost/algorithm/string/find_format.hpp>
|
||||
#include <boost/algorithm/string/finder.hpp>
|
||||
#include <boost/algorithm/string/formatter.hpp>
|
||||
|
||||
// Include unit test framework
|
||||
#include <boost/test/included/test_exec_monitor.hpp>
|
||||
|
||||
#include <boost/test/test_tools.hpp>
|
||||
|
||||
// We're only using const_formatter.
|
||||
template<class Formatter>
|
||||
struct formatter_result {
|
||||
typedef boost::iterator_range<const char*> type;
|
||||
};
|
||||
|
||||
template<class Formatter>
|
||||
struct checked_formatter {
|
||||
public:
|
||||
checked_formatter(const Formatter& formatter) : formatter_(formatter) {}
|
||||
template< typename T >
|
||||
typename formatter_result<Formatter>::type operator()( const T & s ) const {
|
||||
BOOST_CHECK( !s.empty() );
|
||||
return formatter_(s);
|
||||
}
|
||||
private:
|
||||
Formatter formatter_;
|
||||
};
|
||||
|
||||
template<class Formatter>
|
||||
checked_formatter<Formatter>
|
||||
make_checked_formatter(const Formatter& formatter) {
|
||||
return checked_formatter<Formatter>(formatter);
|
||||
}
|
||||
|
||||
void find_format_test()
|
||||
{
|
||||
const std::string source = "$replace $replace";
|
||||
std::string expected = "ok $replace";
|
||||
std::string output(80, '\0');
|
||||
|
||||
std::string::iterator pos =
|
||||
boost::find_format_copy(
|
||||
output.begin(),
|
||||
source,
|
||||
boost::first_finder("$replace"),
|
||||
make_checked_formatter(boost::const_formatter("ok")));
|
||||
BOOST_CHECK(pos == output.begin() + expected.size());
|
||||
output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
|
||||
BOOST_CHECK_EQUAL(output, expected);
|
||||
|
||||
output =
|
||||
boost::find_format_copy(
|
||||
source,
|
||||
boost::first_finder("$replace"),
|
||||
make_checked_formatter(boost::const_formatter("ok")));
|
||||
BOOST_CHECK_EQUAL(output, expected);
|
||||
|
||||
// now try finding a string that doesn't exist
|
||||
output.resize(80);
|
||||
pos =
|
||||
boost::find_format_copy(
|
||||
output.begin(),
|
||||
source,
|
||||
boost::first_finder("$noreplace"),
|
||||
make_checked_formatter(boost::const_formatter("bad")));
|
||||
BOOST_CHECK(pos == output.begin() + source.size());
|
||||
output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
|
||||
BOOST_CHECK_EQUAL(output, source);
|
||||
|
||||
output =
|
||||
boost::find_format_copy(
|
||||
source,
|
||||
boost::first_finder("$noreplace"),
|
||||
make_checked_formatter(boost::const_formatter("bad")));
|
||||
BOOST_CHECK_EQUAL(output, source);
|
||||
|
||||
// in place version
|
||||
output = source;
|
||||
boost::find_format(
|
||||
output,
|
||||
boost::first_finder("$replace"),
|
||||
make_checked_formatter(boost::const_formatter("ok")));
|
||||
BOOST_CHECK_EQUAL(output, expected);
|
||||
output = source;
|
||||
boost::find_format(
|
||||
output,
|
||||
boost::first_finder("$noreplace"),
|
||||
make_checked_formatter(boost::const_formatter("bad")));
|
||||
BOOST_CHECK_EQUAL(output, source);
|
||||
}
|
||||
|
||||
void find_format_all_test()
|
||||
{
|
||||
const std::string source = "$replace $replace";
|
||||
std::string expected = "ok ok";
|
||||
std::string output(80, '\0');
|
||||
|
||||
std::string::iterator pos =
|
||||
boost::find_format_all_copy(output.begin(),
|
||||
source,
|
||||
boost::first_finder("$replace"),
|
||||
boost::const_formatter("ok"));
|
||||
BOOST_CHECK(pos == output.begin() + expected.size());
|
||||
output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
|
||||
BOOST_CHECK_EQUAL(output, expected);
|
||||
|
||||
output =
|
||||
boost::find_format_all_copy(
|
||||
source,
|
||||
boost::first_finder("$replace"),
|
||||
make_checked_formatter(boost::const_formatter("ok")));
|
||||
BOOST_CHECK_EQUAL(output, expected);
|
||||
|
||||
// now try finding a string that doesn't exist
|
||||
output.resize(80);
|
||||
pos =
|
||||
boost::find_format_all_copy(
|
||||
output.begin(),
|
||||
source,
|
||||
boost::first_finder("$noreplace"),
|
||||
make_checked_formatter(boost::const_formatter("bad")));
|
||||
BOOST_CHECK(pos == output.begin() + source.size());
|
||||
output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
|
||||
BOOST_CHECK_EQUAL(output, source);
|
||||
|
||||
output =
|
||||
boost::find_format_all_copy(
|
||||
source,
|
||||
boost::first_finder("$noreplace"),
|
||||
make_checked_formatter(boost::const_formatter("bad")));
|
||||
BOOST_CHECK_EQUAL(output, source);
|
||||
|
||||
// in place version
|
||||
output = source;
|
||||
boost::find_format_all(
|
||||
output,
|
||||
boost::first_finder("$replace"),
|
||||
make_checked_formatter(boost::const_formatter("ok")));
|
||||
BOOST_CHECK_EQUAL(output, expected);
|
||||
output = source;
|
||||
boost::find_format_all(
|
||||
output,
|
||||
boost::first_finder("$noreplace"),
|
||||
make_checked_formatter(boost::const_formatter("bad")));
|
||||
BOOST_CHECK_EQUAL(output, source);
|
||||
}
|
||||
|
||||
int test_main( int, char*[] )
|
||||
{
|
||||
find_format_test();
|
||||
find_format_all_test();
|
||||
|
||||
return 0;
|
||||
}
|
@ -40,6 +40,7 @@ void iterator_test()
|
||||
string str1("xx-abc--xx-abb");
|
||||
string str2("Xx-abc--xX-abb-xx");
|
||||
string str3("xx");
|
||||
string strempty("");
|
||||
const char* pch1="xx-abc--xx-abb";
|
||||
vector<string> tokens;
|
||||
vector< vector<int> > vtokens;
|
||||
@ -123,6 +124,25 @@ void iterator_test()
|
||||
BOOST_CHECK( tokens[3]==string("xx") );
|
||||
BOOST_CHECK( tokens[4]==string("abb") );
|
||||
|
||||
split(
|
||||
tokens,
|
||||
str3,
|
||||
is_any_of(","),
|
||||
token_compress_off);
|
||||
|
||||
BOOST_REQUIRE( tokens.size()==1 );
|
||||
BOOST_CHECK( tokens[0]==string("xx") );
|
||||
|
||||
split(
|
||||
tokens,
|
||||
strempty,
|
||||
is_punct(),
|
||||
token_compress_off);
|
||||
|
||||
BOOST_REQUIRE( tokens.size()==1 );
|
||||
BOOST_CHECK( tokens[0]==string("") );
|
||||
|
||||
|
||||
find_iterator<string::iterator> fiter=make_find_iterator(str1, first_finder("xx"));
|
||||
BOOST_CHECK(equals(*fiter, "xx"));
|
||||
++fiter;
|
||||
@ -139,6 +159,7 @@ void iterator_test()
|
||||
++siter;
|
||||
BOOST_CHECK(equals(*siter, "abb"));
|
||||
++siter;
|
||||
BOOST_CHECK(siter==split_iterator<string::iterator>(siter));
|
||||
BOOST_CHECK(siter==split_iterator<string::iterator>());
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user