Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add String setters to many structs #142

Closed
timsneath opened this issue Jan 20, 2021 · 6 comments
Closed

Add String setters to many structs #142

timsneath opened this issue Jan 20, 2021 · 6 comments

Comments

@timsneath
Copy link
Contributor

No description provided.

@timsneath timsneath added the blocked Waiting on an external issue to be resolved label Jan 27, 2021
@timsneath
Copy link
Contributor Author

This will be much easier when we have fixed-sized arrays in FFI structs. I can add something by hand if it's needed, but when arrays land, I can replace many lines of boilerplate with something much cleaner here.

@timsneath
Copy link
Contributor Author

Fixed in the latest Dart builds: dart-lang/sdk#35763

I can start working on this now in the partitioned dart2.13 branch: https://github.com/timsneath/win32/tree/dart2.13

@timsneath timsneath removed the blocked Waiting on an external issue to be resolved label Mar 29, 2021
@timsneath
Copy link
Contributor Author

Fixed in a1a1d43

@Sunbreak
Copy link
Contributor

How could we turn Array<Int8> into/from String efficently? Code below works but copied the internal data

https://github.com/woodemi/twaindsm.dart/blob/6caefd76590fa59938f9f60184bede5814d00ef6/lib/structs.dart#L273-L288

extension CharArray on Array<Int8> {
  String getDartString(int maxLength) {
    var list = <int>[];
    for (var i = 0; i < maxLength; i++) {
      if (this[i] != 0) list.add(this[i]);
    }
    return utf8.decode(list);
  }

  void setDartString(String s, int maxLength) {
    var list = utf8.encode(s);
    for (var i = 0; i < maxLength; i++) {
      this[i] = i < list.length ? list[i] : 0;
    }
  }
}

@timsneath
Copy link
Contributor Author

My guess is that, while inefficient, the performance cost of the copy is tiny compared to the Win32 calls themselves. If it's only a few thousand instructions, it's probably not worth worrying about. But curious if you've measured this?

@Sunbreak
Copy link
Contributor

Related: dart-lang/sdk#45508

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants