Skip to content

Commit

Permalink
add substring method for Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
maks committed Jun 9, 2022
1 parent bde4006 commit c58466b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/src/eval/compiler/builtins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ final Map<TypeRef, Map<String, KnownMethod>> knownMethods = {
AlwaysReturnType(EvalTypes.boolType, false), [KnownMethodArg('other', EvalTypes.stringType, false, false)], {}),
'toLowerCase': KnownMethod(AlwaysReturnType(EvalTypes.stringType, false), [], {}),
'toUpperCase': KnownMethod(AlwaysReturnType(EvalTypes.stringType, false), [], {}),
'substring': KnownMethod(AlwaysReturnType(EvalTypes.stringType, false), [
KnownMethodArg('start', EvalTypes.intType, false, false),
KnownMethodArg('end', EvalTypes.intType, true, true)
], {}),
..._knownObject
},
EvalTypes.iterableType: {
Expand Down
11 changes: 11 additions & 0 deletions lib/src/eval/shared/stdlib/core/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class $String implements $Instance {
return __toLowerCase;
case 'toUpperCase':
return __toUpperCase;
case 'substring':
return __substring;
}

return _superclass.$getProperty(runtime, identifier);
Expand Down Expand Up @@ -174,6 +176,15 @@ class $String implements $Instance {
return $String((target!.$value as String).toUpperCase());
}

static const $Function __substring = $Function(_substring);

static $Value? _substring(final Runtime runtime, final $Value? target, final List<$Value?> args) {
target as $String;
final start = args[0] as $int;
final end = args[1] as $int;
return $String(target.$value.substring(start.$value, end.$value));
}

@override
String get $reified => $value;

Expand Down
45 changes: 45 additions & 0 deletions test/dart_eval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,51 @@ void main() {
runtime.executeLib('package:example/main.dart', 'main', [56890]);
}, prints('56890\n'));
});

test('String has length getter', () {
final exec = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
int main() {
String cat = "Fluffy";
return cat.length;
}
''',
}
});
expect(exec.executeLib('package:example/main.dart', 'main'), 6);
});

test('String has isEmpty getter', () {
final exec = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
int main() {
String cat = "Fluffy";
if (cat.isNotEmpty) return 1;
}
''',
}
});
expect(exec.executeLib('package:example/main.dart', 'main'), 1);
});

test('String has substring method', () {
final exec = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
int main() {
String cat = "Fluffy";
String sub = cat.substring(0,3);
print(sub);
}
''',
}
});
expect(() {
exec.executeLib('package:example/main.dart', 'main');
}, prints('Flu\n'));
});
});

group('Bridge tests', () {
Expand Down

0 comments on commit c58466b

Please sign in to comment.