summaryrefslogtreecommitdiff
path: root/txt.h
blob: 3af997017039c39fe364277b23fc86afdb5726d4 (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
#ifndef TXT_H
#define TXT_H

#include "dynarr.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;

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

u32 txt_insert(Txt *b, u32 cur, const char *s, u32 n);
u32 txt_delete(Txt *b, u32 cur, u32 n);
u32 txt_insert_c(Txt *b, u32 cur, u32 ch);
u32 txt_delete_c(Txt *b, u32 cur);
int txt_load(Txt *b, const char *path);
int txt_save(Txt *b, const char *path);
void txt_free(Txt *b);

TxtLoc txt_at(Txt *b, u32 cur);
u32 txt_ofs(Txt *b, TxtLoc l);
TxtLoc txt_next(Txt *b, TxtLoc l);
TxtLoc txt_prev(Txt *b, TxtLoc l);
int txt_at_start(Txt *b, TxtLoc l);
int txt_at_end(Txt *b, TxtLoc l);
int txt_before(TxtLoc a, TxtLoc b);
int txt_after(TxtLoc a, TxtLoc b);

u32 txt_chr(Txt *b, TxtLoc l);
u8 txt_byte(Txt *b, TxtLoc l);
u32 txt_chr_next(Txt *b, TxtLoc *l);

#endif