Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort.SearchStrings finds string even if exact string is not found #38386

Closed
prithvipal opened this issue Apr 12, 2020 · 4 comments
Closed

sort.SearchStrings finds string even if exact string is not found #38386

prithvipal opened this issue Apr 12, 2020 · 4 comments

Comments

@prithvipal
Copy link

prithvipal commented Apr 12, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/prithvipalsingh/Library/Caches/go-build"
GOENV="/Users/prithvipalsingh/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/prithvipalsingh/PRITHVI/Github"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/hs/c705kkdj0db6fb0npphggk3m0000gn/T/go-build902606486=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Executed following program:

package main

import (
	"fmt"
	"sort"
)

func main() {
	strs := []string{
		"Delhi",
		"Mumbai",
		"Jaipur",
		"Chandigarh",
	}
	sort.StringSlice(strs).Sort()
	fmt.Println(strs)
	n := sort.SearchStrings(strs, "Jai")
	fmt.Println(n)
}

Please note in above program that I have provided "Jai" to search string. I have not provided "Jaipur" which is availalbe in the string slice.

What did you expect to see?

4
I would expect 4 as output because when the string is not found in slice then the SearchString returns the length of the slice.

What did you see instead?

2

@seankhliao
Copy link
Member

Working as expected

The return value is the index to insert x if x is not present

@jonyhy96
Copy link

According to the comment of the function:

// SearchStrings searches for x in a sorted slice of strings and returns the index
// as specified by Search. The return value is the index to insert x if x is not
// present (it could be len(a)).
// The slice must be sorted in ascending order.
//
func SearchStrings(a []string, x string) int {

It is returning the position of the slice that you can insert your string.

@prithvipal
Copy link
Author

@seankhliao and @joe2far Thank you for the reply.

I still have a question. If we see the above code provided by me, I have given the subset of the string available in the slice. The string available at index 2(after sorting) is "Jaipur" but I have provided "Jai" as input to the SearchString. The SearchString returns the index even there is no exact match of the string. So my question is, does the SearchString function check for a subset of the string available in the slice?

@bradfitz
Copy link
Contributor

For Go questions, see https://golang.org/wiki/Questions

@golang golang locked and limited conversation to collaborators Apr 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants