Bug 13067 – @safe std.algorithm.findSplitBefore

Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2014-07-07T10:10:00Z
Last change time
2014-12-04T21:57:23Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2014-07-07T10:10:54Z
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); ...
Comment #1 by bearophile_hugs — 2014-12-04T21:57:23Z
This seems to work in dmd 2.067alpha.