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

added utf8 support #14

Closed
wants to merge 3 commits into from
Closed
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
32 changes: 22 additions & 10 deletions lib/src/ffi/cstring.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
import "dart:ffi";
import "dart:typed_data";
import "package:utf/src/utf8.dart";
import "package:utf/src/utf16.dart";

// TODO check if revamp structs are relevant (https://github.com/dart-lang/sdk/issues/37229)
// wrapper for a null-terminated array of characters in memory ("c-style string")
class CString {
Pointer<Uint8> _ptr;

CString(String dartStr) { // if this constructor is used, ".free" needs to be called on this instance
_ptr = allocate(count: dartStr.length + 1);
for(int i = 0; i < dartStr.length; ++i)
_ptr.elementAt(i).store(dartStr.codeUnitAt(i));
_ptr.elementAt(dartStr.length).store(0);
// if this constructor is used, ".free" needs to be called on this instance
CString(String dartStr) {
final ints = encodeUtf8(dartStr);
_ptr = Pointer<Uint8>.allocate(count: ints.length + 1);
for(int i = 0; i < ints.length; ++i) {
_ptr.elementAt(i).store(ints.elementAt(i));
}
_ptr.elementAt(ints.length).store(0);
}

CString.fromPtr(this._ptr);

String get val {
String ret = "", c;
int i = 0;
while((c = String.fromCharCode(_ptr.elementAt(i++).load<int>())).codeUnitAt(0) != 0) // TODO: unicode support
ret += c;
return ret;
List<int> utf8CodePoints = new List<int>();

int element;

for (int i=0; element != 0; i++) {
element = _ptr.elementAt(i).load<int>();
utf8CodePoints.add(element);
}

return decodeUtf8(utf8CodePoints);
}

String toString() => val;
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: objectbox
version: 0.0.1
version: 0.0.2
description: >-
ObjectBox binding for Dart.
environment:
sdk: '>=2.2.2 <3.0.0'
dependencies:
flat_buffers: ^1.11.0
utf: ^0.9.0
12 changes: 9 additions & 3 deletions test/test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "../lib/objectbox.dart";
import "package:utf/src/utf8.dart";

@Entity(id: 1, uid: 1)
class Note {
Expand All @@ -17,11 +18,16 @@ class Note {
main() {
var store = Store([Note]);
var box = Box<Note>(store);
insertNote(box, decodeUtf8([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]));
insertNote(box, decodeUtf8([228, 189, 160, 229, 165, 189, 228, 184, 150, 231, 149, 140, 33]));
insertNote(box, decodeUtf8([65, 104, 111, 106, 32, 115, 118, 196, 155, 116, 101, 33]));
store.close();
}

var note = Note.construct("Hello");
insertNote(Box<Note> box, String str) {
var note = Note.construct(str);
box.put(note);

print("new note got id ${note.id}");
print("refetched note: ${box.getById(note.id)}");

store.close();
}