small debug program:
Code:
bool ci_equal(const char ch1, const char ch2)
{
return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
}
size_t ci_find(const std::string& str1, const std::string& str2)
{
std::string::const_iterator pos = search(str1. begin ( ), str1. end ( ), str2.begin ( ), str2. end ( ), ci_equal);
if (pos == str1. end ( ))
return std::string::npos;
else
return pos - str1. begin ( );
}
size_t ci_find_word(const std::string& str1, const std::string& str2)
{
std::string::const_iterator pos = search(str1. begin ( ), str1. end ( ), str2.begin ( ), str2. end ( ), ci_equal);
std::cout<<"pos = "<<pos-str1.begin()<<std::endl;
if ( pos != str1.end() && ( pos == str1.begin() || str1.at(pos-str1.begin()-1 ) == ' ') && ( pos+str2.length() == str1.end() || str1.at((pos-str1.begin())+str2.size()-1 ) == ' ') ) return pos - str1. begin ( );
else return std::string::npos;
}
void cd_to_exe_dir( char *argv[] )
{
std::string path = argv[0];
int ii = path.length();
while ( !( path[ii] == '/' || path[ii] == '\\' ) ) ii--;
path.erase( ii, 100 );
chdir( path.c_str() );
}
int main( int argc, char *argv[] )
{
cd_to_exe_dir( argv );
std::string searchword = "fox";
std::string searchin = "this is fierfox 4.0";
std::string searchin2 = "there is a Fox in here";
if ( ci_find_word( searchword, searchin ) != std::string::npos ) std::cout<<"found \""<<searchword<<"\" in \""<<searchin<<"\"\n";
else std::cout<<"did not find \""<<searchword<<"\" in \""<<searchin<<"\"\n";
if ( ci_find_word( searchword, searchin2 ) != std::string::npos ) std::cout<<"found \""<<searchword<<"\" in \""<<searchin2<<"\"\n";
else std::cout<<"did not find \""<<searchword<<"\" in \""<<searchin2<<"\"\n";
return(0);
}
output:
Code:
pos = 3
did not find "fox" in "this is fierfox 4.0"
pos = 3
did not find "fox" in "there is a Fox in here"
Press ENTER to continue.
Im still wondering, why where iterators included in the standard?