Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / WTL

Boost.Range support for ATL/WTL CString

Rate me:
Please Sign up or sign in to vote.
4.12/5 (9 votes)
2 Sep 20052 min read 36.1K   262   11  
Consistent interfaces to CString using Boost.Range.
//  Boost string_algo library substr_test.cpp file  ------------------//

//  Copyright Pavol Droba 2002-2003. Use, modification and
//  distribution is subject to 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/regex.hpp>
#include <boost/algorithm/string/sequence_traits.hpp>
/*
// Include unit test framework
#include <boost/test/included/test_exec_monitor.hpp>
*/
#include <string>
#include <vector>
#include <iostream>
#include <boost/regex.hpp>
#include <boost/test/test_tools.hpp>

using namespace std;
using namespace boost;

static void regex_find_test()
{
		typedef basic_regex<TCHAR> regex_t;
		typedef basic_string<TCHAR> sequence_string_t;

		string_t str1(_T("123a1cxxxa23cXXXa456c321"));
    const TCHAR* pch1=_T("123a1cxxxa23cXXXa456c321");
    regex_t rx(_T("a[0-9]+c"));
    vector<TCHAR> vec1( begin(str1), end(str1) );
    vector<sequence_string_t> tokens;

    // find results
		iterator_range<range_iterator<string_t>::type> nc_result;
    iterator_range<range_const_iterator<string_t>::type> cv_result;
    
    iterator_range<vector<TCHAR>::iterator> nc_vresult;
    iterator_range<vector<TCHAR>::const_iterator> cv_vresult;

    iterator_range<const TCHAR*> ch_result;

    // basic tests
    nc_result=find_regex( str1, rx );
    BOOST_CHECK( 
        ( (begin(nc_result)-begin(str1)) == 3) &&
        ( (end(nc_result)-begin(str1)) == 6) );

    cv_result=find_regex( str1, rx );
    BOOST_CHECK( 
        ( (begin(cv_result)-begin(str1)) == 3) &&
        ( (end(cv_result)-begin(str1)) == 6) );

    ch_result=find_regex( pch1, rx );
    BOOST_CHECK(( (begin(ch_result) - pch1 ) == 3) && ( (end(ch_result) - pch1 ) == 6 ) );

    // multi-type comparison test
    nc_vresult=find_regex( vec1, rx );
    BOOST_CHECK( 
        ( (begin(nc_result)-begin(str1)) == 3) &&
        ( (end(nc_result)-begin(str1)) == 6) );

    cv_vresult=find_regex( vec1, rx );
    BOOST_CHECK( 
        ( (begin(cv_result)-begin(str1)) == 3) &&
        ( (end(cv_result)-begin(str1)) == 6) );

    // find_all_regex test
    find_all_regex( tokens, str1, rx );

    BOOST_REQUIRE( tokens.size()==3 );
    
    BOOST_CHECK( equals(tokens[0], _T("a1c")) );
    BOOST_CHECK( equals(tokens[1], _T("a23c")) );
    BOOST_CHECK( equals(tokens[2], _T("a456c")) );
    
    // split_regex test
    split_regex(    tokens, str1, rx );

    BOOST_REQUIRE( tokens.size()==4 );
    BOOST_CHECK( equals(tokens[0], _T("123")) );
    BOOST_CHECK( equals(tokens[1], _T("xxx")) );
    BOOST_CHECK( equals(tokens[2], _T("XXX")) );
    BOOST_CHECK( equals(tokens[3], _T("321")) );
}

static void regex_replace_test()
{
		typedef basic_regex<TCHAR> regex_t;
		typedef basic_string<TCHAR> fmt_string_t;

    string_t str1(_T("123a1cxxxa23cXXXa456c321"));
    regex_t rx1(_T("a([0-9]+)c"));
    regex_t rx2(_T("([xX]+)"));
    regex_t rx3(_T("_[^_]*_"));
    fmt_string_t fmt1(_T("_A$1C_"));
    fmt_string_t fmt2(_T("_xXx_"));

    // inmutable tests
/*  
    // basic tests
    BOOST_CHECK( replace_regex_copy( str1, rx1, fmt1 )==string_t(_T("123_A1C_xxxa23cXXXa456c321")) );
    BOOST_CHECK( replace_all_regex_copy( str1, rx1, fmt1 )==string_t(_T("123_A1C_xxx_A23C_XXX_A456C_321")) );
    BOOST_CHECK( erase_regex_copy( str1, rx1 )==string_t(_T("123xxxa23cXXXa456c321")) );
    BOOST_CHECK( erase_all_regex_copy( str1, rx1 )==string_t(string_t(_T("123xxxXXX321"))) );
*/
    // output iterator variants test
    string_t strout;
    replace_regex_copy( tomato::back_inserter(strout), str1, rx1, fmt1 );
    BOOST_CHECK( equals( strout, _T("123_A1C_xxxa23cXXXa456c321") ) );
    tomato::clear(strout);
    replace_all_regex_copy( tomato::back_inserter(strout), str1, rx1, fmt1 );
    BOOST_CHECK( strout==string_t(_T("123_A1C_xxx_A23C_XXX_A456C_321")) );
    tomato::clear(strout);
    erase_regex_copy( tomato::back_inserter(strout), str1, rx1 );
    BOOST_CHECK( strout==string_t(_T("123xxxa23cXXXa456c321")) );
    tomato::clear(strout);
    erase_all_regex_copy( tomato::back_inserter(strout), str1, rx1 );
    BOOST_CHECK( strout==string_t(_T("123xxxXXX321")) );
    tomato::clear(strout);
/*
    // in-place test
    replace_regex( str1, rx1, fmt2 );
    BOOST_CHECK( str1==string_t(_T("123_xXx_xxxa23cXXXa456c321")) );

    replace_all_regex( str1, rx2, fmt1 );
    BOOST_CHECK( str1==string_t(_T("123__AxXxC___AxxxC_a23c_AXXXC_a456c321")) );
    erase_regex( str1, rx3 );
    BOOST_CHECK( str1==string_t(_T("123AxXxC___AxxxC_a23c_AXXXC_a456c321")) );
    erase_all_regex( str1, rx3 );
    BOOST_CHECK( str1==string_t(_T("123AxXxCa23ca456c321")) );
*/
}
/*
int test_main( int, TCHAR*[] )
{
    find_test();
    replace_test();

    return 0;
}
*/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Japan Japan
I am worried about my poor English...

Comments and Discussions