mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
exif: Fix handling of utf8 runes in nullString()
This commit is contained in:
parent
0a2ab3f8fe
commit
f6612d8bd8
2 changed files with 23 additions and 9 deletions
|
@ -226,17 +226,14 @@ func (e *exifWalker) Walk(f _exif.FieldName, tag *tiff.Tag) error {
|
|||
|
||||
func nullString(in []byte) string {
|
||||
var rv bytes.Buffer
|
||||
for _, b := range in {
|
||||
if unicode.IsGraphic(rune(b)) {
|
||||
rv.WriteByte(b)
|
||||
for len(in) > 0 {
|
||||
r, size := utf8.DecodeRune(in)
|
||||
if unicode.IsGraphic(r) {
|
||||
rv.WriteRune(r)
|
||||
}
|
||||
in = in[size:]
|
||||
}
|
||||
rvs := rv.String()
|
||||
if utf8.ValidString(rvs) {
|
||||
return rvs
|
||||
}
|
||||
|
||||
return ""
|
||||
return rv.String()
|
||||
}
|
||||
|
||||
var tcodec *tmc.Codec
|
||||
|
|
|
@ -89,6 +89,23 @@ func TestIssue8079(t *testing.T) {
|
|||
c.Assert(x.Tags["ImageDescription"], qt.Equals, "Città del Vaticano #nanoblock #vatican #vaticancity")
|
||||
}
|
||||
|
||||
func TestNullString(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
for _, test := range []struct {
|
||||
in string
|
||||
expect string
|
||||
}{
|
||||
{"foo", "foo"},
|
||||
{"\x20", "\x20"},
|
||||
{"\xc4\x81", "\xc4\x81"}, // \u0101
|
||||
{"\u0160", "\u0160"}, // non-breaking space
|
||||
} {
|
||||
res := nullString([]byte(test.in))
|
||||
c.Assert(res, qt.Equals, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDecodeExif(b *testing.B) {
|
||||
c := qt.New(b)
|
||||
f, err := os.Open(filepath.FromSlash("../../testdata/sunset.jpg"))
|
||||
|
|
Loading…
Reference in a new issue