Juce::String "GetTrailingIntValue()" method returns negative numbers, contrary to documentation

So, I have the following string: “500-78”, and if I call GetTrailingIntValue() it will return me an -78 integer. However the documentation says :
Negative numbers are not handled, so "xyz-5" returns 5."
from here: JUCE: String Class Reference

I have also tried 500a-78, and the example in case, xyz-5, and they both return the negative number -78 and -5 respectively.

Juce Version 6.0.7
Projuicer on Windows with Visual Studio 2019

As such either the documentation is wrong (easiest fix I would assume) or its unintended behaviour.

I’ve added a check on my own side to revert from negative to positive in case it happens, so no major dealbreaker, but wanted to point out! :smile:

2 Likes

The crazy thing about that documentation note is, well… AFAICT getTrailingIntValue() has always supported negative numbers.

As far back as 7dc04fe3ef3f94af7ae6861ab3f1d503e986202f in August 2008, the code read:

int String::getTrailingIntValue() const throw()
{
    int n = 0;
    int mult = 1;
    const tchar* t = text->text + length();

    while (--t >= text->text)
    {
        const tchar c = *t;

        if (! CharacterFunctions::isDigit (c))
        {
            if (c == T('-'))
                n = -n;

            break;
        }

        n += mult * (c - T('0'));
        mult *= 10;
    }

    return n;
}

(Reading commits older than that is kind of eye-searing, so that’s as far as I went. But there are only 3 more commits prior to that one that even touch that part of the code.)

Thanks for reporting, the documentation now reflects the actual behaviour of the function:

2 Likes