2 * Copyright (C) 1984-2023 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.
22 public int cvt_length(int len, int ops)
26 * Just copying a string in UTF-8 mode can cause it to grow
28 * Four output bytes for one input byte is the worst case.
35 * Allocate a chpos array for use by cvt_text.
37 public int * cvt_alloc_chpos(int len)
40 int *chpos = (int *) ecalloc(sizeof(int), len);
41 /* Initialize all entries to an invalid position. */
42 for (i = 0; i < len; i++)
48 * Convert text. Perform the transformations specified by ops.
49 * Returns converted text in odst. The original offset of each
50 * odst character (when it was in osrc) is returned in the chpos array.
52 public void cvt_text(char *odst, char *osrc, int *chpos, int *lenp, int ops)
61 src_end = osrc + *lenp;
63 src_end = osrc + strlen(osrc);
65 for (src = osrc, dst = odst; src < src_end; )
67 int src_pos = (int) (src - osrc);
68 int dst_pos = (int) (dst - odst);
69 struct ansi_state *pansi;
70 ch = step_char(&src, +1, src_end);
71 if ((ops & CVT_BS) && ch == '\b' && dst > odst)
73 /* Delete backspace and preceding char. */
76 } while (dst > odst && utf_mode &&
77 !IS_ASCII_OCTET(*dst) && !IS_UTF8_LEAD(*dst));
78 } else if ((ops & CVT_ANSI) && (pansi = ansi_start(ch)) != NULL)
80 /* Skip to end of ANSI escape sequence. */
83 if (ansi_step(pansi, ch) != ANSI_MID)
90 /* Just copy the char to the destination buffer. */
91 if ((ops & CVT_TO_LC) && IS_UPPER(ch))
94 /* Record the original position of the char. */
96 chpos[dst_pos] = src_pos;
101 if ((ops & CVT_CRLF) && edst > odst && edst[-1] == '\r')
105 *lenp = (int) (edst - odst);
106 /* FIXME: why was this here? if (chpos != NULL) chpos[dst - odst] = src - osrc; */