dlang-tour / core

D Language online tour (https://tour.dlang.org/) and online editor (https://run.dlang.io/)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

URL Shortener doesn't seem to work for large code examples

JinShil opened this issue · comments

commented

I entered the following code at run.dlang.io:

alias UnsignedStringBuf = char[20];

char[] unsignedToTempString()(ulong value, return scope char[] buf, uint radix = 10) @safe
{
    if (radix < 2)
        // not a valid radix, just return an empty string
        return buf[$ .. $];

    size_t i = buf.length;
    do
    {
        if (value < radix)
        {
            ubyte x = cast(ubyte)value;
            buf[--i] = cast(char)((x < 10) ? x + '0' : x - 10 + 'a');
            break;
        }
        else
        {
            ubyte x = cast(ubyte)(value % radix);
            value = value / radix;
            buf[--i] = cast(char)((x < 10) ? x + '0' : x - 10 + 'a');
        }
    } while (value);
    return buf[i .. $];
}

private struct TempStringNoAlloc
{
    // need to handle 65 bytes for radix of 2 with negative sign.
    private char[65] _buf;
    private ubyte _len;
    auto get() return
    {
        return _buf[$-_len..$];
    }
    alias get this;
}

auto unsignedToTempString()(ulong value, uint radix = 10) @safe
{
    TempStringNoAlloc result = void;
    result._len = unsignedToTempString(value, result._buf, radix).length & 0xff;
    return result;
}

private void onArrayCastError()(string fromType, size_t fromSize, string toType, size_t toSize) @trusted pure
{
    const(char)[][8] msgComponents =
    [
        "Cannot cast `"
        , fromType
        , "` to `"
        , toType
        , "`; an array of size "
        , unsignedToTempString(fromSize)
        , " does not align on an array of size "
        , unsignedToTempString(toSize)
    ];

    // `alloca` would be preferred here, but it doesn't work in -betterC
    // See https://issues.dlang.org/show_bug.cgi?id=19159
    char[1024] msg;

    size_t index = 0;
    foreach (m ; msgComponents)
        foreach (c; m)
            msg[index++] = c;
    msg[index] = '\0';

    // first argument must evaluate to `false` at compile-time to maintain memory safety in release builds
    assert(false, msg);
}

TTo[] __ArrayCast(TFrom, TTo)(TFrom[] from) @nogc pure @trusted
{
    const fromSize = from.length * TFrom.sizeof;
    const toLength = fromSize / TTo.sizeof;

    if ((fromSize % TTo.sizeof) != 0)
    {
        onArrayCastError(TFrom.stringof, fromSize, TTo.stringof, toLength * TTo.sizeof);
    }

    struct Array
    {
        size_t length;
        void* ptr;
    }
    auto a = cast(Array*)&from;
    a.length = toLength; // jam new length
    return *cast(TTo[]*)a;
}

extern(C) void main()
{
    byte[int.sizeof * 3] b = cast(byte) 0xab;
    long[] l;

    l = __ArrayCast!(byte, long)(b);
}

When I tried to shorten to a URL it gave me https://run.dlang.io/gist/f36e15290593b748176915a75e5c6204?args=-betterC which results in a "404: Not Found" in the code editor.

The same for me: https://forum.dlang.org/post/hjehtqghdkzotfjwoqoe@forum.dlang.org

If this is a technical limitation then it would be good to show "too much code" in the URL field instead of the malfunctioning URL.

As a workaround, the "Export Gist" can be used. In fact, I think we should replace the current URL shortener completely with "Export to Gist".