On Friday, 19 July 2013 at 17:18:00 UTC, JS wrote:
both functions work separately but when i uncomment the string version I get an error about the string version shadowing.

import std.stdio, std.cstream;

string[] split(T)(string s, T d) if (is(T == char) || is(T == string))
{
        int i = 0, oldj = 0; bool ok = true;

i and oldj should probably be size_t. The same for other ints
throughout.

        string[] r;
        foreach(j, c; s)

The first j is declared here.

        {
                static if (is(T == char))
                {
                        if (c == d)
                        {
                                if (!ok) { oldj++; continue; }
                                if (r.length <= i) r.length += 5;
                                r[i] = s[oldj..j];
                                i++; oldj = j+1;
                                ok = false;
                        } else if (!ok) ok = true;
                }
                else if (is(T == string))
                {
                /*
                        for(int j = 0; j < s.length - d.length; j++)

This j would shadow the one above. Just choose another name.

                        {
                                if (s[j..j + d.length] == d)
                                {
                                        if (!ok) { oldj++; continue; }
                                        if (i == r.length) r.length += 5;
                                        r[i] = s[oldj..j - d.length + 1];
                                        i++; oldj = j + d.length;
                                        ok = false;
                                } else if (!ok) ok = true;
                
                        }
                */
                }
        }
        if (oldj < s.length)
        {
                if (r.length <= i) r.length++;
                r[i] = s[oldj..$];
                i++;
        }
        r.length = i;
        return r;
}

Reply via email to