1 /* fold.c -- fold a mail header field
3 * This code is Copyright (c), by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information. */
11 fold(charstring_t dst, size_t namelen, const char *restrict body)
13 const char *restrict body_next;
14 const char *restrict wsp;
15 const char *restrict wsp_next;
16 const int crlf = strchr(body, '\r') != NULL;
17 charstring_clear(dst);
21 body_next = strchr(body, '\n');
22 if ((unsigned long) (body_next - body) <= MAXTEXTPERLN - namelen) {
23 charstring_push_back_chars(dst, body, body_next - body + 1);
29 while (namelen == 0 && (*wsp == ' ' || *wsp == '\t')) {
32 wsp = wsp_next = strpbrk(wsp, " \t");
34 /* if now whitespace is in the current line just print the curret line as is */
35 if (!wsp_next || wsp_next > body_next) {
36 charstring_push_back_chars(dst, body, body_next - body + 1);
42 while ((unsigned long)(wsp_next - body) <= MAXTEXTPERLN - namelen) {
44 wsp_next = strpbrk(wsp+1, " \t");
48 if (wsp_next > body_next) {
53 charstring_push_back_chars(dst, body, wsp - body);
55 charstring_push_back(dst, '\r');
57 charstring_push_back(dst, '\n');