Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Nov 5, 2023
1 parent 97d0da3 commit eafd169
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
18 changes: 12 additions & 6 deletions lib/src/codepage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,25 @@ CodePageDecoder _createDecoder(String characters) {
return _NonBmpCodePageDecoder._(result);
}

/// An input Sink for decoders where each input byte can be be considered
/// independantly.
class _CodePageSink implements Sink<List<int>> {
/// An input [ByteConversionSink] for decoders where each input byte can be be
/// considered independantly.
class _CodePageDecoderSink implements ByteConversionSink {
final Sink<String> _output;
final String Function(List<int> input) _convert;

_CodePageSink(this._output, this._convert);
_CodePageDecoderSink(this._output, this._convert);

@override
void add(List<int> chunk) {
_output.add(_convert(chunk));
}

@override
void addSlice(List<int> chunk, int start, int end, bool isLast) {
add(chunk.sublist(start, end));
if (isLast) close();
}

@override
void close() {
_output.close();
Expand Down Expand Up @@ -348,7 +354,7 @@ class _NonBmpCodePageDecoder extends Converter<List<int>, String>

@override
Sink<List<int>> startChunkedConversion(Sink<String> sink) =>
_CodePageSink(sink, convert);
_CodePageDecoderSink(sink, convert);
}

class _BmpCodePageDecoder extends Converter<List<int>, String>
Expand Down Expand Up @@ -385,7 +391,7 @@ class _BmpCodePageDecoder extends Converter<List<int>, String>

@override
Sink<List<int>> startChunkedConversion(Sink<String> sink) =>
_CodePageSink(sink, convert);
_CodePageDecoderSink(sink, convert);

String _convertAllowInvalid(List<int> bytes) {
var count = bytes.length;
Expand Down
23 changes: 20 additions & 3 deletions test/codepage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,30 @@ void main() {
final outputSink = StringConversionSink.withCallback(
(accumulated) => decodedString = accumulated);
final inputSink = cp.decoder.startChunkedConversion(outputSink);

inputSink
..add([1])
..add([0])
..add([3]);

inputSink.close();
..add([3])
..close();
expect(decodedString, 'BAD');
});

test('chunked conversion - byte conversion sink', () {
late final String decodedString;
final outputSink = StringConversionSink.withCallback(
(accumulated) => decodedString = accumulated);
final bytes = [1, 0, 3, 2, 0, 5, 4];

final inputSink = cp.decoder.startChunkedConversion(outputSink);
expect(inputSink, isA<ByteConversionSink>());

(inputSink as ByteConversionSink)
..addSlice(bytes, 1, 3, false)
..addSlice(bytes, 4, 5, false)
..addSlice(bytes, 6, 6, true);

expect(decodedString, 'ADA');
});
});
}

0 comments on commit eafd169

Please sign in to comment.