Sha256: c592691dd66e98b9b69b3d1557b498e55678f720bc2a0719e0ee38a8529918fb

Contents?: true

Size: 1.72 KB

Versions: 12

Compression:

Stored size: 1.72 KB

Contents

#include "prism/enc/pm_encoding.h"

static size_t
pm_encoding_gbk_char_width(const uint8_t *b, ptrdiff_t n) {
    // These are the single byte characters.
    if (*b < 0x80) {
        return 1;
    }

    // These are the double byte characters.
    if (
        (n > 1) &&
        (
            ((b[0] >= 0xA1 && b[0] <= 0xA9) && (b[1] >= 0xA1 && b[1] <= 0xFE)) || // GBK/1
            ((b[0] >= 0xB0 && b[0] <= 0xF7) && (b[1] >= 0xA1 && b[1] <= 0xFE)) || // GBK/2
            ((b[0] >= 0x81 && b[0] <= 0xA0) && (b[1] >= 0x40 && b[1] <= 0xFE) && (b[1] != 0x7F)) || // GBK/3
            ((b[0] >= 0xAA && b[0] <= 0xFE) && (b[1] >= 0x40 && b[1] <= 0xA0) && (b[1] != 0x7F)) || // GBK/4
            ((b[0] >= 0xA8 && b[0] <= 0xA9) && (b[1] >= 0x40 && b[1] <= 0xA0) && (b[1] != 0x7F)) // GBK/5
        )
    ) {
        return 2;
    }

    return 0;
}

static size_t
pm_encoding_gbk_alpha_char(const uint8_t *b, ptrdiff_t n) {
    if (pm_encoding_gbk_char_width(b, n) == 1) {
        return pm_encoding_ascii_alpha_char(b, n);
    } else {
        return 0;
    }
}

static size_t
pm_encoding_gbk_alnum_char(const uint8_t *b, ptrdiff_t n) {
    if (pm_encoding_gbk_char_width(b, n) == 1) {
        return pm_encoding_ascii_alnum_char(b, n);
    } else {
        return 0;
    }
}

static bool
pm_encoding_gbk_isupper_char(const uint8_t *b, ptrdiff_t n) {
    if (pm_encoding_gbk_char_width(b, n) == 1) {
        return pm_encoding_ascii_isupper_char(b, n);
    } else {
        return false;
    }
}

pm_encoding_t pm_encoding_gbk = {
    .name = "gbk",
    .char_width = pm_encoding_gbk_char_width,
    .alnum_char = pm_encoding_gbk_alnum_char,
    .alpha_char = pm_encoding_gbk_alpha_char,
    .isupper_char = pm_encoding_gbk_isupper_char,
    .multibyte = true
};

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/src/enc/pm_gbk.c
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/src/enc/pm_gbk.c
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/src/enc/pm_gbk.c
study_line-0.1.3 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/src/enc/pm_gbk.c
study_line-0.1.2 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/src/enc/pm_gbk.c
study_line-0.1.1 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/src/enc/pm_gbk.c
prism-0.16.0 src/enc/pm_gbk.c
prism-0.15.1 src/enc/pm_gbk.c
prism-0.15.0 src/enc/pm_gbk.c
prism-0.14.0 src/enc/pm_gbk.c
yarp-0.13.0 src/enc/pm_gbk.c
prism-0.13.0 src/enc/pm_gbk.c