/
textbuffer.c
120 lines (98 loc) · 2.84 KB
/
textbuffer.c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright 2014 Dario Hamidi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "textbuffer.h"
static void bufbackward(BUFFER self, int n) {
const char* before = Buffer.before(self);
size_t before_len = Buffer.point(self) - 1;
size_t pos = before_len - 1;
while (n --> 0) {
if ( (unsigned char)before[pos] <= 0x70) {
// ASCII
Buffer.backward(self, 1);
} else {
// UTF-8
while ( (unsigned char)before[pos] >> 6 == 2) {
pos--;
}
Buffer.backward(self, before_len - pos);
}
}
}
static void bufforward(BUFFER self, int n) {
const char* after = Buffer.after(self);
while (n --> 0) {
Buffer.forward(self, 1);
while ( (unsigned char)*(++after) >> 6 == 2)
Buffer.forward(self, 1);
}
}
static void bufdelete(BUFFER self) {
const char* after = Buffer.after(self);
Buffer.delete(self);
while ( (unsigned char)*(++after) >> 6 == 2 )
Buffer.delete(self);
}
static void bufcput(BUFFER self, int c) {
Buffer.cput(self, c);
}
static void bufsput(BUFFER self, const char* str) {
Buffer.sput(self, str);
}
static BUFFER bufnew(size_t hint) {
return Buffer.new(hint);
}
static void bufdestroy(BUFFER* self) {
Buffer.destroy(self);
}
static char* bufstring(BUFFER self, char** out, size_t* len) {
return Buffer.string(self, out, len);
}
static void bufclear(BUFFER self) {
Buffer.clear(self);
}
static void bufdeletetoend(BUFFER self) {
Buffer.delete_to_end(self);
}
static void bufdeletetobeginning(BUFFER self) {
Buffer.delete_to_beginning(self);
}
static size_t bufpoint(BUFFER self) {
return Buffer.point(self);
}
static const char* bufafter(BUFFER self) {
return Buffer.after(self);
}
static const char* bufbefore(BUFFER self) {
return Buffer.before(self);
}
static size_t buflen(BUFFER self) {
return Buffer.length(self);
}
struct buffer_interface TextBuffer = {
.new = bufnew,
.destroy = bufdestroy,
.length = buflen,
.cput = bufcput,
.sput = bufsput,
.string = bufstring,
.forward = bufforward,
.backward = bufbackward,
.delete = bufdelete,
.clear = bufclear,
.delete_to_end = bufdeletetoend,
.delete_to_beginning = bufdeletetobeginning,
.point = bufpoint,
.after = bufafter,
.before = bufbefore,
};