Skip to content

Commit

Permalink
Implement splits and sub.
Browse files Browse the repository at this point in the history
  • Loading branch information
01mf02 committed Dec 28, 2022
1 parent 4760e7c commit 491b332
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion jaq-std/src/std.jq
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,38 @@ def flatten(d): d as $d |
];

# Regular expressions
def capture_of_match: map(select(.name) | {(.name): .string}) | add;

def test(re; flags): captures(re; flags) | length > 0;
def scan(re; flags): captures(re; flags) | .[] | .[0].string;
def match(re; flags): captures(re; flags) | .[] | .[0] + {captures: .[1:]};
def capture(re; flags): captures(re; flags) | .[] | map(select(.name) | {(.name): .string}) | add;
def capture(re; flags): captures(re; flags) | .[] | capture_of_match;

def splits(re; flags): . as $s |
foreach captures(re; "g" + flags)[][0], null as $m
( { i: 0 };
if $m == null
then { s: $s[.i:] }
else { s: $s[.i:$m.offset], i: $m.offset + $m.length }
end
) | .s;
def sub(re; f; flags): . as $s |
reduce captures(re; flags)[], null as $m
( { i: 0, s: "" };
if $m == null
then .s += $s[.i:]
else .s += $s[.i:$m[0].offset] + ($m | capture_of_match | f) |
.i = ($m[0] | .offset + .length)
end
) | .s;

def test(re): test(re; "");
def scan(re): scan(re; "");
def match(re): match(re; "");
def capture(re): capture(re; "");
def splits(re): splits(re; "");
def sub(re; f): sub(re; f; "");
def gsub(re; f): sub(re; f; "g");

# I/O
def input: first(inputs);

0 comments on commit 491b332

Please sign in to comment.