<commit_before>package main

import (
	"bytes"
	"fmt"
	"net"
	"os"
	"strings"
)

// Escape a string so it contains no byte values over 127 and doesn't contain
// any of the characters '\x00', '\n', or '\\'.
func escape(s string) string {
	var buf bytes.Buffer
	for _, b := range []byte(s) {
		if b == '\n' {
			buf.WriteString("\\n")
		} else if b == '\\' {
			buf.WriteString("\\\\")
		} else if 0 < b && b < 128 {
			buf.WriteByte(b)
		} else {
			fmt.Fprintf(&buf, "\\x%02x", b)
		}
	}
	return buf.String()
}

func ptLine(keyword string, v ...string) {
	var buf bytes.Buffer
	buf.WriteString(keyword)
	for _, x := range v {
		buf.WriteString(" " + escape(x))
	}
	fmt.Println(buf.String())
}

func ptEnvError(msg string) {
	ptLine("ENV-ERROR", msg)
	os.Exit(1)
}

func ptVersionError(msg string) {
	ptLine("VERSION-ERROR", msg)
	os.Exit(1)
}

func ptCmethodError(methodName, msg string) {
	ptLine("CMETHOD-ERROR", methodName, msg)
	os.Exit(1)
}

func ptGetManagedTransportVer() string {
	const transportVersion = "1"
	for _, offered := range strings.Split(os.Getenv("TOR_PT_MANAGED_TRANSPORT_VER"), ",") {
		if offered == transportVersion {
			return offered
		}
	}
	return ""
}

func ptGetClientTransports(supported []string) []string {
	clientTransports := os.Getenv("TOR_PT_CLIENT_TRANSPORTS")
	if clientTransports == "" {
		ptEnvError("no TOR_PT_CLIENT_TRANSPORTS environment variable")
	}
	if clientTransports == "*" {
		return supported
	}

	result := make([]string, 0)
	for _, requested := range strings.Split(clientTransports, ",") {
		for _, methodName := range supported {
			if requested == methodName {
				result = append(result, methodName)
			}
		}
	}
	return result
}

func ptCmethod(name string, socks string, addr net.Addr) {
	ptLine("CMETHOD", name, socks, addr.String())
}

func ptCmethodsDone() {
	ptLine("CMETHODS", "DONE")
}

func ptClientSetup(methodNames []string) []string {
	ver := ptGetManagedTransportVer()
	if ver == "" {
		ptVersionError("no-version")
	} else {
		ptLine("VERSION", ver)
	}

	methods := ptGetClientTransports(methodNames)
	if len(methods) == 0 {
		ptCmethodsDone()
		os.Exit(1)
	}

	return methods
}
<commit_msg>Bail out sooner in ptGetClientTransports loop.<commit_after>package main

import (
	"bytes"
	"fmt"
	"net"
	"os"
	"strings"
)

// Escape a string so it contains no byte values over 127 and doesn't contain
// any of the characters '\x00', '\n', or '\\'.
func escape(s string) string {
	var buf bytes.Buffer
	for _, b := range []byte(s) {
		if b == '\n' {
			buf.WriteString("\\n")
		} else if b == '\\' {
			buf.WriteString("\\\\")
		} else if 0 < b && b < 128 {
			buf.WriteByte(b)
		} else {
			fmt.Fprintf(&buf, "\\x%02x", b)
		}
	}
	return buf.String()
}

func ptLine(keyword string, v ...string) {
	var buf bytes.Buffer
	buf.WriteString(keyword)
	for _, x := range v {
		buf.WriteString(" " + escape(x))
	}
	fmt.Println(buf.String())
}

func ptEnvError(msg string) {
	ptLine("ENV-ERROR", msg)
	os.Exit(1)
}

func ptVersionError(msg string) {
	ptLine("VERSION-ERROR", msg)
	os.Exit(1)
}

func ptCmethodError(methodName, msg string) {
	ptLine("CMETHOD-ERROR", methodName, msg)
	os.Exit(1)
}

func ptGetManagedTransportVer() string {
	const transportVersion = "1"
	for _, offered := range strings.Split(os.Getenv("TOR_PT_MANAGED_TRANSPORT_VER"), ",") {
		if offered == transportVersion {
			return offered
		}
	}
	return ""
}

func ptGetClientTransports(supported []string) []string {
	clientTransports := os.Getenv("TOR_PT_CLIENT_TRANSPORTS")
	if clientTransports == "" {
		ptEnvError("no TOR_PT_CLIENT_TRANSPORTS environment variable")
	}
	if clientTransports == "*" {
		return supported
	}

	result := make([]string, 0)
	for _, requested := range strings.Split(clientTransports, ",") {
		for _, methodName := range supported {
			if requested == methodName {
				result = append(result, methodName)
				break
			}
		}
	}
	return result
}

func ptCmethod(name string, socks string, addr net.Addr) {
	ptLine("CMETHOD", name, socks, addr.String())
}

func ptCmethodsDone() {
	ptLine("CMETHODS", "DONE")
}

func ptClientSetup(methodNames []string) []string {
	ver := ptGetManagedTransportVer()
	if ver == "" {
		ptVersionError("no-version")
	} else {
		ptLine("VERSION", ver)
	}

	methods := ptGetClientTransports(methodNames)
	if len(methods) == 0 {
		ptCmethodsDone()
		os.Exit(1)
	}

	return methods
}

{'fraction_non_alphanumeric': 0.15693012600229095, 'fraction_numerical': 0.008247422680412371, 'mean_word_length': 3.90561797752809, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5417719', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1581, 'n_words': 374}