[Issue 7716] Add an indexed overload to countUntil

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Dec 11 05:30:59 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=7716


monarchdodra at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra at gmail.com


--- Comment #3 from monarchdodra at gmail.com 2012-12-11 05:30:54 PST ---
(In reply to comment #0)
> Sometimes I want to find the index of a substring, but not necessarily the
> index of the first occurence. E.g. in the string "foo x foo y" I want to get
> the index of the second foo.

This would conflict with this other request:
http://d.puremagic.com/issues/show_bug.cgi?id=5507

which would allow counting until multiple needles, eg:
//----
string s = "foo x bar y";
size_t index = s.countUntil("bar", 'x');
assert(index == 4);
//----

So what you are asking for may have to be implemented as "countUntilN" or
something:

//-----
string s = "foo x bar y";
size_t index = s.countUntilN(1, "bar", 'x');
assert(index == 6); //First found 'x', then found "bar".
//-----

> Here's a simple implementation based on the
> existing countUntil:

Just want to point out that that implementation is based on an old and buggy
implementation of countUntil. 2 problems:
1. No support for reference type ranges.
2. Incorrect support of unicode.

The issue 1 is not that big of an issue, but is unacceptable of a standard
library.

Issue 2 depends on what you want. Do you want the *index* in the string, or the
logical *range_position*:

"日本語".countUntil(本); //1 popFront once
"日本語".countUntil(本); //3 slice index: "日本語"[3 .. $]

If you plan on using countUntil(N), then I'd recommend basing your
implementation on https://github.com/D-Programming-Language/phobos/pull/951

Finally, keep in mind that the standard function "count" skips over overlaps:
"ababab".count("abab"); //produces 1. Only 1 match.
However, your countUntil implementation would find that second match:
"ababab".countUntilN(1, "abab"); //Produces 2
This would be kind of weird:
"Given a range which holds 1 instance of a needle, countUntilN is capable of
finding the second occurrence of said needle :/"

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


More information about the Digitalmars-d-bugs mailing list