inline StringClass replace_all_occurrences_with_wildcards( StringClass str, ForwardIterator it_string_to_find, ForwardIterator it_string_to_find_end, ForwardIterator it_string_to_replace, ForwardIterator it_string_to_replace_end) { for(; it_string_to_find != it_string_to_find_end && it_string_to_replace != it_string_to_replace_end; ++it_string_to_find, ++ it_string_to_replace) { std::size_t wildcard_pos = it_string_to_find->find("*"); if(wildcard_pos == StringClass::npos) { ForwardIterator it_to_find_current_end(it_string_to_find); ForwardIterator it_to_replace_current_end(it_string_to_replace); str = replace_all_occurrences_of( str, it_string_to_find, ++it_to_find_current_end, it_string_to_replace, ++it_to_replace_current_end); continue; } std::size_t wildcard_pos_replace = it_string_to_replace->find("*"); std::size_t found_begin = str.find( it_string_to_find->substr(0, wildcard_pos) ); while( found_begin != StringClass::npos ) { std::size_t found_end = str.find(it_string_to_find->substr(wildcard_pos+1), found_begin + wildcard_pos + 1); // to simplify if( found_end != StringClass::npos ) { if( wildcard_pos_replace == StringClass::npos ) { StringClass replace_content = *it_string_to_replace; str.replace( found_begin, found_end + (it_string_to_find->size() - wildcard_pos - 1 ) - found_begin, replace_content); } else { StringClass replace_content = it_string_to_replace->substr(0, wildcard_pos_replace) + str.substr(found_begin + wildcard_pos, found_end - found_begin - wildcard_pos) + it_string_to_replace->substr(wildcard_pos_replace+1) ; str.replace( found_begin, found_end + (it_string_to_find->size() - wildcard_pos - 1 ) - found_begin, replace_content); } } // may adapt the restart to the replacement and be more efficient found_begin = str.find( it_string_to_find->substr(0, wildcard_pos), found_begin + 1 ); } } return str; }
inline StringClass replace_all_occurrences_of( StringClass str, ForwardIterator first1, ForwardIterator last1, ForwardIterator first2, ForwardIterator last2) { for(; first1 != last1 && first2 != last2; ++first1, ++first2) { std::size_t found = str.find( *first1 ); while( found != StringClass::npos ) { str.replace(found, first1->size(), *first2 ); found = str.find( *first1, found + first2->size() ); } } return str; }
ForwardIterator scan_keyword(InputIterator& b, InputIterator e, ForwardIterator kb, ForwardIterator ke, std::ios_base::iostate& err ) { typedef typename std::iterator_traits<InputIterator>::value_type CharT; size_t nkw = std::distance(kb, ke); const unsigned char doesnt_match = '\0'; const unsigned char might_match = '\1'; const unsigned char does_match = '\2'; unsigned char statbuf[100]; unsigned char* status = statbuf; // Change free by free_aux to avoid // Error: Could not find a match for boost::interprocess::unique_ptr<unsigned char, void(*)(void*)>::unique_ptr(int, extern "C" void(void*)) unique_ptr<unsigned char, void(*)(void*)> stat_hold(0, free_aux); if (nkw > sizeof(statbuf)) { status = (unsigned char*)malloc(nkw); if (status == 0) throw_exception(std::bad_alloc()); stat_hold.reset(status); } size_t n_might_match = nkw; // At this point, any keyword might match size_t n_does_match = 0; // but none of them definitely do // Initialize all statuses to might_match, except for "" keywords are does_match unsigned char* st = status; for (ForwardIterator ky = kb; ky != ke; ++ky, ++st) { if (!ky->empty()) *st = might_match; else { *st = does_match; --n_might_match; ++n_does_match; } } // While there might be a match, test keywords against the next CharT for (size_t indx = 0; b != e && n_might_match > 0; ++indx) { // Peek at the next CharT but don't consume it CharT c = *b; bool consume = false; // For each keyword which might match, see if the indx character is c // If a match if found, consume c // If a match is found, and that is the last character in the keyword, // then that keyword matches. // If the keyword doesn't match this character, then change the keyword // to doesn't match st = status; for (ForwardIterator ky = kb; ky != ke; ++ky, ++st) { if (*st == might_match) { CharT kc = (*ky)[indx]; if (c == kc) { consume = true; if (ky->size() == indx+1) { *st = does_match; --n_might_match; ++n_does_match; } } else { *st = doesnt_match; --n_might_match; } } } // consume if we matched a character if (consume) { ++b; // If we consumed a character and there might be a matched keyword that // was marked matched on a previous iteration, then such keywords // which are now marked as not matching. if (n_might_match + n_does_match > 1) { st = status; for (ForwardIterator ky = kb; ky != ke; ++ky, ++st) { if (*st == does_match && ky->size() != indx+1) { *st = doesnt_match; --n_does_match; } } } } } // We've exited the loop because we hit eof and/or we have no more "might matches". if (b == e) err |= std::ios_base::eofbit; // Return the first matching result for (st = status; kb != ke; ++kb, ++st) if (*st == does_match) break; if (kb == ke) err |= std::ios_base::failbit; return kb; }