Expected behaviour for sort.SearchStrings?

1,052 views
Skip to first unread message

Andrew Deane

unread,
Jul 10, 2012, 5:28:26 AM7/10/12
to golan...@googlegroups.com
Hello,

I'm trying to find the index of a value in an array of strings. I may be doing this wrong but I'm using sort.SearchStrings and it doesn't seem to do what (I think) it should (arrogant I know).

I would expect that if the value isn't found in the array I would be returned the length of the array (or -1) to indicate I fell off the end. What I do get returned is the index of the first failing value because of the way that SearchStrings calls Search. I'm all for this fail fast short circuit of the search. 

I can check the string at the returned index in the array is equal to the value I am looking for to find my answer, but I was just wondering if this was what is expected.

I'm just looking for the canonical way of detecting a failed search really. I'm sure I'm missing something.

Here is some code:
package main

import (
"fmt"
"sort"
)

func main() {

a := make([]string, 10)

a[0] = "0"
a[1] = "1"
a[2] = "3"
a[3] = "2"
a[4] = "a"
a[5] = "c"
a[6] = "d"
a[7] = "aa"
a[8] = "111"
a[9] = "f"

fmt.Println(a)
sort.Strings(a)
fmt.Println(a)
fmt.Println(sort.SearchStrings(a, "a"))
fmt.Println(sort.SearchStrings(a, "11"))
fmt.Println(sort.SearchStrings(a, "b"))

fmt.Println(Found("b", a))
fmt.Println(Found("a", a))

}

func Found(s string, a []string) bool {
return a[sort.SearchStrings(a, s)] == s
}


Giving:

$ go run array.go 
[0 1 3 2 a c d aa 111 f]
[0 1 111 2 3 a aa c d f]
5
2
7
false
true

Thanks all,
Andy.

Ian Lance Taylor

unread,
Jul 10, 2012, 6:36:28 AM7/10/12
to Andrew Deane, golan...@googlegroups.com
On Tue, Jul 10, 2012 at 2:28 AM, Andrew Deane
<andrew....@googlemail.com> wrote:
>
> I would expect that if the value isn't found in the array I would be
> returned the length of the array (or -1) to indicate I fell off the end.
> What I do get returned is the index of the first failing value because of
> the way that SearchStrings calls Search. I'm all for this fail fast short
> circuit of the search.

In effect, the function returns the point where the new string would
be inserted, if you plan to insert it. You need an additional
comparison to see if the string you want is in the array.

> I can check the string at the returned index in the array is equal to the
> value I am looking for to find my answer, but I was just wondering if this
> was what is expected.

Yes, this is expected.

Ian

Andrew Deane

unread,
Jul 10, 2012, 6:52:35 AM7/10/12
to golan...@googlegroups.com, Andrew Deane
Excellent.

Thanks.


On Tuesday, July 10, 2012 11:36:28 AM UTC+1, Ian Lance Taylor wrote:
On Tue, Jul 10, 2012 at 2:28 AM, Andrew Deane

Martin Angers

unread,
Sep 28, 2012, 11:25:50 AM9/28/12
to golan...@googlegroups.com, Andrew Deane
I think the doc should be updated to make this more obvious. At the moment it redirects the reader to Search() as to how the returned index works (" ...returns the index as specified by Search."), and then the doc for Search() talks about the algorithm and the closure passed to Search() to understand the result. In the case of SearchStrings() (or Ints, ...), we don't pass the closure, so it's already a little confusing, and Search()'s explanation, although precise, requires careful reading. Add this to the fact that many people will just *assume* that this will return the index at which the string is found or -1...
Reply all
Reply to author
Forward
0 new messages