1 /* $NetBSD: cvt.c,v 1.3 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
13 * Routines to convert text in various ways. Used by search.
22 * Get the length of a buffer needed to convert a string.
31 * Just copying a string in UTF-8 mode can cause it to grow
33 * Four output bytes for one input byte is the worst case.
40 * Allocate a chpos array for use by cvt_text.
47 int *chpos
= (int *) ecalloc(sizeof(int), len
);
48 /* Initialize all entries to an invalid position. */
49 for (i
= 0; i
< len
; i
++)
55 * Convert text. Perform the transformations specified by ops.
56 * Returns converted text in odst. The original offset of each
57 * odst character (when it was in osrc) is returned in the chpos array.
60 cvt_text(odst
, osrc
, chpos
, lenp
, ops
)
70 register char *src_end
;
74 src_end
= osrc
+ *lenp
;
76 src_end
= osrc
+ strlen(osrc
);
78 for (src
= osrc
, dst
= odst
; src
< src_end
; )
80 int src_pos
= src
- osrc
;
81 int dst_pos
= dst
- odst
;
82 ch
= step_char(&src
, +1, src_end
);
83 if ((ops
& CVT_BS
) && ch
== '\b' && dst
> odst
)
85 /* Delete backspace and preceding char. */
88 } while (dst
> odst
&&
89 !IS_ASCII_OCTET(*dst
) && !IS_UTF8_LEAD(*dst
));
90 } else if ((ops
& CVT_ANSI
) && IS_CSI_START(ch
))
92 /* Skip to end of ANSI escape sequence. */
93 src
++; /* skip the CSI start char */
95 if (!is_ansi_middle(*src
++))
99 /* Just copy the char to the destination buffer. */
100 if ((ops
& CVT_TO_LC
) && IS_UPPER(ch
))
103 /* Record the original position of the char. */
105 chpos
[dst_pos
] = src_pos
;
110 if ((ops
& CVT_CRLF
) && edst
> odst
&& edst
[-1] == '\r')
115 /* FIXME: why was this here? if (chpos != NULL) chpos[dst - odst] = src - osrc; */