summaryrefslogtreecommitdiff
path: root/src/dged/text.c
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2025-11-05 23:17:10 +0100
committerAlbert Cervin <albert@acervin.com>2025-11-05 23:17:10 +0100
commitd131f7964b04ad71b2bda397beee2aba63a43332 (patch)
tree22003b212e33333d5ef05557eb159166a97c0e3c /src/dged/text.c
parent7f0bd82cfaff98072bd49db6e6308579d013f523 (diff)
downloaddged-d131f7964b04ad71b2bda397beee2aba63a43332.tar.gz
dged-d131f7964b04ad71b2bda397beee2aba63a43332.tar.xz
dged-d131f7964b04ad71b2bda397beee2aba63a43332.zip
Support CRLF line endings
It now detects and saves properly.
Diffstat (limited to 'src/dged/text.c')
-rw-r--r--src/dged/text.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/dged/text.c b/src/dged/text.c
index e609557..59290ca 100644
--- a/src/dged/text.c
+++ b/src/dged/text.c
@@ -38,6 +38,7 @@ struct text {
struct property_layer property_layers[MAX_LAYERS];
uint32_t nproperty_layers;
layer_id current_layer_id;
+ enum line_endings line_ends;
};
struct text *text_create(uint32_t initial_capacity) {
@@ -46,6 +47,7 @@ struct text *text_create(uint32_t initial_capacity) {
txt->capacity = initial_capacity;
txt->nlines = 0;
txt->current_layer_id = 1;
+ txt->line_ends = LineEnding_LF;
VEC_INIT(&txt->properties, 32);
@@ -82,6 +84,10 @@ void text_clear(struct text *text) {
text_clear_properties(text);
}
+enum line_endings text_get_line_ending(const struct text *text) {
+ return text->line_ends;
+}
+
struct utf8_codepoint_iterator
text_line_codepoint_iterator(const struct text *text, uint32_t lineidx) {
if (lineidx >= text_num_lines(text)) {
@@ -253,9 +259,16 @@ static void text_insert_at_inner(struct text *text, uint32_t line,
uint8_t byte = bytes[bytei];
if (byte == '\n') {
uint8_t *line_data = bytes + (bytei - linelen);
- insert_at(text, line, offset, line_data, linelen);
- offset += linelen;
+ uint32_t insertlen = linelen;
+ if (bytei > 0 && bytes[bytei - 1] == '\r') {
+ text->line_ends = LineEnding_CRLF;
+ --insertlen;
+ }
+
+ insert_at(text, line, offset, line_data, insertlen);
+
+ offset += insertlen;
new_line_at(text, line, offset);
++line;