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: - qsizetype - end position of the element or -1 if this element is not first or middle
| 311 | // - qsizetype - end position of the element or -1 if this element is not first |
| 312 | // or middle |
| 313 | qsizetype ReaderPrivate::findMiddleElementPosition( |
| 314 | const QString& str, |
| 315 | const qsizetype& startPos, |
| 316 | const QString& separator, |
| 317 | const QString& txtDelim) |
| 318 | { |
| 319 | const qsizetype ERROR = -1; |
| 320 | if (str.isEmpty() || |
| 321 | startPos < 0 || |
| 322 | separator.isEmpty() || |
| 323 | txtDelim.isEmpty()) |
| 324 | { |
| 325 | return ERROR; |
| 326 | } |
| 327 | |
| 328 | const auto elemEndSymbols = txtDelim + separator; |
| 329 | auto elemEndPos = startPos; |
| 330 | while (elemEndPos < str.size()) { |
| 331 | // Find position of element end symbol |
| 332 | elemEndPos = str.indexOf(elemEndSymbols, elemEndPos); |
| 333 | if (elemEndPos < 0) { |
| 334 | // This element could not be the middle element, becaise string |
| 335 | // do not contains any end symbols |
| 336 | return ERROR; |
| 337 | } |
| 338 | |
| 339 | // Check that this is really the end symbols of the |
| 340 | // element and we don't mix up it with double delimiter |
| 341 | // and separator. Calc number of delimiter symbols from elemEndPos |
| 342 | // to startPos that stands together. |
| 343 | qsizetype numOfDelimiters = 0; |
| 344 | for (auto pos = elemEndPos; startPos <= pos; --pos, ++numOfDelimiters) { |
| 345 | const auto strRef = str.mid(pos, txtDelim.size()); |
| 346 | if (QString::compare(strRef, txtDelim) != 0) { break; } |
| 347 | } |
| 348 | |
| 349 | // If we have odd number of delimiter symbols that stand together, |
| 350 | // then this is the even number of double delimiter symbols + last |
| 351 | // delimiter symbol. That means that we have found end position of |
| 352 | // the middle element. |
| 353 | if (numOfDelimiters % 2 == 1) { |
| 354 | return elemEndPos; |
| 355 | } |
| 356 | else { |
| 357 | // Otherwise this is not the end of the middle element and we |
| 358 | // should try again |
| 359 | elemEndPos += elemEndSymbols.size(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return ERROR; |
| 364 | } |
| 365 | |
| 366 | // Check if current element is the last element |
| 367 | // @input: |