Last active
August 29, 2015 14:09
-
-
Save xxgreg/e380bd3b5a0230fb2ede to your computer and use it in GitHub Desktop.
This file contains 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
import 'dart:convert'; | |
import 'dart:collection'; | |
import 'dart:typed_data'; | |
main() { | |
var str = '12345☺'; | |
var bytes = UTF8.encode(str); | |
print(decodeUtf8Slice(bytes, 5, 3)); | |
print(decodeUtf8Slice(bytes, 5)); | |
var bytes2 = new Uint8List.fromList(bytes); | |
print(decodeUtf8Slice(bytes2, 5, 3)); | |
print(decodeUtf8Slice(bytes2, 5)); | |
var bytes3 = new Uint8ClampedList.fromList(bytes); | |
print(decodeUtf8Slice(bytes3, 5, 3)); | |
print(decodeUtf8Slice(bytes3, 5)); | |
} | |
String decodeUtf8Slice(List<int> bytes, int offset, [int length]) { | |
if (length == null) length = bytes.length - offset; | |
if (offset < 0 || offset > bytes.length) throw new RangeError(offset); | |
if (bytes is Uint8List || bytes is Uint8ClampedList) { | |
bytes = new Uint8ClampedList.view(bytes.buffer, offset, length); | |
} else { | |
bytes = new UnmodifiableListView(bytes.skip(offset).take(length)); | |
} | |
return UTF8.decode(bytes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment