Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-03-08
07:47 • Ticket [d2323d6c28] add parameter ?lastIndex? to string first status still... artifact: 6cbb73674d user: anonymous
2016-03-07
18:22 • Ticket [d2323d6c28]: 3 changes artifact: d71370629e user: dgp
2016-02-25
14:35 • Add attachment tcl-8.6.4-string-first.patch to ticket [d2323d6c284a5081... artifact: 84d752dff4 user: anonymous
14:33 • New ticket [d2323d6c28] add parameter ?lastIndex? to string first. artifact: 48d7e852f6 user: anonymous

add parameter ?lastIndex? to [string first]

Opend2323d6c284a5081e818dc02f0bbe695a7aa1c61Created 11 years agoUpdated 11 years ago
Descriptionplain2016-02-25 14:33:02
The dubious implementation of tcllib's ::textutil::trim::trimPrefix caught my eye. Why scan the full string when actually looking at the start only?

% set haystack [string repeat abc 1000]d
% set needle abcd
% string first $needle $haystack
2997
% time {string first $needle $haystack} 1000
10.149 microseconds per iteration
% time {string first $needle $haystack 0 0} 1000
0.712 microseconds per iteration

In this form, I cannot attach my patch file. Will attach or post after creating this ticket. The patch is for the implementation and the tests, not for the documentation.
dgpplain2016-03-07 18:22:48
I did not (yet) look at the patch, but if I understand the
motivating example of limiting a [string first] search to
a subrange of a string, why wouldn't the right answer
be to compose [string first] with [string range] ?
anonymous (claiming to be heinrichmartin)plain2016-03-08 07:47:49
> to compose [string first] with [string range]

This was my first assumption when looking at the implementation of trimPrefix. Then I thought that someone might have chosen to _not_ create a copy of the prefix of size of needle. And [string first $needle [string range $haystack 0 [string length $needle]-1]] is somewhat lengthy.

% time {string first $needle $haystack} 1000
13.128 microseconds per iteration
% time {string first $needle [string range $haystack 0 [string length $needle]-1]} 1000
1.435 microseconds per iteration
% # other machine than the above measurement: assuming +50% to +100% slower than [string first $needle $haystack 0 0];
% # also note that a string comparison (not integer as after [string first]) is still left to trimPrefix ...

In the end, I looked at the C code and found that it was an easy task to add this feature. It is an extension that might or might not be interesting for the community. I package our custom version from pristine sources and can therefore easily provide the patch as-is. I added tests, but no doc.

OT: I also considered another patch to allow multiple + and - in the index, e.g. consider the postfix scenario [string range $haystack end-[string length $needle]+1 end].
Log in (anonymous is fine) to add a comment.