@@ -5,4 +5,94 @@ def tobytesrange: _tobits(8; true; 0);
55def tobits ($pad ): _tobits (1 ; false ; $pad );
66def tobytes ($pad ): _tobits (8 ; false ; $pad );
77def tobitsrange ($pad ): _tobits (1 ; true ; $pad );
8- def tobytesrange ($pad ): _tobits (8 ; true ; $pad );
8+ def tobytesrange ($pad ): _tobits (8 ; true ; $pad );
9+
10+ # same as regexp.QuoteMeta
11+ def _re_quote_meta :
12+ gsub ("(?<c>[\\ .\\ +\\ *\\ ?\\ (\\ )\\ |\\ [\\ ]\\ {\\ }\\ ^\\ $\\ )])" ; "\\ \( .c ) " );
13+
14+ # TODO:
15+ # maybe implode, join. but what would it mean?
16+ # "abc" | tobits | explode | implode would not work
17+
18+ # helper for overloading regex/string functions to support binary
19+ def _binary_or_orig (bfn ; fn ):
20+ ( _exttype as $exttype
21+ | if . == null or $exttype == "string" then fn
22+ elif $exttype == "binary" then bfn
23+ else
24+ ( . as $s
25+ | try
26+ (tobytesrange | bfn )
27+ catch ($s | fn )
28+ )
29+ end
30+ );
31+
32+ def _orig_explode : explode ;
33+ def explode : _binary_or_orig ([. [range (.size )]]; _orig_explode );
34+
35+ def _orig_splits ($val ): splits ($val );
36+ def _orig_splits ($regex ; $flags ): splits ($regex ; $flags );
37+ def _splits_binary ($regex ; $flags ):
38+ ( . as $b
39+ # last null output is to do a last iteration that output from end of last match to end of binary
40+ | foreach (_match_binary ($regex ; $flags ), null ) as $m (
41+ {prev : null , curr : null };
42+ ( .prev = .curr
43+ | .curr = $m
44+ );
45+ if .prev == null then $b [0 :.curr.offset ]
46+ elif .curr == null then $b [.prev.offset + .prev.length :]
47+ else $b [.prev.offset + .prev.length :.curr.offset + .curr.length ]
48+ end
49+ )
50+ );
51+ def splits ($val ): _binary_or_orig (_splits_binary ($val ; "g" ); _orig_splits ($val ));
52+ def splits ($regex ; $flags ): _binary_or_orig (_splits_binary ($regex ; "g" + $flags ); _orig_splits ($regex ; $flags ));
53+
54+ def _orig_split ($val ): split ($val );
55+ def _orig_split ($regex ; $flags ): split ($regex ; $flags );
56+ # split/1 splits on string not regexp
57+ def split ($val ): [splits ($val | _re_quote_meta )];
58+ def split ($regex ; $flags ): [splits ($regex ; $flags )];
59+
60+ def _orig_test ($val ): test ($val );
61+ def _orig_test ($regex ; $flags ): test ($regex ; $flags );
62+ def _test_binary ($regex ; $flags ):
63+ ( isempty (_match_binary ($regex ; $flags ))
64+ | not
65+ );
66+ def test ($val ): _binary_or_orig (_test_binary ($val ; "" ); _orig_test ($val ));
67+ def test ($regex ; $flags ): _binary_or_orig (_test_binary ($regex ; $flags ); _orig_test ($regex ; $flags ));
68+
69+ def _orig_match ($val ): match ($val );
70+ def _orig_match ($regex ; $flags ): match ($regex ; $flags );
71+ def match ($val ): _binary_or_orig (_match_binary ($val ); _orig_match ($val ));
72+ def match ($regex ; $flags ): _binary_or_orig (_match_binary ($regex ; $flags ); _orig_match ($regex ; $flags ));
73+
74+ def _orig_capture ($val ): capture ($val );
75+ def _orig_capture ($regex ; $flags ): capture ($regex ; $flags );
76+ def _capture_binary ($regex ; $flags ):
77+ ( . as $b
78+ | _match_binary ($regex ; $flags )
79+ | .captures
80+ | map (
81+ ( select (.name )
82+ | {key : .name , value : .string }
83+ )
84+ )
85+ | from_entries
86+ );
87+ def capture ($val ): _binary_or_orig (_capture_binary ($val ; "" ); _orig_capture ($val ));
88+ def capture ($regex ; $flags ): _binary_or_orig (_capture_binary ($regex ; $flags ); _orig_capture ($regex ; $flags ));
89+
90+ def _orig_scan ($val ): scan ($val );
91+ def _orig_scan ($regex ; $flags ): scan ($regex ; $flags );
92+ def _scan_binary ($regex ; $flags ):
93+ ( . as $b
94+ | _match_binary ($regex ; $flags )
95+ | $b [.offset :.offset + .length ]
96+ );
97+ def scan ($val ): _binary_or_orig (_scan_binary ($val ; "g" ); _orig_scan ($val ));
98+ def scan ($regex ; $flags ): _binary_or_orig (_scan_binary ($regex ; "g" + $flags ); _orig_scan ($regex ; $flags ));
0 commit comments