This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Changelog | ||
|
||
## 0.1.1 | ||
|
||
* Add basic Utf16 support | ||
|
||
## 0.1.0 | ||
|
||
* Initial release | ||
* Initial release supporting Utf8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
// BSD-style license that can be found in the LICENSE file. | ||
|
||
export 'src/utf8.dart'; | ||
export 'src/utf16.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
import 'dart:ffi'; | ||
import 'dart:typed_data'; | ||
|
||
/// [Utf16] implements conversion between Dart strings and null-terminated | ||
/// Utf6-encoded "char*" strings in C. | ||
/// | ||
/// [Utf16] is respresented as a struct so that `Pointer<Utf16>` can be used in | ||
/// native function signatures. | ||
class Utf16 extends Struct<Utf16> { | ||
/// Convert a [String] to a Utf16-encoded null-terminated C string. | ||
/// | ||
/// If 'string' contains NULL bytes, the converted string will be truncated | ||
/// prematurely. Unpaired surrogate code points in [string] will be preserved | ||
/// in the UTF-8 encoded result. See [Utf8Encoder] for details on encoding. | ||
/// | ||
/// Returns a malloc-allocated pointer to the result. | ||
static Pointer<Utf16> toUtf16(String s) { | ||
final units = s.codeUnits; | ||
final Pointer<Uint16> result = | ||
Pointer<Uint16>.allocate(count: units.length + 1); | ||
final Uint16List nativeString = | ||
result.asExternalTypedData(count: units.length + 1); | ||
nativeString.setAll(0, units); | ||
nativeString[units.length] = 0; | ||
return result.cast(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:ffi'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:test/test.dart'; | ||
import 'package:ffi/ffi.dart'; | ||
|
||
main() { | ||
test("toUtf16 ASCII", () { | ||
final String start = "Hello World!\n"; | ||
final Pointer<Uint16> converted = Utf16.toUtf16(start).cast(); | ||
final Uint16List end = | ||
converted.asExternalTypedData(count: start.codeUnits.length + 1); | ||
final matcher = equals(start.codeUnits.toList()..add(0)); | ||
expect(end, matcher); | ||
converted.free(); | ||
}); | ||
|
||
test("toUtf16 emoji", () { | ||
final String start = "😎"; | ||
final Pointer<Utf16> converted = Utf16.toUtf16(start).cast(); | ||
final int length = start.codeUnits.length; | ||
final Uint16List end = | ||
converted.cast<Uint16>().asExternalTypedData(count: length + 1); | ||
final matcher = equals(start.codeUnits.toList()..add(0)); | ||
expect(end, matcher); | ||
converted.free(); | ||
}); | ||
} |