<commit_before>package dbus

import (
	"reflect"
	"strings"
)

var (
	byteType       = reflect.TypeOf(byte(0))
	boolType       = reflect.TypeOf(false)
	uint8Type      = reflect.TypeOf(uint8(0))
	int16Type      = reflect.TypeOf(int16(0))
	uint16Type     = reflect.TypeOf(uint16(0))
	int32Type      = reflect.TypeOf(int32(0))
	uint32Type     = reflect.TypeOf(uint32(0))
	int64Type      = reflect.TypeOf(int64(0))
	uint64Type     = reflect.TypeOf(uint64(0))
	float64Type    = reflect.TypeOf(float64(0))
	stringType     = reflect.TypeOf("")
	signatureType  = reflect.TypeOf(Signature{""})
	objectPathType = reflect.TypeOf(ObjectPath(""))
	variantType    = reflect.TypeOf(Variant{Signature{""}, nil})
	interfacesType = reflect.TypeOf([]interface{}{})
)

type invalidTypeError struct {
	reflect.Type
}

func (err invalidTypeError) Error() string {
	return "dbus: invalid type " + err.Type.String()
}

var sigToType = map[byte]reflect.Type{
	'y': byteType,
	'b': boolType,
	'n': int16Type,
	'q': uint16Type,
	'i': int32Type,
	'u': uint32Type,
	'x': int64Type,
	't': uint64Type,
	'd': float64Type,
	's': stringType,
	'g': signatureType,
	'o': objectPathType,
	'v': variantType,
}

// Signature represents a correct type signature as specified
// by the DBus specification.
type Signature struct {
	str string
}

// GetSignature returns the concatenation of all the signatures
// of the given values. It panics if one of them is not representable
// in DBus.
func GetSignature(vs ...interface{}) Signature {
	var s string
	for _, v := range vs {
		s += getSignature(reflect.TypeOf(v))
	}
	return Signature{s}
}

// GetSignatureType returns the signature of the given type. It panics if the
// type is not representable in DBus.
func GetSignatureType(t reflect.Type) Signature {
	return Signature{getSignature(t)}
}

// getSignature returns the signature of the given type and panics on unknown types.
func getSignature(v reflect.Type) string {
	// handle simple types first
	switch v.Kind() {
	case reflect.Uint8:
		return "y"
	case reflect.Bool:
		return "b"
	case reflect.Int16:
		return "n"
	case reflect.Uint16:
		return "q"
	case reflect.Int32, reflect.Int:
		return "i"
	case reflect.Uint32, reflect.Uint:
		return "u"
	case reflect.Int64:
		return "x"
	case reflect.Uint64:
		return "t"
	case reflect.Float64:
		return "d"
	case reflect.Ptr:
		return getSignature(v.Elem())
	case reflect.String:
		if v == objectPathType {
			return "o"
		}
		return "s"
	case reflect.Struct:
		if v == variantType {
			return "v"
		} else if v == signatureType {
			return "g"
		}
		var s string
		for i := 0; i < v.NumField(); i++ {
			s += getSignature(v.Field(i).Type)
		}
		return "(" + s + ")"
	case reflect.Array, reflect.Slice:
		return "a" + getSignature(v.Elem())
	case reflect.Map:
		return "a{" + getSignature(v.Key()) + getSignature(v.Elem()) + "}"
	}
	panic("unknown type " + v.String())
}

// StringToSig returns the signature represented by this string, or a
// SignatureError if the string is not a valid signature.
func StringToSig(s string) (sig Signature, err error) {
	if len(s) == 0 {
		return
	}
	if len(s) > 255 {
		return Signature{""}, SignatureError{s, "too long"}
	}<commit_msg>Fix some errors in getSignature<commit_after>package dbus

import (
	"reflect"
	"strings"
	"unicode"
)

var (
	byteType       = reflect.TypeOf(byte(0))
	boolType       = reflect.TypeOf(false)
	uint8Type      = reflect.TypeOf(uint8(0))
	int16Type      = reflect.TypeOf(int16(0))
	uint16Type     = reflect.TypeOf(uint16(0))
	int32Type      = reflect.TypeOf(int32(0))
	uint32Type     = reflect.TypeOf(uint32(0))
	int64Type      = reflect.TypeOf(int64(0))
	uint64Type     = reflect.TypeOf(uint64(0))
	float64Type    = reflect.TypeOf(float64(0))
	stringType     = reflect.TypeOf("")
	signatureType  = reflect.TypeOf(Signature{""})
	objectPathType = reflect.TypeOf(ObjectPath(""))
	variantType    = reflect.TypeOf(Variant{Signature{""}, nil})
	interfacesType = reflect.TypeOf([]interface{}{})
)

type invalidTypeError struct {
	reflect.Type
}

func (err invalidTypeError) Error() string {
	return "dbus: invalid type " + err.Type.String()
}

var sigToType = map[byte]reflect.Type{
	'y': byteType,
	'b': boolType,
	'n': int16Type,
	'q': uint16Type,
	'i': int32Type,
	'u': uint32Type,
	'x': int64Type,
	't': uint64Type,
	'd': float64Type,
	's': stringType,
	'g': signatureType,
	'o': objectPathType,
	'v': variantType,
}

// Signature represents a correct type signature as specified
// by the DBus specification.
type Signature struct {
	str string
}

// GetSignature returns the concatenation of all the signatures
// of the given values. It panics if one of them is not representable
// in DBus.
func GetSignature(vs ...interface{}) Signature {
	var s string
	for _, v := range vs {
		s += getSignature(reflect.TypeOf(v))
	}
	return Signature{s}
}

// GetSignatureType returns the signature of the given type. It panics if the
// type is not representable in DBus.
func GetSignatureType(t reflect.Type) Signature {
	return Signature{getSignature(t)}
}

// getSignature returns the signature of the given type and panics on unknown types.
func getSignature(t reflect.Type) string {
	// handle simple types first
	switch t.Kind() {
	case reflect.Uint8:
		return "y"
	case reflect.Bool:
		return "b"
	case reflect.Int16:
		return "n"
	case reflect.Uint16:
		return "q"
	case reflect.Int32:
		return "i"
	case reflect.Uint32:
		return "u"
	case reflect.Int64:
		return "x"
	case reflect.Uint64:
		return "t"
	case reflect.Float64:
		return "d"
	case reflect.Ptr:
		return getSignature(t.Elem())
	case reflect.String:
		if t == objectPathType {
			return "o"
		}
		return "s"
	case reflect.Struct:
		if t == variantType {
			return "v"
		} else if t == signatureType {
			return "g"
		}
		var s string
		for i := 0; i < t.NumField(); i++ {
			field := t.Field(i)
			if unicode.IsUpper([]rune(field.Name)[0]) &&
				field.Tag.Get("dbus") != "-" {

				s += getSignature(t.Field(i).Type)
			}
		}
		return "(" + s + ")"
	case reflect.Array, reflect.Slice:
		return "a" + getSignature(t.Elem())
	case reflect.Map:
		if !isKeyType(t.Key()) {
			panic(invalidTypeError{t})
		}
		return "a{" + getSignature(t.Key()) + getSignature(t.Elem()) + "}"
	}
	panic(invalidTypeError{t})
}

// StringToSig returns the signature represented by this string, or a
// SignatureError if the string is not a valid signature.
func StringToSig(s string) (sig Signature, err error) {
	if len(s) == 0 {
		return
	}
	if len(s) > 255 {
		return Signature{""}, SignatureError{s, "too long"}
	}
{'fraction_non_alphanumeric': 0.1363705855373796, 'fraction_numerical': 0.022473627885644398, 'mean_word_length': 3.778670562454346, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3325136', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2265, 'n_words': 633}