2 * Copyright (C) 1984-2015 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
11 * Routines to convert text in various ways. Used by search.
20 * Get the length of a buffer needed to convert a string.
23 cvt_length(int len, int ops)
27 * Just copying a string in UTF-8 mode can cause it to grow
29 * Four output bytes for one input byte is the worst case.
36 * Allocate a chpos array for use by cvt_text.
39 cvt_alloc_chpos(int len)
42 int *chpos = (int *) ecalloc(sizeof(int), len);
43 /* Initialize all entries to an invalid position. */
44 for (i = 0; i < len; i++)
50 * Convert text. Perform the transformations specified by ops.
51 * Returns converted text in odst. The original offset of each
52 * odst character (when it was in osrc) is returned in the chpos array.
55 cvt_text(char *odst, char *osrc, int *chpos, int *lenp, int ops)
64 src_end = osrc + *lenp;
66 src_end = osrc + strlen(osrc);
68 for (src = osrc, dst = odst; src < src_end; )
70 int src_pos = (int) (src - osrc);
71 int dst_pos = (int) (dst - odst);
72 ch = step_char((constant char **)&src, +1, src_end);
73 if ((ops & CVT_BS) && ch == '\b' && dst > odst)
75 /* Delete backspace and preceding char. */
78 } while (dst > odst &&
79 !IS_ASCII_OCTET(*dst) && !IS_UTF8_LEAD(*dst));
80 } else if ((ops & CVT_ANSI) && IS_CSI_START(ch))
82 /* Skip to end of ANSI escape sequence. */
83 src++; /* skip the CSI start char */
85 if (!is_ansi_middle(*src++))
89 /* Just copy the char to the destination buffer. */
90 if ((ops & CVT_TO_LC) && IS_UPPER(ch))
93 /* Record the original position of the char. */
95 chpos[dst_pos] = src_pos;
100 if ((ops & CVT_CRLF) && edst > odst && edst[-1] == '\r')
104 *lenp = (int) (edst - odst);
105 /* FIXME: why was this here? if (chpos != NULL) chpos[dst - odst] = src - osrc; */