aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/util/misc.go
blob: aeda6788664ebb81bf29a108560a72314894bee7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019-2022 Bernd Fix  >Y<
//
// gnunet-go is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// gnunet-go is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later

package util

import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"strings"

	"github.com/bfix/gospel/data"
	"github.com/bfix/gospel/math"
)

//----------------------------------------------------------------------
// Count occurrence of multiple instance at the same time.
//----------------------------------------------------------------------

// Counter is a metric with single key
type Counter[T comparable] map[T]int

// Add one to themetric for a given key and return current value
func (cm Counter[T]) Add(i T) int {
	count, ok := cm[i]
	if !ok {
		count = 1
	} else {
		count++
	}
	cm[i] = count
	return count
}

// Num returns the metric for a given key
func (cm Counter[T]) Num(i T) int {
	count, ok := cm[i]
	if !ok {
		count = 0
	}
	return count
}

//----------------------------------------------------------------------
// Parameter set with string keys and variable value types
//----------------------------------------------------------------------

// ParameterSet with string keys and variable value types
type ParameterSet map[string]any

// Get a parameter value with given type 'V'
func GetParam[V any](params ParameterSet, key string) (i V, ok bool) {
	var v any
	if v, ok = params[key]; ok {
		if i, ok = v.(V); ok {
			return
		}
	}
	return
}

// CastFromString a string to given type (only works for intrinsic type)
func CastFromString[V any](s string) (i V, ok bool) {
	num, err := fmt.Sscanf(s, "%v", &i)
	ok = true
	if err != nil || num != 1 {
		ok = false
	}
	return
}

// CastToString returns a string representation (for intrinsic types)
func CastToString(v any) string {
	return fmt.Sprintf("%v", v)
}

//----------------------------------------------------------------------
// additional helpers
//----------------------------------------------------------------------

// Distance returns the XOR distance between to byte arrays
func Distance(a, b []byte) *math.Int {
	size := len(a)
	d := make([]byte, size)
	for i := range d {
		d[i] = a[i] ^ b[i]
	}
	return math.NewIntFromBytes(d)
}

// StripPathRight returns a dot-separated path without
// its last (right-most) element.
func StripPathRight(s string) string {
	if idx := strings.LastIndex(s, "."); idx != -1 {
		return s[:idx]
	}
	return s
}

// Shorten string for output
func Shorten(s string, n int) string {
	l := len(s)
	p := n - 3
	if p < 3 {
		return s
	}
	k := p % 2
	p /= 2
	return s[:p+k] + "..." + s[l-p:]
}

// Dump instance
func Dump(obj any, format string) string {
	switch format {
	case "json":
		buf, err := json.Marshal(obj)
		if err != nil {
			return err.Error()
		}
		return string(buf)
	case "hex":
		buf, err := data.Marshal(obj)
		if err != nil {
			return err.Error()
		}
		return hex.EncodeToString(buf)
	}
	return "unknown message dump format"
}