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";
9
9
10
10
/// Represents a String in C memory, managed by an [Arena] .
11
11
class Utf8 extends Struct <Utf8 > {
12
- @Int 8 ()
12
+ @Uint 8 ()
13
13
int char;
14
14
15
15
/// Allocates a [CString] in the current [Arena] and populates it with
Original file line number Diff line number Diff line change 7
7
import "package:test/test.dart" ;
8
8
9
9
import '../lib/sqlite.dart' ;
10
+ import '../lib/src/ffi/utf8.dart' ;
10
11
11
12
void main () {
12
13
test ("sqlite integration test" , () {
@@ -161,4 +162,10 @@ void main() {
161
162
r.close ();
162
163
d.close ();
163
164
});
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
+ });
164
171
}
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import 'dart:ffi';
12
12
13
13
import "package:expect/expect.dart" ;
14
14
15
- import 'cstring .dart' ;
15
+ import 'utf8 .dart' ;
16
16
import 'dylib_utils.dart' ;
17
17
18
18
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific ("ffi_test_functions" );
Original file line number Diff line number Diff line change @@ -12,13 +12,15 @@ import "package:expect/expect.dart";
12
12
13
13
import 'coordinate_bare.dart' as bare;
14
14
import 'coordinate.dart' ;
15
+ import 'utf8.dart' ;
15
16
16
17
void main () {
17
18
testStructAllocate ();
18
19
testStructFromAddress ();
19
20
testStructWithNulls ();
20
21
testBareStruct ();
21
22
testTypeTest ();
23
+ testUtf8 ();
22
24
}
23
25
24
26
/// allocates each coordinate separately in c memory
@@ -118,3 +120,10 @@ void testTypeTest() {
118
120
Expect .isTrue (c is Struct <Coordinate >);
119
121
c.addressOf.free ();
120
122
}
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 2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- library FfiTest ;
5
+ library Utf8 ;
6
6
7
7
import 'dart:convert' ;
8
8
import 'dart:ffi' as ffi;
9
9
import 'dart:ffi' show Pointer;
10
10
11
11
/// Sample non-struct Pointer wrapper for dart:ffi library.
12
12
class Utf8 extends ffi.Struct <Utf8 > {
13
- @ffi .Int8 ()
13
+ @ffi .Uint8 ()
14
14
int char;
15
15
16
16
static String fromUtf8 (Pointer <Utf8 > str) {
@@ -25,12 +25,13 @@ class Utf8 extends ffi.Struct<Utf8> {
25
25
}
26
26
27
27
static Pointer <Utf8 > toUtf8 (String s) {
28
- Pointer <Utf8 > result = Pointer <Utf8 >.allocate (count: s.length + 1 ).cast ();
29
28
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++ ) {
31
32
result.elementAt (i).load <Utf8 >().char = units[i];
32
33
}
33
- result.elementAt (s .length).load <Utf8 >().char = 0 ;
34
+ result.elementAt (units .length).load <Utf8 >().char = 0 ;
34
35
return result;
35
36
}
36
37
}
You can’t perform that action at this time.
0 commit comments