Skip to content

Commit 294a273

Browse files
committed
Improve StringLit interaction with immutable.
1 parent c1fd9b9 commit 294a273

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/urt/string/string.d

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ template StringLit(const(char)[] lit, bool zeroTerminate = true)
5757
return buffer;
5858
}();
5959
pragma(aligned, 2)
60-
private __gshared literal = LiteralData;
60+
private __gshared immutable literal = LiteralData;
6161

62-
enum String StringLit = String(literal.ptr + 2, false);
62+
enum StringLit = immutable(String)(literal.ptr + 2, false);
6363
}
6464

65-
6665
String makeString(const(char)[] s) nothrow
6766
{
6867
if (s.length == 0)
@@ -187,7 +186,17 @@ nothrow @nogc:
187186

188187
// TODO: I made this return ushort, but normally length() returns size_t
189188
ushort length() const pure
190-
=> ptr ? ((cast(ushort*)ptr)[-1] & 0x7FFF) : 0;
189+
{
190+
if (__ctfe)
191+
{
192+
version (LittleEndian)
193+
return ptr ? cast(ushort)(ptr[-2] | (ptr[-1] << 8)) & 0x7FFF : 0;
194+
else
195+
return ptr ? cast(ushort)((ptr[-1] | (ptr[-2] << 8)) & 0x7FFF) : 0;
196+
}
197+
else
198+
return ptr ? ((cast(ushort*)ptr)[-1] & 0x7FFF) : 0;
199+
}
191200

192201
bool opCast(T : bool)() const pure
193202
=> ptr != null && ((cast(ushort*)ptr)[-1] & 0x7FFF) != 0;

0 commit comments

Comments
 (0)