diff options
author | Markus Teich <markus.teich@stusta.mhn.de> | 2013-06-22 23:06:49 +0200 |
---|---|---|
committer | Roberto E. Vargas Caballero <k0ga@shike2.com> | 2013-07-04 09:28:19 +0200 |
commit | 6fc471ccc660b305cf836dcb8d57cdbffb4ed017 (patch) | |
tree | 8019296fd8ed6a303d52da3185d1d162b0363f40 | |
parent | 8b602a37a65a3c001bd9334d8b1cfc17fc5dd45c (diff) | |
download | st-6fc471ccc660b305cf836dcb8d57cdbffb4ed017.tar.gz st-6fc471ccc660b305cf836dcb8d57cdbffb4ed017.zip |
fix: consistent usage of bitmask operations on unicode functions
-rw-r--r-- | st.c | 50 |
1 files changed, 23 insertions, 27 deletions
@@ -150,10 +150,6 @@ enum selection_snap { | |||
150 | SNAP_LINE = 2 | 150 | SNAP_LINE = 2 |
151 | }; | 151 | }; |
152 | 152 | ||
153 | /* bit macro */ | ||
154 | #undef B0 | ||
155 | enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 }; | ||
156 | |||
157 | typedef unsigned char uchar; | 153 | typedef unsigned char uchar; |
158 | typedef unsigned int uint; | 154 | typedef unsigned int uint; |
159 | typedef unsigned long ulong; | 155 | typedef unsigned long ulong; |
@@ -527,17 +523,17 @@ utf8decode(char *s, long *u) { | |||
527 | 523 | ||
528 | rtn = 1; | 524 | rtn = 1; |
529 | c = *s; | 525 | c = *s; |
530 | if(~c & B7) { /* 0xxxxxxx */ | 526 | if(~c & 0x80) { /* 0xxxxxxx */ |
531 | *u = c; | 527 | *u = c; |
532 | return rtn; | 528 | return rtn; |
533 | } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */ | 529 | } else if((c & 0xE0) == 0xC0) { /* 110xxxxx */ |
534 | *u = c&(B4|B3|B2|B1|B0); | 530 | *u = c & 0x1F; |
535 | n = 1; | 531 | n = 1; |
536 | } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */ | 532 | } else if((c & 0xF0) == 0xE0) { /* 1110xxxx */ |
537 | *u = c&(B3|B2|B1|B0); | 533 | *u = c & 0x0F; |
538 | n = 2; | 534 | n = 2; |
539 | } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */ | 535 | } else if((c & 0xF8) == 0xF0) { /* 11110xxx */ |
540 | *u = c & (B2|B1|B0); | 536 | *u = c & 0x07; |
541 | n = 3; | 537 | n = 3; |
542 | } else { | 538 | } else { |
543 | goto invalid; | 539 | goto invalid; |
@@ -545,10 +541,10 @@ utf8decode(char *s, long *u) { | |||
545 | 541 | ||
546 | for(i = n, ++s; i > 0; --i, ++rtn, ++s) { | 542 | for(i = n, ++s; i > 0; --i, ++rtn, ++s) { |
547 | c = *s; | 543 | c = *s; |
548 | if((c & (B7|B6)) != B7) /* 10xxxxxx */ | 544 | if((c & 0xC0) != 0x80) /* 10xxxxxx */ |
549 | goto invalid; | 545 | goto invalid; |
550 | *u <<= 6; | 546 | *u <<= 6; |
551 | *u |= c & (B5|B4|B3|B2|B1|B0); | 547 | *u |= c & 0x3F; |
552 | } | 548 | } |
553 | 549 | ||
554 | if((n == 1 && *u < 0x80) || | 550 | if((n == 1 && *u < 0x80) || |
@@ -577,20 +573,20 @@ utf8encode(long *u, char *s) { | |||
577 | *sp = uc; /* 0xxxxxxx */ | 573 | *sp = uc; /* 0xxxxxxx */ |
578 | return 1; | 574 | return 1; |
579 | } else if(*u < 0x800) { | 575 | } else if(*u < 0x800) { |
580 | *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */ | 576 | *sp = (uc >> 6) | 0xC0; /* 110xxxxx */ |
581 | n = 1; | 577 | n = 1; |
582 | } else if(uc < 0x10000) { | 578 | } else if(uc < 0x10000) { |
583 | *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */ | 579 | *sp = (uc >> 12) | 0xE0; /* 1110xxxx */ |
584 | n = 2; | 580 | n = 2; |
585 | } else if(uc <= 0x10FFFF) { | 581 | } else if(uc <= 0x10FFFF) { |
586 | *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */ | 582 | *sp = (uc >> 18) | 0xF0; /* 11110xxx */ |
587 | n = 3; | 583 | n = 3; |
588 | } else { | 584 | } else { |
589 | goto invalid; | 585 | goto invalid; |
590 | } | 586 | } |
591 | 587 | ||
592 | for(i=n,++sp; i>0; --i,++sp) | 588 | for(i=n,++sp; i>0; --i,++sp) |
593 | *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */ | 589 | *sp = ((uc >> 6*(i-1)) & 0x3F) | 0x80; /* 10xxxxxx */ |
594 | 590 | ||
595 | return n+1; | 591 | return n+1; |
596 | invalid: | 592 | invalid: |
@@ -613,16 +609,16 @@ isfullutf8(char *s, int b) { | |||
613 | c3 = (uchar *)++s; | 609 | c3 = (uchar *)++s; |
614 | if(b < 1) { | 610 | if(b < 1) { |
615 | return 0; | 611 | return 0; |
616 | } else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1) { | 612 | } else if((*c1 & 0xE0) == 0xC0 && b == 1) { |
617 | return 0; | 613 | return 0; |
618 | } else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) && | 614 | } else if((*c1 & 0xF0) == 0xE0 && |
619 | ((b == 1) || | 615 | ((b == 1) || |
620 | ((b == 2) && (*c2&(B7|B6)) == B7))) { | 616 | ((b == 2) && (*c2 & 0xC0) == 0x80))) { |
621 | return 0; | 617 | return 0; |
622 | } else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) && | 618 | } else if((*c1 & 0xF8) == 0xF0 && |
623 | ((b == 1) || | 619 | ((b == 1) || |
624 | ((b == 2) && (*c2&(B7|B6)) == B7) || | 620 | ((b == 2) && (*c2 & 0xC0) == 0x80) || |
625 | ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7))) { | 621 | ((b == 3) && (*c2 & 0xC0) == 0x80 && (*c3 & 0xC0) == 0x80))) { |
626 | return 0; | 622 | return 0; |
627 | } else { | 623 | } else { |
628 | return 1; | 624 | return 1; |
@@ -633,11 +629,11 @@ int | |||
633 | utf8size(char *s) { | 629 | utf8size(char *s) { |
634 | uchar c = *s; | 630 | uchar c = *s; |
635 | 631 | ||
636 | if(~c&B7) { | 632 | if(~c & 0x80) { |
637 | return 1; | 633 | return 1; |
638 | } else if((c&(B7|B6|B5)) == (B7|B6)) { | 634 | } else if((c & 0xE0) == 0xC0) { |
639 | return 2; | 635 | return 2; |
640 | } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) { | 636 | } else if((c & 0xF0) == 0xE0) { |
641 | return 3; | 637 | return 3; |
642 | } else { | 638 | } else { |
643 | return 4; | 639 | return 4; |
@@ -3456,7 +3452,7 @@ kpress(XEvent *ev) { | |||
3456 | if(len == 1 && e->state & Mod1Mask) { | 3452 | if(len == 1 && e->state & Mod1Mask) { |
3457 | if(IS_SET(MODE_8BIT)) { | 3453 | if(IS_SET(MODE_8BIT)) { |
3458 | if(*xstr < 0177) { | 3454 | if(*xstr < 0177) { |
3459 | c = *xstr | B7; | 3455 | c = *xstr | 0x80; |
3460 | ret = utf8encode(&c, cp); | 3456 | ret = utf8encode(&c, cp); |
3461 | cp += ret; | 3457 | cp += ret; |
3462 | len = 0; | 3458 | len = 0; |