<commit_before>package form

import (
	"encoding/json"
	"mime/multipart"
	"net/http"
	"net/url"
)

type value struct {
	Form          url.Values
	PostForm      url.Values
	MultipartForm *multipart.Form
}

func newValue(r *http.Request) *value {
	v := &value{}
	count := 0
	if r.Form != nil {
		if len(r.Form) > 0 {
			v.Form = url.Values{}
		}
		for key, value := range r.Form {
			count++
			v.Form[key] = append(v.Form[key], value...)
		}
	}
	if r.PostForm != nil {
		if len(r.PostForm) > 0 {
			v.PostForm = url.Values{}
		}
		for key, value := range r.PostForm {
			count++
			v.PostForm[key] = append(v.PostForm[key], value...)
		}
	}
	if r.MultipartForm != nil {
		if len(r.MultipartForm.Value) > 0 {
			if v.MultipartForm == nil {
				v.MultipartForm = &multipart.Form{}
			}
			v.MultipartForm.Value = map[string][]string{}
		}
		for key, value := range r.MultipartForm.Value {
			count++
			v.MultipartForm.Value[key] = append(v.MultipartForm.Value[key], value...)
		}
		if len(r.MultipartForm.File) > 0 {
			if v.MultipartForm == nil {
				v.MultipartForm = &multipart.Form{}
			}
			v.MultipartForm.File = map[string][]*multipart.FileHeader{}
		}
		for key, value := range r.MultipartForm.File {
			count++
			v.MultipartForm.File[key] = append(v.MultipartForm.File[key], value...)
		}
	}
	if count == 0 {
		return nil
	}
	return v
}

func newValueSerializeArray(r *http.Request) *value {
	v := &value{PostForm: url.Values{}}

	data := []struct {
		Name  string `json:"name"`
		Value string `json:"value"`
	}{}

	jDec := json.NewDecoder(r.Body)
	defer r.Body.Close()

	jDec.Decode(&data)

	for _, item := range data {
		v.PostForm[item.Name] = append(v.PostForm[item.Name], item.Value)
	}

	return v
}

func (r *value) Shift(name string) string {
	str := ""
	if r.MultipartForm != nil && len(r.MultipartForm.Value[name]) > 0 {
		if len(r.MultipartForm.Value[name]) <= 1 {
			if len(r.MultipartForm.Value[name]) == 1 {
				str = r.MultipartForm.Value[name][0]
			}
			delete(r.MultipartForm.Value, name)
		} else {
			str, r.MultipartForm.Value[name] =
				r.MultipartForm.Value[name][0], r.MultipartForm.Value[name][1:]
		}
	} else if r.Form != nil && len(r.Form[name]) > 0 {
		if len(r.Form[name]) <= 1 {
			if len(r.Form[name]) == 1 {
				str = r.Form[name][0]
			}
			delete(r.Form, name)
		} else {
			str, r.Form[name] =
				r.Form[name][0], r.Form[name][1:]
		}
	} else if r.PostForm != nil && len(r.PostForm[name]) > 0 {
		if len(r.PostForm[name]) <= 1 {
			if len(r.PostForm[name]) == 1 {
				str = r.PostForm[name][0]
			}
			delete(r.PostForm, name)
		} else {
			str, r.PostForm[name] =
				r.PostForm[name][0], r.PostForm[name][1:]
		}
	}
	return str
}

func (r *value) All(name string) []string {
	strs := []string{}
	if r.MultipartForm != nil && len(r.MultipartForm.Value[name]) > 0 {
		strs = r.MultipartForm.Value[name]
		delete(r.MultipartForm.Value, name)
	} else if r.Form != nil && len(r.Form[name]) > 0 {
		strs = r.Form[name]
		delete(r.Form, name)
	} else if r.PostForm != nil && len(r.PostForm[name]) > 0 {
		strs = r.PostForm[name]
		delete(r.PostForm, name)
	}
	return strs
}

func (r *value) FileShift(name string) *multipart.FileHeader {
	var fileHeader *multipart.FileHeader
	if r.MultipartForm != nil && len(r.MultipartForm.File[name]) > 0 {
		if len(r.MultipartForm.File[name]) <= 1 {
			if len(r.MultipartForm.File[name]) == 1 {
				fileHeader = r.MultipartForm.File[name][0]
			}
			delete(r.MultipartForm.File, name)
		} else {
			fileHeader, r.MultipartForm.File[name] =
				r.MultipartForm.File[name][0], r.MultipartForm.File[name][1:]
		}
	}
	return fileHeader
}<commit_msg>Improved efficency of dealing with form values<commit_after>package form

import (
	"encoding/json"
	"mime/multipart"
	"net/http"
	"net/url"
)

type value struct {
	Form                url.Values
	PostForm            url.Values
	MultipartForm       *multipart.Form
	formCount           map[string]int
	postFormCount       map[string]int
	multipartValueCount map[string]int
	multipartFileCount  map[string]int
}

func newValue(r *http.Request) *value {
	v := &value{}
	count := 0
	if r.Form != nil {
		if len(r.Form) > 0 {
			v.Form = r.Form
			count++
		}
	}
	if r.PostForm != nil {
		if len(r.PostForm) > 0 {
			v.PostForm = r.PostForm
			count++
		}
	}
	if r.MultipartForm != nil {
		v.MultipartForm = r.MultipartForm
		count++
	}
	if count == 0 {
		return nil
	}
	v.formCount = map[string]int{}
	v.postFormCount = map[string]int{}
	v.multipartValueCount = map[string]int{}
	v.multipartFileCount = map[string]int{}
	return v
}

func newValueSerializeArray(r *http.Request) *value {
	v := &value{PostForm: url.Values{}}

	data := []struct {
		Name  string `json:"name"`
		Value string `json:"value"`
	}{}

	jDec := json.NewDecoder(r.Body)
	defer r.Body.Close()

	jDec.Decode(&data)

	for _, item := range data {
		v.PostForm[item.Name] = append(v.PostForm[item.Name], item.Value)
	}
	v.formCount = map[string]int{}
	v.postFormCount = map[string]int{}
	v.multipartValueCount = map[string]int{}
	v.multipartFileCount = map[string]int{}
	return v
}

func (r *value) Shift(name string) (str string) {
	switch {
	case r.MultipartForm != nil &&
		len(r.MultipartForm.Value[name]) > r.multipartValueCount[name]:

		str = r.MultipartForm.Value[name][r.multipartValueCount[name]]
		r.multipartValueCount[name]++

	case r.Form != nil &&
		len(r.Form[name]) > r.formCount[name]:

		str = r.Form[name][r.formCount[name]]
		r.formCount[name]++

	case r.PostForm != nil &&
		len(r.PostForm[name]) > r.postFormCount[name]:

		str = r.PostForm[name][r.postFormCount[name]]
		r.postFormCount[name]++
	}

	return
}

func (r *value) All(name string) []string {
	strs := []string{}

	switch {
	case r.MultipartForm != nil &&
		len(r.MultipartForm.Value[name]) > r.multipartValueCount[name]:

		strs = r.MultipartForm.Value[name][r.multipartValueCount[name]:]
		r.multipartValueCount[name] = len(r.MultipartForm.Value[name])

	case r.Form != nil &&
		len(r.Form[name]) > r.formCount[name]:

		strs = r.Form[name][r.formCount[name]:]
		r.formCount[name] = len(r.Form[name])

	case r.PostForm != nil &&
		len(r.PostForm[name]) > r.postFormCount[name]:

		strs = r.PostForm[name][r.postFormCount[name]:]
		r.postFormCount[name] = len(r.PostForm[name])
	}

	return strs
}

func (r *value) FileShift(name string) (fileHeader *multipart.FileHeader) {
	if r.MultipartForm != nil &&
		len(r.MultipartForm.File[name]) > r.multipartFileCount[name] {

		fileHeader = r.MultipartForm.File[name][r.multipartFileCount[name]]
		r.multipartFileCount[name]++

	}

	return
}
{'fraction_non_alphanumeric': 0.1706312900274474, 'fraction_numerical': 0.0056419640134187255, 'mean_word_length': 3.520330806340455, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '3030443', 'n_tokens_mistral': 2795, 'n_tokens_neox': 2563, 'n_words': 477}