Skip to content

Commit 415949f

Browse files
committed
* enc/unicode.c: Protect code point count by macro, in order to
be able to use the remaining bits for flags. (with Kimihito Matsui) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53669 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 75687e0 commit 415949f

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Wed Jan 27 17:54:42 2016 Martin Duerst <duerst@it.aoyama.ac.jp>
2+
3+
* enc/unicode.c: Protect code point count by macro, in order to
4+
be able to use the remaining bits for flags.
5+
(with Kimihito Matsui)
6+
17
Wed Jan 27 16:34:35 2016 boshan <boshan@subsplash.com>
28

39
* lib/tempfile.rb (Tempfile#initialize): [DOC] the first parameter

enc/unicode.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ static const unsigned short EncUNICODE_ISO_8859_1_CtypeTable[256] = {
7171
0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2
7272
};
7373

74+
/* use bottom bytes for actual code point count; 3 bits is more than enough */
75+
#define OnigCodePointCount(n) ((n)&0x111)
76+
7477
typedef struct {
7578
int n;
7679
OnigCodePoint code[3];
@@ -247,7 +250,7 @@ onigenc_unicode_mbc_case_fold(OnigEncoding enc,
247250
#endif
248251

249252
if ((to = onigenc_unicode_fold_lookup(code)) != 0) {
250-
if (to->n == 1) {
253+
if (OnigCodePointCount(to->n) == 1) {
251254
return ONIGENC_CODE_TO_MBC(enc, to->code[0], fold);
252255
}
253256
#if 0
@@ -258,7 +261,7 @@ onigenc_unicode_mbc_case_fold(OnigEncoding enc,
258261
#endif
259262
{
260263
rlen = 0;
261-
for (i = 0; i < to->n; i++) {
264+
for (i = 0; i < OnigCodePointCount(to->n); i++) {
262265
len = ONIGENC_CODE_TO_MBC(enc, to->code[i], fold);
263266
fold += len;
264267
rlen += len;
@@ -284,7 +287,7 @@ onigenc_unicode_apply_all_case_fold(OnigCaseFoldType flag,
284287

285288
for (i = 0; i < numberof(CaseUnfold_11); i++) {
286289
p11 = &CaseUnfold_11[i];
287-
for (j = 0; j < p11->to.n; j++) {
290+
for (j = 0; j < OnigCodePointCount(p11->to.n); j++) {
288291
code = p11->from;
289292
r = (*f)(p11->to.code[j], &code, 1, arg);
290293
if (r != 0) return r;
@@ -323,7 +326,7 @@ onigenc_unicode_apply_all_case_fold(OnigCaseFoldType flag,
323326
#endif
324327
for (i = 0; i < numberof(CaseUnfold_11_Locale); i++) {
325328
p11 = &CaseUnfold_11_Locale[i];
326-
for (j = 0; j < p11->to.n; j++) {
329+
for (j = 0; j < OnigCodePointCount(p11->to.n); j++) {
327330
code = p11->from;
328331
r = (*f)(p11->to.code[j], &code, 1, arg);
329332
if (r != 0) return r;
@@ -349,12 +352,12 @@ onigenc_unicode_apply_all_case_fold(OnigCaseFoldType flag,
349352

350353
if ((flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
351354
for (i = 0; i < numberof(CaseUnfold_12); i++) {
352-
for (j = 0; j < CaseUnfold_12[i].to.n; j++) {
355+
for (j = 0; j < OnigCodePointCount(CaseUnfold_12[i].to.n); j++) {
353356
r = (*f)(CaseUnfold_12[i].to.code[j],
354357
(OnigCodePoint* )CaseUnfold_12[i].from, 2, arg);
355358
if (r != 0) return r;
356359

357-
for (k = 0; k < CaseUnfold_12[i].to.n; k++) {
360+
for (k = 0; k < OnigCodePointCount(CaseUnfold_12[i].to.n); k++) {
358361
if (k == j) continue;
359362

360363
r = (*f)(CaseUnfold_12[i].to.code[j],
@@ -368,12 +371,12 @@ onigenc_unicode_apply_all_case_fold(OnigCaseFoldType flag,
368371
if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) == 0) {
369372
#endif
370373
for (i = 0; i < numberof(CaseUnfold_12_Locale); i++) {
371-
for (j = 0; j < CaseUnfold_12_Locale[i].to.n; j++) {
374+
for (j = 0; j < OnigCodePointCount(CaseUnfold_12_Locale[i].to.n); j++) {
372375
r = (*f)(CaseUnfold_12_Locale[i].to.code[j],
373376
(OnigCodePoint* )CaseUnfold_12_Locale[i].from, 2, arg);
374377
if (r != 0) return r;
375378

376-
for (k = 0; k < CaseUnfold_12_Locale[i].to.n; k++) {
379+
for (k = 0; k < OnigCodePointCount(CaseUnfold_12_Locale[i].to.n); k++) {
377380
if (k == j) continue;
378381

379382
r = (*f)(CaseUnfold_12_Locale[i].to.code[j],
@@ -388,12 +391,12 @@ onigenc_unicode_apply_all_case_fold(OnigCaseFoldType flag,
388391
#endif
389392

390393
for (i = 0; i < numberof(CaseUnfold_13); i++) {
391-
for (j = 0; j < CaseUnfold_13[i].to.n; j++) {
394+
for (j = 0; j < OnigCodePointCount(CaseUnfold_13[i].to.n); j++) {
392395
r = (*f)(CaseUnfold_13[i].to.code[j],
393396
(OnigCodePoint* )CaseUnfold_13[i].from, 3, arg);
394397
if (r != 0) return r;
395398

396-
for (k = 0; k < CaseUnfold_13[i].to.n; k++) {
399+
for (k = 0; k < OnigCodePointCount(CaseUnfold_13[i].to.n); k++) {
397400
if (k == j) continue;
398401

399402
r = (*f)(CaseUnfold_13[i].to.code[j],
@@ -452,7 +455,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
452455
#endif
453456

454457
if ((to = onigenc_unicode_fold_lookup(code)) != 0) {
455-
if (to->n == 1) {
458+
if (OnigCodePointCount(to->n) == 1) {
456459
OnigCodePoint orig_code = code;
457460

458461
items[0].byte_len = len;
@@ -462,7 +465,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
462465

463466
code = to->code[0];
464467
if ((to = onigenc_unicode_unfold1_lookup(code)) != 0) {
465-
for (i = 0; i < to->n; i++) {
468+
for (i = 0; i < OnigCodePointCount(to->n); i++) {
466469
if (to->code[i] != orig_code) {
467470
items[n].byte_len = len;
468471
items[n].code_len = 1;
@@ -476,13 +479,13 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
476479
OnigCodePoint cs[3][4];
477480
int fn, ncs[3];
478481

479-
for (fn = 0; fn < to->n; fn++) {
482+
for (fn = 0; fn < OnigCodePointCount(to->n); fn++) {
480483
cs[fn][0] = to->code[fn];
481484
if ((z3 = onigenc_unicode_unfold1_lookup(cs[fn][0])) != 0) {
482-
for (i = 0; i < z3->n; i++) {
485+
for (i = 0; i < OnigCodePointCount(z3->n); i++) {
483486
cs[fn][i+1] = z3->code[i];
484487
}
485-
ncs[fn] = z3->n + 1;
488+
ncs[fn] = OnigCodePointCount(z3->n) + 1;
486489
}
487490
else
488491
ncs[fn] = 1;
@@ -500,7 +503,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
500503
}
501504

502505
if ((z2 = onigenc_unicode_unfold2_lookup(to->code)) != 0) {
503-
for (i = 0; i < z2->n; i++) {
506+
for (i = 0; i < OnigCodePointCount(z2->n); i++) {
504507
if (z2->code[i] == code) continue;
505508

506509
items[n].byte_len = len;
@@ -525,7 +528,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
525528
}
526529

527530
if ((z2 = onigenc_unicode_unfold3_lookup(to->code)) != 0) {
528-
for (i = 0; i < z2->n; i++) {
531+
for (i = 0; i < OnigCodePointCount(z2->n); i++) {
529532
if (z2->code[i] == code) continue;
530533

531534
items[n].byte_len = len;
@@ -542,7 +545,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
542545
}
543546
else {
544547
if ((to = onigenc_unicode_unfold1_lookup(code)) != 0) {
545-
for (i = 0; i < to->n; i++) {
548+
for (i = 0; i < OnigCodePointCount(to->n); i++) {
546549
items[n].byte_len = len;
547550
items[n].code_len = 1;
548551
items[n].code[0] = to->code[i];
@@ -560,7 +563,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
560563
codes[0] = code;
561564
code = ONIGENC_MBC_TO_CODE(enc, p, end);
562565
if ((to = onigenc_unicode_fold_lookup(code)) != 0
563-
&& to->n == 1) {
566+
&& OnigCodePointCount(to->n) == 1) {
564567
codes[1] = to->code[0];
565568
}
566569
else
@@ -569,7 +572,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
569572
clen = enclen(enc, p, end);
570573
len += clen;
571574
if ((z2 = onigenc_unicode_unfold2_lookup(codes)) != 0) {
572-
for (i = 0; i < z2->n; i++) {
575+
for (i = 0; i < OnigCodePointCount(z2->n); i++) {
573576
items[n].byte_len = len;
574577
items[n].code_len = 1;
575578
items[n].code[0] = z2->code[i];
@@ -581,7 +584,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
581584
if (p < end) {
582585
code = ONIGENC_MBC_TO_CODE(enc, p, end);
583586
if ((to = onigenc_unicode_fold_lookup(code)) != 0
584-
&& to->n == 1) {
587+
&& OnigCodePointCount(to->n) == 1) {
585588
codes[2] = to->code[0];
586589
}
587590
else
@@ -590,7 +593,7 @@ onigenc_unicode_get_case_fold_codes_by_str(OnigEncoding enc,
590593
clen = enclen(enc, p, end);
591594
len += clen;
592595
if ((z2 = onigenc_unicode_unfold3_lookup(codes)) != 0) {
593-
for (i = 0; i < z2->n; i++) {
596+
for (i = 0; i < OnigCodePointCount(z2->n); i++) {
594597
items[n].byte_len = len;
595598
items[n].code_len = 1;
596599
items[n].code[0] = z2->code[i];

0 commit comments

Comments
 (0)