<commit_before>/*
 Pretty-print Go data structures
*/
package pretty

import (
	"bytes"
	"fmt"
	"io"
	"os"
	r "reflect"
	"strconv"
	"strings"
)

const (
	DEFAULT_INDENT = "  "
	DEFAULT_NIL    = "nil"
)

// The context for printing
type Pretty struct {
	// indent string
	Indent string
	// output recipient
	Out io.Writer
	// string for nil
	NilString string
}

// pretty print the input value (to stdout)
func PrettyPrint(i interface{}) {
	PrettyPrintTo(os.Stdout, i)
}

// pretty print the input value (to a string)
func PrettyFormat(i interface{}) string {
	var out bytes.Buffer
	PrettyPrintTo(&out, i)
	return out.String()
}

// pretty print the input value (to specified writer)
func PrettyPrintTo(out io.Writer, i interface{}) {
	p := &Pretty{DEFAULT_INDENT, out, DEFAULT_NIL}
	p.Print(i)
}

// pretty print the input value
func (p *Pretty) Print(i interface{}) {
	p.PrintValue(r.ValueOf(i), 0)
	io.WriteString(p.Out, "\n")
}

// recursively print the input value
func (p *Pretty) PrintValue(val r.Value, level int) {
	if !val.IsValid() {
		io.WriteString(p.Out, p.NilString)
		return
	}

	cur := strings.Repeat(p.Indent, level)
	next := strings.Repeat(p.Indent, level+1)

	switch val.Kind() {
	case r.Int, r.Int8, r.Int16, r.Int32, r.Int64:
		io.WriteString(p.Out, strconv.FormatInt(val.Int(), 10))

	case r.Uint, r.Uint8, r.Uint16, r.Uint32, r.Uint64:
		io.WriteString(p.Out, strconv.FormatUint(val.Uint(), 10))

	case r.Float32, r.Float64:
		io.WriteString(p.Out, strconv.FormatFloat(val.Float(), 'f', -1, 64))

	case r.String:
		io.WriteString(p.Out, strconv.Quote(val.String()))

	case r.Bool:
		io.WriteString(p.Out, strconv.FormatBool(val.Bool()))

	case r.Map:
		l := val.Len()

		io.WriteString(p.Out, "{\n")
		for i, k := range val.MapKeys() {
			io.WriteString(p.Out, next)
			io.WriteString(p.Out, strconv.Quote(k.String()))
			io.WriteString(p.Out, ": ")
			p.PrintValue(val.MapIndex(k), level+1)
			if i < l-1 {
				io.WriteString(p.Out, ",\n")
			} else {
				io.WriteString(p.Out, "\n")
			}
		}
		io.WriteString(p.Out, cur)
		io.WriteString(p.Out, "}")

	case r.Array, r.Slice:
		l := val.Len()

		io.WriteString(p.Out, "[\n")
		for i := 0; i < l; i++ {
			io.WriteString(p.Out, next)
			p.PrintValue(val.Index(i), level+1)
			if i < l-1 {
				io.WriteString(p.Out, ",\n")
			} else {
				io.WriteString(p.Out, "\n")
			}
		}
		io.WriteString(p.Out, cur)
		io.WriteString(p.Out, "]")

	case r.Interface, r.Ptr:
		p.PrintValue(val.Elem(), level)

	case r.Struct:
		i := val.Interface()
		if i, ok := i.(fmt.Stringer); ok {
			io.WriteString(p.Out, i.String())
		} else {
			l := val.NumField()

			io.WriteString(p.Out, "struct {\n")
			for i := 0; i < l; i++ {
				io.WriteString(p.Out, next)
				io.WriteString(p.Out, val.Type().Field(i).Name)
				io.WriteString(p.Out, ": ")
				p.PrintValue(val.Field(i), level+1)
				if i < l-1 {
					io.WriteString(p.Out, ",\n")
				} else {
					io.WriteString(p.Out, "\n")
				}
			}
			io.WriteString(p.Out, cur)
			io.WriteString(p.Out, "}")
		}

	default:
		io.WriteString(p.Out, "unsupported:")
		io.WriteString(p.Out, val.String())
	}
}
<commit_msg>PrettyFormat shouldn't add a new line. Also added Println (print with new line) and made Print don't add the newline.<commit_after>/*
 Pretty-print Go data structures
*/
package pretty

import (
	"bytes"
	"fmt"
	"io"
	"os"
	r "reflect"
	"strconv"
	"strings"
)

const (
	DEFAULT_INDENT = "  "
	DEFAULT_NIL    = "nil"
)

// The context for printing
type Pretty struct {
	// indent string
	Indent string
	// output recipient
	Out io.Writer
	// string for nil
	NilString string
}

// pretty print the input value (to stdout)
func PrettyPrint(i interface{}) {
	PrettyPrintTo(os.Stdout, i, true)
}

// pretty print the input value (to a string)
func PrettyFormat(i interface{}) string {
	var out bytes.Buffer
	PrettyPrintTo(&out, i, false)
	return out.String()
}

// pretty print the input value (to specified writer)
func PrettyPrintTo(out io.Writer, i interface{}, nl bool) {
	p := &Pretty{DEFAULT_INDENT, out, DEFAULT_NIL}
	if nl {
		p.Println(i)
	} else {
		p.Print(i)
	}
}

// pretty print the input value (no newline)
func (p *Pretty) Print(i interface{}) {
	p.PrintValue(r.ValueOf(i), 0)
}

// pretty print the input value (newline)
func (p *Pretty) Println(i interface{}) {
	p.PrintValue(r.ValueOf(i), 0)
	io.WriteString(p.Out, "\n")
}

// recursively print the input value
func (p *Pretty) PrintValue(val r.Value, level int) {
	if !val.IsValid() {
		io.WriteString(p.Out, p.NilString)
		return
	}

	cur := strings.Repeat(p.Indent, level)
	next := strings.Repeat(p.Indent, level+1)

	switch val.Kind() {
	case r.Int, r.Int8, r.Int16, r.Int32, r.Int64:
		io.WriteString(p.Out, strconv.FormatInt(val.Int(), 10))

	case r.Uint, r.Uint8, r.Uint16, r.Uint32, r.Uint64:
		io.WriteString(p.Out, strconv.FormatUint(val.Uint(), 10))

	case r.Float32, r.Float64:
		io.WriteString(p.Out, strconv.FormatFloat(val.Float(), 'f', -1, 64))

	case r.String:
		io.WriteString(p.Out, strconv.Quote(val.String()))

	case r.Bool:
		io.WriteString(p.Out, strconv.FormatBool(val.Bool()))

	case r.Map:
		l := val.Len()

		io.WriteString(p.Out, "{\n")
		for i, k := range val.MapKeys() {
			io.WriteString(p.Out, next)
			io.WriteString(p.Out, strconv.Quote(k.String()))
			io.WriteString(p.Out, ": ")
			p.PrintValue(val.MapIndex(k), level+1)
			if i < l-1 {
				io.WriteString(p.Out, ",\n")
			} else {
				io.WriteString(p.Out, "\n")
			}
		}
		io.WriteString(p.Out, cur)
		io.WriteString(p.Out, "}")

	case r.Array, r.Slice:
		l := val.Len()

		io.WriteString(p.Out, "[\n")
		for i := 0; i < l; i++ {
			io.WriteString(p.Out, next)
			p.PrintValue(val.Index(i), level+1)
			if i < l-1 {
				io.WriteString(p.Out, ",\n")
			} else {
				io.WriteString(p.Out, "\n")
			}
		}
		io.WriteString(p.Out, cur)
		io.WriteString(p.Out, "]")

	case r.Interface, r.Ptr:
		p.PrintValue(val.Elem(), level)

	case r.Struct:
		i := val.Interface()
		if i, ok := i.(fmt.Stringer); ok {
			io.WriteString(p.Out, i.String())
		} else {
			l := val.NumField()

			io.WriteString(p.Out, "struct {\n")
			for i := 0; i < l; i++ {
				io.WriteString(p.Out, next)
				io.WriteString(p.Out, val.Type().Field(i).Name)
				io.WriteString(p.Out, ": ")
				p.PrintValue(val.Field(i), level+1)
				if i < l-1 {
					io.WriteString(p.Out, ",\n")
				} else {
					io.WriteString(p.Out, "\n")
				}
			}
			io.WriteString(p.Out, cur)
			io.WriteString(p.Out, "}")
		}

	default:
		io.WriteString(p.Out, "unsupported:")
		io.WriteString(p.Out, val.String())
	}
}

{'fraction_non_alphanumeric': 0.18103844386582937, 'fraction_numerical': 0.0108745596569153, 'mean_word_length': 3.1993569131832795, 'pattern_counts': {'":': 4, '<': 13, '<?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': '6452498', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2641, 'n_words': 582}