substring property

DartBlockNativeFunction substring
final

Implementation

static final substring = DartBlockNativeFunction(
  name: 'substring',
  returnType: DartBlockDataType.stringType,
  parameters: [
    DartBlockVariableDefinition('text', DartBlockDataType.stringType),
    DartBlockVariableDefinition('start', DartBlockDataType.integerType),
    DartBlockVariableDefinition('end', DartBlockDataType.integerType),
  ],
  implementation: (arbiter, args) {
    final text = args[0].getValue(arbiter) as String;
    final start = args[1].getValue(arbiter) as int;
    final end = args[2].getValue(arbiter) as int;
    if (start < 0 || end < 0) {
      throw DartBlockException(
        title: 'Invalid Indices',
        message: 'Start and end indices cannot be negative.',
      );
    }
    if (start > end || end > text.length) {
      throw DartBlockException(
        title: 'Invalid Indices',
        message:
            'Start index cannot be greater than end index, and end index cannot exceed text length.',
      );
    }
    return DartBlockConcatenationValue.fromConstant(
      text.substring(start, end),
    );
  },
  category: DartBlockNativeFunctionCategory.string,
  type: DartBlockNativeFunctionType.substring,
  description:
      'Get a substring of the text, from start index (inclusive) to end index (exclusive).',
);