| 352 | } |
| 353 | |
| 354 | int EditorManager::detectEOLMode(ScintillaNext *editor) const |
| 355 | { |
| 356 | qInfo(Q_FUNC_INFO); |
| 357 | |
| 358 | const int MIN_LINE_THRESHOLD = 3; |
| 359 | const int MAX_BYTES_TO_CHECK = 10*1024; |
| 360 | |
| 361 | int index = 0; |
| 362 | int lf = 0; |
| 363 | int cr = 0; |
| 364 | int crlf = 0; |
| 365 | int chPrev = ' '; |
| 366 | int chNext = editor->charAt(index); |
| 367 | |
| 368 | for (int i = 0; i < qMin(MAX_BYTES_TO_CHECK, (int) editor->length()); ++i) { |
| 369 | int ch = chNext; |
| 370 | chNext = editor->charAt(i + 1); |
| 371 | |
| 372 | if (ch == '\r') { |
| 373 | if (chNext == '\n') crlf++; |
| 374 | else cr++; |
| 375 | } |
| 376 | else if (ch == '\n') { |
| 377 | if (chPrev != '\r') lf++; |
| 378 | } |
| 379 | |
| 380 | chPrev = ch; |
| 381 | |
| 382 | // If any meet some minimum threshold then just declare victory |
| 383 | if (crlf == MIN_LINE_THRESHOLD) return SC_EOL_CRLF; |
| 384 | else if (cr == MIN_LINE_THRESHOLD) return SC_EOL_CR; |
| 385 | else if (lf == MIN_LINE_THRESHOLD) return SC_EOL_LF; |
| 386 | } |
| 387 | |
| 388 | // There are either no lines or only a few, so make a best effort determination |
| 389 | |
| 390 | if (crlf > cr && crlf > lf) { |
| 391 | return SC_EOL_CRLF; |
| 392 | } |
| 393 | else if (cr > lf) { |
| 394 | return SC_EOL_CR; |
| 395 | } |
| 396 | else if (lf > cr) { |
| 397 | return SC_EOL_LF; |
| 398 | } |
| 399 | else { |
| 400 | return -1; |
| 401 | } |
| 402 | } |