changeset 19802:95a94c98c884

fix eol detection in editor which caused a crash for huge files * file-editor-tab.cc (detect_eol_mode): use QByteArray instead of char pointer, manually counting eol chars replaced by count method of QByteArray
author Torsten <ttl@justmail.de>
date Thu, 19 Feb 2015 13:26:40 +0100
parents a010a65ace1f
children 70911df8ad28
files libgui/src/m-editor/file-editor-tab.cc
diffstat 1 files changed, 17 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Thu Feb 19 02:41:42 2015 -0500
+++ b/libgui/src/m-editor/file-editor-tab.cc	Thu Feb 19 13:26:40 2015 +0100
@@ -1349,36 +1349,16 @@
 QsciScintilla::EolMode
 file_editor_tab::detect_eol_mode ()
 {
-  char *text = _edit_area->text ().toAscii ().data ();
-  int text_size = _edit_area->text ().length ();
-
-  char eol_lf = 0x0a;
-  char eol_cr = 0x0d;
-
-  int count_lf = 0;
-  int count_cr = 0;
-  int count_crlf = 0;
+  QByteArray text = _edit_area->text ().toAscii ();
 
-  for (int i = 0; i < text_size; i++)
-    {
-      if (text[i] == eol_lf)
-        {
-          count_lf++;
-        }
-      else
-        {
-          if (text[i] == eol_cr)
-            {
-              if ((i < text_size -1) && text[i+1] == eol_lf)
-                {
-                  count_crlf++;
-                  i++;
-                }
-              else
-                count_cr++;
-            }
-        }
-    }
+  QByteArray eol_lf = QByteArray (1,0x0a);
+  QByteArray eol_cr = QByteArray (1,0x0d);
+  QByteArray eol_crlf = eol_cr;
+  eol_crlf.append (eol_lf);
+
+  int count_crlf = text.count (eol_crlf);
+  int count_lf = text.count (eol_lf) - count_crlf;  // isolated lf
+  int count_cr = text.count (eol_cr) - count_crlf;  // isolated cr;
 
   // get default from OS or from settings
 #if defined (Q_OS_WIN32)
@@ -1388,9 +1368,9 @@
 #else
   int os_eol_mode = QsciScintilla::EolUnix;
 #endif
-QSettings *settings = resource_manager::get_settings ();
-QsciScintilla::EolMode eol_mode = static_cast<QsciScintilla::EolMode> (
-      settings->value("editor/default_eol_mode",os_eol_mode).toInt ());
+  QSettings *settings = resource_manager::get_settings ();
+  QsciScintilla::EolMode eol_mode = static_cast<QsciScintilla::EolMode> (
+        settings->value("editor/default_eol_mode",os_eol_mode).toInt ());
 
   int count_max = 0;
 
@@ -1399,16 +1379,16 @@
       eol_mode = QsciScintilla::EolWindows;
       count_max = count_crlf;
     }
+  if (count_lf > count_max)
+    {
+      eol_mode = QsciScintilla::EolUnix;
+      count_max = count_lf;
+    }
   if (count_cr > count_max)
     {
       eol_mode = QsciScintilla::EolMac;
       count_max = count_cr;
     }
-  if (count_lf > count_max)
-    {
-      eol_mode = QsciScintilla::EolUnix;
-      count_max = count_lf;
-    }
 
   return eol_mode;
 }