Skip to content

Commit 11d8208

Browse files
mkustermannCommit Queue
authored and
Commit Queue
committed
Use utf8.encode() instead of longer const Utf8Encoder.convert()
The change in [0] has propagated now everywhere, so we can use `utf8.encode()` instead of the longer `const Utf8Encoder.convert()`. As the checked-in SDK has been rolled to include [0] we can now rely on the better return type. [0] #52801 TEST=ci CoreLibraryReviewExempt: Minor cleanup. Change-Id: I2c0144023e03b2c265582d83a7fb9469b02f1570 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/313563 Reviewed-by: Lasse Nielsen <[email protected]> Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Ben Konyi <[email protected]>
1 parent 87aba00 commit 11d8208

File tree

16 files changed

+42
-46
lines changed

16 files changed

+42
-46
lines changed

pkg/analysis_server/test/integration/support/integration_tests.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ class Server {
598598
_pendingCommands[id] = completer;
599599
var line = json.encode(command);
600600
_recordStdio('==> $line');
601-
_process.stdin.add(utf8.encoder.convert('$line\n'));
601+
_process.stdin.add(utf8.encode('$line\n'));
602602
return completer.future;
603603
}
604604

pkg/analysis_server/test/stress/utilities/server.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ class Server {
803803
command['params'] = params;
804804
}
805805
var line = json.encode(command);
806-
_process!.stdin.add(utf8.encoder.convert('$line\n'));
806+
_process!.stdin.add(utf8.encode('$line\n'));
807807
logger?.log(fromClient, line);
808808
return requestData;
809809
}

pkg/analysis_server_client/lib/src/server_base.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ abstract class ServerBase {
199199
_pendingCommands[id] = completer;
200200
var line = json.encode(command);
201201
listener?.requestSent(line);
202-
sendWith(utf8.encoder.convert('$line\n'));
202+
sendWith(utf8.encode('$line\n'));
203203
return completer.future;
204204
}
205205

pkg/analysis_server_client/test/server_test.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void main() {
7575
// No 'id', so not a response.
7676
// No 'event', so not a notification.
7777
process.stdout = Stream.value(
78-
utf8.encoder.convert(json.encode({'foo': 'bar'})),
78+
utf8.encode(json.encode({'foo': 'bar'})),
7979
);
8080
process.stderr = _noMessage();
8181

@@ -87,7 +87,7 @@ void main() {
8787

8888
test('unexpected notification format', () async {
8989
process.stdout = Stream.value(
90-
utf8.encoder.convert(json.encode({'event': 'foo', 'noParams': '42'})),
90+
utf8.encode(json.encode({'event': 'foo', 'noParams': '42'})),
9191
);
9292
process.stderr = _noMessage();
9393

@@ -100,7 +100,7 @@ void main() {
100100
test('unexpected response', () async {
101101
// We have no asked anything, but got a response.
102102
process.stdout = Stream.value(
103-
utf8.encoder.convert(json.encode({'id': '0'})),
103+
utf8.encode(json.encode({'id': '0'})),
104104
);
105105
process.stderr = _noMessage();
106106

@@ -114,7 +114,7 @@ void main() {
114114
// We expect that the first request has id `0`.
115115
// The response is invalid - the "result" field is not an object.
116116
process.stdout = Stream.value(
117-
utf8.encoder.convert(json.encode({'id': '0', 'result': '42'})),
117+
utf8.encode(json.encode({'id': '0', 'result': '42'})),
118118
);
119119
process.stderr = _noMessage();
120120

@@ -135,7 +135,7 @@ void main() {
135135
// ignore: unawaited_futures
136136
process.mockin.controller.stream.first.then((_) {
137137
var encoded = json.encode({'id': '0'});
138-
mockout.add(utf8.encoder.convert('$encoded\n'));
138+
mockout.add(utf8.encode('$encoded\n'));
139139
});
140140
process.exitCode = Future.value(0);
141141

@@ -173,34 +173,34 @@ final _badErrorMessage = {
173173
};
174174

175175
Stream<List<int>> _badMessage() async* {
176-
yield utf8.encoder.convert('The Dart VM service is listening on foo bar\n');
176+
yield utf8.encode('The Dart VM service is listening on foo bar\n');
177177
final sampleJson = {
178178
'id': '0',
179179
'error': _badErrorMessage,
180180
};
181-
yield utf8.encoder.convert(json.encode(sampleJson));
181+
yield utf8.encode(json.encode(sampleJson));
182182
}
183183

184184
Stream<List<int>> _eventMessage() async* {
185-
yield utf8.encoder.convert('The Dart VM service is listening on foo bar\n');
185+
yield utf8.encode('The Dart VM service is listening on foo bar\n');
186186
final sampleJson = {
187187
'event': 'fooEvent',
188188
'params': {'foo': 'bar', 'baz': 'bang'}
189189
};
190-
yield utf8.encoder.convert(json.encode(sampleJson));
190+
yield utf8.encode(json.encode(sampleJson));
191191
}
192192

193193
Stream<List<int>> _goodMessage() async* {
194-
yield utf8.encoder.convert('The Dart VM service is listening on foo bar\n');
194+
yield utf8.encode('The Dart VM service is listening on foo bar\n');
195195
final sampleJson = {
196196
'id': '0',
197197
'result': {'foo': 'bar'}
198198
};
199-
yield utf8.encoder.convert(json.encode(sampleJson));
199+
yield utf8.encode(json.encode(sampleJson));
200200
}
201201

202202
Stream<List<int>> _noMessage() async* {
203-
yield utf8.encoder.convert('');
203+
yield utf8.encode('');
204204
}
205205

206206
class MockProcess implements Process {

pkg/analyzer_plugin/test/integration/support/integration_tests.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ class Server {
514514
_pendingCommands[id] = completer;
515515
var line = json.encode(command);
516516
_recordStdio('SEND: $line');
517-
_process!.stdin.add(utf8.encoder.convert('$line\n'));
517+
_process!.stdin.add(utf8.encode('$line\n'));
518518
return completer.future;
519519
}
520520

pkg/front_end/lib/src/api_prototype/memory_file_system.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class MemoryFileSystemEntity implements FileSystemEntity {
131131
/// If no file exists, one is created. If a file exists already, it is
132132
/// overwritten.
133133
void writeAsStringSync(String s) {
134-
_update(uri, const Utf8Encoder().convert(s));
134+
_update(uri, utf8.encode(s));
135135
}
136136

137137
void _update(Uri uri, Uint8List data) {

pkg/front_end/lib/src/fasta/source/source_loader.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
library fasta.source_loader;
66

77
import 'dart:collection' show Queue;
8-
import 'dart:convert' show Utf8Encoder;
8+
import 'dart:convert' show utf8;
99
import 'dart:typed_data' show Uint8List;
1010

1111
import 'package:_fe_analyzer_shared/src/parser/class_member_parser.dart'
@@ -960,7 +960,7 @@ severity: $severity
960960
}
961961

962962
Uint8List synthesizeSourceForMissingFile(Uri uri, Message? message) {
963-
return const Utf8Encoder().convert(switch ("$uri") {
963+
return utf8.encode(switch ("$uri") {
964964
"dart:core" => defaultDartCoreSource,
965965
"dart:async" => defaultDartAsyncSource,
966966
"dart:collection" => defaultDartCollectionSource,

pkg/front_end/test/fasta/messages_suite.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,7 @@ class Script {
872872
preamble = '';
873873
sourceWithoutPreamble = source;
874874
}
875-
return new Script(new Uint8List.fromList(utf8.encode(source)), preamble,
876-
sourceWithoutPreamble);
875+
return new Script(utf8.encode(source), preamble, sourceWithoutPreamble);
877876
}
878877
}
879878

pkg/front_end/test/fasta/testing/suite.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
library fasta.testing.suite;
66

7-
import 'dart:convert' show jsonDecode, utf8, Utf8Encoder;
7+
import 'dart:convert' show jsonDecode, utf8;
88
import 'dart:io' show Directory, File, Platform;
99
import 'dart:typed_data' show Uint8List;
1010

@@ -1528,7 +1528,7 @@ class FuzzCompiles
15281528
for (FuzzAstVisitorSorterChunk chunk in fuzzAstVisitorSorter.chunks) {
15291529
sb.writeln(chunk.getSource());
15301530
}
1531-
Uint8List sortedData = const Utf8Encoder().convert(sb.toString());
1531+
Uint8List sortedData = utf8.encode(sb.toString());
15321532
fs.data[uri] = sortedData;
15331533
incrementalCompiler = new IncrementalCompiler.fromComponent(
15341534
new CompilerContext(compilationSetup.options), platform);
@@ -1695,8 +1695,7 @@ class FuzzCompiles
16951695
sb.writeln(headerSb.toString());
16961696
sb.writeln("import '${uri.pathSegments.last}';");
16971697
sb.writeln(chunk.getSource());
1698-
fs.data[getUriForChunk(currentSubFile)] =
1699-
const Utf8Encoder().convert(sb.toString());
1698+
fs.data[getUriForChunk(currentSubFile)] = utf8.encode(sb.toString());
17001699
print(" => Split into ${getUriForChunk(currentSubFile)}:\n"
17011700
"${sb.toString()}\n-------------\n");
17021701
currentSubFile++;
@@ -1711,7 +1710,7 @@ class FuzzCompiles
17111710
}
17121711
sb.writeln(orgFileOnlyHeaderSb.toString());
17131712
print(" => Main file becomes:\n${sb.toString()}\n-------------\n");
1714-
fs.data[uri] = const Utf8Encoder().convert(sb.toString());
1713+
fs.data[uri] = utf8.encode(sb.toString());
17151714
}
17161715

17171716
Result<ComponentResult>? passResult = await performFileInvalidation(

pkg/front_end/test/scanner_fasta_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void main() {
3030
}
3131

3232
Uint8List encodeAsNullTerminatedUtf8(String source) {
33-
final sourceBytes = const Utf8Encoder().convert(source);
33+
final sourceBytes = utf8.encode(source);
3434
return Uint8List(sourceBytes.length + 1)
3535
..setRange(0, sourceBytes.length, sourceBytes);
3636
}

pkg/kernel/test/load_concat_dill_keeps_source_test.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ void main() {
2727

2828
Component component = new Component(libraries: [library1, library2])
2929
..setMainMethodAndMode(null, false, NonNullableByDefaultCompiledMode.Weak);
30-
component.uriToSource[uri1] = new Source(
31-
[42, 2 * 42], const Utf8Encoder().convert("source #1"), uri1, uri1);
32-
component.uriToSource[uri2] = new Source(
33-
[43, 3 * 43], const Utf8Encoder().convert("source #2"), uri1, uri1);
30+
component.uriToSource[uri1] =
31+
new Source([42, 2 * 42], utf8.encode("source #1"), uri1, uri1);
32+
component.uriToSource[uri2] =
33+
new Source([43, 3 * 43], utf8.encode("source #2"), uri1, uri1);
3434
expectSource(serialize(component), true, true);
3535

3636
Component cPartial1 = new Component(nameRoot: component.root)
3737
..setMainMethodAndMode(null, false, NonNullableByDefaultCompiledMode.Weak)
3838
..libraries.add(library1);
39-
cPartial1.uriToSource[uri1] = new Source(
40-
[42, 2 * 42], const Utf8Encoder().convert("source #1"), uri1, uri1);
39+
cPartial1.uriToSource[uri1] =
40+
new Source([42, 2 * 42], utf8.encode("source #1"), uri1, uri1);
4141
cPartial1.uriToSource[uri2] =
4242
new Source([43, 3 * 43], const <int>[], uri1, uri1);
4343
List<int> partial1Serialized = serialize(cPartial1);
@@ -48,8 +48,8 @@ void main() {
4848
..libraries.add(library2);
4949
cPartial2.uriToSource[uri1] =
5050
new Source([42, 2 * 42], const <int>[], uri1, uri1);
51-
cPartial2.uriToSource[uri2] = new Source(
52-
[43, 3 * 43], const Utf8Encoder().convert("source #2"), uri1, uri1);
51+
cPartial2.uriToSource[uri2] =
52+
new Source([43, 3 * 43], utf8.encode("source #2"), uri1, uri1);
5353
List<int> partial2Serialized = serialize(cPartial2);
5454
expectSource(partial2Serialized, false, true);
5555

pkg/vm_service/test/network_profiling_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ Future<void> socketTest() async {
7272
}
7373
});
7474
var client = await io.RawDatagramSocket.bind(localhost, 0);
75-
client.send(utf8.encoder.convert(udpContent), io.InternetAddress(localhost),
76-
server.port);
75+
client.send(
76+
utf8.encode(udpContent), io.InternetAddress(localhost), server.port);
7777
client.send([1, 2, 3], io.InternetAddress(localhost), server.port);
7878

7979
// Wait for datagram to arrive.

runtime/observatory/tests/service/network_profiling_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Future<void> socketTest() async {
4646
}
4747
});
4848
var client = await io.RawDatagramSocket.bind(localhost, 0);
49-
client.send(utf8.encoder.convert(udpContent), io.InternetAddress(localhost),
50-
server.port);
49+
client.send(
50+
utf8.encode(udpContent), io.InternetAddress(localhost), server.port);
5151
client.send([1, 2, 3], io.InternetAddress(localhost), server.port);
5252

5353
// Wait for datagram to arrive.

runtime/observatory_2/tests/service_2/network_profiling_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Future<void> socketTest() async {
4646
}
4747
});
4848
var client = await io.RawDatagramSocket.bind(localhost, 0);
49-
client.send(utf8.encoder.convert(udpContent), io.InternetAddress(localhost),
50-
server.port);
49+
client.send(
50+
utf8.encode(udpContent), io.InternetAddress(localhost), server.port);
5151
client.send([1, 2, 3], io.InternetAddress(localhost), server.port);
5252

5353
// Wait for datagram to arrive.

sdk/lib/io/file_system_entity.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ abstract class FileSystemEntity {
663663
// The native methods which determine type of the FileSystemEntity require
664664
// that the buffer provided is null terminated.
665665
static Uint8List _toUtf8Array(String s) =>
666-
_toNullTerminatedUtf8Array(utf8.encoder.convert(s));
666+
_toNullTerminatedUtf8Array(utf8.encode(s));
667667

668668
static Uint8List _toNullTerminatedUtf8Array(Uint8List l) {
669669
if (l.isEmpty || (l.isNotEmpty && l.last != 0)) {

sdk/lib/io/overrides.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,15 @@ abstract class IOOverrides {
234234
/// When this override is installed, this function overrides the behavior of
235235
/// `FileSystemEntity.type`.
236236
Future<FileSystemEntityType> fseGetType(String path, bool followLinks) {
237-
return FileSystemEntity._getTypeRequest(
238-
utf8.encoder.convert(path), followLinks);
237+
return FileSystemEntity._getTypeRequest(utf8.encode(path), followLinks);
239238
}
240239

241240
/// Returns the [FileSystemEntityType] for [path].
242241
///
243242
/// When this override is installed, this function overrides the behavior of
244243
/// `FileSystemEntity.typeSync`.
245244
FileSystemEntityType fseGetTypeSync(String path, bool followLinks) {
246-
return FileSystemEntity._getTypeSyncHelper(
247-
utf8.encoder.convert(path), followLinks);
245+
return FileSystemEntity._getTypeSyncHelper(utf8.encode(path), followLinks);
248246
}
249247

250248
// _FileSystemWatcher

0 commit comments

Comments
 (0)