10 #include "fold/fold.h"
12 void fold_text(const char *text,
14 int line_cb(void *arg, const char *start, int len),
17 const char *start, *end, *sep;
18 size_t sep_bytes, len;
22 /* start, end and sep are byte-positions in the string, and should always
23 * lie on the start of a multibyte sequence */
24 start = end = sep = text;
28 memset(&ps, 0, sizeof(ps));
35 bytes = mbrtowc(&wc, end, len - (end - text), &ps);
37 assert(bytes != (size_t)-1);
39 /* we'll get a zero size for the nul terminator, (size_t) -2
40 * if we've reached the end of the buffer, or (size_t) -1 on
42 if (!bytes || bytes == (size_t) -2 || bytes == (size_t) -1) {
43 line_cb(arg, start, end - start);
48 rc = line_cb(arg, start, end - start);
49 start = sep = end += bytes;
57 /* we should have caught this in the !bytes check... */
59 line_cb(arg, start, end - start);
63 /* unprintable character? just add it to the current line */
73 /* split on a previous word boundary, if
75 rc = line_cb(arg, start, sep - start);
76 end = sep + sep_bytes;
78 /* otherwise, break the word */
79 rc = line_cb(arg, start, end - start);
86 /* record our last separator */