📄 slregexp.c
字号:
if (str >= estr) break; /* failed */ } if (NULL != (epos = regexp_looking_at(&ctx_buf, str, estr, buf, cs))) { fixup_beg_end_matches (&ctx_buf, reg, str, epos); return str; } if (str >= estr) break; str++; } fixup_beg_end_matches (&ctx_buf, reg, NULL, epos); return NULL;}static unsigned char *convert_digit(unsigned char *pat, int *nn){ int n = 0, m = 0; unsigned char c; while (c = (unsigned char) *pat, (c <= '9') && (c >= '0')) { pat++; n = 10 * n + (c - '0'); m++; } if (m == 0) { return (NULL); } *nn = n; return pat;}#define ERROR return (int) (pat - reg->pat)/* Returns 0 if successful or offset in pattern of error */int SLang_regexp_compile (SLRegexp_Type *reg){ register unsigned char *buf, *ebuf, *pat; unsigned char *last = NULL, *tmppat; register unsigned char c; int i, reverse = 0, n, cs; int oparen = 0, nparen = 0; /* substring stuff */ int count, last_count, this_max_mm = 0, max_mm = 0, ordinary_search, no_osearch = 0, min_length = 0; unsigned char *mm_p = NULL, *this_mm_p = NULL; static int already_initialized; reg->beg_matches[0] = reg->end_matches[0] = 0; buf = reg->buf; ebuf = (reg->buf + reg->buf_len) - 2; /* make some room */ pat = reg->pat; cs = reg->case_sensitive; if (already_initialized == 0) { SLang_init_case_tables ();#ifdef IBMPC_SYSTEM SLmake_lut (Word_Chars, (unsigned char *) "_0-9a-zA-Z\200-\232\240-\245\341-\353", 0);#else SLmake_lut (Word_Chars, (unsigned char *) "_0-9a-zA-Z\277-\326\330-\336\340-\366\370-\376", 0);#endif already_initialized = 1; } i = 1; while (i < 10) { reg->beg_matches[i] = -1; reg->end_matches[i] = 0; i++; } if (*pat == '\\') { if (pat[1] == 'c') { cs = 1; pat += 2; no_osearch = 1; } else if (pat[1] == 'C') { cs = 0; pat += 2; no_osearch = 1; } } if (*pat == '^') { pat++; *buf++ = BOL; reg->must_match_bol = 1; } else reg->must_match_bol = 0; if (cs != reg->case_sensitive) { if (cs) *buf++ = YES_CASE; else *buf++ = NO_CASE; } *buf = 0; last_count = count = 0; while ((c = *pat++) != 0) { if (buf >= ebuf - 3) { SLang_doerror ("Pattern too large to be compiled."); ERROR; } count++; switch (c) { case '$': if (*pat != 0) goto literal_char; *buf++ = EOL; break; case '\\': c = *pat++; no_osearch = 1; switch(c) { case 'e': c = 033; goto literal_char; case 'n': c = '\n'; goto literal_char; case 't': c = '\t'; goto literal_char; case 'C': cs = 0; *buf++ = NO_CASE; break; case 'c': cs = 1; *buf++ = YES_CASE; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': c = c - '0'; if ((int) c > nparen) ERROR; last = buf; *buf++ = NTH_MATCH; *buf++ = c; break;#ifdef NOT_LITERAL case '~': /* slang extension */ if ((c = *pat) == 0) ERROR; pat++; last = buf; *buf++ = NOT_LITERAL; *buf++ = c; min_length++; break;#endif case 'd': /* slang extension */ last = buf; *buf++ = ANY_DIGIT; min_length++; break; case '<': last = NULL; *buf++ = BOW; break; case '>': last = NULL; *buf++ = EOW; break; case '{': if (last == NULL) goto literal_char; *last |= MANY; tmppat = convert_digit(pat, &n); if (tmppat == NULL) ERROR; pat = tmppat; *buf++ = n; min_length += (n - 1); if (*pat == '\\') { *buf++ = n; } else if (*pat == ',') { pat++; if (*pat == '\\') { n = 255; } else { tmppat = convert_digit(pat, &n); if (tmppat == NULL) ERROR; pat = tmppat; if (*pat != '\\') ERROR; } *buf++ = n; } else ERROR; last = NULL; pat++; if (*pat != '}') ERROR; pat++; break; /* case '{' */ case '(': oparen++; if (oparen > 9) ERROR; *buf++ = OPAREN; break; case ')': if (oparen == 0) ERROR; oparen--; nparen++; *buf++ = CPAREN; break; case 0: ERROR; default: goto literal_char; } break; case '[': *buf = RANGE; last = buf++; if (buf + 32 >= ebuf) ERROR; for (i = 0; i < 32; i++) buf[i] = 0; c = *pat++; if (c == '^') { reverse = 1; SET_BIT(buf, '\n'); c = *pat++; } if (c == ']') { SET_BIT(buf, c); c = *pat++; } while (c && (c != ']')) { if (c == '\\') { c = *pat++; switch(c) { case 'n': c = '\n'; break; case 't': c = '\t'; break; case 0: ERROR; } } if (*pat == '-') { pat++; while (c < *pat) { if (cs == 0) { SET_BIT(buf, UPPERCASE(c)); SET_BIT(buf, LOWERCASE(c)); } else SET_BIT(buf, c); c++; } } if (cs == 0) { SET_BIT(buf, UPPERCASE(c)); SET_BIT(buf, LOWERCASE(c)); } else SET_BIT(buf, c); c = *pat++; } if (c != ']') ERROR; if (reverse) for (i = 0; i < 32; i++) buf[i] = buf[i] ^ 0xFF; reverse = 0; buf += 32; min_length++; break; case '.': last = buf; *buf++ = ANY; min_length++; break; case '*': if (last == NULL) goto literal_char; *last |= STAR; min_length--; last = NULL; break; case '+': if (last == NULL) goto literal_char; *last |= LEAST_ONCE; last = NULL; break; case '?': if (last == NULL) goto literal_char; *last |= MAYBE_ONCE; last = NULL; min_length--; break; literal_char: default: /* This is to keep track of longest substring */ min_length++; this_max_mm++; if (last_count + 1 == count) { if (this_max_mm == 1) { this_mm_p = buf; } else if (max_mm < this_max_mm) { mm_p = this_mm_p; max_mm = this_max_mm; } } else { this_mm_p = buf; this_max_mm = 1; } last_count = count; last = buf; *buf++ = LITERAL; *buf++ = UPPERCASE(c); } } *buf = 0; /* Check for ordinary search */ ebuf = buf; buf = reg->buf; if (no_osearch) ordinary_search = 0; else { ordinary_search = 1; while (buf < ebuf) { if (*buf != LITERAL) { ordinary_search = 0; break; } buf += 2; } } reg->osearch = ordinary_search; reg->must_match_str[15] = 0; reg->min_length = (min_length > 0) ? (unsigned int) min_length : 0; if (ordinary_search) { strncpy((char *) reg->must_match_str, (char *) reg->pat, 15); reg->must_match = 1; return(0); } /* check for longest substring of pattern */ reg->must_match = 0; if ((mm_p == NULL) && (this_mm_p != NULL)) mm_p = this_mm_p; if (mm_p == NULL) { return (0); } n = 15; pat = reg->must_match_str; buf = mm_p; while (n--) { if (*buf++ != LITERAL) break; *pat++ = *buf++; } *pat = 0; if (pat != reg->must_match_str) reg->must_match = 1; return(0);}char *SLregexp_quote_string (char *re, char *buf, unsigned int buflen){ char ch; char *b, *bmax; if (re == NULL) return NULL; b = buf; bmax = buf + buflen; while (b < bmax) { switch (ch = *re++) { case 0: *b = 0; return buf; case '$': case '\\': case '[': case ']': case '.': case '^': case '*': case '+': case '?': *b++ = '\\'; if (b == bmax) break; /* drop */ default: *b++ = ch; } } return NULL;}#if 0#define MAX_EXP 4096int main(int argc, char **argv){ FILE *fp; char *regexp, *file; char expbuf[MAX_EXP], buf[512]; SLRegexp_Type reg; file = argv[2]; regexp = argv[1]; if (NULL == (fp = fopen(file, "r"))) { fprintf(stderr, "File not open\n"); return(1); } reg.buf = expbuf; reg.buf_len = MAX_EXP; reg.pat = regexp; reg.case_sensitive = 1; if (!regexp_compile(®)) while (NULL != fgets(buf, 511, fp)) { if (reg.osearch) { if (NULL == strstr(buf, reg.pat)) continue; } else { if (reg.must_match && (NULL == strstr(buf, reg.must_match_str))) continue; if (0 == regexp_match(buf, buf + strlen(buf), ®)) continue; } fputs(buf, stdout); } return (0);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -