aboutsummaryrefslogtreecommitdiff
path: root/src/helper.c
blob: f258d23fc8b1f64aa5d5762df8c808efa236a3b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "../defs/all.h"
#include "../defs/widechar.h"

/* this reads one utf81 character from the start of bytes
   it puts the character's number in *codepoint and returns how many bytes it used (its always 1 or more)
   invalid input it stores U+FFFD (�) and returns 1, so the caller always moves forward. */
int utf8_decode(const unsigned char *bytes, unsigned long *codepoint){
    unsigned long value = bytes[0];
    unsigned long minallowed = 0;   //rejects overlong encodings 
    int sequence_length;
    int byte_index;

    if(value < 0x80){
        *codepoint = value;
        return 1;
    } else if(value >= 0xC2 && value <= 0xDF){
        sequence_length = 2;
        value &= 0x1F;
        minallowed = 0x80;
    } else if(value >= 0xE0 && value <= 0xEF){
        sequence_length = 3;
        value &= 0x0F;
        minallowed = 0x800;
    } else if(value >= 0xF0 && value <= 0xF4){
        sequence_length = 4;
        value &= 0x07;
        minallowed = 0x10000;
    } else {
        *codepoint = 0xFFFD;
        return 1;
    }

    for(byte_index = 1; byte_index < sequence_length; byte_index++){
        if((bytes[byte_index] & 0xC0) != 0x80){
            *codepoint = 0xFFFD;
            return 1;
        }
        value = (value << 6) | (bytes[byte_index] & 0x3F);
    }

    if(value < minallowed || value > 0x10FFFF || (value >= 0xD800 && value <= 0xDFFF)){
        *codepoint = 0xFFFD;
        return 1;
    }

    *codepoint = value;
    return sequence_length;
}

/* this turns the library's return value into a column count so 0 1 or 2 */
static int codepoint_width(unsigned long codepoint){
    int width = widechar_wcwidth(codepoint);
    if(width > 0)
        return width;

    switch(width){
        case widechar_ambiguous:     
        case widechar_private_use:
            return 1;
        case widechar_widened_in_9:
            return 2;
        default:                     
            return 0;
    }
}

/* how many terminal columns a string takes up */
int displaywidth(const char *text){
    const unsigned char *current_byte = (const unsigned char *)text;
    int total_width = 0;

    while(*current_byte){
        unsigned long codepoint;
        current_byte += utf8_decode(current_byte, &codepoint);
        total_width += codepoint_width(codepoint);
    }
    return total_width;
}