/*
* "LIKE" keyword challenge submissionby AIR
*
* Using C++
*
* Emulates the "*" option used in ScriptBasic's LIKE function
*
* Only tested with the string below, YMMV
*
* Compile with: g++ --std=c++11 like.cpp -o like
*/
#include<iostream>
#include<string>
#include <regex>
#include <vector>
using namespace std;
struct JOKERINFO {
string text;
int length;
int position;
int object_num;
int prev_pos;
// JOKERINFO(const string text,const int length,const int position): text(text),length(length),position(position){}
};
vector< JOKERINFO > JOKER;
int LIKE(string input,string expr) {
size_t pos = 0;
smatch match;
int i;
string src(input);
while( ( pos = expr.find("*", pos) ) != string::npos) {
if (pos==0){
expr.erase(pos,1);
}
if (pos!=0)
expr.replace(pos, 1, "\\b(\\w+)");
pos += 1;
}
regex term(expr);
int count=0;
while ( std::regex_search (src, match, term) ) {
for (int x=0 ; x < match.size(); x++){
JOKERINFO info;
info.text = match[x];
info.length = match[x].length();
info.position = match.position(x);
info.object_num = count+1;
JOKER.push_back(info);
}
count++;
src = match.suffix().str();
}
return count;
}
int main(int argc, char **argv) {
string input = "You can see in the program that the vector 'JOKER' is declared as 'global'.";
input.append(input);
// cout << input << "\n\n";
// SHOULD OUTPUT "JOKER" is global"
// and value of Joker[x] plus position in original string and length of match
if ( LIKE( input, "*vector '*' is * as '*'" ) ) {
// for (int y =0; y < JOKER.size(); y++){
// cout << JOKER[y].text << endl;
// }
cout << "** FIRST OBJECT **" << endl;
cout << "OBJECT Number: " << JOKER[3].object_num << "\n\n";
cout << JOKER[1].text << " is " << JOKER[3].text << "\n";
cout << JOKER[1].text << " starts at position: " << JOKER[1].position << " and is " << JOKER[1].length << " Characters Long." <<endl;
cout << JOKER[3].text << " starts at position: " << JOKER[3].position << " and is " << JOKER[3].length << " Characters Long.\n\n";
cout << "\n\n** SECOND OBJECT **" << endl;
cout << "OBJECT Number: " << JOKER[5].object_num << "\n\n";
cout << JOKER[5].text << " is " << JOKER[7].text << "\n";
cout << JOKER[5].text << " starts at position: " << JOKER[5].position << " and is " << JOKER[5].length << " Characters Long." <<endl;
cout << JOKER[7].text << " starts at position: " << JOKER[7].position << " and is " << JOKER[7].length << " Characters Long.\n\n";
}
return 0;
}