aboutsummaryrefslogtreecommitdiff
path: root/utilities/hex.go
blob: 3fe05bca27e0740d034f3c1f4efddb14a256a86e (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
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

package utilities

import "unicode/utf8"

// https://stackoverflow.com/a/43461796/14452787
//
// I cannot explain the joy that flew through me once I found this ISO-8859-1
// (Latin-1) to UTF-8 converter. I was looking for a valid solution on how to
// insert the username and password into a hex-escaped string sequence for
// EncodeSessionInitialization for hours and this finally solved all of my
// problems.
func ISO88591ToString(iso string) string {
	var utf []rune
	for i := 0; i < len(iso); i++ {
		r := iso[i]
		if utf == nil {
			if r < utf8.RuneSelf {
				continue
			}
			utf = make([]rune, len(iso))
			for j, r := range iso[:i] {
				utf[j] = rune(r)
			}
		}
		utf[i] = rune(r)
	}
	if utf == nil {
		return string(iso)
	}
	return string(utf)
}