Skip to content

Commit

Permalink
fix: remove unicode chars before parsing battery XML
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Sep 9, 2020
1 parent 7e1bf2b commit bb0a756
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion battery_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package battery
import (
"math"
"os/exec"
"strings"
"unicode"

plist "howett.net/plist"
)
Expand All @@ -50,8 +52,17 @@ func readBatteries() ([]*battery, error) {
return nil, nil
}

// Battery XML data can contain illegal unicode characters
printOnly := func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}
batteryXML := []byte(strings.Map(printOnly, string(out)))

var data []*battery
if _, err = plist.Unmarshal(out, &data); err != nil {
if _, err = plist.Unmarshal(batteryXML, &data); err != nil {
return nil, err
}
return data, nil
Expand Down

0 comments on commit bb0a756

Please sign in to comment.