mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
string.c: share UTF-8 length table with mruby-string-ext.
This commit is contained in:
@@ -989,11 +989,7 @@ mrb_str_succ(mrb_state *mrb, mrb_value self)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MRB_UTF8_STRING
|
#ifdef MRB_UTF8_STRING
|
||||||
static const char utf8len_table[] =
|
extern const char mrb_utf8len_table[];
|
||||||
{
|
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0
|
|
||||||
};
|
|
||||||
|
|
||||||
static mrb_int
|
static mrb_int
|
||||||
utf8code(unsigned char* p, mrb_int limit)
|
utf8code(unsigned char* p, mrb_int limit)
|
||||||
@@ -1003,7 +999,7 @@ utf8code(unsigned char* p, mrb_int limit)
|
|||||||
if (p[0] < 0x80)
|
if (p[0] < 0x80)
|
||||||
return p[0];
|
return p[0];
|
||||||
|
|
||||||
len = utf8len_table[p[0]>>3];
|
len = mrb_utf8len_table[p[0]>>3];
|
||||||
if (len <= limit && len > 1 && (p[1] & 0xc0) == 0x80) {
|
if (len <= limit && len > 1 && (p[1] & 0xc0) == 0x80) {
|
||||||
if (len == 2)
|
if (len == 2)
|
||||||
return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
|
return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
|
||||||
|
|||||||
+6
-5
@@ -239,16 +239,17 @@ mrb_gc_free_str(mrb_state *mrb, struct RString *str)
|
|||||||
|
|
||||||
#ifdef MRB_UTF8_STRING
|
#ifdef MRB_UTF8_STRING
|
||||||
|
|
||||||
|
const char mrb_utf8len_table[] = {
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0
|
||||||
|
};
|
||||||
|
|
||||||
#define utf8_islead(c) ((unsigned char)((c)&0xc0) != 0x80)
|
#define utf8_islead(c) ((unsigned char)((c)&0xc0) != 0x80)
|
||||||
|
|
||||||
mrb_int
|
mrb_int
|
||||||
mrb_utf8len(const char* p, const char* e)
|
mrb_utf8len(const char* p, const char* e)
|
||||||
{
|
{
|
||||||
static const char lengths[] = {
|
mrb_int len = mrb_utf8len_table[(unsigned char)p[0] >> 3];
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0
|
|
||||||
};
|
|
||||||
mrb_int len = lengths[(unsigned char)p[0] >> 3];
|
|
||||||
if (len > e - p) return 1;
|
if (len > e - p) return 1;
|
||||||
switch (len) {
|
switch (len) {
|
||||||
case 1:
|
case 1:
|
||||||
|
|||||||
Reference in New Issue
Block a user