<commit_before>package utils

import (
	"encoding/json"
	"fmt"
	"strings"
)

// GetPrelude decodes and remove the prelude from the input string.
func GetPrelude(s string) (string, map[string]interface{}, error) {

	var v map[string]interface{}
	k := strings.Split(string(s), "\n")
	i := 0
	prelude := ""
	if len(k) > 1 && k[0] == "---" {
		i++
		for _, l := range k[1:] {
			i++
			if l == "---" {
				break
			}
			z := strings.Index(l, ":")
			name := l[0:z]
			val := l[z+1:]
			// quote raw values, it allows easier writing of strings containing strings.
			if val[0] != '[' && val[0] != '"' && val[0] != '\'' {
				val = fmt.Sprintf("%q", val)
			}
			prelude += fmt.Sprintf("%q:%v,\n", name, val)
		}

		prelude = "{" + prelude[:len(prelude)-2] + "}"

		if err := json.Unmarshal([]byte(prelude), &v); err != nil {
			return "", v, err
		}
	}<commit_msg>prelude: trim leading whitespaces of unquoted values<commit_after>package utils

import (
	"encoding/json"
	"fmt"
	"strings"
)

// GetPrelude decodes and remove the prelude from the input string.
func GetPrelude(s string) (string, map[string]interface{}, error) {

	var v map[string]interface{}
	k := strings.Split(string(s), "\n")
	i := 0
	prelude := ""
	if len(k) > 1 && k[0] == "---" {
		i++
		for _, l := range k[1:] {
			i++
			if l == "---" {
				break
			}
			z := strings.Index(l, ":")
			name := l[0:z]
			val := l[z+1:]
			// quote raw values, it allows easier writing of strings containing strings.
			if val[0] != '[' && val[0] != '"' && val[0] != '\'' {
				val = fmt.Sprintf("%q", strings.TrimLeft(val, " "))
			}
			prelude += fmt.Sprintf("%q:%v,\n", name, val)
		}

		prelude = "{" + prelude[:len(prelude)-2] + "}"

		if err := json.Unmarshal([]byte(prelude), &v); err != nil {
			return "", v, err
		}
	}
{'fraction_non_alphanumeric': 0.22078651685393258, 'fraction_numerical': 0.011235955056179775, 'mean_word_length': 2.957777777777778, 'pattern_counts': {'":': 2, '<': 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': '3257764', 'n_tokens_mistral': 782, 'n_tokens_neox': 699, 'n_words': 172}