Problem with GetSubstring

SyntaxEditor for Windows Forms Forum

Posted 18 years ago by Karl Grambow
Avatar
Hi,

This is SyntaxEditor v4.0 on .NET 1.1.

I'm having a problem with GetSubString (or rather, document.text.length).

I need to use GetSubstring, where under certain circumstances, the length parameter corresponds to the length of the text. So I effectively do something like:

Dim text As String = SyntaxEditor1.Document.GetSubstring(0, SyntaxEditor1.Text.Length)
The problem is, if I have multiple lines in the document, the Length property returns 2 for every line so the ultimate length returned is longer than the GetSubString method can handle and it throws an exception.

System.ArgumentException - The text range '{StartOffset=0,EndOffset=88}' must be less than or equal to the count of characters in the document.

If the Length property counts each lineterminator as two characters then shouldn't the text range find that length a valid length?

Thanks,

Karl

Comments (3)

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Karl,

In v4.0 we changed things so that the Document.Text property returns the document text using CR/LF line ends since that is more naturally used. The reason we did this is that a lot of people would just save off the Document.Text to a file and then find that they saved LF-only terminated lines and not CR/LF like they thought.

Internally SyntaxEditor stores line ends as LF only while editing a document. This is to make it easy for parsing since you know exactly what character is there and that there is only one character flagging a line end. The Document.Length will return the number of characters in the document counting only one LF character for each line.

So to sum up, Document.Text.Length will NOT equal Document.Length if you have more than one line. In fact, for performance reasons and because of the problem you are experiencing, you should ALWAYS use Document.Length instead of Document.Text.Length since the latter constructs an entire string of the document and will be much slower than calling Document.Length.

Back to your case, you can do this:
Dim text As String = SyntaxEditor1.Document.GetSubstring(0, SyntaxEditor1.Document.Length)
Please note that the smaller overload of GetSubstring will also perform like the Document.Text property and will return CR/LF. There is an overload of it that lets you indicate if you want a different LineTerminator though such as LF-only. There also is a Document.GetText which takes a LineTerminator and returns the same as the Document.Text property but lets you indicate which type of LineTerminator to use for the text.

Hope that helps explain it.


Actipro Software Support

Posted 18 years ago by Karl Grambow
Avatar
Thanks a lot for the reply.

I noticed that SyntaxEditor now used CR/LF in the text and I'd just assumed that internally you were doing the same.

Regards,

Karl
Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
We stuck with LF for internals because it's easier to always know that there is one character that marks a line terminator instead of two.


Actipro Software Support

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.