From 5235133f321b71f041b515002d3c8f7049f497de Mon Sep 17 00:00:00 2001 From: Ivan Dlugos <6349682+vaind@users.noreply.github.com> Date: Thu, 29 Jul 2021 19:39:03 +0200 Subject: [PATCH] Dart - make ascii optimization optional in StringReader, same as in writeString() (#6758) --- dart/lib/flat_buffers.dart | 11 ++++++----- dart/test/flat_buffers_test.dart | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) 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); }