<commit_before>package protobuf

import (
	"errors"
	"io"
	"reflect"
)

// Marshal traverses the value v recursively and returns the protocol
// buffer encoding of v. The struct underlying v must be a pointer.
//
// Marshal currently encodes all visible field, which does not allow
// distinction between 'required' and 'optional' fields. Marshal ignores
// unsupported struct field types.
//
// The returned slice may be a sub- slice of data if data was large
// enough to hold the entire encoded block. Otherwise, a newly allocated
// slice will be returned.
func Marshal(data []byte, v interface{}) ([]byte, error) {
	val := reflect.ValueOf(v)
	if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
		return data, errors.New("v must be a pointer to a struct")
	}

	val = val.Elem()
	fields := val.NumField()
	b := buffer(data)
	enc := NewEncoder(&b, 0)
	if err := enc.encodeStruct(val, fields); err != nil {
		return nil, err
	}
	return b, nil
}

// Unmarshal parses the protocol buffer representation in data and places
// the decoded result in v. If the struct underlying v does not match the
// data, the results can be unpredictable.
//
// Unmarshal uses the inverse of the encodings that Marshal uses,
// allocating slices and pointers as necessary.
func Unmarshal(data []byte, v interface{}) error {
	val := reflect.ValueOf(v)
	if !val.IsValid() || val.IsNil() {
		return nil
	}
	if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
		return errors.New("v must be a pointer to a struct")
	}

	//b := buffer(data)
	//dec := NewDecoder(&b, 0)
	//return dec.decodeStruct(val.Elem(), val.Elem().NumField(), len(data))
	return decodeStruct(val.Elem(), data, false)
}

// UnmarshalUnsafe parses the protocol buffer representation in data and
// places the decoded result in v. If the struct underlying v does not
// match the data, the results can be unpredictable.
//
// UnmarshalUnsafe uses the inverse of the encodings that Marshal uses,
// allocating slices and pointers as necessary.
//
// UnmarshalUnsafe does not copy raw byte slices. Most code should use
// Unmarshal instead.<commit_msg>cleanup buffer<commit_after>package protobuf

import (
	"errors"
	"io"
	"reflect"
)

// Marshal traverses the value v recursively and returns the protocol
// buffer encoding of v. The struct underlying v must be a pointer.
//
// Marshal currently encodes all visible field, which does not allow
// distinction between 'required' and 'optional' fields. Marshal ignores
// unsupported struct field types.
//
// The returned slice may be a sub- slice of data if data was large
// enough to hold the entire encoded block. Otherwise, a newly allocated
// slice will be returned.
func Marshal(data []byte, v interface{}) ([]byte, error) {
	val := reflect.ValueOf(v)
	if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
		return data, errors.New("v must be a pointer to a struct")
	}

	val = val.Elem()
	fields := val.NumField()
	b := buffer(data)
	enc := NewEncoder(&b, 0)
	if err := enc.encodeStruct(val, fields); err != nil {
		return nil, err
	}
	return b, nil
}

// Unmarshal parses the protocol buffer representation in data and places
// the decoded result in v. If the struct underlying v does not match the
// data, the results can be unpredictable.
//
// Unmarshal uses the inverse of the encodings that Marshal uses,
// allocating slices and pointers as necessary.
func Unmarshal(data []byte, v interface{}) error {
	val := reflect.ValueOf(v)
	if !val.IsValid() || val.IsNil() {
		return nil
	}
	if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
		return errors.New("v must be a pointer to a struct")
	}

	return decodeStruct(val.Elem(), data, false)
}

// UnmarshalUnsafe parses the protocol buffer representation in data and
// places the decoded result in v. If the struct underlying v does not
// match the data, the results can be unpredictable.
//
// UnmarshalUnsafe uses the inverse of the encodings that Marshal uses,
// allocating slices and pointers as necessary.
//
// UnmarshalUnsafe does not copy raw byte slices. Most code should use
// Unmarshal instead.
{'fraction_non_alphanumeric': 0.11296162201303403, 'fraction_numerical': 0.000724112961622013, 'mean_word_length': 3.6666666666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4275512', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1165, 'n_words': 529}