diff --git a/dart/lib/flat_buffers.dart b/dart/lib/flat_buffers.dart index 500228576..5b85a7bf9 100644 --- a/dart/lib/flat_buffers.dart +++ b/dart/lib/flat_buffers.dart @@ -1033,20 +1033,21 @@ abstract class Reader { /// The reader of string values. class StringReader extends Reader { - 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); } diff --git a/dart/test/flat_buffers_test.dart b/dart/test/flat_buffers_test.dart index 6d595698e..050c477c6 100644 --- a/dart/test/flat_buffers_test.dart +++ b/dart/test/flat_buffers_test.dart @@ -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); }