Skip to content

Commit

Permalink
enc/unicode.c: lookup functions
Browse files Browse the repository at this point in the history
* enc/unicode.c (onigenc_unicode_{fold,unfold{1,2,3}}_lookup):
  abstract lookup functions.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46057 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed May 23, 2014
1 parent dcaf699 commit 90fb753
Showing 1 changed file with 51 additions and 15 deletions.
66 changes: 51 additions & 15 deletions enc/unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,46 @@ static int init_case_fold_table(void)
return 0;
}

static inline const CodePointList3 *
onigenc_unicode_fold_lookup(OnigCodePoint code)
{
st_data_t to;
if (onig_st_lookup(FoldTable, (st_data_t)code, &to) != 0) {
return (const CodePointList3 *)to;
}
return 0;
}

static inline const CodePointList3 *
onigenc_unicode_unfold1_lookup(OnigCodePoint code)
{
st_data_t to;
if (onig_st_lookup(Unfold1Table, (st_data_t )code, &to) != 0) {
return (const CodePointList3 *)to;
}
return 0;
}

static inline const CodePointList2 *
onigenc_unicode_unfold2_lookup(const OnigCodePoint *code)
{
st_data_t to;
if (onig_st_lookup(Unfold2Table, (st_data_t )code, &to) != 0) {
return (const CodePointList2 *)to;
}
return 0;
}

static inline const CodePointList2 *
onigenc_unicode_unfold3_lookup(const OnigCodePoint *code)
{
st_data_t to;
if (onig_st_lookup(Unfold3Table, (st_data_t )code, &to) != 0) {
return (const CodePointList2 *)to;
}
return 0;
}

extern int
onigenc_unicode_mbc_case_fold(OnigEncoding enc,
OnigCaseFoldType flag ARG_UNUSED, const UChar** pp, const UChar* end,
Expand Down Expand Up @@ -319,7 +359,7 @@ onigenc_unicode_mbc_case_fold(OnigEncoding enc,
}
#endif

if (onig_st_lookup(FoldTable, (st_data_t )code, (void* )&to) != 0) {
if ((to = onigenc_unicode_fold_lookup(code)) != 0) {
if (to->n == 1) {
return ONIGENC_CODE_TO_MBC(enc, to->code[0], fold);
}
Expand Down Expand Up @@ -528,7 +568,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
}
#endif

if (onig_st_lookup(FoldTable, (st_data_t )code, (void* )&to) != 0) {
if ((to = onigenc_unicode_fold_lookup(code)) != 0) {
if (to->n == 1) {
OnigCodePoint orig_code = code;

Expand All @@ -538,7 +578,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
n++;

code = to->code[0];
if (onig_st_lookup(Unfold1Table, (st_data_t )code, (void* )&to) != 0) {
if ((to = onigenc_unicode_unfold1_lookup(code)) != 0) {
for (i = 0; i < to->n; i++) {
if (to->code[i] != orig_code) {
items[n].byte_len = len;
Expand All @@ -555,8 +595,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,

for (fn = 0; fn < to->n; fn++) {
cs[fn][0] = to->code[fn];
if (onig_st_lookup(Unfold1Table, (st_data_t )cs[fn][0],
(void* )&z3) != 0) {
if ((z3 = onigenc_unicode_unfold1_lookup(cs[fn][0])) != 0) {
for (i = 0; i < z3->n; i++) {
cs[fn][i+1] = z3->code[i];
}
Expand All @@ -577,8 +616,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
}
}

if (onig_st_lookup(Unfold2Table, (st_data_t )to->code,
(void* )&z2) != 0) {
if ((z2 = onigenc_unicode_unfold2_lookup(to->code)) != 0) {
for (i = 0; i < z2->n; i++) {
if (z2->code[i] == code) continue;

Expand All @@ -603,8 +641,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
}
}

if (onig_st_lookup(Unfold3Table, (st_data_t )to->code,
(void* )&z2) != 0) {
if ((z2 = onigenc_unicode_unfold3_lookup(to->code)) != 0) {
for (i = 0; i < z2->n; i++) {
if (z2->code[i] == code) continue;

Expand All @@ -621,7 +658,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
}
}
else {
if (onig_st_lookup(Unfold1Table, (st_data_t )code, (void* )&to) != 0) {
if ((to = onigenc_unicode_unfold1_lookup(code)) != 0) {
for (i = 0; i < to->n; i++) {
items[n].byte_len = len;
items[n].code_len = 1;
Expand All @@ -639,7 +676,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,

codes[0] = code;
code = ONIGENC_MBC_TO_CODE(enc, p, end);
if (onig_st_lookup(FoldTable, (st_data_t )code, (void* )&to) != 0
if ((to = onigenc_unicode_fold_lookup(code)) != 0
&& to->n == 1) {
codes[1] = to->code[0];
}
Expand All @@ -648,7 +685,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,

clen = enclen(enc, p, end);
len += clen;
if (onig_st_lookup(Unfold2Table, (st_data_t )codes, (void* )&z2) != 0) {
if ((z2 = onigenc_unicode_unfold2_lookup(codes)) != 0) {
for (i = 0; i < z2->n; i++) {
items[n].byte_len = len;
items[n].code_len = 1;
Expand All @@ -660,7 +697,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
p += clen;
if (p < end) {
code = ONIGENC_MBC_TO_CODE(enc, p, end);
if (onig_st_lookup(FoldTable, (st_data_t )code, (void* )&to) != 0
if ((to = onigenc_unicode_fold_lookup(code)) != 0
&& to->n == 1) {
codes[2] = to->code[0];
}
Expand All @@ -669,8 +706,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,

clen = enclen(enc, p, end);
len += clen;
if (onig_st_lookup(Unfold3Table, (st_data_t )codes,
(void* )&z2) != 0) {
if ((z2 = onigenc_unicode_unfold3_lookup(codes)) != 0) {
for (i = 0; i < z2->n; i++) {
items[n].byte_len = len;
items[n].code_len = 1;
Expand Down

0 comments on commit 90fb753

Please sign in to comment.