2 * Copyright (C) 1984-2014 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.
29 * Just copying a string in UTF-8 mode can cause it to grow
31 * Four output bytes for one input byte is the worst case.
38 * Allocate a chpos array for use by cvt_text.
45 int *chpos
= (int *) ecalloc(sizeof(int), len
);
46 /* Initialize all entries to an invalid position. */
47 for (i
= 0; i
< len
; i
++)
53 * Convert text. Perform the transformations specified by ops.
54 * Returns converted text in odst. The original offset of each
55 * odst character (when it was in osrc) is returned in the chpos array.
58 cvt_text(odst
, osrc
, chpos
, lenp
, ops
)
68 register char *src_end
;
72 src_end
= osrc
+ *lenp
;
74 src_end
= osrc
+ strlen(osrc
);
76 for (src
= osrc
, dst
= odst
; src
< src_end
; )
78 int src_pos
= (int) (src
- osrc
);
79 int dst_pos
= (int) (dst
- odst
);
80 ch
= step_char(&src
, +1, src_end
);
81 if ((ops
& CVT_BS
) && ch
== '\b' && dst
> odst
)
83 /* Delete backspace and preceding char. */
86 } while (dst
> odst
&&
87 !IS_ASCII_OCTET(*dst
) && !IS_UTF8_LEAD(*dst
));
88 } else if ((ops
& CVT_ANSI
) && IS_CSI_START(ch
))
90 /* Skip to end of ANSI escape sequence. */
91 src
++; /* skip the CSI start char */
93 if (!is_ansi_middle(*src
++))
97 /* Just copy the char to the destination buffer. */
98 if ((ops
& CVT_TO_LC
) && IS_UPPER(ch
))
101 /* Record the original position of the char. */
103 chpos
[dst_pos
] = src_pos
;
108 if ((ops
& CVT_CRLF
) && edst
> odst
&& edst
[-1] == '\r')
112 *lenp
= (int) (edst
- odst
);
113 /* FIXME: why was this here? if (chpos != NULL) chpos[dst - odst] = src - osrc; */