|
47 | 47 | #include "regex/regex.h"
|
48 | 48 | #include "regex/utils.h"
|
49 | 49 | #include "regex/regex2.h"
|
50 |
| -#include "regex/cclass.h" |
51 | 50 | #include "regex/cname.h"
|
| 51 | +#include <locale.h> |
| 52 | + |
| 53 | +struct cclass |
| 54 | +{ |
| 55 | + char *name; |
| 56 | + char *chars; |
| 57 | + char *multis; |
| 58 | +}; |
| 59 | +static struct cclass* cclasses = NULL; |
| 60 | +static struct cclass* cclass_init(void); |
52 | 61 |
|
53 | 62 | /*
|
54 | 63 | * parse structure, passed up and down to avoid global variables and
|
@@ -174,6 +183,9 @@ pg95_regcomp(regex_t *preg, const char *pattern, int cflags)
|
174 | 183 | pg_wchar *wcp;
|
175 | 184 | #endif
|
176 | 185 |
|
| 186 | + if ( cclasses == NULL ) |
| 187 | + cclasses = cclass_init(); |
| 188 | + |
177 | 189 | #ifdef REDEBUG
|
178 | 190 | #define GOODFLAGS(f) (f)
|
179 | 191 | #else
|
@@ -884,7 +896,7 @@ p_b_cclass(struct parse * p, cset *cs)
|
884 | 896 | struct cclass *cp;
|
885 | 897 | size_t len;
|
886 | 898 | char *u;
|
887 |
| - char c; |
| 899 | + unsigned char c; |
888 | 900 |
|
889 | 901 | while (MORE() && pg_isalpha(PEEK()))
|
890 | 902 | NEXT();
|
@@ -1716,3 +1728,81 @@ pg_islower(int c)
|
1716 | 1728 | return (islower((unsigned char) c));
|
1717 | 1729 | #endif
|
1718 | 1730 | }
|
| 1731 | + |
| 1732 | +static struct cclass * |
| 1733 | +cclass_init(void) |
| 1734 | +{ |
| 1735 | + static struct cclass cclasses_C[] = { |
| 1736 | + { "alnum", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "" }, |
| 1737 | + { "alpha", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "" }, |
| 1738 | + { "blank", " \t", "" }, |
| 1739 | + { "cntrl", "\007\b\t\n\v\f\r\1\2\3\4\5\6\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\177", "" }, |
| 1740 | + { "digit", "0123456789", "" }, |
| 1741 | + { "graph", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", "" }, |
| 1742 | + { "lower", "abcdefghijklmnopqrstuvwxyz", "" }, |
| 1743 | + { "print", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ ", "" }, |
| 1744 | + { "punct", "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", "" }, |
| 1745 | + { "space", "\t\n\v\f\r ", "" }, |
| 1746 | + { "upper", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "" }, |
| 1747 | + { "xdigit", "0123456789ABCDEFabcdef", "" }, |
| 1748 | + { NULL, NULL, "" } |
| 1749 | + }; |
| 1750 | + struct cclass *cp = NULL; |
| 1751 | + struct cclass *classes = NULL; |
| 1752 | + struct cclass_factory |
| 1753 | + { |
| 1754 | + char *name; |
| 1755 | + int (*func)(int); |
| 1756 | + char *chars; |
| 1757 | + } cclass_factories [] = |
| 1758 | + { |
| 1759 | + { "alnum", isalnum, NULL }, |
| 1760 | + { "alpha", isalpha, NULL }, |
| 1761 | + { "blank", NULL, " \t" }, |
| 1762 | + { "cntrl", iscntrl, NULL }, |
| 1763 | + { "digit", NULL, "0123456789" }, |
| 1764 | + { "graph", isgraph, NULL }, |
| 1765 | + { "lower", islower, NULL }, |
| 1766 | + { "print", isprint, NULL }, |
| 1767 | + { "punct", ispunct, NULL }, |
| 1768 | + { "space", NULL, "\t\n\v\f\r " }, |
| 1769 | + { "upper", isupper, NULL }, |
| 1770 | + { "xdigit", NULL, "0123456789ABCDEFabcdef" }, |
| 1771 | + { NULL, NULL, NULL } |
| 1772 | + }; |
| 1773 | + struct cclass_factory *cf = NULL; |
| 1774 | + |
| 1775 | + if ( strcmp( setlocale( LC_CTYPE, NULL ), "C" ) == 0 ) |
| 1776 | + return cclasses_C; |
| 1777 | + |
| 1778 | + classes = malloc(sizeof(struct cclass) * (sizeof(cclass_factories) / sizeof(struct cclass_factory))); |
| 1779 | + if (classes == NULL) |
| 1780 | + elog(ERROR,"cclass_init: out of memory"); |
| 1781 | + |
| 1782 | + cp = classes; |
| 1783 | + for(cf = cclass_factories; cf->name != NULL; cf++) |
| 1784 | + { |
| 1785 | + cp->name = strdup(cf->name); |
| 1786 | + if ( cf->chars ) |
| 1787 | + cp->chars = strdup(cf->chars); |
| 1788 | + else |
| 1789 | + { |
| 1790 | + int x = 0, y = 0; |
| 1791 | + cp->chars = malloc(sizeof(char) * 256); |
| 1792 | + if (cp->chars == NULL) |
| 1793 | + elog(ERROR,"cclass_init: out of memory"); |
| 1794 | + for (x = 0; x < 256; x++) |
| 1795 | + { |
| 1796 | + if((cf->func)(x)) |
| 1797 | + *(cp->chars + y++) = x; |
| 1798 | + } |
| 1799 | + *(cp->chars + y) = '\0'; |
| 1800 | + } |
| 1801 | + cp->multis = ""; |
| 1802 | + cp++; |
| 1803 | + } |
| 1804 | + cp->name = cp->chars = NULL; |
| 1805 | + cp->multis = ""; |
| 1806 | + |
| 1807 | + return classes; |
| 1808 | +} |
0 commit comments