SubString constructor

SubString()

Implementation

SubString()
  : super(
      methodName: "substring",
      arity: 2,
      targetType: .string,
      function: (target, _, args) {
        final from = args[0];
        final to = args[1];

        if (from is! int || to is! int) {
          throw RuntimeError("substring() takes two integers as arguments.");
        }

        if (from < 0 ||
            to < 0 ||
            from > target.length ||
            to > target.length) {
          throw RuntimeError(
            "substring() arguments must be between 0 and the length of the string.",
          );
        }

        if (from > to) {
          throw RuntimeError(
            "substring() arguments must be in the order from, to.",
          );
        }

        return target.substring(from, to);
      },
    );