forked from livegrep/livegrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre_width.cc
72 lines (61 loc) · 1.88 KB
/
re_width.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/********************************************************************
* livegrep -- re_width.cc
* Copyright (c) 2011-2013 Nelson Elhage
*
* This program is free software. You may use, redistribute, and/or
* modify it under the terms listed in the COPYING file.
********************************************************************/
#include "re_width.h"
using namespace re2;
int WidthWalker::ShortVisit(Regexp *re, int parent_arg) {
return 0;
}
int WidthWalker::PostVisit(Regexp *re, int parent_arg,
int pre_arg,
int *child_args, int nchild_args) {
int width;
switch (re->op()) {
case kRegexpRepeat:
width = child_args[0] * re->max();
break;
case kRegexpNoMatch:
// These ops match the empty string:
case kRegexpEmptyMatch: // anywhere
case kRegexpBeginLine: // at beginning of line
case kRegexpEndLine: // at end of line
case kRegexpBeginText: // at beginning of text
case kRegexpEndText: // at end of text
case kRegexpWordBoundary: // at word boundary
case kRegexpNoWordBoundary: // not at word boundary
width = 0;
break;
case kRegexpLiteral:
case kRegexpAnyChar:
case kRegexpAnyByte:
case kRegexpCharClass:
width = 1;
break;
case kRegexpLiteralString:
width = re->nrunes();
break;
case kRegexpConcat:
width = 0;
for (int i = 0; i < nchild_args; i++)
width += child_args[i];
break;
case kRegexpAlternate:
width = 0;
for (int i = 0; i < nchild_args; i++)
width = std::max(width, child_args[i]);
break;
case kRegexpStar:
case kRegexpPlus:
case kRegexpQuest:
case kRegexpCapture:
width = child_args[0];
break;
default:
abort();
}
return width;
}