-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| /// big endian byte order. | ||
| String encodeIntsAsBigEndianBase64String(List<int> dataPoints) { | ||
szakarias marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @szakarias I'd suggest: return base64.encode(Uint8List.sublistView(Uint32List.fromList(dataPoints)));Or return base64.encode(Uint32List.fromList(dataPoints).buffer.asUint8List());But I'd advise against getting used to using So technically, you'd have to read the documentation for the
Relying on this promise it is safe to use If we had gotten the Hence, why I'd generally causing against accessing |
||
| } | ||
|
|
||
| /// Counter part to [encodeIntsAsBigEndianBase64String]. | ||
| List<int> decodeIntsFromBigEndianBase64String(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; | ||
| } | ||
| 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 = encodeIntsAsBigEndianBase64String(data); | ||
| expect(encoded, 'AQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAA'); | ||
| expect(decodeIntsFromBigEndianBase64String(encoded), data); | ||
| }); | ||
|
|
||
| test('encode/decode empty', () { | ||
| final data = <int>[]; | ||
| final encoded = encodeIntsAsBigEndianBase64String(data); | ||
| expect(encoded, ''); | ||
| expect(decodeIntsFromBigEndianBase64String(encoded), data); | ||
| }); | ||
|
|
||
| test('encode/decode failure with negative integers', () { | ||
| final data = <int>[-1, -2]; | ||
| final encoded = encodeIntsAsBigEndianBase64String(data); | ||
| expect(encoded, '//////7///8='); | ||
| expect(decodeIntsFromBigEndianBase64String(encoded), isNot(data)); | ||
| }); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.