Error when calling Amarok::decapitateString()

Nicholas Sinlock isolatedincident at gmail.com
Thu Jan 8 22:23:09 UTC 2009


Pavel Shved wrote:
> Mark Kretschmann wrote:
>
>> Cool. It would be great if you could send us a patch when you are done
>> fixing it :)
>
> Hm, now i tihnk i should've just sent a patch instead of long 
> description.
>
> Please, check the file attached and test cases there--in case i 
> misunderstood the function's purpose.  If it's OK, commit it to 
> appropriate place; don't think you allow anonymous users into your svn.
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Amarok mailing list
> Amarok at kde.org
> https://mail.kde.org/mailman/listinfo/amarok
I kinda like it.  I see a few things about it though.   First, even if 
the reference string is the exact common prefix, you'll still
step through the entire length.   Second it doesn't do anything about 
extraneous symbols or white space in the "clean" string
after the common prefix is removed, which I think might be useful 
considering it's application and that it is a Amarok specific function.

I had already refactored the existing function myself, so i'll throw it 
in here.   Maybe we can combine the two somehow, since I really like the 
idea of using a dumb for loop.
My version is below.

QString decapitateString( const QString &input, const QString &ref )
{
    QString t = ref;
    QString clean = input;
    int length = t.length();
    int commonLength = 0;
    int iterator;
    while( length > 0 )
    {
        if ( input.startsWith( t, Qt::CaseInsensitive ) )
        {
            commonLength = t.length();
            if ( t.length() == ref.length() )
            {
                length = 0;
                break;
             }
        length = length/2;
        t = ref.left( t.length() + length );
        }
        else
        {
            length = length/2;
            t = ref.left( t.length() - length );
        }
    }
    iterator = commonLength;
    while (true)
    {
        if( (clean.at(iterator)).isLetterOrNumber())
        {
            clean = clean.right( clean.length() - iterator );
            clean = clean.trimmed();
            break;
        }
        else if ( (iterator + 1) == clean.length() )
        {
            clean = clean.right( clean.length() - commonLength );
            clean = clean.trimmed();
            break;
        }
        else
            iterator++;
    }
    return clean;
}

Lemme know what you think...



More information about the Amarok mailing list