Skip to content

Commit 75dd7ed

Browse files
facchinmcmaglie
authored andcommitted
Add NormalizeUTF8 utility function
1 parent da69174 commit 75dd7ed

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: utils/utils.go

+15
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ import (
4040
"path/filepath"
4141
"runtime"
4242
"strings"
43+
"unicode"
4344
"unicode/utf8"
4445

4546
"github.com/arduino/arduino-builder/constants"
4647
"github.com/arduino/arduino-builder/gohasissues"
4748
"github.com/arduino/arduino-builder/i18n"
4849
"github.com/arduino/arduino-builder/types"
50+
"golang.org/x/text/transform"
51+
"golang.org/x/text/unicode/norm"
4952
)
5053

5154
func KeysOfMapOfStringInterface(input map[string]interface{}) []string {
@@ -535,6 +538,18 @@ func ParseCppString(line string) (string, string, bool) {
535538
}
536539
}
537540

541+
func isMn(r rune) bool {
542+
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
543+
}
544+
545+
// Normalizes an UTF8 byte slice
546+
// TODO: use it more often troughout all the project (maybe on logger interface?)
547+
func NormalizeUTF8(buf []byte) []byte {
548+
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
549+
result, _, _ := transform.Bytes(t, buf)
550+
return result
551+
}
552+
538553
// CopyFile copies the contents of the file named src to the file named
539554
// by dst. The file will be created if it does not already exist. If the
540555
// destination file exists, all it's contents will be replaced by the contents

0 commit comments

Comments
 (0)