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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
#include "module-text.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { CHUNK = 4096 };
/* state: pending continuation bytes (bits 0-1) and the allowed range of the
* next one (bits 2-4): 0 any, 1 A0-BF after E0, 2 80-9F after ED, 3 90-BF
* after F0, 4 80-8F after F4. */
bool pure_utf8_step(unsigned *state, PureSpan span) {
unsigned remaining = *state & 3, range = *state >> 2;
for (size_t i = 0; i < span.size; ++i) {
/* Eight ASCII bytes at a time between characters. */
uint64_t word;
while (!remaining && span.size - i >= 8 && (memcpy(&word, span.data + i, 8), !(word & UINT64_C(0x8080808080808080))))
i += 8;
if (i == span.size) break;
unsigned char b = span.data[i];
if (remaining) {
unsigned low = range == 1 ? 0xA0 : range == 3 ? 0x90 : 0x80;
unsigned high = range == 2 ? 0x9F : range == 4 ? 0x8F : 0xBF;
if (b < low || b > high) return false;
--remaining;
range = 0;
} else if (b < 0x80) {
continue;
} else if (b >= 0xC2 && b <= 0xDF) {
remaining = 1;
} else if (b >= 0xE0 && b <= 0xEF) {
remaining = 2;
range = b == 0xE0 ? 1 : b == 0xED ? 2 : 0;
} else if (b >= 0xF0 && b <= 0xF4) {
remaining = 3;
range = b == 0xF0 ? 3 : b == 0xF4 ? 4 : 0;
} else return false;
}
*state = remaining | range << 2;
return true;
}
bool pure_utf8_complete(unsigned state) { return (state & 3) == 0; }
bool pure_utf8_valid(PureSpan span) {
unsigned state = 0;
return pure_utf8_step(&state, span) && pure_utf8_complete(state);
}
size_t pure_utf8_scalars(PureSpan span) {
size_t count = 0;
for (size_t i = 0; i < span.size; ++i) count += (span.data[i] & 0xC0) != 0x80;
return count;
}
size_t pure_utf8_newlines(PureSpan span) {
size_t count = 0;
for (size_t i = 0; i < span.size; ++i) count += span.data[i] == '\n';
return count;
}
size_t pure_format_number(double value, char out[32]) {
if (value == 0) { memcpy(out, "0", 2); return 1; }
if (value == floor(value) && fabs(value) < 1e15) {
/* An integer's digits, without printf (the common case, many times cheaper). */
char digits[16];
uint64_t n = (uint64_t)fabs(value);
size_t count = 0, length = 0;
do { digits[count++] = (char)('0' + n % 10); n /= 10; } while (n);
if (value < 0) out[length++] = '-';
while (count) out[length++] = digits[--count];
out[length] = 0;
return length;
}
/* The fewest decimals k whose digits, the value * 10^k rounded to the
* nearest integer m, read back as the value: %g's digits. m and 10^k are
* exact (m < 2^53, k <= 22), so m / 10^k is the correctly rounded value of
* those digits, as reading them is; the product's rounding error (fma)
* tells the nearest m, and a half between two goes to %g. Else, and for
* what %g would write with an exponent, the fewest %g digits that do. */
static const double tens[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
1e12, 1e13, 1e14, 1e15, 1e16};
double magnitude = fabs(value);
if (magnitude >= 1e-4 && magnitude < 1e15)
for (int k = 1; k <= 16; ++k) {
double scaled = magnitude * tens[k];
if (scaled >= 9007199254740992.0) break;
uint64_t m = (uint64_t)scaled;
double beyond = (scaled - (double)m) + fma(magnitude, tens[k], -scaled);
if (beyond == 0.5) break;
m += beyond > 0.5;
if ((double)m / tens[k] != magnitude) continue;
char digits[24];
size_t count = 0, length = 0;
do { digits[count++] = (char)('0' + m % 10); m /= 10; } while (m);
while (count <= (size_t)k) digits[count++] = '0'; /* a leading zero before the point */
if (value < 0) out[length++] = '-';
while (count > (size_t)k) out[length++] = digits[--count];
out[length++] = '.';
while (count) out[length++] = digits[--count];
out[length] = 0;
return length;
}
for (int precision = 1; precision <= 17; ++precision) {
snprintf(out, 32, "%.*g", precision, value);
if (strtod(out, NULL) == value) break;
}
char *exponent = strchr(out, 'e');
if (exponent) {
char *digits = exponent + 2; /* after the sign */
while (digits[0] == '0' && digits[1]) memmove(digits, digits + 1, strlen(digits));
}
return strlen(out);
}
typedef struct { unsigned char *out; } Copy;
static bool copy(void *context, PureSpan span) {
Copy *c = context;
memcpy(c->out, span.data, span.size);
c->out += span.size;
return true;
}
static void read_bytes(const PureDraft *draft, PureVersion version, size_t offset, size_t length,
unsigned char *out) {
Copy c = {out};
if (length) pure_draft_read_version(draft, version, offset, length, copy, &c);
}
static size_t forward(const PureDraft *draft, PureVersion version, size_t position) {
if (position >= version.bytes) return version.bytes;
unsigned char lead;
read_bytes(draft, version, position, 1, &lead);
size_t length = lead < 0x80 ? 1 : lead < 0xE0 ? 2 : lead < 0xF0 ? 3 : 4;
return position + length <= version.bytes ? position + length : version.bytes;
}
static size_t backward(const PureDraft *draft, PureVersion version, size_t position) {
if (!position) return 0;
size_t take = position < 4 ? position : 4;
unsigned char bytes[4];
read_bytes(draft, version, position - take, take, bytes);
size_t at = take - 1;
while (at && (bytes[at] & 0xC0) == 0x80) --at;
return position - take + at;
}
/* Saturating: stops at the start or end, so work is bounded by the text size. */
size_t pure_text_step(const PureDraft *draft, PureVersion version, size_t position, double count) {
for (; count > 0 && position < version.bytes; --count) position = forward(draft, version, position);
for (; count < 0 && position > 0; ++count) position = backward(draft, version, position);
return position;
}
size_t pure_text_line_start(const PureDraft *draft, PureVersion version, size_t position) {
unsigned char chunk[CHUNK];
while (position) {
size_t take = position < CHUNK ? position : CHUNK;
read_bytes(draft, version, position - take, take, chunk);
for (size_t i = take; i-- > 0;)
if (chunk[i] == '\n') return position - take + i + 1;
position -= take;
}
return 0;
}
size_t pure_text_line_end(const PureDraft *draft, PureVersion version, size_t position) {
unsigned char chunk[CHUNK];
while (position < version.bytes) {
size_t take = version.bytes - position < CHUNK ? version.bytes - position : CHUNK;
read_bytes(draft, version, position, take, chunk);
for (size_t i = 0; i < take; ++i)
if (chunk[i] == '\n') return position + i;
position += take;
}
return version.bytes;
}
static size_t advance_within(const PureDraft *draft, PureVersion version, size_t position,
size_t columns, size_t limit) {
for (; columns && position < limit; --columns) position = forward(draft, version, position);
return position < limit ? position : limit;
}
/* Moves keep the column; with no further line the cursor goes to the end or
* the start of the text. */
size_t pure_text_line_step(const PureDraft *draft, PureVersion version, size_t position, double count) {
size_t start = pure_text_line_start(draft, version, position), column = 0;
for (size_t at = start; at < position; at = forward(draft, version, at)) ++column;
for (; count > 0; --count) {
size_t end = pure_text_line_end(draft, version, position);
if (end == version.bytes) return version.bytes;
size_t next = end + 1;
position = advance_within(draft, version, next, column, pure_text_line_end(draft, version, next));
}
for (; count < 0; ++count) {
size_t here = pure_text_line_start(draft, version, position);
if (!here) return 0;
size_t previous = pure_text_line_start(draft, version, here - 1);
position = advance_within(draft, version, previous, column, here - 1);
}
return position;
}
typedef struct { size_t count; unsigned unit; } Count;
static bool count_span(void *context, PureSpan span) {
Count *c = context;
c->count += c->unit ? pure_utf8_newlines(span) : pure_utf8_scalars(span);
return true;
}
size_t pure_text_count(const PureDraft *draft, PureVersion version, size_t position, unsigned unit) {
Count c = {0, unit};
if (position) pure_draft_read_version(draft, version, 0, position, count_span, &c);
return c.count + (unit ? 1 : 0);
}
|