#include <iostream>

#include <qt4/QtCore/QString>

    /* Strip the common prefix of two strings from the first one and trim 
     * whitespaces from the beginning of the resultant string.
     * Case-insensitive.
     *
     * @param input the string being processed
     * @param ref the string used to determine prefix
     */
    QString decapitateString( const QString &input, const QString &ref )
    {
        //Sometimes it's good to be c-like...
        int len;    //the length of common prefix calculated so far
        for (len = 0; len<input.length() && len<ref.length(); len++){
            if (input.at(len).toUpper() != ref.at(len).toUpper())
                break;
        }
        
        return input.right(input.length() - len).trimmed();
    }



int main()
{
    using namespace std;
    //let's test it
    QString p[][3] = {
        //argument1, argument2, correct answer
        {"simple prefix","simple test","prefix"},
        {"just", "test", "just"},
        {"ContainsOther","Contains","Other"},
        {"Contains","ContainsOther",""},
        {"Equals","Equals",""},
        {"","",""},
        {"","AnotherIsEmpty",""},
        {"AnotherIsEmpty","","AnotherIsEmpty"},
        {"","something",""},
        {"something","","something"},
        {"   \t  ","   whatever",""},
        {"sEvERal wOrDs in One StrinG","several words indeed!","One StrinG"},
        {"sEvERal wOrDs  --  now it's more simple","several words, yeah!","--  now it's more simple"},
    };
    int const num = 13;
    int failed = num;
    for (int i = 0; i<num ; i++){
        cout << "Test "<<(i+1)<<" ";
        QString res = decapitateString(p[i][0],p[i][1]);
        if (res == p[i][2]){
            cout <<"ok"<<endl;
            failed--;
        }else{
            cout <<"FAIL: "<<"f(`"<<p[i][0].toStdString()<<"',`"<<p[i][1].toStdString()<<"')"<<endl
                <<"  yields  "<<res.toStdString()<<endl
                <<"  must be "<<p[i][2].toStdString()<<endl;
        }
    }
    cerr << failed << " tests failed."<<endl;
    return failed;
}

