Skip to content

Commit

Permalink
FIX: issue extrame#47
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeilem committed Dec 3, 2018
1 parent d1f3d0b commit 6a6d15a
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions workbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package xls
import (
"bytes"
"encoding/binary"
"golang.org/x/text/encoding/charmap"
"io"
"os"
"unicode/utf16"
"golang.org/x/text/encoding/charmap"
)

//xls workbook type
Expand Down Expand Up @@ -86,11 +86,6 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
case 0x042: // CODEPAGE
binary.Read(buf_item, binary.LittleEndian, &wb.Codepage)
case 0x3c: // CONTINUE
// step back if previous element not yet completed
if wb.continue_utf16 > 0 {
offset_pre--
}

if pre.Id == 0xfc {
var size uint16
var err error
Expand All @@ -102,10 +97,8 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
}
for err == nil && offset_pre < len(wb.sst) {
var str string
if size > 0 {
str, err = wb.get_string(buf_item, size)
wb.sst[offset_pre] = wb.sst[offset_pre] + str
}
str, err = wb.get_string(buf_item, size)
wb.sst[offset_pre] = wb.sst[offset_pre] + str

if err == io.EOF {
break
Expand All @@ -128,7 +121,8 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
offset = 0
for ; i < int(info.Count); i++ {
var err error
if err = binary.Read(buf_item, binary.LittleEndian, &size); err == nil {
err = binary.Read(buf_item, binary.LittleEndian, &size)
if err == nil {
var str string
str, err = wb.get_string(buf_item, size)
wb.sst[i] = wb.sst[i] + str
Expand All @@ -139,7 +133,7 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
}
}
offset = i
case 0x85: // bOUNDSHEET
case 0x85: // boundsheet
var bs = new(boundsheet)
binary.Read(buf_item, binary.LittleEndian, bs)
// different for BIFF5 and BIFF8
Expand Down Expand Up @@ -169,9 +163,9 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
return
}
func decodeWindows1251(enc []byte) string {
dec := charmap.Windows1251.NewDecoder()
out, _ := dec.Bytes(enc)
return string(out)
dec := charmap.Windows1251.NewDecoder()
out, _ := dec.Bytes(enc)
return string(out)
}
func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err error) {
if w.Is5ver {
Expand Down Expand Up @@ -199,24 +193,22 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err e
if flag&0x1 != 0 {
var bts = make([]uint16, size)
var i = uint16(0)
// we need local err here
var err error
for ; i < size && err == nil; i++ {
err = binary.Read(buf, binary.LittleEndian, &bts[i])
}

// when eof found, we dont want to append last element
var runes []rune
if err == io.EOF {
runes = utf16.Decode(bts[:i-1])
} else {
runes = utf16.Decode(bts[:i])
i = i - 1
}
runes = utf16.Decode(bts[:i])

res = string(runes)
if i < size {
w.continue_utf16 = size - i + 1
w.continue_utf16 = size - i
}

} else {
var bts = make([]byte, size)
var n int
Expand Down

0 comments on commit 6a6d15a

Please sign in to comment.