Problem with TextEditor


#1

Just as a sample, I want to load a text file into a TextEditor component and save the text afterwards in the same or a different file.

The code sequence is:
String text = chosenFile.loadFileAsString();
textEditor->setText (text)

String text = textEditor->getText();
chosenFile.replaceWithText (text);

Unfortunatly this code sequence replaces “\r\n” sequences in the text file with single “\r” characters, which corrupts the text file in Windows.
As far as I have debugged the code, when using “setText” with “\r\n” sequences, these are inserted into the TextEditor as atoms with the single character “\r”, whereas newlines entered in the editor are represented as “\n” atoms. When writing the text into the file, “\n” is written as “\r\n”, whereas a single “\r” is written as it is.

Am I right? Can that be fixed or do I have to code a workaround?
Thank You,
Andreas


#2

I’d be very surprised if it used a single “\r” - do you mean a “\n” ?

It’s an interesting point - maybe in texteditor.cpp, line 1917 should be

instead?


#3

I use the released code of version 1.44. The line that you mean is there at 1835.
No, I think this “insertTextAtCursor (T(”\n"));" is fine, I would not change that.
To my opinion, the code in “initialiseAtoms” at line 302 should be changed to:

            if (text[i] == T('\r'))
            {
                ++i;
                end = i;

                if ((i < len) && (text[i] == T('\n')))
                {
                    start = i;
                    ++i;
                    end = i;
                }
            }

This would insert the “\n” and not the “\r” as atom, when a “\r\n” is found. That works for me.
Thank you


#4

Yes, you’re quite right. Looking at it, there was also a related bug in there when pasting text. I’ve checked in a fix now - thanks!


#5