It's not efficient, but with how much memory we have to work with these days I think a text editor could just use an array, and just copy to a whole new array during an insert operation.. Basically still just O(n) time. Or it could just use a tree with each word being a node for O(log n) time . Yes use more memory but hey why not As long as you aren't creating gigabytes files, probably work just fine these days.
I'd stay away from word wrap I hear it turns out to be 20x harder than most people think
Fortunately all the other projects are ones that I actually have done. Just never bothered with a text editor yet lol.
The array solution is O(N) in the length of the whole text, the proper solution is O(1) in the length of the text and O(N) in the length of the inserted text.
A nice approach is to use a "gap buffer", which keeps all text as two contiguous blocks, one of which is located at the start of the buffer and one at the end. Any time one needs to perform an operation which isn't at the gap, one would move all the text between the gap location and the new edit location to the other end of the buffer.
As for word wrap, the complexity of that depends upon whether one is using proportional fonts and/or Unicode. Proportional fonts add some complexity, but nothing overly difficult. Full Unicode support, however, is another story. Even not counting font shapes, I'm not sure it would be possible to implement an HTML-canvas based text editor with full Unicode support, which uses fillText for all its text rendering (and no external references), in less than a megabyte of Javascript. I'd even regard a "render paragraph" function that handles all applicable corner cases involving grapheme clusters, zero-width joins, bidirectional text, Emoji skin tone modifiers, etc. in less than a megabyte as being rather impressive.
Jesus Christ people downvotes this? You guys are so damn harsh. Maybe have a fucking conversation?
How many lines typically are in a basic text editor! Even windows notepad chokes in large files.
If you for example use it for a programming scratchpad well the longest since file I ever had was maybe 8000 lines long. And say 80 characters wide. They ain't shit for memory. You could literally just make a whole new array each time without problems. Not even something to worry about in modern computers
I get some of you guys are data structure nerds and I never said it wasn't worth using them. I just said you probably could get away with not even bothering to do so anymore, unless you are planning on working on some giant manuscripts. And for just text O(n) is plenty fast
Fucking premature optimizers the lot of ya
Even my solution is faster than O(n) if you resize the array in place (or even just allocate a big ass chunk of ram ahead of time why not) you only have to move the characters after the insert to make room!
The longest files I have during programming are million-line log files. Open that, do one edit command, and watch the computer churn a little too long.
Good that you get downvoted into oblivion, where the rest of you poisonous morons belong.
Premature optimization is only premature in situations where performance is clearly irrelevant or when optimizations should be delayed for the sake of achieving a reference that can be measured against.
In those situations, your optimizations should always be easy to integrate or add to the system.
Resources are also always limited.
So shut the fuck up, you dumb fucking code monkey. You know nothing and do not deserve to speak. Your words are cancerous and reek of the filth that this industry is plagued with.
-73
u/MetalSlug20 Dec 14 '19 edited Dec 14 '19
It's not efficient, but with how much memory we have to work with these days I think a text editor could just use an array, and just copy to a whole new array during an insert operation.. Basically still just O(n) time. Or it could just use a tree with each word being a node for O(log n) time . Yes use more memory but hey why not As long as you aren't creating gigabytes files, probably work just fine these days.
I'd stay away from word wrap I hear it turns out to be 20x harder than most people think
Fortunately all the other projects are ones that I actually have done. Just never bothered with a text editor yet lol.