www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13067] New: safe std.algorithm.findSplitBefore

https://issues.dlang.org/show_bug.cgi?id=13067

          Issue ID: 13067
           Summary:  safe std.algorithm.findSplitBefore
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

I am not sure if this enhancement is possible.


void main()  safe {
    import std.algorithm: findSplitBefore;
    auto txt = "just testing";
    const part = txt.findSplitBefore(" ")[0];
    assert(part == "just");
}



dmd 2.066beta1 gives:

test.d(4,37): Error: safe function 'D main' cannot call system function
'std.algorithm.findSplitBefore!("a == b", string, string).findSplitBefore'


To look for the cause, I've seen that findSplitBefore calls std.algorithm.find,
and one of the oveloads of find contains unsafe code (the cast(Representation)
):


R1 find(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle)  safe
if (isForwardRange!R1 && isForwardRange!R2
        && is(typeof(binaryFun!pred(haystack.front, needle.front)) : bool)
        && !isRandomAccessRange!R1)
{
...
        alias Representation =
            Select!(haystack[0].sizeof == 1, ubyte[],
                Select!(haystack[0].sizeof == 2, ushort[], uint[]));
        // Will use the array specialization
        return cast(R1) .find!(pred, Representation, Representation)
            (cast(Representation) haystack, cast(Representation) needle);
...

--
Jul 07 2014