Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into osa1/avoid_empty_sp…
Browse files Browse the repository at this point in the history
…lices
  • Loading branch information
osa1 committed Oct 26, 2023
2 parents 60bec12 + 23dffde commit 0dde18e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
5 changes: 4 additions & 1 deletion protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 4.0.0-dev

* The following types and members are now removed:
* **Breaking:** The following types and members are now removed:

- `PbEventMixin`
- `PbFieldChange`
Expand All @@ -12,6 +12,9 @@
surface small (to make it easier to change the library or migrate to another
library) these types and members are removed. ([#738])

* **Breaking:** `CodedBufferWriter.writeRawBytes` now takes a `Uint8List`
argument (instead of `TypedData`).

[#738]: https://github.com/google/protobuf.dart/issues/738

## 3.1.0
Expand Down
15 changes: 10 additions & 5 deletions protobuf/lib/protobuf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ library;

import 'dart:collection' show ListBase, MapBase;
import 'dart:convert'
show Utf8Codec, base64Decode, base64Encode, jsonDecode, jsonEncode;
show
Utf8Decoder,
Utf8Encoder,
base64Decode,
base64Encode,
jsonDecode,
jsonEncode;
import 'dart:math' as math;
import 'dart:typed_data' show ByteData, Endian, TypedData, Uint8List;
import 'dart:typed_data' show ByteData, Endian, Uint8List;

import 'package:fixnum/fixnum.dart' show Int64;
import 'package:meta/meta.dart' show UseResult;
Expand Down Expand Up @@ -50,13 +56,12 @@ part 'src/protobuf/unpack.dart';
part 'src/protobuf/utils.dart';
part 'src/protobuf/wire_format.dart';

// TODO(sra): Use Int64.parse() when available - see http://dartbug.com/21915.
// TODO(sra): Use `Int64.parse()` when available:
// https://github.com/dart-lang/fixnum/issues/18.
/// @nodoc
Int64 parseLongInt(String text) {
if (text.startsWith('0x')) return Int64.parseHex(text.substring(2));
if (text.startsWith('+0x')) return Int64.parseHex(text.substring(3));
if (text.startsWith('-0x')) return -Int64.parseHex(text.substring(3));
return Int64.parseInt(text);
}

const _utf8 = Utf8Codec(allowMalformed: true);
18 changes: 14 additions & 4 deletions protobuf/lib/src/protobuf/coded_buffer_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ class CodedBufferReader {
int readFixed32() => _readByteData(4).getUint32(0, Endian.little);
Int64 readFixed64() => readSfixed64();
int readSfixed32() => _readByteData(4).getInt32(0, Endian.little);

Int64 readSfixed64() {
final data = _readByteData(8);
final view = Uint8List.view(data.buffer, data.offsetInBytes, 8);
final pos = _bufferPos;
_checkLimit(8);
final view = Uint8List.sublistView(_buffer, pos, pos + 8);
return Int64.fromBytes(view);
}

Expand All @@ -148,7 +150,14 @@ class CodedBufferReader {
_buffer.buffer, _buffer.offsetInBytes + _bufferPos - length, length);
}

String readString() => _utf8.decode(readBytesAsView());
String readString() {
final length = readInt32();
final stringPos = _bufferPos;
_checkLimit(length);
return const Utf8Decoder(allowMalformed: true)
.convert(_buffer, stringPos, stringPos + length);
}

double readFloat() => _readByteData(4).getFloat32(0, Endian.little);
double readDouble() => _readByteData(8).getFloat64(0, Endian.little);

Expand Down Expand Up @@ -180,7 +189,8 @@ class CodedBufferReader {
readFixed64();
return true;
case WIRETYPE_LENGTH_DELIMITED:
readBytesAsView();
final length = readInt32();
_checkLimit(length);
return true;
case WIRETYPE_FIXED32:
readFixed32();
Expand Down
39 changes: 12 additions & 27 deletions protobuf/lib/src/protobuf/coded_buffer_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ part of '../../protobuf.dart';
class CodedBufferWriter {
/// Array of splices representing the data written into the writer.
/// Each element might be one of:
/// * a TypedData object - represents a sequence of bytes that need to be
/// emitted into the result as-is;
/// * a [Uint8List] object - represents a sequence of bytes that need to be
/// emitted into the result as-is;
/// * a positive integer - a number of bytes to copy from [_outputChunks]
/// into resulting buffer;
/// * a non-positive integer - a positive number that needs to be emitted
Expand Down Expand Up @@ -166,9 +166,12 @@ class CodedBufferWriter {
}
}
} else {
// action is a TypedData containing bytes to emit into the output
// action is a `Uint8List` containing bytes to emit into the output
// buffer.
outPos = _copyInto(buffer, outPos, action);
final Uint8List value = action;
final end = outPos + value.length;
buffer.setRange(outPos, end, value);
outPos = end;
}
}

Expand Down Expand Up @@ -210,7 +213,7 @@ class CodedBufferWriter {
/// Record number of bytes written into output chunks since last splice.
///
/// This is used before reserving space for an unknown varint splice or
/// adding a TypedData array splice.
/// adding a [Uint8List] array splice.
void _commitSplice() {
final pos = _bytesInChunk + _outputChunksBytes;
final bytes = pos - _lastSplicePos;
Expand All @@ -220,9 +223,9 @@ class CodedBufferWriter {
_lastSplicePos = pos;
}

/// Add TypedData splice - these bytes would be directly copied into the
/// output buffer by [writeTo].
void writeRawBytes(TypedData value) {
/// Add a [Uint8List] splice, without copying. These bytes will be directly
/// copied into the output buffer by [writeTo].
void writeRawBytes(Uint8List value) {
final length = value.lengthInBytes;
if (length == 0) return;
_commitSplice();
Expand Down Expand Up @@ -353,7 +356,7 @@ class CodedBufferWriter {
if (string.isEmpty) {
writeInt32NoTag(0);
} else {
_writeBytesNoTag(_utf8.encoder.convert(string));
_writeBytesNoTag(const Utf8Encoder().convert(string));
}
break;
case PbFieldType._DOUBLE_BIT:
Expand Down Expand Up @@ -432,24 +435,6 @@ class CodedBufferWriter {
_writeVarint32(value & 0xFFFFFFFF);
}

/// Copy bytes from the given typed data array into the output buffer.
///
/// Has a specialization for [Uint8List] for performance.
int _copyInto(Uint8List buffer, int pos, TypedData value) {
if (value is Uint8List) {
final len = value.length;
final end = pos + len;
buffer.setRange(pos, end, value);
return end;
} else {
final len = value.lengthInBytes;
final end = pos + len;
final u8 = Uint8List.view(value.buffer, value.offsetInBytes, len);
buffer.setRange(pos, end, u8);
return end;
}
}

/// This function maps a power-of-2 value (2^0 .. 2^31) to a unique value
/// in the 0..31 range.
///
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/field_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class FieldInfo<T> {

/// Convenience method to thread this FieldInfo's reified type parameter to
/// `_FieldSet._ensureRepeatedField`.
List<T> _ensureRepeatedField(BuilderInfo meta, _FieldSet fs) {
PbList<T> _ensureRepeatedField(BuilderInfo meta, _FieldSet fs) {
return fs._ensureRepeatedField<T>(meta, this);
}

Expand Down

0 comments on commit 0dde18e

Please sign in to comment.