-
Notifications
You must be signed in to change notification settings - Fork 166
Add encoding functionality for int list to string #8206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'dart:convert'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| /// Base64 encodes [dataPoints] as 32-bit unsigned integers serialized with | ||
| /// little endian byte order. | ||
| String encodeIntsAsLittleEndianBase64String(List<int> dataPoints) { | ||
| final byteData = ByteData(4 * dataPoints.length); | ||
| for (int i = 0; i < dataPoints.length; i++) { | ||
| byteData.setUint32(4 * i, dataPoints[i], Endian.little); | ||
| } | ||
| return base64Encode(byteData.buffer.asUint8List()); | ||
| } | ||
|
|
||
| /// Counter part to [encodeIntsAsLittleEndianBase64String]. | ||
| List<int> decodeIntsFromLittleEndianBase64String(String encoded) { | ||
| final bytes = base64Decode(encoded); | ||
| final resLength = bytes.length ~/ 4; | ||
| final dataPoints = List.filled(resLength, -1); | ||
| final sublist = ByteData.sublistView(bytes); | ||
| for (int i = 0; i < resLength; i++) { | ||
| dataPoints[i] = sublist.getUint32(4 * i, Endian.little); | ||
| } | ||
| return dataPoints; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:_pub_shared/format/encoding.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| test('encode/decode success', () { | ||
| final data = [1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
| final encoded = encodeIntsAsLittleEndianBase64String(data); | ||
| expect(encoded, 'AQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAA'); | ||
| expect(decodeIntsFromLittleEndianBase64String(encoded), data); | ||
| }); | ||
|
|
||
| test('encode/decode empty', () { | ||
| final data = <int>[]; | ||
| final encoded = encodeIntsAsLittleEndianBase64String(data); | ||
| expect(encoded, ''); | ||
| expect(decodeIntsFromLittleEndianBase64String(encoded), data); | ||
| }); | ||
|
|
||
| test('encode/decode failure with negative integers', () { | ||
| final data = <int>[-1, -2]; | ||
| final encoded = encodeIntsAsLittleEndianBase64String(data); | ||
| expect(encoded, '//////7///8='); | ||
| expect(decodeIntsFromLittleEndianBase64String(encoded), isNot(data)); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just use the Uint32List constructor. Give it a length and use
setAllThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@szakarias I'd suggest:
Or
But I'd advise against getting used to using
.buffer.asUint8List(). Because when you have aUint32Listthe underlying buffer could be longer than the contents of the actualUint32List.So technically, you'd have to read the documentation for the
Uint32Listconstructor to see that it says:Relying on this promise it is safe to use
.buffer.asUint8List().If we had gotten the
Uint32Listfrom somewhere else, we'd have to readoffsetInBytesandlengthInBytesand pass them when doing.buffer.asUint8List().Hence, why I'd generally causing against accessing
.bufferdirectly, because unless you know where the buffer was created, the location of your data within the buffer is not guaranteed. ButUint8List.sublistViewwill avoid such concerns.