Skip to content

Commit e45d5f1

Browse files
committed
Add NormalizeUTF8 utility function
1 parent 47a19e0 commit e45d5f1

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Diff for: src/arduino.cc/builder/utils/utils.go

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

46+
"golang.org/x/text/transform"
47+
"golang.org/x/text/unicode/norm"
48+
4549
"arduino.cc/builder/constants"
4650
"arduino.cc/builder/gohasissues"
4751
"arduino.cc/builder/i18n"
@@ -535,6 +539,18 @@ func ParseCppString(line string) (string, string, bool) {
535539
}
536540
}
537541

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

0 commit comments

Comments
 (0)