ffi_utf8.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
  2. // for details. All rights reserved. Use of this source code is governed by a
  3. // BSD-style license that can be found in the LICENSE file.
  4. import 'dart:convert';
  5. import 'dart:typed_data';
  6. import 'ffi_proxy.dart';
  7. /// The contents of a native zero-terminated array of UTF-8 code units.
  8. ///
  9. /// The Utf8 type itself has no functionality, it's only intended to be used
  10. /// through a `Pointer<Utf8>` representing the entire array. This pointer is
  11. /// the equivalent of a char pointer (`const char*`) in C code.
  12. class Utf8 extends Opaque {}
  13. /// Extension method for converting a`Pointer<Utf8>` to a [String].
  14. extension Utf8Pointer on Pointer<Utf8> {
  15. /// The number of UTF-8 code units in this zero-terminated UTF-8 string.
  16. ///
  17. /// The UTF-8 code units of the strings are the non-zero code units up to the
  18. /// first zero code unit.
  19. int get length {
  20. _ensureNotNullptr('length');
  21. final codeUnits = cast<Uint8>();
  22. return _length(codeUnits);
  23. }
  24. /// Converts this UTF-8 encoded string to a Dart string.
  25. ///
  26. /// Decodes the UTF-8 code units of this zero-terminated byte array as
  27. /// Unicode code points and creates a Dart string containing those code
  28. /// points.
  29. ///
  30. /// If [length] is provided, zero-termination is ignored and the result can
  31. /// contain NUL characters.
  32. ///
  33. /// If [length] is not provided, the returned string is the string up til
  34. /// but not including the first NUL character.
  35. String toDartString({int? length}) {
  36. _ensureNotNullptr('toDartString');
  37. final codeUnits = cast<Uint8>();
  38. if (length != null) {
  39. RangeError.checkNotNegative(length, 'length');
  40. } else {
  41. length = _length(codeUnits);
  42. }
  43. return utf8.decode(codeUnits.asTypedList(length));
  44. }
  45. static int _length(Pointer<Uint8> codeUnits) {
  46. var length = 0;
  47. while (codeUnits[length] != 0) {
  48. length++;
  49. }
  50. return length;
  51. }
  52. void _ensureNotNullptr(String operation) {
  53. if (this == nullptr) {
  54. throw UnsupportedError("Operation '$operation' not allowed on a 'nullptr'.");
  55. }
  56. }
  57. }
  58. /// Extension method for converting a [String] to a `Pointer<Utf8>`.
  59. extension StringUtf8Pointer on String {
  60. /// Creates a zero-terminated [Utf8] code-unit array from this String.
  61. ///
  62. /// If this [String] contains NUL characters, converting it back to a string
  63. /// using [Utf8Pointer.toDartString] will truncate the result if a length is
  64. /// not passed.
  65. ///
  66. /// Unpaired surrogate code points in this [String] will be encoded as
  67. /// replacement characters (U+FFFD, encoded as the bytes 0xEF 0xBF 0xBD) in
  68. /// the UTF-8 encoded result. See [Utf8Encoder] for details on encoding.
  69. ///
  70. /// Returns an [allocator]-allocated pointer to the result.
  71. Pointer<Utf8> toNativeUtf8(Allocator allocator) {
  72. final units = utf8.encode(this);
  73. final Pointer<Uint8> result = allocator<Uint8>(units.length + 1);
  74. final Uint8List nativeString = result.asTypedList(units.length + 1);
  75. nativeString.setAll(0, units);
  76. nativeString[units.length] = 0;
  77. return result.cast();
  78. }
  79. }