1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-11-27 08:00:11 +00:00
freebsd/contrib/less/cvt.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

108 lines
2.5 KiB
C
Raw Permalink Normal View History

2012-07-23 21:31:53 +00:00
/*
2023-05-01 08:22:19 +00:00
* Copyright (C) 1984-2023 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.
*/
2023-05-01 08:22:19 +00:00
public int cvt_length(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.
*/
2023-05-01 08:22:19 +00:00
public int * cvt_alloc_chpos(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.
*/
2023-05-01 08:22:19 +00:00
public void cvt_text(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);
2021-04-19 01:15:19 +00:00
struct ansi_state *pansi;
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));
2021-04-19 01:15:19 +00:00
} else if ((ops & CVT_ANSI) && (pansi = ansi_start(ch)) != NULL)
2009-07-27 07:05:08 +00:00
{
/* Skip to end of ANSI escape sequence. */
while (src < src_end)
2021-04-19 01:15:19 +00:00
{
if (ansi_step(pansi, ch) != ANSI_MID)
2009-07-27 07:05:08 +00:00
break;
2021-04-19 01:15:19 +00:00
ch = *src++;
}
ansi_done(pansi);
2009-07-27 07:05:08 +00:00
} 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
}