qtcsv/sources/reader.cpp
int ReaderPrivate::FindMiddleElementPositioin(const QString& str,
                                              const int& startPos,
                                              const QString& separator,
                                              const QString& txtDelim)
{
    const int ERROR = -1;
    if (str.isEmpty() ||
            startPos < 0 ||
            separator.isEmpty() ||
            txtDelim.isEmpty())
    {
        return ERROR;
    }

    const QString elemEndSymbols = txtDelim + separator;
    int elemEndPos = startPos;
    while(elemEndPos < str.size())
    {
        // Find position of element end symbol
        elemEndPos = str.indexOf(elemEndSymbols, elemEndPos);
        if (elemEndPos < 0)
        {
            // This element could not be the middle element, becaise string
            // do not contains any end symbols
            return ERROR;
        }

        // Check that this is really the end symbols of the
        // element and we don't mix up it with double delimiter
        // and separator. Calc number of delimiter symbols from elemEndPos
        // to startPos that stands together.
        int numOfDelimiters = 0;
        for (int pos = elemEndPos; startPos <= pos; --pos, ++numOfDelimiters)
        {
            QStringRef strRef = str.midRef(pos, txtDelim.size());
            if (QStringRef::compare(strRef, txtDelim) != 0)
            {
                break;
            }
        }

        // If we have odd number of delimiter symbols that stand together,
        // then this is the even number of double delimiter symbols + last
        // delimiter symbol. That means that we have found end position of
        // the middle element.
        if (numOfDelimiters % 2 == 1)
        {
            return elemEndPos;
        }
        else
        {
            // Otherwise this is not the end of the middle element and we
            // should try again
            elemEndPos += elemEndSymbols.size();
        }
    }

    return ERROR;
}
// Try to find end position of first or middle element
// @input:
// - str - string with data
// - startPos - start position of the current element in the string
// - separator - string or character that separate elements
// - textDelimiter - string that is used as text delimiter
// @output:
// - int - end position of the element or -1 if this element is not first
// or middle
Try to find end position of first or middle element
Try to find end position of first or middle element
int ReaderPrivate::FindMiddleElementPositioin(const QString& str,
                                              const int& startPos,
                                              const QString& separator,
                                              const QString& txtDelim)
{
    const int ERROR = -1;
    if (str.isEmpty() ||
            startPos < 0 ||
            separator.isEmpty() ||
            txtDelim.isEmpty())
    {
        return ERROR;
    }
    const QString elemEndSymbols = txtDelim + separator;
    int elemEndPos = startPos;
    while(elemEndPos < str.size())
    {
        elemEndPos = str.indexOf(elemEndSymbols, elemEndPos);
        if (elemEndPos < 0)
        {
            return ERROR;
        }
        int numOfDelimiters = 0;
        for (int pos = elemEndPos; startPos <= pos; --pos, ++numOfDelimiters)
        {
            QStringRef strRef = str.midRef(pos, txtDelim.size());
            if (QStringRef::compare(strRef, txtDelim) != 0)
            {
                break;
            }
        }
        if (numOfDelimiters % 2 == 1)
        {
            return elemEndPos;
        }
        else
        {
            elemEndPos += elemEndSymbols.size();
        }
    }
    return ERROR;
}
int ReaderPrivate :: FindMiddleElementPositioin ( const QString & str , const int & startPos , const QString & separator , const QString & txtDelim ) { const int ERROR = -1 ; if ( str . isEmpty ( ) || startPos < 0 || separator . isEmpty ( ) || txtDelim . isEmpty ( ) ) { return ERROR ; } const QString elemEndSymbols = txtDelim + separator ; int elemEndPos = startPos ; while ( elemEndPos < str . size ( ) ) { elemEndPos = str . indexOf ( elemEndSymbols , elemEndPos ) ; if ( elemEndPos < 0 ) { return ERROR ; } int numOfDelimiters = 0 ; for ( int pos = elemEndPos ; startPos <= pos ; -- pos , ++ numOfDelimiters ) { QStringRef strRef = str . midRef ( pos , txtDelim . size ( ) ) ; if ( QStringRef :: compare ( strRef , txtDelim ) != 0 ) { break ; } } if ( numOfDelimiters % 2 == 1 ) { return elemEndPos ; } else { elemEndPos += elemEndSymbols . size ( ) ; } } return ERROR ; }
Try to find end position of first or middle element
Try to find end position of first or middle element
// Find position of element end symbol // This element could not be the middle element, becaise string // do not contains any end symbols // Check that this is really the end symbols of the // element and we don't mix up it with double delimiter // and separator. Calc number of delimiter symbols from elemEndPos // to startPos that stands together. // If we have odd number of delimiter symbols that stand together, // then this is the even number of double delimiter symbols + last // delimiter symbol. That means that we have found end position of // the middle element. // Otherwise this is not the end of the middle element and we // should try again