Skip to content
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

Support UTF-8 keys #928

Merged
merged 5 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions hive/lib/src/binary/binary_reader_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ class BinaryReaderImpl extends BinaryReader {
var keyType = readByte();
if (keyType == FrameKeyType.uintT) {
return readUint32();
} else if (keyType == FrameKeyType.asciiStringT) {
var keyLength = readByte();
return String.fromCharCodes(viewBytes(keyLength));
} else if (keyType == FrameKeyType.utf8StringT) {
var byteCount = readByte();
return BinaryReader.utf8Decoder.convert(viewBytes(byteCount));
} else {
throw HiveError('Unsupported key type. Frame might be corrupted.');
}
Expand Down
7 changes: 4 additions & 3 deletions hive/lib/src/binary/binary_writer_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,10 @@ class BinaryWriterImpl extends BinaryWriter {
ArgumentError.checkNotNull(key);

if (key is String) {
writeByte(FrameKeyType.asciiStringT);
writeByte(key.length);
_addBytes(key.codeUnits);
writeByte(FrameKeyType.utf8StringT);
var bytes = BinaryWriter.utf8Encoder.convert(key);
writeByte(bytes.length);
_addBytes(bytes);
} else {
writeByte(FrameKeyType.uintT);
writeUint32(key as int);
Expand Down
7 changes: 3 additions & 4 deletions hive/lib/src/binary/frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ class Frame {
throw HiveError('Integer keys need to be in the range 0 - 0xFFFFFFFF');
}
} else if (key is String) {
if (key.length > 0xFF || !key.isAscii) {
throw HiveError(
'String keys need to be ASCII Strings with a max length of 255');
if (key.length > 0xFF) {
throw HiveError('String keys need to be a max length of 255');
}
} else {
throw HiveError('Keys need to be Strings or integers');
Expand Down Expand Up @@ -112,7 +111,7 @@ class FrameKeyType {
static const uintT = 0;

/// String key
static const asciiStringT = 1;
static const utf8StringT = 1;
}

/// Possible value types
Expand Down
6 changes: 3 additions & 3 deletions hive/test/tests/binary/frame_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ void main() {
Frame.lazy('a' * 255);
Frame.deleted('a' * 255);

expect(() => Frame('hellö', null), throwsHiveError());
expect(() => Frame.lazy('hellö'), throwsHiveError());
expect(() => Frame.deleted('hellö'), throwsHiveError());
Frame('hellö', null);
Frame.lazy('hellö');
Frame.deleted('hellö');

expect(() => Frame('a' * 256, null), throwsHiveError());
expect(() => Frame.lazy('a' * 256), throwsHiveError());
Expand Down