git.delta.rocks / jrsonnet / refs/commits / 00fce89fa19e

difftreelog

perf make std.base64Decode[Bytes] builtin

Yaroslav Bolyukin2021-07-12parent: #747b2b7.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
114 ("encodeUTF8".into(), builtin_encode_utf8),114 ("encodeUTF8".into(), builtin_encode_utf8),
115 ("md5".into(), builtin_md5),115 ("md5".into(), builtin_md5),
116 ("base64".into(), builtin_base64),116 ("base64".into(), builtin_base64),
117 ("base64DecodeBytes".into(), builtin_base64_decode_bytes),
118 ("base64Decode".into(), builtin_base64_decode),
117 ("trace".into(), builtin_trace),119 ("trace".into(), builtin_trace),
118 ("join".into(), builtin_join),120 ("join".into(), builtin_join),
119 ("escapeStringJson".into(), builtin_escape_string_json),121 ("escapeStringJson".into(), builtin_escape_string_json),
626 })628 })
627}629}
630
631fn builtin_base64_decode_bytes(
632 context: Context,
633 _loc: Option<&ExprLocation>,
634 args: &ArgsDesc,
635) -> Result<Val> {
636 parse_args!(context, "base64DecodeBytes", args, 1, [
637 0, input: ty!(string) => Val::Str;
638 ], {
639 Ok(Val::Arr(
640 base64::decode(&input.as_bytes())
641 .map_err(|_| RuntimeError("bad base64".into()))?
642 .iter()
643 .map(|v| Val::Num(*v as f64)).collect::<Vec<_>>().into()
644 ))
645 })
646}
647
648fn builtin_base64_decode(
649 context: Context,
650 _loc: Option<&ExprLocation>,
651 args: &ArgsDesc,
652) -> Result<Val> {
653 parse_args!(context, "base64Decode", args, 1, [
654 0, input: ty!(string) => Val::Str;
655 ], {
656 Ok(Val::Str(
657 String::from_utf8(base64::decode(&input.as_bytes())
658 .map_err(|_| RuntimeError("bad base64".into()))?)
659 .map_err(|_| RuntimeError("bad utf8".into()))?.into()
660 ))
661 })
662}
628663
629fn builtin_join(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {664fn builtin_join(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
630 parse_args!(context, "join", args, 2, [665 parse_args!(context, "join", args, 2, [
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
531531
532 base64:: $intrinsic(base64),532 base64:: $intrinsic(base64),
533533
534 base64DecodeBytes(str)::534 base64DecodeBytes:: $intrinsic(base64DecodeBytes),
535 if std.length(str) % 4 != 0 then
536 error 'Not a base64 encoded string "%s"' % str
537 else
538 local aux(str, i, r) =
539 if i >= std.length(str) then
540 r
541 else
542 // all 6 bits of i, 2 MSB of i+1
543 local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];
544 // 4 LSB of i+1, 4MSB of i+2
545 local n2 =
546 if str[i + 2] == '=' then []
547 else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];
548 // 2 LSB of i+2, all 6 bits of i+3
549 local n3 =
550 if str[i + 3] == '=' then []
551 else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];
552 aux(str, i + 4, r + n1 + n2 + n3) tailstrict;
553 aux(str, 0, []),
554535
555 base64Decode(str)::536 base64Decode:: $intrinsic(base64Decode),
556 local bytes = std.base64DecodeBytes(str);
557 std.join('', std.map(function(b) std.char(b), bytes)),
558537
559 reverse:: $intrinsic(reverse),538 reverse:: $intrinsic(reverse),
560539