2 karma · joined Aug 5, 2019 (7y ago) · 3 submissions
comment 6y ago · link · parent
Also, this implies some copy-on-write semantics. At first thought, this makes code significantly more complex.
comment 6y ago · link · parent
1. data structures don't support efficient insertion/removal at arbitrary positions in the string
2. "replace" method is O(n) where n == length of the string
3. string searching inside replace is sub-optimal. e.g., given buffer of "aaaa" and replace("aaab", "xxxx"), you need to do 4 comparisons to determine there is no match.
1 is easily rectified by using a gap buffer, or possibly a rope or a piece table, but to be honest Gap Buffer is so simple.For 2, given a more efficient backing store (e.g., gap buffer), you should be able to eliminate the O(n) buffer copy.
For 3, you can implement Boyer-Moore string search.
Gap Buffer: https://en.wikipedia.org/wiki/Gap_buffer
Rope: https://en.wikipedia.org/wiki/Rope_%28data_structure%29
Piece Table: https://en.wikipedia.org/wiki/Piece_table
Boyer-Moore: https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-sea...
comment 7y ago · link · parent
For scaling large docs, you can do a linked list of gap buffers and avoid re-allocs of large buffers.
Super simple, efficient enough for most “i wrote my own text editor”, can always plug in a more complex structure later.
Author’s implementation will drag down if you have very long lines (e.g., transpiled JS).