Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix Issue 8516 - std.string.representation works incorrect for shared(const(T)) types #742

Merged
merged 1 commit into from

2 participants

@andralex andralex commented on the diff
std/string.d
((13 lines not shown))
- else static if (Char.sizeof == 4) enum t = "uint";
- else static assert(false); // can't happen due to isSomeChar!Char
-
- // Get representation qualifier
- static if (is(Char == immutable)) enum q = "immutable";
- else static if (is(Char == const)) enum q = "const";
- else static if (is(Char == shared)) enum q = "shared";
- else enum q = "";
-
- // Result type is qualifier(RepType)[]
- static if (q.length)
- return mixin("cast(" ~ q ~ "(" ~ t ~ ")[]) s");
- else
- return mixin("cast(" ~ t ~ "[]) s");
-}
+ alias TypeTuple!(ubyte, ushort, uint)[Char.sizeof / 2] U;
@andralex Owner

ingenious

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

Thanks, loved it.

@andralex andralex merged commit e68ec9c into from
@denis-sh

As I asked in druntime pull 286:

And who have to close the issue? Is it issue reporter, pull sender or pull committer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 7, 2012
  1. @denis-sh
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 38 deletions.
  1. +34 −38 std/string.d
View
72 std/string.d
@@ -774,7 +774,7 @@ unittest
/**
- * Returns the representation type of a string, which is the same type
+ * Returns the representation of a string, which has the same type
* as the string except the character type is replaced by $(D ubyte),
* $(D ushort), or $(D uint) depending on the character width.
*
@@ -782,58 +782,54 @@ unittest
----
string s = "hello";
static assert(is(typeof(representation(s)) == immutable(ubyte)[]));
+assert(representation(s) is cast(immutable(ubyte)[]) s);
----
*/
auto representation(Char)(Char[] s) pure nothrow
if(isSomeChar!Char)
{
// Get representation type
- static if (Char.sizeof == 1) enum t = "ubyte";
- else static if (Char.sizeof == 2) enum t = "ushort";
- else static if (Char.sizeof == 4) enum t = "uint";
- else static assert(false); // can't happen due to isSomeChar!Char
-
- // Get representation qualifier
- static if (is(Char == immutable)) enum q = "immutable";
- else static if (is(Char == const)) enum q = "const";
- else static if (is(Char == shared)) enum q = "shared";
- else enum q = "";
-
- // Result type is qualifier(RepType)[]
- static if (q.length)
- return mixin("cast(" ~ q ~ "(" ~ t ~ ")[]) s");
- else
- return mixin("cast(" ~ t ~ "[]) s");
-}
+ alias TypeTuple!(ubyte, ushort, uint)[Char.sizeof / 2] U;
@andralex Owner

ingenious

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
-unittest
-{
- auto c = to!(char[])("hello");
- static assert(is(typeof(representation(c)) == ubyte[]));
+ // const and immutable storage classes
+ static if (is(Char == immutable)) alias immutable(U) T;
+ else static if (is(Char == const)) alias const(U) T;
+ else alias U T;
- auto w = to!(wchar[])("hello");
- static assert(is(typeof(representation(w)) == ushort[]));
+ // shared storage class (because shared(const(T)) is possible)
+ static if (is(Char == shared)) alias shared(T) ST;
+ else alias T ST;
- auto d = to!(dchar[])("hello");
- static assert(is(typeof(representation(d)) == uint[]));
+ return cast(ST[]) s;
+}
- const(char[]) cc = "hello";
- static assert(is(typeof(representation(cc)) == const(ubyte)[]));
+unittest
+{
+ void test(Char, T)(Char[] str)
+ {
+ static assert(is(typeof(representation(str)) == T[]));
+ assert(representation(str) is cast(T[]) str);
+ }
- const(wchar[]) cw = "hello"w;
- static assert(is(typeof(representation(cw)) == const(ushort)[]));
+ test!(immutable(char) , immutable(ubyte) )("hello" );
+ test!(immutable(wchar), immutable(ushort))("hello"w);
+ test!(immutable(dchar), immutable(uint) )("hello"d);
- const(dchar[]) cd = "hello"d;
- static assert(is(typeof(representation(cd)) == const(uint)[]));
+ test!(const(char) , const(ubyte) )("hello" );
+ test!(const(wchar), const(ushort))("hello"w);
+ test!(const(dchar), const(uint) )("hello"d);
- string s = "hello";
- static assert(is(typeof(representation(s)) == immutable(ubyte)[]));
+ test!(char , ubyte )("hello" .dup);
+ test!(wchar, ushort)("hello"w.dup);
+ test!(dchar, uint )("hello"d.dup);
- wstring iw = "hello"w;
- static assert(is(typeof(representation(iw)) == immutable(ushort)[]));
+ test!(shared(char) , shared(ubyte) )(cast(shared) "hello" .dup);
+ test!(shared(wchar), shared(ushort))(cast(shared) "hello"w.dup);
+ test!(shared(dchar), shared(uint) )(cast(shared) "hello"d.dup);
- dstring id = "hello"d;
- static assert(is(typeof(representation(id)) == immutable(uint)[]));
+ test!(const(shared(char)) , const(shared(ubyte)) )("hello" );
+ test!(const(shared(wchar)), const(shared(ushort)))("hello"w);
+ test!(const(shared(dchar)), const(shared(uint)) )("hello"d);
}
Something went wrong with that request. Please try again.