<commit_before>package dom

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"io"
)

// ToXML is a utility function to serialize a Node and its children to the
// given writer 'w'. The whole tree is traversed using recursion, which is
// pretty much eligible for a refactor for optimalization reasons.
func ToXML(node Node, omitXMLDecl bool, w io.Writer) {
	// Must define the function here so we can refer to ourselves in
	// the traverse function.
	var traverse func(n Node)

	if !omitXMLDecl {
		fmt.Fprintf(w, "%s", XMLDeclaration)
	}

	traverse = func(n Node) {
		switch t := n.(type) {
		case Element:
			if t.HasChildNodes() {
				fmt.Fprintf(w, "<%s>", t.GetTagName())
			} else {
				fmt.Fprintf(w, "<%s/>", t.GetTagName())
			}
		case Text:
			fmt.Fprintf(w, "%s", escape(t.GetData()))
		case Comment:
			fmt.Fprintf(w, "<!-- %s -->", t.GetComment())
		}

		// For each child node, call traverse() again.
		for _, node := range n.GetChildNodes() {
			traverse(node)
		}

		switch t := n.(type) {
		case Element:
			if t.HasChildNodes() {<commit_msg>Processing instruction serialization start.<commit_after>package dom

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"io"
)

// ToXML is a utility function to serialize a Node and its children to the
// given writer 'w'. The whole tree is traversed using recursion, which is
// pretty much eligible for a refactor for optimalization reasons.
func ToXML(node Node, omitXMLDecl bool, w io.Writer) {
	// Must define the function here so we can refer to ourselves in
	// the traverse function.
	var traverse func(n Node)

	if !omitXMLDecl {
		fmt.Fprintf(w, "%s", XMLDeclaration)
	}

	traverse = func(n Node) {
		switch t := n.(type) {
		case Element:
			if t.HasChildNodes() {
				fmt.Fprintf(w, "<%s>", t.GetTagName())
			} else {
				fmt.Fprintf(w, "<%s/>", t.GetTagName())
			}
		case Text:
			fmt.Fprintf(w, "%s", escape(t.GetData()))
		case Comment:
			fmt.Fprintf(w, "<!-- %s -->", t.GetComment())
		case ProcessingInstruction:
			// TODO: proper serialization of target/data. Must include valid chars etc.
			// Also, if target/data contains '?>', generate a fatal error.
			fmt.Fprintf(w, "<?%v %v?>", t.GetTarget(), t.GetData())
		}

		// For each child node, call traverse() again.
		for _, node := range n.GetChildNodes() {
			traverse(node)
		}

		switch t := n.(type) {
		case Element:
			if t.HasChildNodes() {
{'fraction_non_alphanumeric': 0.14695945945945946, 'fraction_numerical': 0.0, 'mean_word_length': 3.2455197132616487, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4237744', 'n_tokens_mistral': 890, 'n_tokens_neox': 799, 'n_words': 266}