Thread overview
[Issue 5201] New: std.string.indexOf and std.algorithm.indexOf return different things for narrow strings
Nov 16, 2010
Jonathan M Davis
Nov 17, 2010
Jonathan M Davis
November 11, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5201

           Summary: std.string.indexOf and std.algorithm.indexOf return
                    different things for narrow strings
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrei@metalanguage.com


--- Comment #0 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-11-11 10:56:43 PST ---
std.algorithm.indexOf returns logical position because it uses the forward range interface, and std.string.indexOf returns the physical position.

This is potentially confusing (the semantics of a program depends on its
imports).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 11, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5201


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 16, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5201


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #1 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-11-16 12:16:03 PST ---
I would think that you'd want character arrays and indexOf to always function like it does in std.string - the index is the index of the array, but comparison is done by code point rather than code unit. So, then I would think that the correct solution would be either to special case std.algorithm.indexOf() for all character arrays (and deprecated std.string.indexOf()) or to make it so that it doesn't work with character arrays and have std.string.indexOf() deal with them instead. There could be a good reason that I haven't thought of to not to do it that way though.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 17, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5201



--- Comment #2 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-11-16 16:19:18 PST ---
Glancing at std.string.indexOf() again, it has different parameters than std.algorithm - dealing with case sensitivity and whatnot. It also takes a dchar rather than a string or some variety to find the index of, which differs from what std.algorithm does - since it uses startsWith(). Given the differences, I'd argue in favor of just making std.algorithm.indexOf() not working with char arrays. However, it would be nice if std.string.indexOf() were made to work with strings rather than dchar (or in addition to it), and it doesn't take a predicate like std.algorithm's indexOf() does, and those could be useful.

Ultimately, I think that the two functions should be made to work more similarly than they do on more than just what type of index that they're returning - particularly since a _lot_ of programs are going to be importing both std.algorithm and std.string (most of mine definitely do). Fixing the index issue though is the highest priority in that it changes behavior based on imports whereas the other differences are major enough to result in compilation errors if you use the wrong one.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5201


Andrei Alexandrescu <andrei@erdani.com> changed:

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


--- Comment #3 from Andrei Alexandrescu <andrei@erdani.com> 2013-03-09 19:26:10 PST ---
std.algorithm.indexOf has been deprecated.

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