version 1.3, 2014/04/25 13:38:21 |
version 1.4, 2015/11/05 22:08:44 |
|
|
* |
* |
* For more information, see the README file. |
* For more information, see the README file. |
*/ |
*/ |
|
/* |
|
* Modified for use with illumos. |
|
* Copyright 2014 Garrett D'Amore <garrett@damore.org> |
|
*/ |
|
|
/* |
/* |
* Routines to convert text in various ways. Used by search. |
* Routines to convert text in various ways. Used by search. |
|
|
/* |
/* |
* Get the length of a buffer needed to convert a string. |
* Get the length of a buffer needed to convert a string. |
*/ |
*/ |
public int |
int |
cvt_length(len, ops) |
cvt_length(int len) |
int len; |
|
int ops; |
|
{ |
{ |
if (utf_mode) |
if (utf_mode) |
/* |
/* |
* Just copying a string in UTF-8 mode can cause it to grow |
* Just copying a string in UTF-8 mode can cause it to grow |
* in length. |
* in length. |
* Four output bytes for one input byte is the worst case. |
* Four output bytes for one input byte is the worst case. |
*/ |
*/ |
|
|
/* |
/* |
* Allocate a chpos array for use by cvt_text. |
* Allocate a chpos array for use by cvt_text. |
*/ |
*/ |
public int * |
int * |
cvt_alloc_chpos(len) |
cvt_alloc_chpos(int len) |
int len; |
|
{ |
{ |
int i; |
int i; |
int *chpos = (int *) ecalloc(sizeof(int), len); |
int *chpos = ecalloc(sizeof (int), len); |
/* Initialize all entries to an invalid position. */ |
/* Initialize all entries to an invalid position. */ |
for (i = 0; i < len; i++) |
for (i = 0; i < len; i++) |
chpos[i] = -1; |
chpos[i] = -1; |
|
|
* Returns converted text in odst. The original offset of each |
* Returns converted text in odst. The original offset of each |
* odst character (when it was in osrc) is returned in the chpos array. |
* odst character (when it was in osrc) is returned in the chpos array. |
*/ |
*/ |
public void |
void |
cvt_text(odst, osrc, chpos, lenp, ops) |
cvt_text(char *odst, char *osrc, int *chpos, int *lenp, int ops) |
char *odst; |
|
char *osrc; |
|
int *chpos; |
|
int *lenp; |
|
int ops; |
|
{ |
{ |
char *dst; |
char *dst; |
char *edst = odst; |
char *edst = odst; |
char *src; |
char *src; |
register char *src_end; |
char *src_end; |
LWCHAR ch; |
LWCHAR ch; |
|
|
if (lenp != NULL) |
if (lenp != NULL) |
|
|
else |
else |
src_end = osrc + strlen(osrc); |
src_end = osrc + strlen(osrc); |
|
|
for (src = osrc, dst = odst; src < src_end; ) |
for (src = osrc, dst = odst; src < src_end; ) { |
{ |
|
int src_pos = src - osrc; |
int src_pos = src - osrc; |
int dst_pos = dst - odst; |
int dst_pos = dst - odst; |
ch = step_char(&src, +1, src_end); |
ch = step_char(&src, +1, src_end); |
if ((ops & CVT_BS) && ch == '\b' && dst > odst) |
if ((ops & CVT_BS) && ch == '\b' && dst > odst) { |
{ |
|
/* Delete backspace and preceding char. */ |
/* Delete backspace and preceding char. */ |
do { |
do { |
dst--; |
dst--; |
} while (dst > odst && |
} while (dst > odst && |
!IS_ASCII_OCTET(*dst) && !IS_UTF8_LEAD(*dst)); |
!IS_ASCII_OCTET(*dst) && !IS_UTF8_LEAD(*dst)); |
} else if ((ops & CVT_ANSI) && IS_CSI_START(ch)) |
} else if ((ops & CVT_ANSI) && IS_CSI_START(ch)) { |
{ |
|
/* Skip to end of ANSI escape sequence. */ |
/* Skip to end of ANSI escape sequence. */ |
src++; /* skip the CSI start char */ |
src++; /* skip the CSI start char */ |
while (src < src_end) |
while (src < src_end) |
if (!is_ansi_middle(*src++)) |
if (!is_ansi_middle(*src++)) |
break; |
break; |
} else |
} else { |
{ |
|
/* Just copy the char to the destination buffer. */ |
/* Just copy the char to the destination buffer. */ |
if ((ops & CVT_TO_LC) && IS_UPPER(ch)) |
if ((ops & CVT_TO_LC) && IS_UPPER(ch)) |
ch = TO_LOWER(ch); |
ch = TO_LOWER(ch); |
#if !SMALL |
|
put_wchar(&dst, ch); |
put_wchar(&dst, ch); |
#else |
|
*dst++ = (char)ch; |
|
#endif /* !SMALL */ |
|
/* Record the original position of the char. */ |
/* Record the original position of the char. */ |
if (chpos != NULL) |
if (chpos != NULL) |
chpos[dst_pos] = src_pos; |
chpos[dst_pos] = src_pos; |
|
|
*edst = '\0'; |
*edst = '\0'; |
if (lenp != NULL) |
if (lenp != NULL) |
*lenp = edst - odst; |
*lenp = edst - odst; |
/* FIXME: why was this here? if (chpos != NULL) chpos[dst - odst] = src - osrc; */ |
|
} |
} |