Skip to content

Commit

Permalink
Ref qax-os#1533, this made number format text handler just handle tex…
Browse files Browse the repository at this point in the history
…t tokens

- Fix race conditions for concurrency read and write shared string table
- Unit tests has been updated
  • Loading branch information
xuri committed Jul 11, 2023
1 parent 5a0a812 commit bcbcd91
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ func (f *File) setSharedString(val string) (int, error) {
if i, ok := f.sharedStringsMap[val]; ok {
return i, nil
}
sst.mu.Lock()
defer sst.mu.Unlock()
sst.Count++
sst.UniqueCount++
t := xlsxT{Val: val}
Expand Down Expand Up @@ -581,6 +583,8 @@ func (c *xlsxC) getValueFrom(f *File, d *xlsxSST, raw bool) (string, error) {
if _, ok := f.tempFiles.Load(defaultXMLPathSharedStrings); ok {
return f.formattedValue(&xlsxC{S: c.S, V: f.getFromStringItem(xlsxSI)}, raw, CellTypeSharedString)
}
d.mu.Lock()
defer d.mu.Unlock()
if len(d.SI) > xlsxSI {
return f.formattedValue(&xlsxC{S: c.S, V: d.SI[xlsxSI].String()}, raw, CellTypeSharedString)
}
Expand Down
8 changes: 4 additions & 4 deletions numfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,13 +948,10 @@ func (nf *numberFormat) zeroHandler() string {
// textHandler will be handling text selection for a number format expression.
func (nf *numberFormat) textHandler() (result string) {
for _, token := range nf.section[nf.sectionIdx].Items {
if inStrSlice([]string{nfp.TokenTypeDateTimes, nfp.TokenTypeElapsedDateTimes}, token.TType, false) != -1 {
return nf.value
}
if token.TType == nfp.TokenTypeLiteral {
result += token.TValue
}
if token.TType == nfp.TokenTypeGeneral || token.TType == nfp.TokenTypeTextPlaceHolder || token.TType == nfp.TokenTypeZeroPlaceHolder {
if token.TType == nfp.TokenTypeTextPlaceHolder || token.TType == nfp.TokenTypeZeroPlaceHolder {
result += nf.value
}
}
Expand All @@ -964,6 +961,9 @@ func (nf *numberFormat) textHandler() (result string) {
// getValueSectionType returns its applicable number format expression section
// based on the given value.
func (nf *numberFormat) getValueSectionType(value string) (float64, string) {
if nf.cellType != CellTypeNumber && nf.cellType != CellTypeDate {
return 0, nfp.TokenSectionText
}
isNum, _, _ := isNumeric(value)
if !isNum {
return 0, nfp.TokenSectionText
Expand Down
19 changes: 12 additions & 7 deletions numfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,12 +1008,17 @@ func TestNumFmt(t *testing.T) {
result := format(item[0], item[1], false, CellTypeNumber)
assert.Equal(t, item[2], result, item)
}
for _, item := range [][]string{
{"1234.5678", "General", "1234.5678"},
{"1234.5678", "yyyy\"\"m\"\"d\"\";@", "1234.5678"},
{"1234.5678", "0_);[Red]\\(0\\)", "1234.5678"},
} {
result := format(item[0], item[1], false, CellTypeSharedString)
assert.Equal(t, item[2], result, item)
for _, cellType := range []CellType{CellTypeSharedString, CellTypeInlineString} {
for _, item := range [][]string{
{"1234.5678", "General", "1234.5678"},
{"1234.5678", "yyyy\"\"m\"\"d\"\";@", "1234.5678"},
{"1234.5678", "h\"\"mm\"\"ss\"\";@", "1234.5678"},
{"1234.5678", "\"¥\"#,##0.00_);\\(\"¥\"#,##0.00\\)", "1234.5678"},
{"1234.5678", "0_);[Red]\\(0\\)", "1234.5678"},
{"1234.5678", "\"text\"@", "text1234.5678"},
} {
result := format(item[0], item[1], false, cellType)
assert.Equal(t, item[2], result, item)
}
}
}
6 changes: 5 additions & 1 deletion xmlSharedStrings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

package excelize

import "encoding/xml"
import (
"encoding/xml"
"sync"
)

// xlsxSST directly maps the sst element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main. String values may
Expand All @@ -21,6 +24,7 @@ import "encoding/xml"
// is an indexed list of string values, shared across the workbook, which allows
// implementations to store values only once.
type xlsxSST struct {
mu sync.Mutex
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main sst"`
Count int `xml:"count,attr"`
UniqueCount int `xml:"uniqueCount,attr"`
Expand Down

0 comments on commit bcbcd91

Please sign in to comment.