2022-02-16 19:10:41 +03:00
|
|
|
include "internal";
|
|
|
|
include "options";
|
|
|
|
include "binary";
|
2022-07-16 19:39:57 +03:00
|
|
|
include "decode";
|
2021-09-22 22:08:36 +03:00
|
|
|
|
2021-10-30 02:17:14 +03:00
|
|
|
def intdiv(a; b): _intdiv(a; b);
|
2021-09-20 18:05:24 +03:00
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
def trim: capture("^\\s*(?<str>.*?)\\s*$"; "").str;
|
|
|
|
|
|
|
|
# does +1 and [:1] as " "*0 is null
|
|
|
|
def rpad($s; $w): . + ($s * ($w+1-length))[1:];
|
|
|
|
|
2022-06-21 16:18:12 +03:00
|
|
|
# add missing group/0 function
|
|
|
|
# https://github.com/stedolan/jq/issues/2444
|
|
|
|
def group: group_by(.);
|
|
|
|
|
2021-09-04 02:43:56 +03:00
|
|
|
# like group but groups streaks based on condition
|
|
|
|
def streaks_by(f):
|
2021-08-18 17:49:56 +03:00
|
|
|
( . as $a
|
|
|
|
| length as $l
|
|
|
|
| if $l == 0 then []
|
|
|
|
else
|
|
|
|
( [ foreach $a[] as $v (
|
|
|
|
{cf: ($a[0] | f), index: 0, start: 0, extract: null};
|
|
|
|
( ($v | f) as $vf
|
|
|
|
| (.index == 0 or (.cf == $vf)) as $equal
|
|
|
|
| if $equal then
|
|
|
|
( .extract = null
|
|
|
|
)
|
|
|
|
else
|
|
|
|
( .cf = $vf
|
|
|
|
| .extract = [.start, .index]
|
|
|
|
| .start = .index
|
|
|
|
)
|
|
|
|
end
|
|
|
|
| .index += 1
|
|
|
|
);
|
|
|
|
( if .extract then .extract else empty end
|
|
|
|
, if .index == $l then [.start, .index] else empty end
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
| map($a[.[0]:.[1]])
|
|
|
|
)
|
|
|
|
end
|
|
|
|
);
|
2021-08-19 19:08:33 +03:00
|
|
|
# [1, 2, 2, 3] => [[1], [2, 2], [3]]
|
2021-09-04 02:43:56 +03:00
|
|
|
def streaks: streaks_by(.);
|
2021-08-18 17:49:56 +03:00
|
|
|
|
2021-09-04 02:43:56 +03:00
|
|
|
# same as group_by but counts, array or pairs with [value, count]
|
2021-08-18 17:49:56 +03:00
|
|
|
def count_by(exp):
|
|
|
|
group_by(exp) | map([(.[0] | exp), length]);
|
2021-08-27 15:47:09 +03:00
|
|
|
def count: count_by(.);
|
2021-08-18 17:49:56 +03:00
|
|
|
|
2021-08-27 16:04:07 +03:00
|
|
|
# array of result of applying f on all consecutive pairs
|
|
|
|
def delta_by(f):
|
|
|
|
( . as $a
|
|
|
|
| if length < 1 then []
|
|
|
|
else
|
|
|
|
[ range(length-1) as $i
|
|
|
|
| {a: $a[$i], b: $a[$i+1]}
|
|
|
|
| f
|
|
|
|
]
|
|
|
|
end
|
|
|
|
);
|
|
|
|
# array of minus between all consecutive pairs
|
|
|
|
def delta: delta_by(.b - .a);
|
|
|
|
|
2021-09-04 12:38:49 +03:00
|
|
|
# split array or string into even chunks, except maybe the last
|
|
|
|
def chunk($size):
|
|
|
|
if length == 0 then []
|
|
|
|
else
|
|
|
|
[ ( range(
|
|
|
|
( (length / $size)
|
|
|
|
| ceil
|
|
|
|
| if . == 0 then 1 end
|
|
|
|
)
|
|
|
|
) as $i
|
|
|
|
| .[$i * $size:($i + 1) * $size]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
end;
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
# [{a: 123, ...}, ...]
|
|
|
|
# colmap maps something into [col, ...]
|
|
|
|
# render maps [{column: 0, string: "coltext", maxwidth: 12}, ..] into a row
|
|
|
|
def table(colmap; render):
|
|
|
|
def _column_widths:
|
|
|
|
[ . as $rs
|
|
|
|
| range($rs[0] | length) as $i
|
|
|
|
| [$rs[] | colmap | (.[$i] | length)]
|
|
|
|
| max
|
|
|
|
];
|
|
|
|
if length == 0 then ""
|
|
|
|
else
|
|
|
|
( _column_widths as $cw
|
|
|
|
| . as $rs
|
|
|
|
| ( $rs[]
|
|
|
|
| . as $r
|
|
|
|
| [ range($r | length) as $i
|
|
|
|
| ($r | colmap | {column: $i, string: .[$i], maxwidth: $cw[$i]})
|
|
|
|
]
|
|
|
|
| render
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end;
|
|
|
|
|
2021-10-22 12:01:27 +03:00
|
|
|
# TODO: rename keys and add more, ascii/utf8/utf16/codepoint name?, le/be, signed/unsigned?
|
2022-07-16 19:39:57 +03:00
|
|
|
# TODO: move?
|
2021-08-28 21:07:57 +03:00
|
|
|
def iprint:
|
2020-06-08 03:29:51 +03:00
|
|
|
{
|
2022-12-21 15:59:54 +03:00
|
|
|
bin: "0b\(to_radix(2))",
|
|
|
|
oct: "0o\(to_radix(8))",
|
2020-06-08 03:29:51 +03:00
|
|
|
dec: "\(.)",
|
2022-12-21 15:59:54 +03:00
|
|
|
hex: "0x\(to_radix(16))",
|
2020-06-08 03:29:51 +03:00
|
|
|
str: (try ([.] | implode) catch null),
|
|
|
|
};
|
|
|
|
|
|
|
|
# produce a/b pairs for diffing values
|
|
|
|
def diff($a; $b):
|
|
|
|
( ( $a | type) as $at
|
|
|
|
| ( $b | type) as $bt
|
|
|
|
| if $at != $bt then {a: $a, b: $b}
|
|
|
|
elif ($at == "array" or $at == "object") then
|
|
|
|
( [ ((($a | keys) + ($b | keys)) | unique)[] as $k
|
|
|
|
| {
|
|
|
|
($k | tostring):
|
|
|
|
( [($a | has($k)), ($b | has($k))]
|
|
|
|
| if . == [true, true] then diff($a[$k]; $b[$k])
|
|
|
|
elif . == [true, false] then {a: $a[$k]}
|
|
|
|
elif . == [false, true] then {b: $b[$k]}
|
|
|
|
else empty # TODO: can't happen? error?
|
|
|
|
end
|
|
|
|
)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
| add
|
|
|
|
| if . == null then empty end
|
|
|
|
)
|
|
|
|
else
|
|
|
|
if $a == $b then empty else {a: $a, b: $b} end
|
|
|
|
end
|
|
|
|
);
|
2022-01-13 20:34:59 +03:00
|
|
|
|
2022-02-10 20:46:23 +03:00
|
|
|
def paste:
|
|
|
|
if _is_completing | not then
|
|
|
|
( [ _repeat_break(
|
|
|
|
try _stdin(64*1024)
|
|
|
|
catch if . == "eof" then error("break") end
|
|
|
|
)
|
|
|
|
]
|
|
|
|
| join("")
|
|
|
|
)
|
|
|
|
end;
|
2021-12-09 19:15:21 +03:00
|
|
|
|
|
|
|
def expr_to_path: _expr_to_path;
|
|
|
|
def path_to_expr: _path_to_expr;
|
|
|
|
|
|
|
|
def torepr:
|
|
|
|
( format as $f
|
|
|
|
| if $f == null then error("value is not a format root") end
|
|
|
|
| _format_func($f; "torepr")
|
|
|
|
);
|