freebsd-skq/contrib/less/cvt.c

115 lines
2.5 KiB
C
Raw Normal View History

2012-07-23 21:31:53 +00:00
/*
* Copyright (C) 1984-2019 Mark Nudelman
2012-07-23 21:31:53 +00:00
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
2009-07-27 07:05:08 +00:00
/*
* Routines to convert text in various ways. Used by search.
*/
#include "less.h"
#include "charset.h"
extern int utf_mode;
/*
* Get the length of a buffer needed to convert a string.
*/
public int
2017-04-25 03:42:16 +00:00
cvt_length(len, ops)
int len;
int ops;
2009-07-27 07:05:08 +00:00
{
if (utf_mode)
/*
* Just copying a string in UTF-8 mode can cause it to grow
* in length.
* Four output bytes for one input byte is the worst case.
*/
len *= 4;
return (len + 1);
}
/*
* Allocate a chpos array for use by cvt_text.
*/
public int *
2017-04-25 03:42:16 +00:00
cvt_alloc_chpos(len)
int len;
2009-07-27 07:05:08 +00:00
{
int i;
int *chpos = (int *) ecalloc(sizeof(int), len);
/* Initialize all entries to an invalid position. */
for (i = 0; i < len; i++)
chpos[i] = -1;
return (chpos);
}
/*
* Convert text. Perform the transformations specified by ops.
* Returns converted text in odst. The original offset of each
* odst character (when it was in osrc) is returned in the chpos array.
*/
public void
2017-04-25 03:42:16 +00:00
cvt_text(odst, osrc, chpos, lenp, ops)
char *odst;
char *osrc;
int *chpos;
int *lenp;
int ops;
2009-07-27 07:05:08 +00:00
{
char *dst;
2012-06-26 20:38:53 +00:00
char *edst = odst;
2009-07-27 07:05:08 +00:00
char *src;
2017-04-29 06:30:21 +00:00
char *src_end;
2009-07-27 07:05:08 +00:00
LWCHAR ch;
if (lenp != NULL)
src_end = osrc + *lenp;
else
src_end = osrc + strlen(osrc);
for (src = osrc, dst = odst; src < src_end; )
{
2016-01-04 00:22:34 +00:00
int src_pos = (int) (src - osrc);
int dst_pos = (int) (dst - odst);
2017-04-25 03:42:16 +00:00
ch = step_char(&src, +1, src_end);
2009-07-27 07:05:08 +00:00
if ((ops & CVT_BS) && ch == '\b' && dst > odst)
{
/* Delete backspace and preceding char. */
do {
dst--;
2017-11-20 06:53:49 +00:00
} while (dst > odst && utf_mode &&
2009-07-27 07:05:08 +00:00
!IS_ASCII_OCTET(*dst) && !IS_UTF8_LEAD(*dst));
} else if ((ops & CVT_ANSI) && IS_CSI_START(ch))
{
/* Skip to end of ANSI escape sequence. */
src++; /* skip the CSI start char */
while (src < src_end)
if (!is_ansi_middle(*src++))
break;
} else
{
/* Just copy the char to the destination buffer. */
if ((ops & CVT_TO_LC) && IS_UPPER(ch))
ch = TO_LOWER(ch);
put_wchar(&dst, ch);
2012-06-26 20:38:53 +00:00
/* Record the original position of the char. */
if (chpos != NULL)
2009-07-27 07:05:08 +00:00
chpos[dst_pos] = src_pos;
}
2012-06-26 20:38:53 +00:00
if (dst > edst)
edst = dst;
2009-07-27 07:05:08 +00:00
}
2012-06-26 20:38:53 +00:00
if ((ops & CVT_CRLF) && edst > odst && edst[-1] == '\r')
edst--;
*edst = '\0';
2009-07-27 07:05:08 +00:00
if (lenp != NULL)
2016-01-04 00:22:34 +00:00
*lenp = (int) (edst - odst);
2012-06-26 20:38:53 +00:00
/* FIXME: why was this here? if (chpos != NULL) chpos[dst - odst] = src - osrc; */
2009-07-27 07:05:08 +00:00
}