Originally Posted by
Dae
Python. Python is my dirty lover, and it's been good to me.
It's possible switching to C++ might help you in this case, if it helps you with memory management. I believe the majority of the coders using languages purely for math algorithms use C though, and Python for quick testing. I remember a lot of complaints about Python's core global interpreter lock. So computing an algorithm in parallel is blocked (threads are useless) - think there's a workaround.
I dunno bro, Python's string parsing is so much more convenient and readable. In the long-run it doesn't matter, but scripting languages are masters of the short-run.
someString[5:10]
Hmm, let's see:
Code:
/*
Read a phone number in the format (XXX) XXX-XXXX
*/
template < typename Iterator, typename Result >
bool read_phone_number
(
Iterator begin,
Iterator end,
Result& area_code,
Result& exchange,
Result& extension
)
{
using namespace
xtd::parser;
static syntax< >
ws = skip( token_range( " \n\t" ) ),
lp = ws + '(' + ws,
rp = ws + ')' + ws,
dash = ws + '-' + ws,
digit = terminal_range( '0', '9' ),
digits_3 = ws + ( digit * 3 ) + ws,
digits_4 = ws + ( digit * 4 ) + ws;
return
(
lp + ( digits_3 >> store( area_code ) ) + rp
+ ( digits_3 >> store( exchange ) ) + dash
+ ( digits_4 >> store( extension ) )
).parse_all( begin, end );
}
template < typename Container, typename Result >
inline bool read_phone_number
(
Container const& data,
Result& area_code,
Result& exchange,
Result& extension
)
{
return read_phone_number( data.begin( ), data.end( ), area_code, exchange, extension );
}
That's a very simple example, of course, but extending it to accept more formats would be trivial. And note that the parser doesn't skip whitespace by default, but that could be easily built into a library, if desired.
Here's a sample test program:
Code:
int main( void )
{
string
input_as_string = "( 800 )555 - 1212";
list< int >
input_as_list_of_ints( input_as_string.begin( ), input_as_string.end( ) );
vector< double >
numbers_as_doubles( 3 );
vector< int >
numbers_as_ints( 3 );
assert
(
read_phone_number
(
input_as_string,
numbers_as_doubles[ 0 ],
numbers_as_doubles[ 1 ],
numbers_as_doubles[ 2 ]
)
);
assert
(
read_phone_number
(
input_as_list_of_ints,
numbers_as_ints[ 0 ],
numbers_as_ints[ 1 ],
numbers_as_ints[ 2 ]
)
);
copy
(
numbers_as_doubles.begin( ),
numbers_as_doubles.end( ),
ostream_iterator< double >( cout, "\n" )
);
copy
(
numbers_as_ints.begin( ),
numbers_as_ints.end( ),
ostream_iterator< int >( cout, "\n" )
);
return 0;
}
Now it may seem silly to pass the input as a list< int > or place the result into a double, but my point is that it is incredibly flexible, and AFAIK would quite complicated to work out with a scripting language (correct me if I'm wrong though).
Besides all of this, the parsing is done roughly as fast as had you done it "by hand" (maybe 5% slower, max).
Anyway, so my whole point is that C++ is more than adequate for this sort of thing (although you may need to do a fair bit of coding, initially). Not that I think there's anything wrong with using a scripting language, either - I'm just defending C++ position as a contender.
EDIT:
OK, well the assertion failed. Wrong grammer. Fixed.