File: wordwrap.c

package info (click to toggle)
putty 0.83-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,216 kB
  • sloc: ansic: 148,476; python: 8,466; perl: 1,830; makefile: 128; sh: 117
file content (36 lines) | stat: -rw-r--r-- 1,009 bytes parent folder | download | duplicates (2)
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
/*
 * Function to wrap text to a fixed number of columns.
 *
 * Currently, assumes the text is in a single-byte character set,
 * because it's only used to display host key prompt messages.
 * Extending to Unicode and using wcwidth() could be an extension.
 */

#include "misc.h"

void wordwrap(BinarySink *bs, ptrlen input, size_t maxwid)
{
    size_t col = 0;
    while (true) {
        ptrlen word = ptrlen_get_word(&input, " ");
        if (!word.len)
            break;

        /* At the start of a line, any word is legal, even if it's
         * overlong, because we have to display it _somehow_ and
         * wrapping to the next line won't make it any better. */
        if (col > 0) {
            size_t newcol = col + 1 + word.len;
            if (newcol <= maxwid) {
                put_byte(bs, ' ');
                col++;
            } else {
                put_byte(bs, '\n');
                col = 0;
            }
        }

        put_datapl(bs, word);
        col += word.len;
    }
}