Skip to content
This repository was archived by the owner on Dec 14, 2021. It is now read-only.

Commit cc3e4c3

Browse files
committed
unsigned char to char, name union
1 parent 5289759 commit cc3e4c3

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
match
2+
match.exe

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ all: match
22

33
match: tre.h
44
match: match.c
5-
gcc -O2 -Wall -Wextra -pedantic -std=c99 -o $@ match.c
5+
gcc -O2 -Wall -Wextra -Wsign-conversion -pedantic -std=c99 -o $@ match.c
66

77
clean:
88
rm -f match

tre.h

+25-25
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ struct tre_node
5454
unsigned char type;
5555
union
5656
{
57-
unsigned char ch; // character
58-
unsigned char *cc; // character class buffer
57+
char ch; // character
58+
char *cc; // character class buffer
5959
unsigned short mn[2]; // {m,n} quantifier
60-
};
60+
} u;
6161
};
6262

6363
struct tre_comp
6464
{
6565
tre_node nodes[TRE_MAX_NODES];
66-
unsigned char buffer[TRE_MAX_BUFLEN];
66+
char buffer[TRE_MAX_BUFLEN];
6767
};
6868

6969
// Compile regex string pattern as tre_comp struct tregex
@@ -190,10 +190,10 @@ TRE_DEF int tre_ncompile(const char *pattern, unsigned plen, tre_comp *tregex)
190190
return tre_err("NULL/empty string or tre_comp");
191191

192192
tre_node *tnode = tregex->nodes;
193-
unsigned char *buf = tregex->buffer;
193+
char *buf = tregex->buffer;
194194
unsigned buflen = sizeof tregex->buffer;
195-
unsigned char quable = 0; // is the last node quantifiable
196-
unsigned char temp;
195+
char quable = 0; // is the last node quantifiable
196+
char temp;
197197

198198
unsigned idx = 0;
199199

@@ -243,7 +243,7 @@ TRE_DEF int tre_ncompile(const char *pattern, unsigned plen, tre_comp *tregex)
243243
case 'S': tnode[j].type = TRE_NSPACE; break;
244244

245245
// Not in [dDwWsS]
246-
default: tnode[j].type = TRE_CHAR; tnode[j].ch = pattern[i]; break;
246+
default: tnode[j].type = TRE_CHAR; tnode[j].u.ch = pattern[i]; break;
247247
}
248248
} break;
249249

@@ -254,7 +254,7 @@ TRE_DEF int tre_ncompile(const char *pattern, unsigned plen, tre_comp *tregex)
254254

255255
// Look-ahead to determine if negated
256256
tnode[j].type = (pattern[i + 1] == '^') ? (i++, TRE_NCLASS) : TRE_CLASS;
257-
tnode[j].cc = buf + idx;
257+
tnode[j].u.cc = buf + idx;
258258

259259
// Copy characters inside [...] to buffer
260260
while (pattern[++i] != ']' && i < plen)
@@ -318,13 +318,13 @@ TRE_DEF int tre_ncompile(const char *pattern, unsigned plen, tre_comp *tregex)
318318
{
319319
if (i >= plen || pattern[i] < '0' || pattern[i] > '9')
320320
return tre_err("Non-digit min value in quantifier");
321-
val = 10 * val + (pattern[i++] - '0');
321+
val = 10 * val + (unsigned) (pattern[i++] - '0');
322322
}
323323
while (pattern[i] != ',' && pattern[i] != '}');
324324

325325
if (val > TRE_MAXQUANT)
326326
return tre_err("Min value too big in quantifier");
327-
tnode[j].mn[0] = val;
327+
tnode[j].u.mn[0] = val;
328328

329329
if (pattern[i] == ',')
330330
{
@@ -341,19 +341,19 @@ TRE_DEF int tre_ncompile(const char *pattern, unsigned plen, tre_comp *tregex)
341341
{
342342
if (i >= plen || pattern[i] < '0' || pattern[i] > '9')
343343
return tre_err("Non-digit max value in quantifier");
344-
val = 10 * val + (pattern[i++] - '0');
344+
val = 10 * val + (unsigned) (pattern[i++] - '0');
345345
}
346346

347-
if (val > TRE_MAXQUANT || val < tnode[j].mn[0])
347+
if (val > TRE_MAXQUANT || val < tnode[j].u.mn[0])
348348
return tre_err("Max value too big or less than min value in quantifier");
349349
}
350350
}
351351
tnode[j].type = (i + 1 < plen && pattern[i + 1] == '?') ? (i++, TRE_LQUANT) : TRE_QUANT;
352-
tnode[j].mn[1] = val;
352+
tnode[j].u.mn[1] = val;
353353
} break;
354354

355355
// Regular characters
356-
default: quable = 1; tnode[j].type = TRE_CHAR; tnode[j].ch = pattern[i]; break;
356+
default: quable = 1; tnode[j].type = TRE_CHAR; tnode[j].u.ch = pattern[i]; break;
357357
}
358358
i++;
359359
j++;
@@ -389,9 +389,9 @@ static int matchmetachar(char c, char mc)
389389
}
390390

391391
// note: compiler makes sure '\\' is followed by one of 'dDwWsS\\'
392-
static int matchcharclass(char c, const unsigned char *str)
392+
static int matchcharclass(char c, const char *str)
393393
{
394-
unsigned char rmax;
394+
char rmax;
395395
while (*str != '\0')
396396
{
397397
if (str[0] == '\\')
@@ -436,10 +436,10 @@ static int matchone(const tre_node *tnode, char c)
436436
{
437437
switch (tnode->type)
438438
{
439-
case TRE_CHAR: return (tnode->ch == c);
439+
case TRE_CHAR: return (tnode->u.ch == c);
440440
case TRE_DOT: return TRE_MATCHDOT(c);
441-
case TRE_CLASS: return matchcharclass(c, tnode->cc);
442-
case TRE_NCLASS: return !matchcharclass(c, tnode->cc);
441+
case TRE_CLASS: return matchcharclass(c, tnode->u.cc);
442+
case TRE_NCLASS: return !matchcharclass(c, tnode->u.cc);
443443
case TRE_DIGIT: return TRE_MATCHDIGIT(c);
444444
case TRE_NDIGIT: return !TRE_MATCHDIGIT(c);
445445
case TRE_ALPHA: return TRE_MATCHALNUM(c);
@@ -511,9 +511,9 @@ static const char *matchpattern(const tre_node *nodes, const char *text, const c
511511
case TRE_LQMARK:
512512
return matchquant_lazy(nodes, text, tend, 0, 1);
513513
case TRE_QUANT:
514-
return matchquant(nodes, text, tend, nodes[1].mn[0], nodes[1].mn[1]);
514+
return matchquant(nodes, text, tend, nodes[1].u.mn[0], nodes[1].u.mn[1]);
515515
case TRE_LQUANT:
516-
return matchquant_lazy(nodes, text, tend, nodes[1].mn[0], nodes[1].mn[1]);
516+
return matchquant_lazy(nodes, text, tend, nodes[1].u.mn[0], nodes[1].u.mn[1]);
517517
case TRE_STAR:
518518
return matchquant(nodes, text, tend, 0, TRE_MAXPLUS);
519519
case TRE_LSTAR:
@@ -554,15 +554,15 @@ void tre_print(const tre_comp *tregex)
554554
printf("type: %s", tre_typenames[tnode[i].type]);
555555
if (tnode[i].type == TRE_CLASS || tnode[i].type == TRE_NCLASS)
556556
{
557-
printf(" \"%s\"", tnode[i].cc);
557+
printf(" \"%s\"", tnode[i].u.cc);
558558
}
559559
else if (tnode[i].type == TRE_QUANT || tnode[i].type == TRE_LQUANT)
560560
{
561-
printf(" {%d,%d}", tnode[i].mn[0], tnode[i].mn[1]);
561+
printf(" {%d,%d}", tnode[i].u.mn[0], tnode[i].u.mn[1]);
562562
}
563563
else if (tnode[i].type == TRE_CHAR)
564564
{
565-
printf(" '%c'", tnode[i].ch);
565+
printf(" '%c'", tnode[i].u.ch);
566566
}
567567
printf("\n");
568568
}

0 commit comments

Comments
 (0)