<<< In-line text transformations | Lambda Home | Text replace by cttl::edge >>> |
Common Text Transformation Library http://cttl.sourceforge.net/
Program node_insert.cpp inserts text fragments into the input string by using overloaded cttl::node::operator-= and cttl::node::operator+= operators. This type of manipulation is limited to the data source specified by an instance of std::string.
Keywords: node_insert.cpp, cttl node operators, const_edge, alias::offset, itos
// node_insert.cpp // Program demonstrates cttl::node functionality. #define CTTL_TRACE_EVERYTHING #include "cttl/cttl.h" #include "lambda/lambda.h" #include "utils/itos.h" using namespace cttl; int main(/*int argc, char* argv[]*/) { typedef CTTL_STD_STRING ( *function_T ) ( size_t ); std::string inp; lambda< std::string >::scalar_reference sinp( &inp ); node<> node1( inp ); node<> node2 = node1; const_edge<> edge12( node1, node2 ); lambda< node<> >::scalar_reference snode1( &node1 ); lambda< node<> >::scalar_reference snode2( &node2 ); lambda< const_edge<> >::scalar_reference sedge12( &edge12 ); ( CTTL_LAMBDA_ASSERT( scalar( 0u ) == alias::offset( snode1 ) ), snode1 -= scalar( std::string( "1" ) ), // positions of nodes at insertion point remain unchanged: CTTL_LAMBDA_ASSERT( sinp == scalar( std::string( "1" ) ) ), CTTL_LAMBDA_ASSERT( scalar( 0u ) == alias::offset( snode1 ) ), CTTL_LAMBDA_ASSERT( scalar( 0u ) == alias::offset( snode2 ) ), snode1 += scalar( std::string( "2" ) ), // positions of all nodes adjusted: CTTL_LAMBDA_ASSERT( sinp == scalar( std::string( "21" ) ) ), CTTL_LAMBDA_ASSERT( scalar( 1u ) == alias::offset( snode1 ) ), CTTL_LAMBDA_ASSERT( scalar( 1u ) == alias::offset( snode2 ) ), snode1 -= scalar( std::string( "3" ) ), CTTL_LAMBDA_ASSERT( sinp == scalar( std::string( "231" ) ) ), CTTL_LAMBDA_ASSERT( scalar( 1u ) == alias::offset( snode1 ) ), CTTL_LAMBDA_ASSERT( scalar( 1u ) == alias::offset( snode2 ) ), snode1 += scalar( std::string( "4" ) ), CTTL_LAMBDA_ASSERT( sinp == scalar( std::string( "2431" ) ) ), CTTL_LAMBDA_ASSERT( scalar( 2u ) == alias::offset( snode1 ) ), CTTL_LAMBDA_ASSERT( scalar( 2u ) == alias::offset( snode2 ) ), // translate node offset: ( sinp^function_T( &itos< size_t > ) ) = alias::offset( snode2 ), CTTL_LAMBDA_ASSERT( sinp == scalar( std::string( "2" ) ) ), snode1 = size_t( 0 ), // set node offset snode2 = size_t( 1 ), CTTL_LAMBDA_ASSERT( snode1 <= snode2 ), // comapare node offsets // edge may be used anywhere in place of std::string: scalar( &edge12.first ) = snode1, scalar( &edge12.second ) = snode2, snode1 += sedge12, CTTL_LAMBDA_ASSERT( sinp == scalar( std::string( "22" ) ) ) ).evaluate(); assert( inp == "22" ); return 0; }
The second sample, node_insert_substring.cpp, demonstrates substring edge12 being inserted at positions specified by the nodes node1 and node2. The implementation invokes a specific std::string::insert() member function.
Keywords: node_insert_substring.cpp, edge, alias::edge::insert_stay, alias::edge::insert_go
// node_insert_substring.cpp // Program demonstrates substrings inserted at the // specified cttl::node positions. #define CTTL_TRACE_EVERYTHING #include "cttl/cttl.h" #include "lambda/lambda.h" #include "utils/itos.h" using namespace cttl; int main(/*int argc, char* argv[]*/) { typedef CTTL_STD_STRING ( *function_T ) ( size_t ); std::string inp = "ABC"; lambda< std::string >::scalar_reference sinp( &inp ); node<> node1( inp ); node<> node2 = node1; edge<> edge12( node1, node2, 0, 2 ); lambda< node<> >::scalar_reference snode1( &node1 ); lambda< node<> >::scalar_reference snode2( &node2 ); lambda< edge<> >::scalar_reference sedge12( &edge12 ); ( CTTL_LAMBDA_ASSERT( scalar( 3u ) == alias::offset( snode1 ) ) ).evaluate(); ( CTTL_LAMBDA_ASSERT( alias::text( sedge12 ) == scalar( std::string( "AB" ) ) ) ).evaluate(); ( alias::edge::insert_stay( &sedge12, &node1 ) ).evaluate(); // positions of nodes at insertion point remain unchanged: ( CTTL_LAMBDA_ASSERT( sinp == scalar( std::string( "ABCAB" ) ) ) ).evaluate(); ( CTTL_LAMBDA_ASSERT( scalar( 3u ) == alias::offset( snode1 ) ) ).evaluate(); ( CTTL_LAMBDA_ASSERT( scalar( 3u ) == alias::offset( snode2 ) ) ).evaluate(); ( CTTL_LAMBDA_ASSERT( alias::text( sedge12 ) == scalar( std::string( "AB" ) ) ) ).evaluate(); ( alias::edge::insert_go( &sedge12, &node1 ) ).evaluate(); // node positions adjusted: ( CTTL_LAMBDA_ASSERT( sinp == scalar( std::string( "ABCABAB" ) ) ) ).evaluate(); ( CTTL_LAMBDA_ASSERT( scalar( 5u ) == alias::offset( snode1 ) ) ).evaluate(); ( CTTL_LAMBDA_ASSERT( scalar( 5u ) == alias::offset( snode2 ) ) ).evaluate(); ( CTTL_LAMBDA_ASSERT( alias::text( sedge12 ) == scalar( std::string( "AB" ) ) ) ).evaluate(); assert( inp == "ABCABAB" ); return 0; }
Copyright © 1997-2009 Igor Kholodov mailto:cttl@users.sourceforge.net.
Permission to copy and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.
<<< In-line text transformations | Lambda Home | Text replace by cttl::edge >>> |