links

lynx-like text mode web browser
git clone anongit@rnpnr.xyz:links.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 0e1adba099db74866fb51a34a771602279aed5eb
Parent: ba732f2067b20d5f456082053caa551478366bfa
Author: opask
Date:   Mon, 20 Aug 2018 20:13:42 -0600

simplification: charsets.c

Diffstat:
Mbfu.c | 29+++++++++++------------------
Mcharsets.c | 108++++++++++++++++++++++++++-----------------------------------------------------
Mhtml.c | 2+-
Mhttp.c | 14++++++--------
Mlinks.h | 5-----
Mlistedit.c | 2+-
Mmenu.c | 4++--
Msession.c | 4++--
Mview.c | 24++++++++----------------
9 files changed, 66 insertions(+), 126 deletions(-)

diff --git a/bfu.c b/bfu.c @@ -84,13 +84,6 @@ static inline int is_utf_8(struct terminal *term) return 0; } -static inline int ttxtlen(struct terminal *term, unsigned char *s) -{ - if (!term_charset(term)) - return strlen_utf8(s); - return (int)strlen(cast_const_char s); -} - static inline int txtlen(struct terminal *term, unsigned char *s) { #ifdef G @@ -98,7 +91,7 @@ static inline int txtlen(struct terminal *term, unsigned char *s) return g_text_width(bfu_style_wb, s); else #endif - return ttxtlen(term, s); + return strlen((char *)s); } #ifdef G @@ -165,7 +158,7 @@ static unsigned select_hotkey(struct terminal *term, unsigned char *text, unsign for (i = 0; i < n; i++) if (hotkeys[i] == c) continue; - if (!text || cp_strchr(term_charset(term), text, c)) + if (!text || strchr((char *)text, c)) break; } free(text); @@ -204,7 +197,7 @@ void do_menu_selected(struct terminal *term, struct menu_item *items, void *data unsigned u; txt2 = txt3; GET_UTF_8(txt3, u); - u = uni_upcase(u); + u = charset_upcase(u, 0); if (u == menu->hotkeys[i]) { menu->hktxt1[i] = memacpy(txt, txt2 - txt); menu->hktxt2[i] = memacpy(txt2, txt3 - txt2); @@ -347,7 +340,7 @@ static void display_menu_txt(struct terminal *term, void *menu_) } if (menu->items[p].hotkey != M_BAR || (tmptext[0])) { unsigned char *rt = get_text_translation(get_rtext(menu->items[p].rtext), term); - int l = ttxtlen(term, rt); + int l = strlen((char *)rt); for (x = 0;; x++) { c = GET_TERM_CHAR(term, &rt); if (!c) break; @@ -889,8 +882,8 @@ void display_dlg_item(struct dialog_data *dlg, struct dialog_item_data *di, int case D_FIELD_PASS: fill_area(term, di->x, di->y, di->l, 1, ' ', COLOR_DIALOG_FIELD); if (di->vpos > di->cpos) di->vpos = di->cpos; - vposlen = ttxtlen(term, di->cdata + di->vpos); - cposlen = ttxtlen(term, di->cdata + di->cpos); + vposlen = strlen((char *)(di->cdata + di->vpos)); + cposlen = strlen((char *)(di->cdata + di->cpos)); if (!di->l) { di->vpos = di->cpos; vposlen = cposlen; @@ -921,8 +914,8 @@ void display_dlg_item(struct dialog_data *dlg, struct dialog_item_data *di, int co = sel ? COLOR_DIALOG_BUTTON_SELECTED : COLOR_DIALOG_BUTTON; text = get_text_translation(di->item->text, term); print_text(term, di->x, di->y, 2, cast_uchar "[ ", co); - print_text(term, di->x + 2, di->y, ttxtlen(term, text), text, co); - print_text(term, di->x + 2 + ttxtlen(term, text), di->y, 2, cast_uchar " ]", co); + print_text(term, di->x + 2, di->y, strlen((char *)text), text, co); + print_text(term, di->x + 2 + strlen((char *)text), di->y, 2, cast_uchar " ]", co); if (sel) { set_cursor(term, di->x + 2, di->y, di->x + 2, di->y); set_window_ptr(dlg->win, di->x, di->y); @@ -1130,7 +1123,7 @@ static int dlg_mouse(struct dialog_data *dlg, struct dialog_item_data *di, struc { switch (di->item->type) { case D_BUTTON: - if (gf_val(ev->y != di->y, ev->y < di->y || ev->y >= di->y + G_BFU_FONT_SIZE) || ev->x < di->x || ev->x >= di->x + gf_val(ttxtlen(dlg->win->term, get_text_translation(di->item->text, dlg->win->term)) + 4, di->l)) return 0; + if (gf_val(ev->y != di->y, ev->y < di->y || ev->y >= di->y + G_BFU_FONT_SIZE) || ev->x < di->x || ev->x >= di->x + gf_val(strlen((char *)get_text_translation(di->item->text, dlg->win->term)) + 4, di->l)) return 0; if (dlg->selected != di - dlg->items) { x_display_dlg_item(dlg, &dlg->items[dlg->selected], 0); dlg->selected = (int)(di - dlg->items); @@ -1691,7 +1684,7 @@ void draw_dlg(struct dialog_data *dlg) struct terminal *term = dlg->win->term; fill_area(term, dlg->x, dlg->y, dlg->xw, dlg->yw, ' ', COLOR_DIALOG); draw_frame(term, dlg->x + DIALOG_LEFT_BORDER, dlg->y + DIALOG_TOP_BORDER, dlg->xw - 2 * DIALOG_LEFT_BORDER, dlg->yw - 2 * DIALOG_TOP_BORDER, COLOR_DIALOG_FRAME, DIALOG_FRAME); - i = ttxtlen(term, get_text_translation(dlg->dlg->title, term)); + i = strlen((char *)get_text_translation(dlg->dlg->title, term)); tpos = (dlg->xw - i) / 2; print_text(term, tpos + dlg->x - 1, dlg->y + DIALOG_TOP_BORDER, 1, cast_uchar " ", COLOR_DIALOG_TITLE); print_text(term, tpos + dlg->x, dlg->y + DIALOG_TOP_BORDER, i, get_text_translation(dlg->dlg->title, term), COLOR_DIALOG_TITLE); @@ -2061,7 +2054,7 @@ void dlg_format_group(struct dialog_data *dlg, struct terminal *term, unsigned c (*y) += gf_val(2, G_BFU_FONT_SIZE * 2); } if (term) { - if (!F) print_text(term, x + nx + 4 * (item->item->type == D_CHECKBOX), *y, ttxtlen(term, get_text_translation(texts[0], dlg->win->term)), get_text_translation(texts[0], dlg->win->term), COLOR_DIALOG_TEXT); + if (!F) print_text(term, x + nx + 4 * (item->item->type == D_CHECKBOX), *y, strlen((char *)get_text_translation(texts[0], dlg->win->term)), get_text_translation(texts[0], dlg->win->term), COLOR_DIALOG_TEXT); #ifdef G else { int l, ll; diff --git a/charsets.c b/charsets.c @@ -181,8 +181,12 @@ static int utf_table_init = 1; static void free_utf_table(void) { int i; - for (i = 128; i < 256; i++) + for (i = 128; i < 256; i += 4) { free(utf_table[i].u.str); + free(utf_table[i + 1].u.str); + free(utf_table[i + 2].u.str); + free(utf_table[i + 3].u.str); + } } static struct conv_table *get_translation_table_to_utf_8(int from) @@ -196,21 +200,30 @@ static struct conv_table *get_translation_table_to_utf_8(int from) lfr = from; if (utf_table_init) { memset(utf_table, 0, sizeof(struct conv_table) * 256); - for (i = 0; i < 128; i++) - utf_table[i].u.str = cast_uchar strings[i]; + for (i = 0; i < 128; i += 4) { + utf_table[i].u.str = (unsigned char *)strings[i]; + utf_table[i + 1].u.str = (unsigned char *)strings[i + 1]; + utf_table[i + 2].u.str = (unsigned char *)strings[i + 2]; + utf_table[i + 3].u.str = (unsigned char *)strings[i + 3]; + } utf_table_init = 0; } else free_utf_table(); if (!from) { - for (i = 128; i < 256; i++) + for (i = 128; i < 256; i += 4) { utf_table[i].u.str = stracpy(strings[i]); + utf_table[i + 1].u.str = stracpy(strings[i + 1]); + utf_table[i + 2].u.str = stracpy(strings[i + 2]); + utf_table[i + 3].u.str = stracpy(strings[i + 3]); + } return utf_table; } - for (i = 128; i < 256; i++) - utf_table[i].u.str = NULL; - for (i = 128; i < 256; i++) - if (!utf_table[i].u.str) - utf_table[i].u.str = stracpy(no_str); + for (i = 128; i < 256; i += 4) { + utf_table[i].u.str = stracpy(no_str); + utf_table[i + 1].u.str = stracpy(no_str); + utf_table[i + 2].u.str = stracpy(no_str); + utf_table[i + 3].u.str = stracpy(no_str); + } return utf_table; } @@ -415,7 +428,8 @@ decode: e = t[c[i]].u.str; } else { t = t[c[i++]].u.tbl; - if (i >= l) goto put_c; + if (i >= l) + goto put_c; goto decode; } pp = i + 1; @@ -491,17 +505,18 @@ fail:; unsigned char *get_cp_name(int index) { - if (index < 0) return cast_uchar "none"; - return cast_uchar codepages[index].name; + if (index < 0) + return (unsigned char *)"none"; + return (unsigned char *)codepages[index].name; } unsigned char *get_cp_mime_name(int index) { if (index < 0) - return cast_uchar "none"; + return (unsigned char *)"none"; if (!codepages[index].aliases) return NULL; - return cast_uchar codepages[index].aliases[0]; + return (unsigned char *)codepages[index].aliases[0]; } #define LO_EQUAL(a, b) unicode_locase[a].o == (b) @@ -523,44 +538,28 @@ unsigned charset_upcase(unsigned ch, int cp) { unsigned u; int res; - unsigned char *str; if (ch < 0x80) return upcase(ch); u = cp2u(ch, cp); BIN_SEARCH(array_elements(unicode_upcase), UP_EQUAL, UP_ABOVE, u, res); if (res == -1) return ch; - if (!cp) - return unicode_upcase[res].n; - str = u2cp(unicode_upcase[res].n, cp, 0); - if (!str || !str[0] || str[1]) - return ch; - return str[0]; -} - -unsigned uni_upcase(unsigned ch) -{ - return charset_upcase(ch, 0); + return unicode_upcase[res].n; } void charset_upcase_string(unsigned char **chp, int cp) { unsigned char *ch = *chp; - int i; - if (!cp) { - ch = unicode_upcase_string(ch); - free(*chp); - *chp = ch; - } else - for (i = 0; ch[i]; i++) - ch[i] = charset_upcase(ch[i], cp); + ch = unicode_upcase_string(ch); + free(*chp); + *chp = ch; } unsigned char *unicode_upcase_string(unsigned char *ch) { unsigned char *r = init_str(); int rl = 0; - while (1) { + for (;;) { unsigned c; int res; GET_UTF_8(ch, c); @@ -588,7 +587,7 @@ int compare_case_utf8(unsigned char *u1, unsigned char *u2) unsigned char *uu1 = u1; unsigned c1, c2; int cc1; - while (1) { + for (;;) { GET_UTF_8(u2, c2); if (!c2) return (int)(u1 - uu1); @@ -614,40 +613,3 @@ skip_discr: } } } - -int strlen_utf8(unsigned char *s) -{ - int len = 0; - while (1) { - unsigned c; - GET_UTF_8(s, c); - if (!c) - return len; - len++; - } -} - -int cp_len(int cp, unsigned char *s) -{ - if (!cp) - return strlen_utf8(s); - return strlen((char *)s); -} - -unsigned char *cp_strchr(int charset, unsigned char *str, unsigned chr) -{ - if (charset) { - if (chr >= 0x100) - return NULL; - return cast_uchar strchr((char *)str, chr); - } - while (1) { - unsigned char *o_str = str; - unsigned c; - GET_UTF_8(str, c); - if (!c) - return NULL; - if (c == chr) - return o_str; - } -} diff --git a/html.c b/html.c @@ -2368,7 +2368,7 @@ static int do_html_select(unsigned char *attr, unsigned char *html, unsigned cha format_.attr |= AT_BOLD | AT_FIXED; format_.fontsize = 3; mw = 0; - for (i = 0; i < order; i++) if (lbls[i] && cp_len(d_opt->cp, lbls[i]) > mw) mw = cp_len(d_opt->cp, lbls[i]); + for (i = 0; i < order; i++) if (lbls[i] && strlen((char *)lbls[i]) > mw) mw = strlen((char *)lbls[i]); for (i = 0; i < mw; i++) put_chrs(cast_uchar "_", 1); kill_html_stack_item(&html_top); put_chrs(cast_uchar "]", 1); diff --git a/http.c b/http.c @@ -485,17 +485,15 @@ static void add_accept_charset(unsigned char **hdr, int *l, struct http_connecti return; if (!accept_charset) { - int i; unsigned char *cs, *ac; int aclen = 0; ac = init_str(); - for (i = 0; (cs = get_cp_mime_name(i)); i++) { - if (aclen) - add_to_str(&ac, &aclen, cast_uchar ","); - else - add_to_str(&ac, &aclen, cast_uchar "Accept-Charset: "); - add_to_str(&ac, &aclen, cs); - } + cs = get_cp_mime_name(0); + if (aclen) + add_to_str(&ac, &aclen, cast_uchar ","); + else + add_to_str(&ac, &aclen, cast_uchar "Accept-Charset: "); + add_to_str(&ac, &aclen, cs); if (aclen) add_to_str(&ac, &aclen, cast_uchar "\r\n"); if (!(accept_charset = cast_uchar strdup((char *)ac))) { diff --git a/links.h b/links.h @@ -2844,13 +2844,10 @@ int cp2u(unsigned, int); unsigned uni_locase(unsigned); unsigned charset_upcase(unsigned, int); -unsigned uni_upcase(unsigned); void charset_upcase_string(unsigned char **, int); unsigned char *unicode_upcase_string(unsigned char *ch); unsigned char *to_utf8_upcase(unsigned char *str, int cp); int compare_case_utf8(unsigned char *u1, unsigned char *u2); -int strlen_utf8(unsigned char *s); -unsigned char *cp_strchr(int charset, unsigned char *str, unsigned chr); unsigned get_utf_8(unsigned char **p); #define GET_UTF_8(s, c) \ @@ -2879,8 +2876,6 @@ do { \ } \ } while (0) -int cp_len(int cp, unsigned char *s); - extern unsigned char utf_8_1[256]; static inline int utf8chrlen(unsigned char c) diff --git a/listedit.c b/listedit.c @@ -1069,7 +1069,7 @@ static int redraw_list_element(struct terminal *term, struct dialog_data *dlg, i if (!F) { print_text(term,dlg->x+x+DIALOG_LB,y,w-x,txt,color); - x+=cp_len(term_charset(term), txt); + x += strlen((char *)txt); fill_area(term,dlg->x+DIALOG_LB+x,y,w-x,1,' ',0); set_line_color(term,dlg->x+DIALOG_LB+x,y,w-x,color); } diff --git a/menu.c b/menu.c @@ -28,7 +28,7 @@ static unsigned char * const version_texts[] = { static void add_and_pad(unsigned char **s, int *l, struct terminal *term, unsigned char *str, int maxlen) { unsigned char *x = get_text_translation(str, term); - int len = cp_len(term_charset(term), x); + int len = strlen((char *)x); add_to_str(s, l, x); add_to_str(s, l, cast_uchar ": "); while (len++ < maxlen) add_chr_to_str(s, l, ' '); @@ -44,7 +44,7 @@ static void menu_version(void *term_) unsigned char * const *text_ptr; for (i = 0; version_texts[i]; i++) { unsigned char *t = get_text_translation(version_texts[i], term); - int tl = cp_len(term_charset(term), t); + int tl = strlen((char *)t); if (tl > maxlen) maxlen = tl; } diff --git a/session.c b/session.c @@ -294,9 +294,9 @@ static void x_print_screen_title(struct terminal *term, void *ses_) unsigned char color = get_attribute(ses->ds.t_text_color, ses->ds.t_background_color); fill_area(term, 0, 0, term->x, 1, ' ', color); if ((m = print_current_title(ses))) { - int p = term->x - 1 - cp_len(term_charset(ses->term), m); + int p = term->x - 1 - strlen((char *)m); if (p < 0) p = 0; - print_text(term, p, 0, cp_len(term_charset(ses->term), m), m, color); + print_text(term, p, 0, strlen((char *)m), m, color); free(m); } } diff --git a/view.c b/view.c @@ -449,10 +449,7 @@ static int is_in_range(struct f_data *f, int y, int yw, unsigned char *txt, int *min = INT_MAX; *max = 0; - if (!utf8) - l = (int)strlen((const char *)txt); - else - l = strlen_utf8(txt); + l = strlen((char *)txt); if (get_range(f, y, yw, l, &s1, &s2)) return 0; @@ -518,14 +515,9 @@ static int get_searched(struct f_data_c *scr, struct point **pt, int *pl) scr->ses->search_word = NULL; return -1; } - if (!utf8) { - l = (int)strlen((const char *)w); - c = w[0]; - } else { - l = strlen_utf8(w); - ww = w; - GET_UTF_8(ww, c); - } + l = strlen((char *)w); + ww = w; + GET_UTF_8(ww, c); if (get_range(f, scr->vs->view_pos, scr->yw, l, &s1, &s2)) goto ret; for (; s1 <= s2; s1++) { @@ -2094,7 +2086,7 @@ int field_op(struct session *ses, struct f_data_c *f, struct link *l, struct lin if (cp2u(ev->x, term_charset(ses->term)) == -1) goto done; set_br_pos(f, l); - if (!form->ro && cp_len(term_charset(ses->term), fs->value) < form->maxlength) { + if (!form->ro && strlen((char *)fs->value) < form->maxlength) { unsigned char *v; unsigned char a_[2]; unsigned char *nw; @@ -2239,7 +2231,7 @@ int field_op(struct session *ses, struct f_data_c *f, struct link *l, struct lin unsigned char *nl = clipboard; while ((nl = cast_uchar strchr(cast_const_char nl, '\n'))) *nl = ' '; } - if (!form->ro && cp_len(term_charset(ses->term), fs->value) + cp_len(term_charset(ses->term), clipboard) <= form->maxlength) { + if (!form->ro && strlen((char *)fs->value) + strlen((char *)clipboard) <= form->maxlength) { unsigned char *v; v = xrealloc(fs->value, strlen(cast_const_char fs->value) + strlen(cast_const_char clipboard) +1); @@ -3361,8 +3353,8 @@ static unsigned char *print_current_titlex(struct f_data_c *fd, int w) if (!fd->f_data->title) return p; m = init_str(); add_to_str(&m, &ml, fd->f_data->title); - mul = cp_len(term_charset(fd->ses->term), m); - pul = cp_len(term_charset(fd->ses->term), p); + mul = strlen((char *)m); + pul = strlen((char *)p); if (mul + pul > w) { unsigned char *mm; if ((mul = w - pul) < 0) mul = 0;