Skip to content

Add generic function to convert D string to C string#9482

Closed
jacob-carlborg wants to merge 1 commit intodlang:masterfrom
jacob-carlborg:toStringzThen
Closed

Add generic function to convert D string to C string#9482
jacob-carlborg wants to merge 1 commit intodlang:masterfrom
jacob-carlborg:toStringzThen

Conversation

@jacob-carlborg
Copy link
Contributor

Adds a new function toStringzThen which will convert the given D string to a null terminated C string. It passes the C string to the given callable. The C string is only valid during the call to the callable and will be freed after it returns.

Adds a new function `toStringzThen` which will convert the given
D string to a null terminated C string. It passes the C string to the
given callable. The C string is only valid during the call to the
callable and will be freed after it returns.
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @jacob-carlborg! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#9482"

@thewilsonator
Copy link
Contributor

We already have this in dmd.utils. https://github.com/dlang/dmd/blob/master/src/dmd/utils.d#L192

import dmd.root.rmem : pureMalloc, pureFree;

if (str.length == 0)
return func(&""[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check here if the string is already null-terminated?

@Geod24
Copy link
Member

Geod24 commented Mar 24, 2019

Yep we have toCStringThen. We could move that if needed, but what's your use-case ?

@jacob-carlborg
Copy link
Contributor Author

We could move that if needed, but what's your use-case ?

There's no need to move it yet. The use case is this PR #9480 and it could be used in the Objective-C integration as well.

@jacob-carlborg jacob-carlborg deleted the toStringzThen branch March 24, 2019 13:07
@jacob-carlborg
Copy link
Contributor Author

New PR with a minor tweak to the existing function: #9483.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants