You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.1 KiB
36 lines
1.1 KiB
|
2 days ago
|
// Helpers of the tests of the key of words: the words of a case of
|
||
|
|
// test/vectors/wordkey_vectors.json, and the String of bytes that hold a
|
||
|
|
// surrogate, as utf8Bytes writes one.
|
||
|
|
|
||
|
|
import 'dart:typed_data';
|
||
|
|
|
||
|
|
import 'package:datekeys/src/bytes.dart';
|
||
|
|
|
||
|
|
/// The bytes of each hexadecimal word of [c].
|
||
|
|
List<Uint8List> wordBytes(Map<String, Object?> c) => [
|
||
|
|
for (final w in (c['words']! as List).cast<String>()) fromHex(w),
|
||
|
|
];
|
||
|
|
|
||
|
|
/// The String whose [utf8Bytes] are [b], also when they hold a surrogate,
|
||
|
|
/// which utf8Bytes writes as the three bytes of its code point; null for
|
||
|
|
/// other bytes that are not valid UTF-8.
|
||
|
|
String? fromWtf8(Uint8List b) {
|
||
|
|
final units = <int>[];
|
||
|
|
for (var i = 0; i < b.length;) {
|
||
|
|
if (b[i] == 0xed &&
|
||
|
|
i + 2 < b.length &&
|
||
|
|
b[i + 1] >= 0xa0 &&
|
||
|
|
b[i + 1] <= 0xbf &&
|
||
|
|
b[i + 2] & 0xc0 == 0x80) {
|
||
|
|
units.add(0xd000 | (b[i + 1] & 0x3f) << 6 | b[i + 2] & 0x3f);
|
||
|
|
i += 3;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
final (r, size) = decodeRune(b, i);
|
||
|
|
if (size == 1 && b[i] >= 0x80) return null;
|
||
|
|
units.addAll(String.fromCharCode(r).codeUnits);
|
||
|
|
i += size;
|
||
|
|
}
|
||
|
|
return String.fromCharCodes(units);
|
||
|
|
}
|