ex_cmds: Simplify do_ascii

Specifically apply constants like enc_utf8 (which are constants *now*) and 
simplify conditions after that. Also some style changes.
This commit is contained in:
ZyX
2018-04-15 19:01:58 +03:00
parent db7f80302b
commit aa5008c1f0

View File

@@ -109,79 +109,61 @@ typedef struct {
# include "ex_cmds.c.generated.h" # include "ex_cmds.c.generated.h"
#endif #endif
/* /// ":ascii" and "ga" implementation
* ":ascii" and "ga". void do_ascii(const exarg_T *const eap)
*/
void do_ascii(exarg_T *eap)
{ {
int c;
int cval;
char buf1[20];
char buf2[20];
char_u buf3[7];
int cc[MAX_MCO]; int cc[MAX_MCO];
int ci = 0; int c = utfc_ptr2char(get_cursor_pos_ptr(), cc);
int len;
const bool l_enc_utf8 = enc_utf8;
if (l_enc_utf8)
c = utfc_ptr2char(get_cursor_pos_ptr(), cc);
else
c = gchar_cursor();
if (c == NUL) { if (c == NUL) {
MSG("NUL"); MSG("NUL");
return; return;
} }
IObuff[0] = NUL; IObuff[0] = NUL;
if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80) {
if (c == NL) /* NUL is stored as NL */ int ci = 0;
if (c < 0x80) {
if (c == NL) { // NUL is stored as NL.
c = NUL; c = NUL;
if (c == CAR && get_fileformat(curbuf) == EOL_MAC) }
cval = NL; /* NL is stored as CR */ const int cval = (c == CAR && get_fileformat(curbuf) == EOL_MAC
else ? NL // NL is stored as CR.
cval = c; : c);
if (vim_isprintc_strict(c) && (c < ' ' char buf1[20];
|| c > '~' if (vim_isprintc_strict(c) && (c < ' ' || c > '~')) {
)) { char_u buf3[7];
transchar_nonprint(buf3, c); transchar_nonprint(buf3, c);
vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3); vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
} else } else {
buf1[0] = NUL; buf1[0] = NUL;
if (c >= 0x80) }
vim_snprintf(buf2, sizeof(buf2), " <M-%s>", char buf2[20];
(char *)transchar(c & 0x7f)); buf2[0] = NUL;
else
buf2[0] = NUL;
vim_snprintf((char *)IObuff, IOSIZE, vim_snprintf((char *)IObuff, IOSIZE,
_("<%s>%s%s %d, Hex %02x, Octal %03o"), _("<%s>%s%s %d, Hex %02x, Octal %03o"),
transchar(c), buf1, buf2, cval, cval, cval); transchar(c), buf1, buf2, cval, cval, cval);
if (l_enc_utf8) c = cc[ci++];
c = cc[ci++];
else
c = 0;
} }
/* Repeat for combining characters. */ // Repeat for combining characters.
while (has_mbyte && (c >= 0x100 || (l_enc_utf8 && c >= 0x80))) { while (c >= 0x80) {
len = (int)STRLEN(IObuff); int len = (int)STRLEN(IObuff);
/* This assumes every multi-byte char is printable... */ // This assumes every multi-byte char is printable...
if (len > 0) if (len > 0) {
IObuff[len++] = ' '; IObuff[len++] = ' ';
}
IObuff[len++] = '<'; IObuff[len++] = '<';
if (l_enc_utf8 && utf_iscomposing(c)) { if (utf_iscomposing(c)) {
IObuff[len++] = ' '; // Draw composing char on top of a space. IObuff[len++] = ' '; // Draw composing char on top of a space.
} }
len += (*mb_char2bytes)(c, IObuff + len); len += utf_char2bytes(c, IObuff + len);
vim_snprintf((char *)IObuff + len, IOSIZE - len, vim_snprintf((char *)IObuff + len, IOSIZE - len,
c < 0x10000 ? _("> %d, Hex %04x, Octal %o") c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
: _("> %d, Hex %08x, Octal %o"), c, c, c); : _("> %d, Hex %08x, Octal %o"), c, c, c);
if (ci == MAX_MCO) if (ci == MAX_MCO) {
break; break;
if (l_enc_utf8) }
c = cc[ci++]; c = cc[ci++];
else
c = 0;
} }
msg(IObuff); msg(IObuff);