File tree Expand file tree Collapse file tree 5 files changed +24
-7
lines changed
Expand file tree Collapse file tree 5 files changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import "arena.dart";
99
1010/// Represents a String in C memory, managed by an [Arena] .
1111class Utf8 extends Struct <Utf8 > {
12- @Int 8 ()
12+ @Uint 8 ()
1313 int char;
1414
1515 /// Allocates a [CString] in the current [Arena] and populates it with
Original file line number Diff line number Diff line change 77import "package:test/test.dart" ;
88
99import '../lib/sqlite.dart' ;
10+ import '../lib/src/ffi/utf8.dart' ;
1011
1112void main () {
1213 test ("sqlite integration test" , () {
@@ -161,4 +162,10 @@ void main() {
161162 r.close ();
162163 d.close ();
163164 });
165+ test ("Utf8 unit test" , () {
166+ final String test = 'Hasta Mañana' ;
167+ final medium = Utf8 .allocate (test);
168+ expect (test, medium.load <Utf8 >().toString ());
169+ medium.free ();
170+ });
164171}
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import 'dart:ffi';
1212
1313import "package:expect/expect.dart" ;
1414
15- import 'cstring .dart' ;
15+ import 'utf8 .dart' ;
1616import 'dylib_utils.dart' ;
1717
1818DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific ("ffi_test_functions" );
Original file line number Diff line number Diff line change @@ -12,13 +12,15 @@ import "package:expect/expect.dart";
1212
1313import 'coordinate_bare.dart' as bare;
1414import 'coordinate.dart' ;
15+ import 'utf8.dart' ;
1516
1617void main () {
1718 testStructAllocate ();
1819 testStructFromAddress ();
1920 testStructWithNulls ();
2021 testBareStruct ();
2122 testTypeTest ();
23+ testUtf8 ();
2224}
2325
2426/// allocates each coordinate separately in c memory
@@ -118,3 +120,10 @@ void testTypeTest() {
118120 Expect .isTrue (c is Struct <Coordinate >);
119121 c.addressOf.free ();
120122}
123+
124+ void testUtf8 () {
125+ final String test = 'Hasta Mañana' ;
126+ final Pointer <Utf8 > medium = Utf8 .toUtf8 (test);
127+ Expect .equals (test, Utf8 .fromUtf8 (medium));
128+ medium.free ();
129+ }
Original file line number Diff line number Diff line change 22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5- library FfiTest ;
5+ library Utf8 ;
66
77import 'dart:convert' ;
88import 'dart:ffi' as ffi;
99import 'dart:ffi' show Pointer;
1010
1111/// Sample non-struct Pointer wrapper for dart:ffi library.
1212class Utf8 extends ffi.Struct <Utf8 > {
13- @ffi .Int8 ()
13+ @ffi .Uint8 ()
1414 int char;
1515
1616 static String fromUtf8 (Pointer <Utf8 > str) {
@@ -25,12 +25,13 @@ class Utf8 extends ffi.Struct<Utf8> {
2525 }
2626
2727 static Pointer <Utf8 > toUtf8 (String s) {
28- Pointer <Utf8 > result = Pointer <Utf8 >.allocate (count: s.length + 1 ).cast ();
2928 List <int > units = Utf8Encoder ().convert (s);
30- for (int i = 0 ; i < s.length; i++ ) {
29+ Pointer <Utf8 > result =
30+ Pointer <Utf8 >.allocate (count: units.length + 1 ).cast ();
31+ for (int i = 0 ; i < units.length; i++ ) {
3132 result.elementAt (i).load <Utf8 >().char = units[i];
3233 }
33- result.elementAt (s .length).load <Utf8 >().char = 0 ;
34+ result.elementAt (units .length).load <Utf8 >().char = 0 ;
3435 return result;
3536 }
3637}
You can’t perform that action at this time.
0 commit comments