SPlatten
SPlatten

Reputation: 5758

Qt5.5 QString indexOf odd result

I am using Qt Creator to develop a C++ application and the debugger to examine the code, I am trying to understand some very odd results reported by the debugger.

    if ( intDelimiter == -1
      && (intOpB = strProcessed.indexOf("[")) >= 0
      && (intClB = strProcessed.indexOf("]", ++intOpB) >= 0) ) {
        strRef = strProcessed.mid(intOpB, intClB - intOpB);

        if ( pobjNode != NULL ) {
            strProcessed.replace(strRef, pobjNode->strGetAttr(strRef));
        }

I have a breakpoint on the line:

    strRef = strProcessed.mid(intOpB, intClB - intOpB);

In the code snippet above strProcessed contains:

    "1079-[height]"

When the breakpoint is hit, intClB contains 1 and intOpB contains 6.

intOpB is correct because the returned value from indexOf is 5 then its incremented before the search for "]", but intClB is not correct, why is the debugger reporting it as 1? This makes no sense to me.

I am using:

    Qt Creator 3.6.0
    Based on Qt 5.5.1 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 64bit)
    Built On Dec 15 2015 01:01:12
    Revision: b52c2f91f5

As spotted by king_nak, the corrected code should read:

    if ( intDelimiter == -1
    && ((intOpB = strProcessed.indexOf("[")) >= 0
    && (intClB = strProcessed.indexOf("]", ++intOpB)) >= 0) ) {
        strRef = strProcessed.mid(intOpB, intClB - intOpB);

        if ( pobjNode != NULL ) {
            strProcessed.replace(strRef, pobjNode->strGetAttr(strRef));
        }
    }

Upvotes: 1

Views: 107

Answers (1)

king_nak
king_nak

Reputation: 11513

You have misplaced a brace:

(intClB = strProcessed.indexOf("]", ++intOpB) >= 0)

This assigns the result of strProcessed.indexOf("]", ++intOpB) >= 0 to intClB, interpreted as int. As this statement is true, intClB = 1.

You want:

(intClB = strProcessed.indexOf("]", ++intOpB) ) >= 0
                                              ^ Brace here

Upvotes: 2

Related Questions