[BR] Rectangle<float>::fromString() giving integers

fromString won’t work propertly on float rectangles, as it parses to int:

juce::Rectangle<float> rect { 0.1f, 2.3f, 0.4f, 4.5f };
DBG ("rect " << rect.toString());

auto rectBack = juce::Rectangle<float>::fromString (rect.toString());
DBG ("rectBack " << rectBack.toString());

rect 0.1 2.3 0.4 4.5
rectBack 0 2 0 4

That’s because Rectangle::fromString is looking for integer tokens, not floats:

/** Parses a string containing a rectangle's details.

    The string should contain 4 integer tokens, in the form "x y width height". They
    can be comma or whitespace separated.

    This method is intended to go with the toString() method, to form an easy way
    of saving/loading rectangles as strings.

    @see toString
*/
static Rectangle fromString (StringRef stringVersion)
{
    StringArray toks;
    toks.addTokens (stringVersion.text.findEndOfWhitespace(), ",; \t\r\n", "");

    return { parseIntAfterSpace (toks[0]),
             parseIntAfterSpace (toks[1]),
             parseIntAfterSpace (toks[2]),
             parseIntAfterSpace (toks[3]) };
}

Doesn’t appear to be a floating-point version of that.

yep. but I think this is the most important bit :

This method is intended to go with the toString() method, to form an easy way
    of saving/loading rectangles as strings.

I know it will be a breaking change, but float support should be added in my opinion.

2 Likes

I think it needs specialisation for Rectangle<int> and Rectangle<float>.

Or changing the parseIntAfterSpace into a

template<typename ValueType>
ValueType parseNumberAfterSpace (StringRef s) noexcept;

With specialisations to use getIntValue32() or getFloatValue().

Thanks for reporting this. I think the old behaviour was an oversight, so I’ve added parsers for float and double Rectangles too:

2 Likes