Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dmd/root/rmem.d
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ static if (OVERRIDE_MEMALLOC)
// corresponding to 2.074.0 or later.
static if (!is(typeof(pureMalloc)))
{
private:
package(dmd):
static import core.stdc.errno;

/**
Expand Down
8 changes: 4 additions & 4 deletions src/dmd/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,24 @@ dg = Delegate to call afterwards
Returns:
The return value of `T`
*/
auto toCStringThen(alias dg)(const(char)[] src) nothrow
Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove nothrow?

Copy link
Member

Choose a reason for hiding this comment

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

My guess is that it should be inferred, and maybe @jacob-carlborg has a non-nothrow delegate.

However we don't have any place in DMD that use exceptions, and IIRC Walter is clearly against it, which is why this is here in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah the only reason I could see to remove it is if it were exposed in dmd as a library.

auto toCStringThen(alias dg)(const(char)[] src)
{
const len = src.length + 1;
char[512] small = void;
scope ptr = (src.length < (small.length - 1))
? small[0 .. len]
: (cast(char*)mem.xmalloc(len))[0 .. len];
: (cast(char*)pureMalloc(len))[0 .. len];
scope (exit)
{
if (&ptr[0] != &small[0])
mem.xfree(&ptr[0]);
pureFree(&ptr[0]);
}
ptr[0 .. src.length] = src[];
ptr[src.length] = '\0';
return dg(ptr);
}

unittest
pure nothrow @nogc unittest
{
assert("Hello world".toCStringThen!((v) => v == "Hello world\0"));
assert("Hello world\0".toCStringThen!((v) => v == "Hello world\0\0"));
Expand Down