diff --git a/lib/src/ffi/cstring.dart b/lib/src/ffi/cstring.dart index 6f04cdc84..898f26e4b 100644 --- a/lib/src/ffi/cstring.dart +++ b/lib/src/ffi/cstring.dart @@ -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 _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.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())).codeUnitAt(0) != 0) // TODO: unicode support - ret += c; - return ret; + List utf8CodePoints = new List(); + + int element; + + for (int i=0; element != 0; i++) { + element = _ptr.elementAt(i).load(); + utf8CodePoints.add(element); + } + + return decodeUtf8(utf8CodePoints); } String toString() => val; diff --git a/pubspec.yaml b/pubspec.yaml index e53166272..ff901d14e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/test.dart b/test/test.dart index 70068f746..6507bf1a2 100644 --- a/test/test.dart +++ b/test/test.dart @@ -1,4 +1,5 @@ import "../lib/objectbox.dart"; +import "package:utf/src/utf8.dart"; @Entity(id: 1, uid: 1) class Note { @@ -17,11 +18,16 @@ class Note { main() { var store = Store([Note]); var box = Box(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 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(); }