3 ms·
Briefly looking at the code: 1. data structures don't support efficient insertion/removal at arbitrary positions in the string 2. "replace" method is O(n)
by mikeeeek 7y ago
Briefly looking at the code:
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 https://en.wikipedia.org/wiki/Gap_buffer
Rope: https://en.wikipedia.org/wiki/Rope_%28data_structure%29 https://en.wikipedia.org/wiki/Rope_%28data_structure%29
Piece Table: https://en.wikipedia.org/wiki/Piece_table https://en.wikipedia.org/wiki/Piece_table
Boyer-Moore: https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-search_algorithm https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-sea...