Dart - make ascii optimization optional in StringReader, same as in writeString() (#6758)

This commit is contained in:
Ivan Dlugos 2021-07-29 19:39:03 +02:00 committed by GitHub
parent c871df7702
commit 5235133f32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -1033,20 +1033,21 @@ abstract class Reader<T> {
/// The reader of string values.
class StringReader extends Reader<String> {
const StringReader() : super();
final bool asciiOptimization;
const StringReader({this.asciiOptimization = false}) : super();
@override
@pragma('vm:prefer-inline')
int get size => 4;
int get size => _sizeofUint32;
@override
@pragma('vm:prefer-inline')
String read(BufferContext bc, int offset) {
int strOffset = bc.derefObject(offset);
int length = bc._getUint32(strOffset);
Uint8List bytes = bc._asUint8List(strOffset + 4, length);
if (_isLatin(bytes)) {
return new String.fromCharCodes(bytes);
Uint8List bytes = bc._asUint8List(strOffset + _sizeofUint32, length);
if (asciiOptimization && _isLatin(bytes)) {
return String.fromCharCodes(bytes);
}
return utf8.decode(bytes);
}

View File

@ -379,7 +379,7 @@ class BuilderTest {
.vTableGetNullable(buf, objectOffset, indexToField(0)),
latinString);
expect(
const StringReader()
const StringReader(asciiOptimization: true)
.vTableGetNullable(buf, objectOffset, indexToField(1)),
unicodeString);
}