What is the best way to implement a buffer for managing text editing?

So, I am creating a custom control. To track the contents of an edit control, I use a dynamically allocated char array.

  • Now I know that I need to insert the middle of the array in some situations, for example, when the user clicks on a specific point. So, I thought, instead of an array of characters, I can use std :: vector, so I could use the .insert function, and also did not have to worry much about memory management.
  • I also thought that it is possible to directly store the input stream into an array / vector of words (without saving a continuous buffer), since my whole goal is to implement syntax highlighting.

What would be the best approach to the idea? And why?

+5
source share
5 answers

For a text buffer with today's computers, you can really just use one continuous buffer (like a vector) because the processors are fast enough to make the insert time (an operation o(n)with this naive approach) still a viable option.

In the past, when computers were a thousand times slower, a simple simple approach was to save the text in the buffer, but with a β€œhole” corresponding to the cursor position, to insert an operation o(1)and move characters from one (basically, moving the cursor a o(k), where kis the number of missing characters).

, , , , , . -, ... , ( ) .

+4

- Richedit . - , - , IME , , , , .

, . , , . . , .

+4

- (ref: " " ) , std::list<std::string> , , .

(, std::string std::vector), , . , , . , ( ). , , .

std::list<std::string> , (, ).

: http://www.cplusplus.com/reference/stl/list/

/, :

namespace MON {
class t_line {
public:
  /* ... */
private:
  std::string d_string;
  t_lexer_stuff d_lexerStuff;
};
}
+3

, , char, ( , ).

, , - , . ( Windows, ) , . Qt, ++, Windows, Linux, MacOSX,...

, , Windows ( Linux).

+2

All Articles