-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-annotator
executable file
·48 lines (37 loc) · 1.02 KB
/
test-annotator
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
#!/usr/bin/env perl
use strict;
use Encode 'encode';
# Use annotator protocol version 1
print(pack("C", 1));
my $regex;
my $value_match = 0;
my $boundary_match = 0;
sub invalid_usage {
print STDERR "Usage: $0 <pattern> [--value-match <group-index>] [--boundary-match <group-index>]";
exit 1;
}
while (@ARGV > 0) {
my $arg = shift;
if ($arg eq "--value-match") {
$value_match = shift;
} elsif ($arg eq "--boundary-match") {
$boundary_match = shift;
} elsif (!(defined $regex)) {
$regex = $arg;
} else {
invalid_usage
}
}
if (!(defined $regex)) {
invalid_usage
}
my $input = do { local $/; <STDIN> };
while ($input =~ /$regex/g) {
my $boundary_start = $-[$boundary_match];
my $boundary_end = $+[$boundary_match];
my $value_start = $-[$value_match];
my $value_end = $+[$value_match];
my $value = substr($input, $value_start, ($value_end - $value_start));
my $value_len = length(encode('UTF-8', $value));
print(pack("QQQa$value_len", $boundary_start, $boundary_end, $value_len, $value));
}