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
| 349 | // - int - end position of the element or -1 if this element is not first |
| 350 | // or middle |
| 351 | int ReaderPrivate::findMiddleElementPosition( |
| 352 | const QString& str, |
| 353 | const int& startPos, |
| 354 | const QString& separator, |
| 355 | const QString& txtDelim) |
| 356 | { |
| 357 | const int ERROR = -1; |
| 358 | if (str.isEmpty() || |
| 359 | startPos < 0 || |
| 360 | separator.isEmpty() || |
| 361 | txtDelim.isEmpty()) |
| 362 | { |
| 363 | return ERROR; |
| 364 | } |
| 365 | |
| 366 | const QString elemEndSymbols = txtDelim + separator; |
| 367 | int elemEndPos = startPos; |
| 368 | while (elemEndPos < str.size()) |
| 369 | { |
| 370 | // Find position of element end symbol |
| 371 | elemEndPos = str.indexOf(elemEndSymbols, elemEndPos); |
| 372 | if (elemEndPos < 0) |
| 373 | { |
| 374 | // This element could not be the middle element, becaise string |
| 375 | // do not contains any end symbols |
| 376 | return ERROR; |
| 377 | } |
| 378 | |
| 379 | // Check that this is really the end symbols of the |
| 380 | // element and we don't mix up it with double delimiter |
| 381 | // and separator. Calc number of delimiter symbols from elemEndPos |
| 382 | // to startPos that stands together. |
| 383 | int numOfDelimiters = 0; |
| 384 | for (int pos = elemEndPos; startPos <= pos; --pos, ++numOfDelimiters) |
| 385 | { |
| 386 | QStringRef strRef = str.midRef(pos, txtDelim.size()); |
| 387 | if (QStringRef::compare(strRef, txtDelim) != 0) |
| 388 | { |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // If we have odd number of delimiter symbols that stand together, |
| 394 | // then this is the even number of double delimiter symbols + last |
| 395 | // delimiter symbol. That means that we have found end position of |
| 396 | // the middle element. |
| 397 | if (numOfDelimiters % 2 == 1) |
| 398 | { |
| 399 | return elemEndPos; |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | // Otherwise this is not the end of the middle element and we |
| 404 | // should try again |
| 405 | elemEndPos += elemEndSymbols.size(); |
| 406 | } |
| 407 | } |
| 408 |