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, or (size_t) -2
40 * if we've reached the end of the buffer */
41 if (!bytes || bytes == (size_t) -2) {
42 line_cb(arg, start, end - start);
47 rc = line_cb(arg, start, end - start);
48 start = sep = end += bytes;
56 /* we should have caught this in the !bytes check... */
58 line_cb(arg, start, end - start);
62 /* unprintable character? just add it to the current line */
72 /* split on a previous word boundary, if
74 rc = line_cb(arg, start, sep - start);
75 end = sep + sep_bytes;
77 /* otherwise, break the word */
78 rc = line_cb(arg, start, end - start);
85 /* record our last separator */