7 Run::Run(Style style_in
)
8 : style(style_in
), is_tab(false), characters(nullptr)
13 Run::Run(char* initial_characters
, Style style_in
)
14 : style(style_in
), is_tab(false), characters(initial_characters
)
21 free((void*) characters
);
25 int Run::num_characters()
27 if (characters
== nullptr)
29 return UTF8::num_characters(characters
, strlen(characters
));
33 void Run::append_characters(const char* new_chars
, int num_bytes
)
37 old_length
= strlen(characters
);
38 characters
= (char*) realloc(characters
, old_length
+ num_bytes
+ 1);
41 characters
= (char*) malloc(num_bytes
+ 1);
42 memcpy(characters
+ old_length
, new_chars
, num_bytes
);
43 characters
[old_length
+ num_bytes
] = 0;
47 void Run::replace_characters(int column
, const char* new_chars
, int num_bytes
)
51 int replaced_bytes
= 0;
53 old_length
= strlen(characters
);
54 start_index
= UTF8::bytes_for_n_characters(characters
, old_length
, column
);
55 int num_chars
= UTF8::num_characters(new_chars
, num_bytes
);
57 UTF8::bytes_for_n_characters(
58 characters
+ start_index
, old_length
- start_index
, num_chars
);
60 char* new_bytes
= (char*) malloc(old_length
+ num_bytes
- replaced_bytes
+ 1);
62 // Build the new string.
63 // Initial old characters.
65 memcpy(new_bytes
, characters
, start_index
);
67 memcpy(new_bytes
+ start_index
, new_chars
, num_bytes
);
68 // Any bytes left after the replaced characters.
69 int bytes_left
= old_length
- (start_index
+ replaced_bytes
);
72 new_bytes
+ start_index
+ num_bytes
,
73 characters
+ start_index
+ replaced_bytes
,
75 new_bytes
[start_index
+ num_bytes
+ bytes_left
] = 0;
78 new_bytes
[start_index
+ num_bytes
] = 0;
81 characters
= new_bytes
;
85 void Run::insert_characters(int column
, const char* new_chars
, int num_bytes
)
87 if (characters
== nullptr) {
88 append_characters(new_chars
, num_bytes
);
92 // Build the new string.
93 int old_length
= strlen(characters
);
94 int start_index
= UTF8::bytes_for_n_characters(characters
, old_length
, column
);
95 char* new_bytes
= (char*) malloc(old_length
+ num_bytes
+ 1);
96 // Initial old characters.
98 memcpy(new_bytes
, characters
, start_index
);
100 memcpy(new_bytes
+ start_index
, new_chars
, num_bytes
);
101 // Any bytes left after the replaced characters.
102 int bytes_left
= old_length
- start_index
;
103 if (bytes_left
> 0) {
105 new_bytes
+ start_index
+ num_bytes
,
106 characters
+ start_index
,
108 new_bytes
[start_index
+ num_bytes
+ bytes_left
] = 0;
111 new_bytes
[start_index
+ num_bytes
] = 0;
114 characters
= new_bytes
;
118 void Run::append_spaces(int num_spaces
)
122 old_length
= strlen(characters
);
123 characters
= (char*) realloc(characters
, old_length
+ num_spaces
+ 1);
126 characters
= (char*) malloc(num_spaces
+ 1);
127 memset(characters
+ old_length
, ' ', num_spaces
);
128 characters
[old_length
+ num_spaces
] = 0;
132 void Run::shorten_to(int new_length
)
134 // Don't bother to reallocate.
136 UTF8::bytes_for_n_characters(characters
, strlen(characters
), new_length
);
137 characters
[end_index
] = 0;
141 void Run::delete_first_characters(int num_chars
)
143 int old_num_bytes
= strlen(characters
);
145 UTF8::bytes_for_n_characters(characters
, old_num_bytes
, num_chars
);
146 memmove(characters
, characters
+ index
, old_num_bytes
- index
+ 1); // Include terminating zero byte.
147 characters
= (char*) realloc(characters
, old_num_bytes
- index
+ 1);
151 void Run::delete_characters(int column
, int num_chars
)
153 // This is not called unless it has been determined that the deletion is
154 // entirely within the run.
155 int old_num_bytes
= strlen(characters
);
157 UTF8::bytes_for_n_characters(characters
, old_num_bytes
, column
);
160 UTF8::bytes_for_n_characters(
161 characters
+ start_index
, old_num_bytes
- start_index
, num_chars
);
163 characters
+ start_index
,
164 characters
+ end_index
,
165 old_num_bytes
- end_index
);
166 characters
[old_num_bytes
- (end_index
- start_index
)] = 0;