strings.IndexAll?

371 views
Skip to first unread message

Ilya Kowalewski

unread,
Dec 2, 2015, 4:59:14 PM12/2/15
to golang-nuts
Hey there,

// IndexAll returns indeces of all instances of sep in s.
func
IndexAll(s, sep string) []int

The function signature above looks like one I'd love to see in strings package. We've got Index and IndexLast already though. Would my implementation get rejected if I submit one?

Brad Fitzpatrick

unread,
Dec 2, 2015, 5:07:18 PM12/2/15
to Ilya Kowalewski, golang-nuts
The standard library is mostly frozen. This probably wouldn't meet the utility bar.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Symonds

unread,
Dec 2, 2015, 5:07:42 PM12/2/15
to Ilya Kowalewski, golang-nuts
It doesn't seem like a particularly significant addition. Any use of
it would probably be better written as an explicit loop, calling Index
repeatedly, which would not require memory allocation (like your
suggestion would).

for i := strings.Index(s, sep); i >= 0; i += strings.Index(s[i:], sep) {
... // i is the next index
}

David Symonds

unread,
Dec 2, 2015, 5:08:16 PM12/2/15
to Ilya Kowalewski, golang-nuts
(oh, except you'll need an extra +1 in my code. that'll teach me.)

Dan Kortschak

unread,
Dec 2, 2015, 5:41:57 PM12/2/15
to Ilya Kowalewski, golang-nuts
Would this work for you? Otherwise David's is simple.

https://golang.org/pkg/index/suffixarray/

Ian Byrd

unread,
Dec 2, 2015, 6:05:29 PM12/2/15
to David Symonds, golang-nuts
Your implementation results in an endless loop, this is the correct one:

for i := strings.Index(s, sep); i >= 0; {
// utilizing s[i:]

// the next instance
j := strings.Index(s[i+1:], sep)

if j >= 0 {
i += j + 1
} else {
break

Ian Byrd

unread,
Dec 2, 2015, 6:06:31 PM12/2/15
to Dan Kortschak, golang-nuts
Wow, didn't know stdlib had builtin suffix trees! Pretty handy, thanks.

Tim K

unread,
Dec 2, 2015, 7:33:22 PM12/2/15
to golang-nuts, dan.ko...@adelaide.edu.au, i...@byrd.im
It's Google we're talking about here. Search is their bread & butter :-)
Reply all
Reply to author
Forward
0 new messages