From ff131af5fa448b454b66f5602813aa108be768e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 4 Mar 2023 21:31:03 +0800 Subject: [PATCH] reduce unsafe --- apipara.go | 12 ++++-------- apitable.go | 22 ++++++++-------------- structdoc_test.go | 2 +- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/apipara.go b/apipara.go index d1e0d93..1ad272a 100644 --- a/apipara.go +++ b/apipara.go @@ -20,18 +20,14 @@ package docx -import "unsafe" - // AddParagraph adds a new paragraph func (f *Docx) AddParagraph() *Paragraph { - f.Document.Body.Items = append(f.Document.Body.Items, &Paragraph{ + p := &Paragraph{ Children: make([]interface{}, 0, 64), file: f, - }) - - p := f.Document.Body.Items[len(f.Document.Body.Items)-1] - - return *(**Paragraph)(unsafe.Add(unsafe.Pointer(&p), unsafe.Sizeof(uintptr(0)))) + } + f.Document.Body.Items = append(f.Document.Body.Items, p) + return p } // AddParagraph adds a new paragraph diff --git a/apitable.go b/apitable.go index c13311d..eaeca58 100644 --- a/apitable.go +++ b/apitable.go @@ -20,8 +20,6 @@ package docx -import "unsafe" - // AddTable add a new table to body by col*row // // unit: twips (1/20 point) @@ -42,7 +40,7 @@ func (f *Docx) AddTable(row int, col int) *WTable { TableCells: cells, } } - f.Document.Body.Items = append(f.Document.Body.Items, &WTable{ + tbl := &WTable{ TableProperties: &WTableProperties{ Width: &WTableWidth{Type: "auto"}, TableBorders: &WTableBorders{ @@ -59,11 +57,9 @@ func (f *Docx) AddTable(row int, col int) *WTable { }, TableGrid: &WTableGrid{}, TableRows: trs, - }) - - t := f.Document.Body.Items[len(f.Document.Body.Items)-1] - - return *(**WTable)(unsafe.Add(unsafe.Pointer(&t), unsafe.Sizeof(uintptr(0)))) + } + f.Document.Body.Items = append(f.Document.Body.Items, tbl) + return tbl } // AddTableTwips add a new table to body by height and width @@ -99,7 +95,7 @@ func (f *Docx) AddTableTwips(rowHeights []int64, colWidths []int64) *WTable { } } } - f.Document.Body.Items = append(f.Document.Body.Items, &WTable{ + tbl := &WTable{ TableProperties: &WTableProperties{ Width: &WTableWidth{Type: "auto"}, TableBorders: &WTableBorders{ @@ -118,11 +114,9 @@ func (f *Docx) AddTableTwips(rowHeights []int64, colWidths []int64) *WTable { GridCols: grids, }, TableRows: trs, - }) - - t := f.Document.Body.Items[len(f.Document.Body.Items)-1] - - return *(**WTable)(unsafe.Add(unsafe.Pointer(&t), unsafe.Sizeof(uintptr(0)))) + } + f.Document.Body.Items = append(f.Document.Body.Items, tbl) + return tbl } // Justification allows to set table's horizonal alignment diff --git a/structdoc_test.go b/structdoc_test.go index 41a3446..fa1a840 100644 --- a/structdoc_test.go +++ b/structdoc_test.go @@ -50,7 +50,7 @@ func TestUnmarshalPlainStructure(t *testing.T) { t.Fatalf("We expected %d paragraphs, we got %d", tc.numParagraphs, len(doc.Body.Items)) } for i, it := range doc.Body.Items { - p := it.(Paragraph) + p := it.(*Paragraph) if len(p.Children) == 0 { t.Fatalf("We were not able to parse paragraph %d", i) }