Skip to content

Commit

Permalink
Partial fix of Issue 8384 - std.conv.to should allow conversion betwe…
Browse files Browse the repository at this point in the history
…en any pair of string/wstring/dstring/char*/wchar*/dchar*
  • Loading branch information
Biotronic committed Apr 16, 2016
1 parent 4cb7c01 commit 60a2333
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
48 changes: 44 additions & 4 deletions std/conv.d
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ $(UL
If pointer is $(D char*), treat it as C-style strings.
In that case, this function is $(D @system).))
*/

T toImpl(T, S)(S value)
if (!(isImplicitlyConvertible!(S, T) &&
!isEnumStrToStr!(S, T) && !isNullToStr!(S, T)) &&
Expand Down Expand Up @@ -875,11 +876,21 @@ T toImpl(T, S)(S value)
()@trusted{ memcpy(result.ptr, value.ptr, value.length); }();
return cast(T) result;
}
else static if (isPointer!S && is(S : const(char)*))
else static if (isPointer!S && isSomeChar!(PointerTarget!S))
{
import core.stdc.string : strlen;
// It is unsafe because we cannot guarantee that the pointer is null terminated.
return value ? cast(T) value[0 .. strlen(value)].dup : null;
// This is unsafe because we cannot guarantee that the pointer is null terminated.
return () @system {
static if (is(S : const(char)*))
import core.stdc.string : strlen;
else
size_t strlen(S s) nothrow
{
S p = s;
while (*p++) {}
return p-s-1;
}
return toImpl!T(value ? value[0 .. strlen(value)].dup : null);
}();
}
else static if (isSomeString!T && is(S == enum))
{
Expand Down Expand Up @@ -930,6 +941,35 @@ unittest
immutable(char)* ptr = "hello".ptr;
auto result = ptr.to!(char[]);
}
// Bugzilla 8384
unittest
{
void test1(T)(T lp, string cmp)
{
foreach (e; AliasSeq!(char, wchar, dchar))
{
test2!(e[])(lp, cmp);
test2!(const(e)[])(lp, cmp);
test2!(immutable(e)[])(lp, cmp);
}
}

void test2(D, S)(S lp, string cmp)
{
assert(to!string(to!D(lp)) == cmp);
}

foreach (e; AliasSeq!("Hello, world!", "Hello, world!"w, "Hello, world!"d))
{
test1(e, "Hello, world!");
test1(e.ptr, "Hello, world!");
}
foreach (e; AliasSeq!("", ""w, ""d))
{
test1(e, "");
test1(e.ptr, "");
}
}

/*
Check whether type $(D T) can be used in a switch statement.
Expand Down
2 changes: 1 addition & 1 deletion std/datetime.d
Original file line number Diff line number Diff line change
Expand Up @@ -29929,7 +29929,7 @@ else version(Windows)
}


this(string name, TIME_ZONE_INFORMATION tzInfo) @safe immutable pure
this(string name, TIME_ZONE_INFORMATION tzInfo) @trusted immutable pure
{
super(name, to!string(tzInfo.StandardName.ptr), to!string(tzInfo.DaylightName.ptr));
_tzInfo = tzInfo;
Expand Down

0 comments on commit 60a2333

Please sign in to comment.