<commit_before>package main

import (
	"bufio"
	"bytes"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

type mPkgdoc struct {
	Q    jString
	Path jString
}

type mPkgdocDoc struct {
	Path string
	Doc  string
}

func mPkgdocFetchDoc(m *mPkgdoc) (interface{}, string) {
	res := M{}
	path := strings.TrimSpace(m.Path.String())
	if path == "" {
		return res, "invalid query"
	}

	req, err := http.NewRequest("GET", "http://godoc.org/"+path, nil)
	if err != nil {
		return res, errStr(err)
	}

	req.Header.Add("Accept", "text/plain")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return res, errStr(err)
	}
	defer resp.Body.Close()
	s, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		return res, errStr(err)
	}

	res["doc"] = mPkgdocDoc{
		Path: path,
		Doc:  string(s),
	}
	return res, ""
}

func mPkgdocSearch(m *mPkgdoc) (interface{}, string) {
	res := M{}
	s := strings.TrimSpace(m.Q.String())
	if s == "" {
		return res, "invalid query"
	}

	req, err := http.NewRequest("GET", "http://godoc.org/?q="+url.QueryEscape(s), nil)
	if err != nil {
		return res, errStr(err)
	}

	req.Header.Set("User-Agent", "GoSublime")
	req.Header.Add("Accept", "text/plain")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return res, errStr(err)
	}
	defer resp.Body.Close()

	packages := []mPkgdocDoc{}
	rd := bufio.NewReader(resp.Body)
	for {
		s, err := rd.ReadBytes('\n')
		if err != nil {
			break
		}

		s = bytes.TrimSpace(s)
		if len(s) > 0 {
			l := bytes.SplitN(s, []byte{' '}, 2)
			v := mPkgdocDoc{<commit_msg>* add common code to its own function - avoid forgetting which headers to set<commit_after>package main

import (
	"bufio"
	"bytes"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

type mPkgdoc struct {
	Q    jString
	Path jString
}

type mPkgdocDoc struct {
	Path string
	Doc  string
}

func setupReq(req *http.Request) {
	req.Header.Set("User-Agent", "GoSublime")
	req.Header.Set("Accept", "text/plain")
}

func mPkgdocFetchDoc(m *mPkgdoc) (interface{}, string) {
	res := M{}
	path := strings.TrimSpace(m.Path.String())
	if path == "" {
		return res, "invalid query"
	}

	req, err := http.NewRequest("GET", "http://godoc.org/"+path, nil)
	if err != nil {
		return res, errStr(err)
	}

	setupReq(req)
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return res, errStr(err)
	}
	defer resp.Body.Close()
	s, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		return res, errStr(err)
	}

	res["doc"] = mPkgdocDoc{
		Path: path,
		Doc:  string(s),
	}
	return res, ""
}

func mPkgdocSearch(m *mPkgdoc) (interface{}, string) {
	res := M{}
	s := strings.TrimSpace(m.Q.String())
	if s == "" {
		return res, "invalid query"
	}

	req, err := http.NewRequest("GET", "http://godoc.org/?q="+url.QueryEscape(s), nil)
	if err != nil {
		return res, errStr(err)
	}

	setupReq(req)
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return res, errStr(err)
	}
	defer resp.Body.Close()

	packages := []mPkgdocDoc{}
	rd := bufio.NewReader(resp.Body)
	for {
		s, err := rd.ReadBytes('\n')
		if err != nil {
			break
		}

		s = bytes.TrimSpace(s)
		if len(s) > 0 {
			l := bytes.SplitN(s, []byte{' '}, 2)
			v := mPkgdocDoc{
{'fraction_non_alphanumeric': 0.1827956989247312, 'fraction_numerical': 0.001265022137887413, 'mean_word_length': 3.473833097595474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4966793', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1308, 'n_words': 301}