From 9d67881661d98e07b8e14340a69bba2e486f9bf4 Mon Sep 17 00:00:00 2001 From: "Geoffrey J. Teale" Date: Fri, 1 Nov 2024 17:31:25 +0100 Subject: [PATCH] We know how large our shared string ref table should be, sometimes --- file.go | 14 +++++++------- file_test.go | 1 - memory.go | 3 ++- reftable.go | 9 +++++---- reftable_test.go | 16 ++++++++-------- sheet_test.go | 26 +++++++++++++------------- 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/file.go b/file.go index 9733d421..c57c7919 100644 --- a/file.go +++ b/file.go @@ -202,15 +202,15 @@ func (f *File) AddSheetWithCellStore(sheetName string, constructor CellStoreCons if _, exists := f.Sheet[sheetName]; exists { return nil, fmt.Errorf("duplicate sheet name '%s'.", sheetName) } - + if err := IsSaneSheetName(sheetName); err != nil { return nil, fmt.Errorf("sheet name is not valid: %w", err) } sheet := &Sheet{ - Name: sheetName, - File: f, - Selected: len(f.Sheets) == 0, - Cols: &ColStore{}, + Name: sheetName, + File: f, + Selected: len(f.Sheets) == 0, + Cols: &ColStore{}, cellStoreName: sheetName, } @@ -335,7 +335,7 @@ func autoFilterDefinedName(sheet *Sheet, sheetIndex int) (*xlsxDefinedName, erro // representing the file in terms of the structure of an XLSX file. func (f *File) MakeStreamParts() (map[string]string, error) { var parts map[string]string - var refTable *RefTable = NewSharedStringRefTable() + var refTable *RefTable = NewSharedStringRefTable(10000) // 10000 is arbitrary refTable.isWrite = true var workbookRels WorkBookRels = make(WorkBookRels) var err error @@ -465,7 +465,7 @@ func (f *File) MakeStreamParts() (map[string]string, error) { // MarshallParts constructs a map of file name to XML content representing the file // in terms of the structure of an XLSX file. func (f *File) MarshallParts(zipWriter *zip.Writer) error { - var refTable *RefTable = NewSharedStringRefTable() + var refTable *RefTable = NewSharedStringRefTable(10000) // 10000 is arbitrary refTable.isWrite = true var workbookRels WorkBookRels = make(WorkBookRels) var err error diff --git a/file_test.go b/file_test.go index 532021f4..8b1f3963 100644 --- a/file_test.go +++ b/file_test.go @@ -405,7 +405,6 @@ func TestFile(t *testing.T) { blokes, err := OpenFile(p) c.Assert(err, qt.IsNil) - dave := blokes.Sheets[0] if dave.currentRow != nil { diff --git a/memory.go b/memory.go index df83c2b1..aab8d924 100644 --- a/memory.go +++ b/memory.go @@ -102,9 +102,10 @@ func (mr *MemoryRow) ForEachCell(cvf CellVisitorFunc, option ...CellVisitorOptio } } cellCount := len(mr.cells) + var c *Cell if !flags.skipEmptyCells { for ci := cellCount; ci < mr.row.Sheet.MaxCol; ci++ { - c := mr.GetCell(ci) + c = mr.GetCell(ci) err := cvf(c) if err != nil { return err diff --git a/reftable.go b/reftable.go index fb711b26..0195b3b2 100644 --- a/reftable.go +++ b/reftable.go @@ -14,10 +14,11 @@ type RefTable struct { } // NewSharedStringRefTable creates a new, empty RefTable. -func NewSharedStringRefTable() *RefTable { +func NewSharedStringRefTable(size int) *RefTable { rt := RefTable{} - rt.knownStrings = make(map[string]int) - rt.knownRichTexts = make(map[string][]int) + rt.indexedStrings = make([]plainTextOrRichText, 0, size) + rt.knownStrings = make(map[string]int, size) + rt.knownRichTexts = make(map[string][]int, size) return &rt } @@ -26,7 +27,7 @@ func NewSharedStringRefTable() *RefTable { // by numeric index - this is the model used within XLSX worksheet (a // numeric reference is stored to a shared cell value). func MakeSharedStringRefTable(source *xlsxSST) *RefTable { - reftable := NewSharedStringRefTable() + reftable := NewSharedStringRefTable(len(source.SI)) reftable.isWrite = false for _, si := range source.SI { if len(si.R) > 0 { diff --git a/reftable_test.go b/reftable_test.go index 7bb304af..9e8d537f 100644 --- a/reftable_test.go +++ b/reftable_test.go @@ -50,7 +50,7 @@ var reftabletest_sharedStringsXMLStr = (`