unpack method

String? unpack()

change code to value

Implementation

String? unpack() {
  try {
    /// pattern
    var exp = RegExp(
      "\\}\\s*\\('(.*)',\\s*(.*?),\\s*(\\d+),\\s*'(.*?)'\\.split\\('\\|'\\)",
      dotAll: true,
    );

    /// get value from elementAt 0
    var matches = exp.allMatches(packedJS).elementAt(0);

    /// if group count is 4
    if (matches.groupCount == 4) {
      /// get value with group
      var payload = matches.group(1)!.replaceAll("\\'", "\'");
      final radixStr = matches.group(2);
      final countStr = matches.group(3);
      final sym = matches.group(4)!.split('\|');

      /// initial value
      int radix;
      int count;

      /// set radix value
      try {
        radix = int.parse(radixStr!);
      } catch (_) {
        radix = 36;
      }

      /// set count value
      try {
        count = int.parse(countStr!);
      } catch (_) {
        count = 0;
      }

      /// error condition
      if (sym.length != count) {
        throw Exception('Unknown p.a.c.k.e.r. encoding');
      }

      /// call UnBase class
      final unBase = UnBase(radix);

      /// Pattern
      exp = RegExp('\\b\\w+\\b');

      /// get value from elementAt 0
      matches = exp.allMatches(payload).elementAt(0);

      /// initial value
      var replaceOffset = 0;

      /// foreach looping
      exp.allMatches(payload).forEach((element) {
        /// get word from group 0
        final word = element.group(0);

        var value = '';

        /// change code to value
        final x = unBase.unBase(word!);

        /// set value
        if (x < sym.length) {
          value = sym[x];
        }

        if (value.isNotEmpty) {
          payload = payload.replaceRange(element.start + replaceOffset,
              element.end + replaceOffset, value);
          replaceOffset += value.length - word.length;
        }
      });

      /// return result
      return payload;
    }
    return null;
  } catch (_) {
    return null;
  }
}