Prepare V2 corpus, editorial journal and reproducible publication boundary
This commit is contained in:
1 parent
7b3f49788d
commit
69607c52fd
498 files changed
+97671
-468
No files matched your search
@@ -0,0 +1,84 @@
|
||||
module preg;
|
||||
|
||||
import std.regex : matchAll, regex, ctRegex;
|
||||
|
||||
struct Capture {
|
||||
string text;
|
||||
long position; // byte offset, -1 when an optional group did not participate
|
||||
}
|
||||
|
||||
class PReg {
|
||||
static string[][] matchAllPatternOrder(R)(string subject, R re, out size_t count) {
|
||||
string[][] result;
|
||||
count = 0;
|
||||
foreach (hit; matchAll(subject, re)) {
|
||||
if (result.length == 0) result.length = hit.length;
|
||||
foreach (i; 0 .. hit.length) result[i] ~= hit[i];
|
||||
++count;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static string[][] matchAllSetOrder(R)(string subject, R re, out size_t count) {
|
||||
string[][] result;
|
||||
count = 0;
|
||||
foreach (hit; matchAll(subject, re)) {
|
||||
string[] row;
|
||||
foreach (i; 0 .. hit.length) row ~= hit[i];
|
||||
result ~= row;
|
||||
++count;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static Capture[][] matchAllOffsetCapture(R)(string subject, R re, out size_t count) {
|
||||
Capture[][] result;
|
||||
count = 0;
|
||||
foreach (hit; matchAll(subject, re)) {
|
||||
if (result.length == 0) result.length = hit.length;
|
||||
foreach (i; 0 .. hit.length) {
|
||||
auto part = hit[i];
|
||||
long position = part.ptr is null
|
||||
? -1
|
||||
: cast(long)(part.ptr - subject.ptr);
|
||||
result[i] ~= Capture(part, position);
|
||||
}
|
||||
++count;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
unittest {
|
||||
size_t count;
|
||||
auto pattern = regex(`(\w)(\w)\w+`);
|
||||
|
||||
auto byGroup = PReg.matchAllPatternOrder("one two", pattern, count);
|
||||
assert(count == 2);
|
||||
assert(byGroup == [["one", "two"], ["o", "t"], ["n", "w"]]);
|
||||
|
||||
auto byMatch = PReg.matchAllSetOrder("one two", pattern, count);
|
||||
assert(count == 2);
|
||||
assert(byMatch == [["one", "o", "n"], ["two", "t", "w"]]);
|
||||
|
||||
auto offsets = PReg.matchAllOffsetCapture("one two", ctRegex!(`(\w)(\w)\w+`), count);
|
||||
assert(count == 2);
|
||||
assert(offsets[0][0] == Capture("one", 0));
|
||||
assert(offsets[0][1] == Capture("two", 4));
|
||||
assert(offsets[1][1] == Capture("t", 4));
|
||||
assert(offsets[2][1] == Capture("w", 5));
|
||||
|
||||
assert(PReg.matchAllPatternOrder("---", pattern, count).length == 0);
|
||||
assert(count == 0);
|
||||
assert(PReg.matchAllSetOrder("", pattern, count).length == 0);
|
||||
assert(count == 0);
|
||||
assert(PReg.matchAllOffsetCapture("---", pattern, count).length == 0);
|
||||
assert(count == 0);
|
||||
|
||||
auto moreGroups = PReg.matchAllPatternOrder("abc def", regex(`(\w)(\w)(\w)`), count);
|
||||
assert(moreGroups == [["abc", "def"], ["a", "d"], ["b", "e"], ["c", "f"]]);
|
||||
|
||||
auto unicodeOffsets = PReg.matchAllOffsetCapture("я x", regex(`x`), count);
|
||||
assert(count == 1);
|
||||
assert(unicodeOffsets[0][0].position == 3); // UTF-8 bytes, not character index
|
||||
}
|
||||
Reference in new issue
Block a user