freezer/lib/utils.dart

48 lines
1.1 KiB
Dart

import 'dart:typed_data';
class Utils {
static List<int> splitDigits(int number) {
final digits = <int>[];
while (number != 0) {
digits.add(number % 10);
number = number ~/ 10;
}
return digits;
}
static Uint8List serializeBigInt(BigInt bi) {
Uint8List array = Uint8List((bi.bitLength / 8).ceil());
for (int i = 0; i < array.length; i++) {
array[i] = (bi >> (i * 8)).toUnsigned(8).toInt();
}
return array;
}
static BigInt deserializeBigInt(Uint8List array) {
var bi = BigInt.zero;
for (var byte in array.reversed) {
bi <<= 8;
bi |= BigInt.from(byte);
}
return bi;
}
/// FOR ISAR, FROM ISAR DOCUMENTATION
/// FNV-1a 64bit hash algorithm optimized for Dart Strings
static int fastHash(String string) {
var hash = 0xcbf29ce484222325;
var i = 0;
while (i < string.length) {
final codeUnit = string.codeUnitAt(i++);
hash ^= codeUnit >> 8;
hash *= 0x100000001b3;
hash ^= codeUnit & 0xFF;
hash *= 0x100000001b3;
}
return hash;
}
}