-
Notifications
You must be signed in to change notification settings - Fork 32
Conversation
I had to rebase on top of #60 to be able to run tests. |
0dc5bd1
to
c82f6bb
Compare
The latest revision uses |
Adding a new static method to differentiate between null-terminated and not will solve the problem for now. @lrhn what do you think from an API design point of view? Alternatively, you can fork the package and work on your own branch for the time being. |
I've had similar helpers in a couple of FFI-based projects, so I thought it would be nice to have in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an optional argument is fine, it really is two ways to specify the same input to the same function.
A default of -1
will work both before and after null safety, but that shouldn't be important.
You should not be trying to make code which works with both. A library is either null safe or not, and you must pick one.
The ffi
package has SDK >= 2.12.0-dev
so it is null safety enabled, and you should be able to use int? length = null
. If that doesn't work, then null safety (of the ffi
package or any other package) is simply not compatible with what you're using it for, and if it works today, it's an accident.
|
ACK, that's a lint, so you should probably follow it an omit the |
Let me know if I should push separately the doc changes that are not strictly related to this PR. The Utf8 & Utf16 class descriptions are side by side in the class list so I couldn't leave it like that... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this look fine, apart from referring to UTF-16 code units as "zero byte"s.
I've made some extra notes, but that's just drive-by comments on things you haven't touched. I'd let @dcharkes ponder those.
Also, it's his package, so I'll let him decide how to structure CLs (I'm a fan of monolithic CLs that fix everything in one go. Other people tend to disagree). 😁
lib/src/utf16.dart
Outdated
@@ -8,15 +8,15 @@ import 'dart:typed_data'; | |||
|
|||
import 'package:ffi/ffi.dart'; | |||
|
|||
/// [Utf16] implements conversion between Dart strings and null-terminated | |||
/// [Utf16] implements conversion between Dart strings and zero byte terminated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It's not a zero byte when it's 16-bit. Maybe a a zero "word" or "code unit". Maybe just "zero terminated", then it's the zero value of the appropriate type.
lib/src/utf16.dart
Outdated
/// | ||
/// If 'string' contains NULL bytes, the converted string will be truncated | ||
/// If 'string' contains zero 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably say UTF-16 here.
(You don't have to fix all existing typos, I'm just mentioning it so someone, like @dcharkes, can fix it later). 😉
lib/src/utf16.dart
Outdated
/// native function signatures. | ||
class Utf16 extends Struct { | ||
/// Convert a [String] to a Utf16-encoded null-terminated C string. | ||
/// Convert a [String] to a UTF-16 encoded zero byte terminated C string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto: zero terminated.
lib/src/utf16.dart
Outdated
/// | ||
/// If 'string' contains NULL bytes, the converted string will be truncated | ||
/// If 'string' contains zero bytes, the converted string will be truncated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... contains a zero code unit ....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually won't be truncated. It allocates and copies all the code units over.
If that result is interpreted as zero-terminated, then that result will be truncated, but if you know the length, you should be able to get it all back.
(Not your text either.)
lib/src/utf8.dart
Outdated
/// Returns the length of a null-terminated string -- the number of (one-byte) | ||
/// characters before the first null byte. | ||
/// Returns the length of a zero byte terminated string — the number of | ||
/// bytes before the first zero byte. | ||
static int strlen(Pointer<Utf8> string) { | ||
final Pointer<Uint8> array = string.cast<Uint8>(); | ||
final Uint8List nativeString = array.asTypedList(_maxSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea: Maybe use indexOf(0)
below instead of indexWhere(closure)
. Should have less overhead.
Be consistent about zero- vs. null-terminated (the latter was more commonly used, but the former was suggested in dart-archive#61). Furthermore, align UTF-8/16 spelling and fix a couple of minor typos.
Be consistent about zero- vs. null-terminated (the latter was more commonly used, but using the former was suggested in dart-archive#61). Furthermore, align UTF-8/16 spelling and fix a couple of minor typos and formatting issues.
Be consistent about zero- vs. null-terminated (the latter was more commonly used, but using the former was suggested in dart-archive#61). Furthermore, align UTF-8/16 spelling and fix a couple of minor typos and formatting issues.
Be consistent about zero- vs. null-terminated (the latter was more commonly used, but using the former was suggested in #61). Furthermore, align UTF-8/16 spelling and fix a couple of minor typos and formatting issues.
The string could be non-zero-terminated, for example, or even if it is, the length may be already known, in which case an unnecessary strlen() call can be avoided.
Thanks! P.S. There's still one
|
@jpnurmi, 1 thing is missing for publishing a new version of the package. Please make a PR that bumps the version number to Alternatively, you can just depend on a git hash instead (or your own branch) for the time being until Dart 1.12 is released to stable. |
Do you want to address this before I merge #64? |
As suggested in dart-archive#61, using indexOf(0) to search the zero byte has a bit less overhead than indexWhere(closure).
I don't mind :) => #65 |
As suggested in #61, using indexOf(0) to search the zero byte has a bit less overhead than indexWhere(closure).
Be consistent about zero- vs. null-terminated (the latter was more commonly used, but using the former was suggested in dart-archive/ffi#61). Furthermore, align UTF-8/16 spelling and fix a couple of minor typos and formatting issues.
The string could be non-zero-terminated, for example, or even if it is, the length may be already known, in which case an unnecessary strlen() call can be avoided.
As suggested in dart-archive/ffi#61, using indexOf(0) to search the zero byte has a bit less overhead than indexWhere(closure).
No description provided.