1. Ross Light
  2. stackexchange

Commits

Sergio Tulentsev  committed 803300c

Revert deletion of JoinIDs

  • Participants
  • Parent commits 0a0ef3c
  • Branches default

Comments (0)

Files changed (2)

File stackexchange.go

View file
 	return string(buf)
 }
 
+// JoinIDs builds a string of semicolon-separated IDs.
+func JoinIDs(ids []int) string {
+	const bytesPerID = 9
+	buf := make([]byte, 0, bytesPerID*len(ids))
+	for _, id := range ids {
+		if len(buf) > 0 {
+			buf = append(buf, ';')
+		}
+		buf = strconv.AppendInt(buf, int64(id), 10)
+	}
+	return string(buf)
+}
+
 func parseResponse(r io.Reader, v interface{}) (*Wrapper, error) {
 	var result struct {
 		Items items `json:"items"`

File stackexchange_test.go

View file
 	}
 }
 
+func TestJoinIDs(t *testing.T) {
+	tests := []struct {
+		IDs    []int
+		String string
+	}{
+		{nil, ""},
+		{[]int{}, ""},
+		{[]int{1}, "1"},
+		{[]int{40}, "40"},
+		{[]int{1, 40}, "1;40"},
+		{[]int{1, 40, 72}, "1;40;72"},
+		{[]int{1, 5, 2, 3}, "1;5;2;3"},
+	}
+	for _, test := range tests {
+		if out := JoinIDs(test.IDs); out != test.String {
+			t.Errorf("JoinIDs(%v) = %q; want %q", test.IDs, out, test.String)
+		}
+	}
+}
+
 func ExampleDo() {
 	var questions []Question
 	wrapper, err := Do(PathQuestions, &questions, &Params{