all repos — fae @ a784a3abf61d3b9b4466895935ec1d9e83f08024

A minuscule find and edit utility.

Started refactoring to use sgregex instead of slre.
h3rald h3rald@h3rald.com
Sun, 01 Nov 2020 10:11:12 +0100
commit

a784a3abf61d3b9b4466895935ec1d9e83f08024

parent

24817a3826c544776b0f39bce0ba5b01f7f33218

7 files changed, 104 insertions(+), 575 deletions(-)

jump to
M .gitignore.gitignore

@@ -1,2 +1,3 @@

nimcache -far +packages +fae
M far.nimfae.nim

@@ -1,6 +1,7 @@

import - slre, - parseopt2, + packages/nim-sgregex/sgregex, + std/exitprocs, + parseopt, os, terminal, strutils,

@@ -11,7 +12,8 @@ StringBounds = array[0..1, int]

StringMatches = object start: int finish: int - captures: ptr array[0..9, Capture] + #captures: ptr array[0..9, Capture] + captures: seq[string] FarOptions = object regex: string recursive: bool

@@ -21,21 +23,23 @@ directory: string

apply: bool test: bool silent: bool - flags: int -system.addQuitProc(resetAttributes) +addExitProc(resetAttributes) const version = "1.1.0" -const usage = """FAR v""" & version & """ - Find & Replace Utility - (c) 2015-2016 Fabio Cevasco +const usage = """FAE v""" & version & """ - Find & Edit Utility + (c) 2015-2020 Fabio Cevasco Usage: - far <pattern> [<replacement>] [option1 option2 ...] + far [s/]<pattern>/[<replacement>/][flags] [option1 option2 ...] Where: <pattern> A regular expression to search for - <replacement> An optional replacement string + <replacoement> An optional replacement string + <flags> i: case-insensitive match + m: multiline match + s: treat newlines as spaces Options: -a, --apply Substitute all occurrences of <pattern> with <replacement> in all files

@@ -43,7 +47,6 @@ without asking for confirmation.

-d, --directory Search in the specified directory (default: .) -f, --filter Specify a regular expression to filter file paths. -h, --help Display this message. - -i, --ignore-case Case-insensitive matching. -r, --recursive Search directories recursively. -s, --silent Do not display matches. -t, --test Do not perform substitutions, just print results.

@@ -71,50 +74,63 @@ quit("Regex Error: Too many brackets", match)

else: result = [-1, match] -proc matchBounds(str, regex: string, start = 0, flags = 0): StringBounds = - var c = cast[ptr array[0..9,Capture]](alloc0(sizeof(array[0..9, Capture]))) - var s = str.substr(start).cstring - let match = slre_match(("(" & regex & ")").cstring, s, s.len.cint, c, 10, flags.cint) - if match >= 0: - result = [match-c[0].len+start, match-1+start] - else: - result = match.handleRegexErrors() + +proc matchBounds(str, expr: string, start = 0): StringBounds = + let s = str.substr(start) + let c = s =~ expr + let match = c.len + result = [match-c[0].len+start, match-1+start] + -proc matchCaptures(str, regex: string, start = 0, flags = 0): StringMatches = - var c = cast[ptr array[0..9,Capture]](alloc0(sizeof(array[0..9, Capture]))) - var s = str.substr(start).cstring - let match = slre_match(("(" & regex & ")").cstring, s, s.len.cint, c, 10, flags.cint) - if match >= 0: - result = StringMatches(start: match-c[0].len+start, finish: match-1+start, captures: c) - else: - result = StringMatches(start: match, finish: match, captures: c) +#proc old_matchBounds(str, regex: string, start = 0, flags = 0): StringBounds = +# var c = cast[ptr array[0..9,Capture]](alloc0(sizeof(array[0..9, Capture]))) +# var s = str.substr(start).cstring +# let match = slre_match(("(" & regex & ")").cstring, s, s.len.cint, c, 10, flags.cint) +# if match >= 0: +# result = [match-c[0].len+start, match-1+start] +# else: +# result = match.handleRegexErrors() -proc matchBoundsRec(str, regex: string, start = 0, matches: var seq[StringBounds], flags = 0) = - let match = str.matchBounds(regex, start, flags = flags) +proc matchCaptures(str, expr: string, start = 0): StringMatches = + let s = str.substr(start) + let c = s =~ expr + let match = c.len + result = StringMatches(start: match-c[0].len+start, finish: match-1+start, captures: c) + +#proc old_matchCaptures(str, regex: string, start = 0, flags = 0): StringMatches = +# var c = cast[ptr array[0..9,Capture]](alloc0(sizeof(array[0..9, Capture]))) +# var s = str.substr(start).cstring +# let match = slre_match(("(" & regex & ")").cstring, s, s.len.cint, c, 10, flags.cint) +# if match >= 0: +# result = StringMatches(start: match-c[0].len+start, finish: match-1+start, captures: c) +# else: +# result = StringMatches(start: match, finish: match, captures: c) + +proc matchBoundsRec(str, regex: string, start = 0, matches: var seq[StringBounds]) = + let match = str.matchBounds(regex, start) if match[0] >= 0: matches.add(match) - matchBoundsRec(str, regex, match[1]+1, matches, flags = flags) + matchBoundsRec(str, regex, match[1]+1, matches) -proc match(str, regex: string, flags = 0): bool = - var c = cast[ptr array[0..9,Capture]](alloc0(sizeof(array[0..9, Capture]))) - return slre_match(regex.cstring, str.cstring, str.len.cint, c, 10, flags.cint) >= 0 +proc match(str, regex: string): bool = + str.match(regex) proc rawReplace(str: var string, sub: string, start, finish: int) = str.delete(start, finish) str.insert(sub, start) -proc replace(str, regex: string, substitute: var string, start = 0, flags = 0): string = +proc replace(str, regex: string, substitute: var string, start = 0): string = var newstr = str - let match = str.matchCaptures(regex, start, flags = flags) + let match = str.matchCaptures(regex, start) if match.finish >= 0: for i in 1..9: # Substitute captures var submatches = newSeq[StringBounds](0) - substitute.matchBoundsRec("\\\\" & $i, 0, submatches, flags = flags) + substitute.matchBoundsRec("\\\\" & $i, 0, submatches) for submatch in submatches: var capture = match.captures[i] if capture.len > 0: - substitute.rawReplace(substr($capture.str, 0, (capture.len-1).int), submatch[0], submatch[1]) + substitute.rawReplace(substr(capture, 0, (capture.len-1).int), submatch[0], submatch[1]) newstr.rawReplace(substitute, match.start, match.finish) return newstr

@@ -161,9 +177,9 @@

proc confirm(msg: string): bool = stdout.write(msg) var answer = stdin.readLine() - if answer.match("y(es)?", 1): + if answer.match("y(es)?", "i"): return true - elif answer.match("n(o)?", 1): + elif answer.match("n(o)?", "i"): return false else: return confirm(msg)

@@ -183,14 +199,14 @@ while file.readline(contents):

lineN.inc contentsLen = contents.len fileLines.add contents - var match = matchBounds(contents, options.regex, 0, flags = options.flags) + var match = matchBounds(contents, options.regex, 0) while match[0] > 0: matchesN.inc var offset = 0 var matchstart, matchend: int matchstart = match[0] matchend = match[1] - if options.substitute != nil: + if options.substitute != "": displayFile(f) displayMatch(contents, matchstart, matchend, fgRed, lineN) var substitute = options.substitute

@@ -207,9 +223,9 @@ fileLines[fileLines.high] = replacement

else: displayFile(f, silent = options.silent) displayMatch(contents, match[0], match[1], fgYellow, lineN, silent = options.silent) - match = matchBounds(contents, options.regex, matchend+offset+1, flags = options.flags) + match = matchBounds(contents, options.regex, matchend+offset+1) file.close() - if (not options.test) and (not options.substitute.isNil) and hasSubstitutions: + if (not options.test) and (options.substitute != "") and hasSubstitutions: f.writefile(fileLines.join("\n")) return [matchesN, subsN]

@@ -219,16 +235,16 @@ ## Processing Options

var duration = cpuTime() -var options = FarOptions(regex: nil, recursive: false, filter: nil, substitute: nil, directory: ".", apply: false, test: false, flags: 0, silent: false) +var options = FarOptions(regex: "", recursive: false, filter: "", substitute: "", directory: ".", apply: false, test: false, silent: false) for kind, key, val in getOpt(): case kind: of cmdArgument: - if options.regex == nil: + if options.regex == "": options.regex = key - elif options.substitute == nil: + elif options.substitute == "": options.substitute = key - elif options.regex == nil and options.substitute == nil: + elif options.regex == "" and options.substitute == "": quit("Too many arguments", 1) of cmdLongOption, cmdShortOption: case key:

@@ -248,8 +264,6 @@ quit(0)

of "version", "v": echo version quit(0) - of "ignore-case", "i": - options.flags = 1 of "silent", "s": options.silent = true else:

@@ -257,7 +271,7 @@ discard

else: discard -if options.regex == nil: +if options.regex == "": echo usage quit(0)

@@ -270,7 +284,7 @@ var res: array[0..1, int]

if options.recursive: for f in walkDirRec(options.directory): - if options.filter == nil or f.match(options.filter): + if options.filter == "" or f.match(options.filter): try: count.inc res = processFile(f, options)

@@ -281,7 +295,7 @@ stderr.writeLine getCurrentExceptionMsg()

continue else: for kind, f in walkDir(options.directory): - if kind == pcFile and (options.filter == nil or f.match(options.filter)): + if kind == pcFile and (options.filter == "" or f.match(options.filter)): try: count.inc res = processFile(f, options)

@@ -291,7 +305,7 @@ except:

stderr.writeLine getCurrentExceptionMsg() continue -if options.substitute != nil: +if options.substitute != "": echo "=== ", count, " files processed - ", matchesN, " matches, ", subsN, " substitutions (", (cpuTime()-duration), " seconds)." else: echo "=== ", count, " files processed - ", matchesN, " matches (", (cpuTime()-duration), " seconds)."
A nifty.json

@@ -0,0 +1,36 @@

+{ + "storage": "packages", + "commands": { + "install": { + "_syntax": "install [<package>]", + "_description": "Installs the specified package (or all mapped packages) to the storage directory.", + "git+src": { + "cmd": "git clone {{src}} --depth 1" + }, + "git+src+tag": { + "cmd": "git clone --branch {{tag}} {{src}} --depth 1" + }, + "curl+src+name": { + "cmd": "curl {{src}} -o {{name}}" + } + }, + "upgrade": { + "_syntax": "upgrade [<package>]", + "_description": "Upgrades the specified previously-installed package (or all packages).", + "git+name": { + "cmd": "git pull", + "pwd": "{{name}}" + }, + "curl+src+name": { + "cmd": "curl {{src}} -o {{name}}" + } + } + }, + "packages": { + "nim-sgregex": { + "name": "nim-sgregex", + "src": "git@github.com:h3rald/nim-sgregex.git", + "git": true + } + } +}
D slre.nim

@@ -1,25 +0,0 @@

-{.compile: "vendor/libslre.c".} -{.push importc, cdecl.} -const - SLRE_HEADER_DEFINED* = true -type - Capture* = object - str*: cstring - len*: cint - -proc slre_match*(regexp: cstring; buf: cstring; buf_len: cint; - caps: ptr array[0..9, Capture]; num_caps: cint; flags: cint): cint -# Possible flags for slre_match() -const - SLRE_IGNORE_CASE* = 1 -# slre_match() failure codes -const - SLRE_NO_MATCH* = - 1 - SLRE_UNEXPECTED_QUANTIFIER* = - 2 - SLRE_UNBALANCED_BRACKETS* = - 3 - SLRE_INTERNAL_ERROR* = - 4 - SLRE_INVALID_CHARACTER_SET* = - 5 - SLRE_INVALID_METACHARACTER* = - 6 - SLRE_CAPS_ARRAY_TOO_SMALL* = - 7 - SLRE_TOO_MANY_BRANCHES* = - 8 - SLRE_TOO_MANY_BRACKETS* = - 9
D vendor/libslre.c

@@ -1,437 +0,0 @@

-/* - * Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com> - * Copyright (c) 2013 Cesanta Software Limited - * All rights reserved - * - * This library is dual-licensed: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. For the terms of this - * license, see <http://www.gnu.org/licenses/>. - * - * You are free to use this library under the terms of the GNU General - * Public License, but WITHOUT ANY WARRANTY; without even the implied - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * Alternatively, you can license this library under a commercial - * license, as set out in <http://cesanta.com/products.html>. - */ - -#include <stdio.h> -#include <ctype.h> -#include <string.h> - -#include "slre.h" - -#define MAX_BRANCHES 100 -#define MAX_BRACKETS 100 -#define FAIL_IF(condition, error_code) if (condition) return (error_code) - -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(ar) (sizeof(ar) / sizeof((ar)[0])) -#endif - -#ifdef SLRE_DEBUG -#define DBG(x) printf x -#else -#define DBG(x) -#endif - -struct bracket_pair { - const char *ptr; /* Points to the first char after '(' in regex */ - int len; /* Length of the text between '(' and ')' */ - int branches; /* Index in the branches array for this pair */ - int num_branches; /* Number of '|' in this bracket pair */ -}; - -struct branch { - int bracket_index; /* index for 'struct bracket_pair brackets' */ - /* array defined below */ - const char *schlong; /* points to the '|' character in the regex */ -}; - -struct regex_info { - /* - * Describes all bracket pairs in the regular expression. - * First entry is always present, and grabs the whole regex. - */ - struct bracket_pair brackets[MAX_BRACKETS]; - int num_brackets; - - /* - * Describes alternations ('|' operators) in the regular expression. - * Each branch falls into a specific branch pair. - */ - struct branch branches[MAX_BRANCHES]; - int num_branches; - - /* Array of captures provided by the user */ - struct slre_cap *caps; - int num_caps; - - /* E.g. SLRE_IGNORE_CASE. See enum below */ - int flags; -}; - -static int is_metacharacter(const unsigned char *s) { - static const char *metacharacters = "^$().[]*+?|\\Ssdbfnrtv"; - return strchr(metacharacters, *s) != NULL; -} - -static int op_len(const char *re) { - return re[0] == '\\' && re[1] == 'x' ? 4 : re[0] == '\\' ? 2 : 1; -} - -static int set_len(const char *re, int re_len) { - int len = 0; - - while (len < re_len && re[len] != ']') { - len += op_len(re + len); - } - - return len <= re_len ? len + 1 : -1; -} - -static int get_op_len(const char *re, int re_len) { - return re[0] == '[' ? set_len(re + 1, re_len - 1) + 1 : op_len(re); -} - -static int is_quantifier(const char *re) { - return re[0] == '*' || re[0] == '+' || re[0] == '?'; -} - -static int toi(int x) { - return isdigit(x) ? x - '0' : x - 'W'; -} - -static int hextoi(const unsigned char *s) { - return (toi(tolower(s[0])) << 4) | toi(tolower(s[1])); -} - -static int match_op(const unsigned char *re, const unsigned char *s, - struct regex_info *info) { - int result = 0; - switch (*re) { - case '\\': - /* Metacharacters */ - switch (re[1]) { - case 'S': FAIL_IF(isspace(*s), SLRE_NO_MATCH); result++; break; - case 's': FAIL_IF(!isspace(*s), SLRE_NO_MATCH); result++; break; - case 'd': FAIL_IF(!isdigit(*s), SLRE_NO_MATCH); result++; break; - case 'b': FAIL_IF(*s != '\b', SLRE_NO_MATCH); result++; break; - case 'f': FAIL_IF(*s != '\f', SLRE_NO_MATCH); result++; break; - case 'n': FAIL_IF(*s != '\n', SLRE_NO_MATCH); result++; break; - case 'r': FAIL_IF(*s != '\r', SLRE_NO_MATCH); result++; break; - case 't': FAIL_IF(*s != '\t', SLRE_NO_MATCH); result++; break; - case 'v': FAIL_IF(*s != '\v', SLRE_NO_MATCH); result++; break; - - case 'x': - /* Match byte, \xHH where HH is hexadecimal byte representaion */ - FAIL_IF(hextoi(re + 2) != *s, SLRE_NO_MATCH); - result++; - break; - - default: - /* Valid metacharacter check is done in bar() */ - FAIL_IF(re[1] != s[0], SLRE_NO_MATCH); - result++; - break; - } - break; - - case '|': FAIL_IF(1, SLRE_INTERNAL_ERROR); break; - case '$': FAIL_IF(1, SLRE_NO_MATCH); break; - case '.': result++; break; - - default: - if (info->flags & SLRE_IGNORE_CASE) { - FAIL_IF(tolower(*re) != tolower(*s), SLRE_NO_MATCH); - } else { - FAIL_IF(*re != *s, SLRE_NO_MATCH); - } - result++; - break; - } - - return result; -} - -static int match_set(const char *re, int re_len, const char *s, - struct regex_info *info) { - int len = 0, result = -1, invert = re[0] == '^'; - - if (invert) re++, re_len--; - - while (len <= re_len && re[len] != ']' && result <= 0) { - /* Support character range */ - if (re[len] != '-' && re[len + 1] == '-' && re[len + 2] != ']' && - re[len + 2] != '\0') { - result = info->flags & SLRE_IGNORE_CASE ? - tolower(*s) >= tolower(re[len]) && tolower(*s) <= tolower(re[len + 2]) : - *s >= re[len] && *s <= re[len + 2]; - len += 3; - } else { - result = match_op((unsigned char *) re + len, (unsigned char *) s, info); - len += op_len(re + len); - } - } - return (!invert && result > 0) || (invert && result <= 0) ? 1 : -1; -} - -static int doh(const char *s, int s_len, struct regex_info *info, int bi); - -static int bar(const char *re, int re_len, const char *s, int s_len, - struct regex_info *info, int bi) { - /* i is offset in re, j is offset in s, bi is brackets index */ - int i, j, n, step; - - for (i = j = 0; i < re_len && j <= s_len; i += step) { - - /* Handle quantifiers. Get the length of the chunk. */ - step = re[i] == '(' ? info->brackets[bi + 1].len + 2 : - get_op_len(re + i, re_len - i); - - DBG(("%s [%.*s] [%.*s] re_len=%d step=%d i=%d j=%d\n", __func__, - re_len - i, re + i, s_len - j, s + j, re_len, step, i, j)); - - FAIL_IF(is_quantifier(&re[i]), SLRE_UNEXPECTED_QUANTIFIER); - FAIL_IF(step <= 0, SLRE_INVALID_CHARACTER_SET); - - if (i + step < re_len && is_quantifier(re + i + step)) { - DBG(("QUANTIFIER: [%.*s]%c [%.*s]\n", step, re + i, - re[i + step], s_len - j, s + j)); - if (re[i + step] == '?') { - int result = bar(re + i, step, s + j, s_len - j, info, bi); - j += result > 0 ? result : 0; - i++; - } else if (re[i + step] == '+' || re[i + step] == '*') { - int j2 = j, nj = j, n1, n2 = -1, ni, non_greedy = 0; - - /* Points to the regexp code after the quantifier */ - ni = i + step + 1; - if (ni < re_len && re[ni] == '?') { - non_greedy = 1; - ni++; - } - - do { - if ((n1 = bar(re + i, step, s + j2, s_len - j2, info, bi)) > 0) { - j2 += n1; - } - if (re[i + step] == '+' && n1 < 0) break; - - if (ni >= re_len) { - /* After quantifier, there is nothing */ - nj = j2; - } else if ((n2 = bar(re + ni, re_len - ni, s + j2, - s_len - j2, info, bi)) >= 0) { - /* Regex after quantifier matched */ - nj = j2 + n2; - } - if (nj > j && non_greedy) break; - } while (n1 > 0); - - /* - * Even if we found one or more pattern, this branch will be executed, - * changing the next captures. - */ - if (n1 < 0 && n2 < 0 && re[i + step] == '*' && - (n2 = bar(re + ni, re_len - ni, s + j, s_len - j, info, bi)) > 0) { - nj = j + n2; - } - - DBG(("STAR/PLUS END: %d %d %d %d %d\n", j, nj, re_len - ni, n1, n2)); - FAIL_IF(re[i + step] == '+' && nj == j, SLRE_NO_MATCH); - - /* If while loop body above was not executed for the * quantifier, */ - /* make sure the rest of the regex matches */ - FAIL_IF(nj == j && ni < re_len && n2 < 0, SLRE_NO_MATCH); - - /* Returning here cause we've matched the rest of RE already */ - return nj; - } - continue; - } - - if (re[i] == '[') { - n = match_set(re + i + 1, re_len - (i + 2), s + j, info); - DBG(("SET %.*s [%.*s] -> %d\n", step, re + i, s_len - j, s + j, n)); - FAIL_IF(n <= 0, SLRE_NO_MATCH); - j += n; - } else if (re[i] == '(') { - n = SLRE_NO_MATCH; - bi++; - FAIL_IF(bi >= info->num_brackets, SLRE_INTERNAL_ERROR); - DBG(("CAPTURING [%.*s] [%.*s] [%s]\n", - step, re + i, s_len - j, s + j, re + i + step)); - - if (re_len - (i + step) <= 0) { - /* Nothing follows brackets */ - n = doh(s + j, s_len - j, info, bi); - } else { - int j2; - for (j2 = 0; j2 <= s_len - j; j2++) { - if ((n = doh(s + j, s_len - (j + j2), info, bi)) >= 0 && - bar(re + i + step, re_len - (i + step), - s + j + n, s_len - (j + n), info, bi) >= 0) break; - } - } - - DBG(("CAPTURED [%.*s] [%.*s]:%d\n", step, re + i, s_len - j, s + j, n)); - FAIL_IF(n < 0, n); - if (info->caps != NULL && n > 0) { - info->caps[bi - 1].ptr = s + j; - info->caps[bi - 1].len = n; - } - j += n; - } else if (re[i] == '^') { - FAIL_IF(j != 0, SLRE_NO_MATCH); - } else if (re[i] == '$') { - FAIL_IF(j != s_len, SLRE_NO_MATCH); - } else { - FAIL_IF(j >= s_len, SLRE_NO_MATCH); - n = match_op((unsigned char *) (re + i), (unsigned char *) (s + j), info); - FAIL_IF(n <= 0, n); - j += n; - } - } - - return j; -} - -/* Process branch points */ -static int doh(const char *s, int s_len, struct regex_info *info, int bi) { - const struct bracket_pair *b = &info->brackets[bi]; - int i = 0, len, result; - const char *p; - - do { - p = i == 0 ? b->ptr : info->branches[b->branches + i - 1].schlong + 1; - len = b->num_branches == 0 ? b->len : - i == b->num_branches ? (int) (b->ptr + b->len - p) : - (int) (info->branches[b->branches + i].schlong - p); - DBG(("%s %d %d [%.*s] [%.*s]\n", __func__, bi, i, len, p, s_len, s)); - result = bar(p, len, s, s_len, info, bi); - DBG(("%s <- %d\n", __func__, result)); - } while (result <= 0 && i++ < b->num_branches); /* At least 1 iteration */ - - return result; -} - -static int baz(const char *s, int s_len, struct regex_info *info) { - int i, result = -1, is_anchored = info->brackets[0].ptr[0] == '^'; - - for (i = 0; i <= s_len; i++) { - result = doh(s + i, s_len - i, info, 0); - if (result >= 0) { - result += i; - break; - } - if (is_anchored) break; - } - - return result; -} - -static void setup_branch_points(struct regex_info *info) { - int i, j; - struct branch tmp; - - /* First, sort branches. Must be stable, no qsort. Use bubble algo. */ - for (i = 0; i < info->num_branches; i++) { - for (j = i + 1; j < info->num_branches; j++) { - if (info->branches[i].bracket_index > info->branches[j].bracket_index) { - tmp = info->branches[i]; - info->branches[i] = info->branches[j]; - info->branches[j] = tmp; - } - } - } - - /* - * For each bracket, set their branch points. This way, for every bracket - * (i.e. every chunk of regex) we know all branch points before matching. - */ - for (i = j = 0; i < info->num_brackets; i++) { - info->brackets[i].num_branches = 0; - info->brackets[i].branches = j; - while (j < info->num_branches && info->branches[j].bracket_index == i) { - info->brackets[i].num_branches++; - j++; - } - } -} - -static int foo(const char *re, int re_len, const char *s, int s_len, - struct regex_info *info) { - int i, step, depth = 0; - - /* First bracket captures everything */ - info->brackets[0].ptr = re; - info->brackets[0].len = re_len; - info->num_brackets = 1; - - /* Make a single pass over regex string, memorize brackets and branches */ - for (i = 0; i < re_len; i += step) { - step = get_op_len(re + i, re_len - i); - - if (re[i] == '|') { - FAIL_IF(info->num_branches >= (int) ARRAY_SIZE(info->branches), - SLRE_TOO_MANY_BRANCHES); - info->branches[info->num_branches].bracket_index = - info->brackets[info->num_brackets - 1].len == -1 ? - info->num_brackets - 1 : depth; - info->branches[info->num_branches].schlong = &re[i]; - info->num_branches++; - } else if (re[i] == '\\') { - FAIL_IF(i >= re_len - 1, SLRE_INVALID_METACHARACTER); - if (re[i + 1] == 'x') { - /* Hex digit specification must follow */ - FAIL_IF(re[i + 1] == 'x' && i >= re_len - 3, - SLRE_INVALID_METACHARACTER); - FAIL_IF(re[i + 1] == 'x' && !(isxdigit(re[i + 2]) && - isxdigit(re[i + 3])), SLRE_INVALID_METACHARACTER); - } else { - FAIL_IF(!is_metacharacter((unsigned char *) re + i + 1), - SLRE_INVALID_METACHARACTER); - } - } else if (re[i] == '(') { - FAIL_IF(info->num_brackets >= (int) ARRAY_SIZE(info->brackets), - SLRE_TOO_MANY_BRACKETS); - depth++; /* Order is important here. Depth increments first. */ - info->brackets[info->num_brackets].ptr = re + i + 1; - info->brackets[info->num_brackets].len = -1; - info->num_brackets++; - FAIL_IF(info->num_caps > 0 && info->num_brackets - 1 > info->num_caps, - SLRE_CAPS_ARRAY_TOO_SMALL); - } else if (re[i] == ')') { - int ind = info->brackets[info->num_brackets - 1].len == -1 ? - info->num_brackets - 1 : depth; - info->brackets[ind].len = (int) (&re[i] - info->brackets[ind].ptr); - DBG(("SETTING BRACKET %d [%.*s]\n", - ind, info->brackets[ind].len, info->brackets[ind].ptr)); - depth--; - FAIL_IF(depth < 0, SLRE_UNBALANCED_BRACKETS); - FAIL_IF(i > 0 && re[i - 1] == '(', SLRE_NO_MATCH); - } - } - - FAIL_IF(depth != 0, SLRE_UNBALANCED_BRACKETS); - setup_branch_points(info); - - return baz(s, s_len, info); -} - -int slre_match(const char *regexp, const char *s, int s_len, - struct slre_cap *caps, int num_caps, int flags) { - struct regex_info info; - - /* Initialize info structure */ - info.flags = flags; - info.num_brackets = info.num_branches = 0; - info.num_caps = num_caps; - info.caps = caps; - - DBG(("========================> [%s] [%.*s]\n", regexp, s_len, s)); - return foo(regexp, (int) strlen(regexp), s, s_len, &info); -}
D vendor/slre.h

@@ -1,60 +0,0 @@

-/* - * Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com> - * Copyright (c) 2013 Cesanta Software Limited - * All rights reserved - * - * This library is dual-licensed: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. For the terms of this - * license, see <http://www.gnu.org/licenses/>. - * - * You are free to use this library under the terms of the GNU General - * Public License, but WITHOUT ANY WARRANTY; without even the implied - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * Alternatively, you can license this library under a commercial - * license, as set out in <http://cesanta.com/products.html>. - */ - -/* - * This is a regular expression library that implements a subset of Perl RE. - * Please refer to README.md for a detailed reference. - */ - -#ifndef SLRE_HEADER_DEFINED -#define SLRE_HEADER_DEFINED - -#ifdef __cplusplus -extern "C" { -#endif - -struct slre_cap { - const char *ptr; - int len; -}; - - -int slre_match(const char *regexp, const char *buf, int buf_len, - struct slre_cap *caps, int num_caps, int flags); - -/* Possible flags for slre_match() */ -enum { SLRE_IGNORE_CASE = 1 }; - - -/* slre_match() failure codes */ -#define SLRE_NO_MATCH -1 -#define SLRE_UNEXPECTED_QUANTIFIER -2 -#define SLRE_UNBALANCED_BRACKETS -3 -#define SLRE_INTERNAL_ERROR -4 -#define SLRE_INVALID_CHARACTER_SET -5 -#define SLRE_INVALID_METACHARACTER -6 -#define SLRE_CAPS_ARRAY_TOO_SMALL -7 -#define SLRE_TOO_MANY_BRANCHES -8 -#define SLRE_TOO_MANY_BRACKETS -9 - -#ifdef __cplusplus -} -#endif - -#endif /* SLRE_HEADER_DEFINED */