1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-07-04 02:35:29 +00:00

Populate search function with utf8 indicating whether we really use UTF-8

This commit is contained in:
Witold Filipczyk 2006-07-18 17:39:02 +02:00 committed by Witold Filipczyk
parent 02e098dc5a
commit 681cfc4ae5

View File

@ -454,14 +454,23 @@ is_in_range_regex(struct document *document, int y, int height,
#endif /* HAVE_REGEX_H */ #endif /* HAVE_REGEX_H */
static UCHAR * static UCHAR *
memacpy_u(unsigned char *text, int textlen) memacpy_u(unsigned char *text, int textlen, int utf8)
{ {
#ifdef CONFIG_UTF_8 #ifdef CONFIG_UTF_8
UCHAR *mem = mem_alloc((textlen + 1) * sizeof(UCHAR)); UCHAR *mem = mem_alloc((textlen + 1) * sizeof(UCHAR));
int i;
if (!mem) return NULL; if (!mem) return NULL;
for (i = 0; i < textlen; i++) mem[i] = utf_8_to_unicode(&text, text + 7); if (utf8) {
int i;
for (i = 0; i < textlen; i++)
mem[i] = utf_8_to_unicode(&text, text + 7);
} else {
int i;
for (i = 0; i < textlen; i++)
mem[i] = text[i];
}
mem[textlen] = 0; mem[textlen] = 0;
return mem; return mem;
#else #else
@ -470,28 +479,29 @@ memacpy_u(unsigned char *text, int textlen)
} }
static int static int
strlen_u(unsigned char *text) strlen_u(unsigned char *text, int utf8)
{ {
#ifdef CONFIG_UTF_8 #ifdef CONFIG_UTF_8
return strlen_utf8(&text); if (utf8)
#else return strlen_utf8(&text);
return strlen(text);
#endif #endif
return strlen(text);
} }
/* Returns an allocated string which is a lowered copy of passed one. */ /* Returns an allocated string which is a lowered copy of passed one. */
static UCHAR * static UCHAR *
lowered_string(unsigned char *text, int textlen) lowered_string(unsigned char *text, int textlen, int utf8)
{ {
UCHAR *ret; UCHAR *ret;
if (textlen < 0) textlen = strlen_u(text); if (textlen < 0) textlen = strlen_u(text, utf8);
ret = memacpy_u(text, textlen); ret = memacpy_u(text, textlen, utf8);
if (ret && textlen) { if (ret && textlen) {
do { do {
#if defined(CONFIG_UTF_8) && defined(HAVE_WCTYPE_H) #if defined(CONFIG_UTF_8) && defined(HAVE_WCTYPE_H)
ret[textlen] = towlower(ret[textlen]); ret[textlen] = utf8 ? towlower(ret[textlen]) : tolower(ret[textlen]);
#else #else
ret[textlen] = tolower(ret[textlen]); ret[textlen] = tolower(ret[textlen]);
#endif #endif
@ -505,14 +515,14 @@ static int
is_in_range_plain(struct document *document, int y, int height, is_in_range_plain(struct document *document, int y, int height,
unsigned char *text, int textlen, unsigned char *text, int textlen,
int *min, int *max, int *min, int *max,
struct search *s1, struct search *s2) struct search *s1, struct search *s2, int utf8)
{ {
int yy = y + height; int yy = y + height;
UCHAR *txt; UCHAR *txt;
int found = 0; int found = 0;
int case_sensitive = get_opt_bool("document.browse.search.case"); int case_sensitive = get_opt_bool("document.browse.search.case");
txt = case_sensitive ? memacpy_u(text, textlen) : lowered_string(text, textlen); txt = case_sensitive ? memacpy_u(text, textlen, utf8) : lowered_string(text, textlen, utf8);
if (!txt) return -1; if (!txt) return -1;
/* TODO: This is a great candidate for nice optimizations. Fresh CS /* TODO: This is a great candidate for nice optimizations. Fresh CS
@ -521,7 +531,7 @@ is_in_range_plain(struct document *document, int y, int height,
* maybe some other Boyer-Moore variant, I don't feel that strong in * maybe some other Boyer-Moore variant, I don't feel that strong in
* this area), hmm? >:) --pasky */ * this area), hmm? >:) --pasky */
#if defined(CONFIG_UTF_8) && defined(HAVE_WCTYPE_H) #if defined(CONFIG_UTF_8) && defined(HAVE_WCTYPE_H)
#define maybe_tolower(c) (case_sensitive ? (c) : towlower(c)) #define maybe_tolower(c) (case_sensitive ? (c) : utf8 ? towlower(c) : tolower(c))
#else #else
#define maybe_tolower(c) (case_sensitive ? (c) : tolower(c)) #define maybe_tolower(c) (case_sensitive ? (c) : tolower(c))
#endif #endif
@ -564,12 +574,16 @@ is_in_range(struct document *document, int y, int height,
{ {
struct search *s1, *s2; struct search *s1, *s2;
int textlen; int textlen;
int utf8 = 0;
assert(document && text && min && max); assert(document && text && min && max);
if_assert_failed return -1; if_assert_failed return -1;
#ifdef CONFIG_UTF_8
utf8 = is_cp_special(document->options.cp);
#endif
*min = INT_MAX, *max = 0; *min = INT_MAX, *max = 0;
textlen = strlen_u(text); textlen = strlen_u(text, utf8);
if (get_range(document, y, height, textlen, &s1, &s2)) if (get_range(document, y, height, textlen, &s1, &s2))
return 0; return 0;
@ -580,7 +594,7 @@ is_in_range(struct document *document, int y, int height,
min, max, s1, s2); min, max, s1, s2);
#endif #endif
return is_in_range_plain(document, y, height, text, textlen, return is_in_range_plain(document, y, height, text, textlen,
min, max, s1, s2); min, max, s1, s2, utf8);
} }
#define realloc_points(pts, size) \ #define realloc_points(pts, size) \
@ -588,7 +602,7 @@ is_in_range(struct document *document, int y, int height,
static void static void
get_searched_plain(struct document_view *doc_view, struct point **pt, int *pl, get_searched_plain(struct document_view *doc_view, struct point **pt, int *pl,
int l, struct search *s1, struct search *s2) int l, struct search *s1, struct search *s2, int utf8)
{ {
UCHAR *txt; UCHAR *txt;
struct point *points = NULL; struct point *points = NULL;
@ -597,8 +611,8 @@ get_searched_plain(struct document_view *doc_view, struct point **pt, int *pl,
int len = 0; int len = 0;
int case_sensitive = get_opt_bool("document.browse.search.case"); int case_sensitive = get_opt_bool("document.browse.search.case");
txt = case_sensitive ? memacpy_u(*doc_view->search_word, l) txt = case_sensitive ? memacpy_u(*doc_view->search_word, l, utf8)
: lowered_string(*doc_view->search_word, l); : lowered_string(*doc_view->search_word, l, utf8);
if (!txt) return; if (!txt) return;
box = &doc_view->box; box = &doc_view->box;
@ -606,7 +620,7 @@ get_searched_plain(struct document_view *doc_view, struct point **pt, int *pl,
yoffset = box->y - doc_view->vs->y; yoffset = box->y - doc_view->vs->y;
#if defined(CONFIG_UTF_8) && defined(HAVE_WCTYPE_H) #if defined(CONFIG_UTF_8) && defined(HAVE_WCTYPE_H)
#define maybe_tolower(c) (case_sensitive ? (c) : towlower(c)) #define maybe_tolower(c) (case_sensitive ? (c) : utf8 ? towlower(c) : tolower(c))
#else #else
#define maybe_tolower(c) (case_sensitive ? (c) : tolower(c)) #define maybe_tolower(c) (case_sensitive ? (c) : tolower(c))
#endif #endif
@ -720,7 +734,7 @@ get_searched_regex(struct document_view *doc_view, struct point **pt, int *pl,
#endif /* HAVE_REGEX_H */ #endif /* HAVE_REGEX_H */
static void static void
get_searched(struct document_view *doc_view, struct point **pt, int *pl) get_searched(struct document_view *doc_view, struct point **pt, int *pl, int utf8)
{ {
struct search *s1, *s2; struct search *s1, *s2;
int l; int l;
@ -732,7 +746,7 @@ get_searched(struct document_view *doc_view, struct point **pt, int *pl)
return; return;
get_search_data(doc_view->document); get_search_data(doc_view->document);
l = strlen_u(*doc_view->search_word); l = strlen_u(*doc_view->search_word, utf8);
if (get_range(doc_view->document, doc_view->vs->y, if (get_range(doc_view->document, doc_view->vs->y,
doc_view->box.height, l, &s1, &s2)) { doc_view->box.height, l, &s1, &s2)) {
*pt = NULL; *pt = NULL;
@ -746,7 +760,7 @@ get_searched(struct document_view *doc_view, struct point **pt, int *pl)
get_searched_regex(doc_view, pt, pl, l, s1, s2); get_searched_regex(doc_view, pt, pl, l, s1, s2);
else else
#endif #endif
get_searched_plain(doc_view, pt, pl, l, s1, s2); get_searched_plain(doc_view, pt, pl, l, s1, s2, utf8);
} }
/* Highlighting of searched strings. */ /* Highlighting of searched strings. */
@ -755,6 +769,7 @@ draw_searched(struct terminal *term, struct document_view *doc_view)
{ {
struct point *pt = NULL; struct point *pt = NULL;
int len = 0; int len = 0;
int utf8 = 0;
assert(term && doc_view); assert(term && doc_view);
if_assert_failed return; if_assert_failed return;
@ -762,7 +777,10 @@ draw_searched(struct terminal *term, struct document_view *doc_view)
if (!has_search_word(doc_view)) if (!has_search_word(doc_view))
return; return;
get_searched(doc_view, &pt, &len); #ifdef CONFIG_UTF_8
utf8 = is_cp_special(doc_view->document->options.cp);
#endif
get_searched(doc_view, &pt, &len, utf8);
if (len) { if (len) {
int i; int i;
struct color_pair *color = get_bfu_color(term, "searched"); struct color_pair *color = get_bfu_color(term, "searched");
@ -925,10 +943,14 @@ find_next_link_in_search(struct document_view *doc_view, int direction)
struct point *pt = NULL; struct point *pt = NULL;
struct link *link; struct link *link;
int len; int len;
int utf8 = 0;
#ifdef CONFIG_UTF_8
utf8 = is_cp_special(doc_view->document->options.cp);
#endif
nt: nt:
link = &doc_view->document->links[doc_view->vs->current_link]; link = &doc_view->document->links[doc_view->vs->current_link];
get_searched(doc_view, &pt, &len); get_searched(doc_view, &pt, &len, utf8);
if (point_intersect(pt, len, link->points, link->npoints)) { if (point_intersect(pt, len, link->points, link->npoints)) {
mem_free(pt); mem_free(pt);
return 0; return 0;