www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9456] New: decodeFront erroneously alters its input range with reference type ranges

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9456

           Summary: decodeFront erroneously alters its input range with
                    reference type ranges
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: jmdavisProg gmx.com


--- Comment #0 from Jonathan M Davis <jmdavisProg gmx.com> 2013-02-05 17:05:12
PST ---
This code

import std.array;
import std.conv;
import std.utf;

class RefRange(C)
{
     property bool empty() { return _str.empty; }
     property C front() { return _str[0]; }
    void popFront() { _str = _str[1 .. $]; }
     property C back() { return _str[$ - 1]; }
    void popBack() { _str = _str[0 .. $ - 1]; }
     property auto save() { return new RefRange(_str); }
     property size_t length() { return _str.length; }

    this(inout(C)[] str)
    {
        _str = to!(C[])(str);
    }

    C[] _str;
}

void main()
{
    auto asciiStr = "hello world";
    auto asciiRange = new RefRange!char(asciiStr);
    //auto asii = asciiStr;
    size_t index = 1;
    assert(asciiRange.decodeFront(index) == 'h');
    assert(asciiRange.length == asciiStr.length);

    auto uniStr = "プログラミング";
    auto uniRange = new RefRange!char(uniStr);
    //auto uniRange = uniStr;
    index = 3;
    assert(uniRange.decodeFront(index) == 'プ');
    assert(uniRange.length == uniStr.length);
}

gives this error when it runs

core.exception.AssertError q(37): Assertion failure
----------------
./q(_d_assertm+0x26) [0x42c946]
./q() [0x42ac22]
./q(_Dmain+0xfa) [0x42a3da]
./q(extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).void runMain()+0x18) [0x42d0dc]
./q(extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).void tryExec(scope void delegate())+0x2a) [0x42cc1a]
./q(extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).void runAll()+0x3b) [0x42d123]
./q(extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).void tryExec(scope void delegate())+0x2a) [0x42cc1a]
./q(_d_run_main+0x1a8) [0x42cbd4]
./q(main+0x17) [0x42ca27]
/usr/lib/libc.so.6(__libc_start_main+0xf5) [0x7f60c980aa15]
----------------

The last assertion fails. If you swap which lines are commented out so that
strings are used instead of RefRange, then the code succeeds. The problem is
that decodeFront calls popFront when dealing with unicode, and it doesn't save,
so depending on what's passed to it, it may or may not pop elements. It's more
efficient to save internally in the cases where it needs to rather than ask the
caller to do so, so the correct fix is to make it save if it's going to pop
anything.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 05 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9456


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9456



--- Comment #1 from Jonathan M Davis <jmdavisProg gmx.com> 2013-02-05 22:31:00
PST ---
https://github.com/D-Programming-Language/phobos/pull/1116

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9456


deadalnix <deadalnix gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |deadalnix gmail.com


--- Comment #2 from deadalnix <deadalnix gmail.com> 2013-02-08 09:15:12 PST ---
I think this should be the other way around. decodeFront should ALWAYS alter
its input.

This is the only way to make it work with input ranges.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 08 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9456



--- Comment #3 from Jonathan M Davis <jmdavisProg gmx.com> 2013-02-10 03:47:54
PST ---
New pull request: https://github.com/D-Programming-Language/phobos/pull/1129

It makes it so that decodeFront takes its argument by ref, since that's the
only way to make it consistent across range types, much as it would ideally act
like decode and never pop off any elements.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 10 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9456



--- Comment #4 from github-bugzilla puremagic.com 2013-04-27 23:10:35 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/aae9d3806abaaf389814b674658f4b67f19d42e6
Fix for issue# 9456.

This change makes it so that decodeFront takes its range by ref and pops
off the code units that it decodes (unlike decode). It pretty much has
to do that, since it supports input ranges, and they can't do anything
else.

https://github.com/D-Programming-Language/phobos/commit/3e60ef9eca29156f8e1e153d42a04ec59d8df2bd
Merge pull request #1129 from jmdavis/decode

Fix Issue 9456 -  decodeFront is inconsistent in whether it pops elements off
of the range or not

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9456


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 28 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9456



--- Comment #5 from github-bugzilla puremagic.com 2013-05-17 13:34:51 PDT ---
Commits pushed to 2.063 at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/aae9d3806abaaf389814b674658f4b67f19d42e6
Fix for issue# 9456.

https://github.com/D-Programming-Language/phobos/commit/3e60ef9eca29156f8e1e153d42a04ec59d8df2bd
Merge pull request #1129 from jmdavis/decode

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 17 2013