summaryrefslogtreecommitdiff
path: root/txt.h
blob: 6bc5dd1875eae4bf8f2c5534d78c296446b5a919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef TXT_H
#define TXT_H

#include "dynarr.h"
#include "arena.h"
#include "str.h"

typedef enum : u8 {
	TXT_SRC,
	TXT_ADD
} TxtBufIdx;

typedef struct TxtPiece {
	TxtBufIdx buf;
	u32 ofs, n;
} TxtPiece;

typedef struct {
	char *s;
	u32 n, c;
} TxtBuf;

typedef struct {
	DYNARR(TxtPiece) ptbl;
	TxtBuf buf[2];
	u32 len;
} Txt;

typedef struct {
	Txt *t;
	u32 p, i;
} TxtLoc;

/* text buffer manipulation */

TxtLoc txt_split_piece(TxtLoc l);
void txt_remove_piece(Txt *b, u32 pi);
void txt_insert_piece(Txt *b, u32 pi, TxtBufIdx buf, u32 ofs, u32 n);

int txt_load(Txt *b, const char *path);
int txt_save(Txt *b, const char *path);
void txt_free(Txt *b);

Str txt_collect_range(TxtLoc lo, TxtLoc hi, Arena *a);
u32 txt_read_chunk(TxtLoc *lo, TxtLoc hi, char *buf, u32 sz);
Str txt_next_chunk(TxtLoc *l);
int txt_range_equal(TxtLoc lo, TxtLoc hi, Str s);

/* insertion & deletion */

TxtLoc txt_insert(TxtLoc l, const char *s, u32 n);
TxtLoc txt_delete(TxtLoc l, u32 n);
TxtLoc txt_delete_range(TxtLoc lo, TxtLoc hi);
TxtLoc txt_insert_c(TxtLoc l, u32 ch);
TxtLoc txt_delete_c(TxtLoc l);
u32 txt_range_len(TxtLoc lo, TxtLoc hi);

/* navigation */

int txt_valid_loc(TxtLoc l);
TxtLoc txt_at(Txt *b, u32 ofs);
u32 txt_ofs(TxtLoc l);
int txt_before(TxtLoc a, TxtLoc b);
int txt_after(TxtLoc a, TxtLoc b);
TxtLoc txt_start(Txt *t);
TxtLoc txt_end(Txt *t);

TxtLoc bnext(TxtLoc l);
TxtLoc bprev(TxtLoc l);
TxtLoc cnext(TxtLoc l);
TxtLoc cprev(TxtLoc l);
int at_start(TxtLoc l);
int at_end(TxtLoc l);

TxtLoc next_newline(TxtLoc l);
TxtLoc prev_newline(TxtLoc l);
TxtLoc start_of_line(TxtLoc l);
TxtLoc end_of_line(TxtLoc l);
TxtLoc prev_line(TxtLoc l);
TxtLoc next_line(TxtLoc l);
TxtLoc at_col(TxtLoc l, u32 col);
u32 get_col(TxtLoc l);

/* reading */

u32 txt_chr(TxtLoc l);
u8 txt_byte(TxtLoc l);
u32 txt_chr_next(TxtLoc *l);

#endif