1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-20 09:30:50 +08:00

reduce unsafe

This commit is contained in:
源文雨
2023-03-04 21:31:03 +08:00
parent 1e227341ee
commit ff131af5fa
3 changed files with 13 additions and 23 deletions

View File

@@ -20,18 +20,14 @@
package docx package docx
import "unsafe"
// AddParagraph adds a new paragraph // AddParagraph adds a new paragraph
func (f *Docx) AddParagraph() *Paragraph { func (f *Docx) AddParagraph() *Paragraph {
f.Document.Body.Items = append(f.Document.Body.Items, &Paragraph{ p := &Paragraph{
Children: make([]interface{}, 0, 64), Children: make([]interface{}, 0, 64),
file: f, file: f,
}) }
f.Document.Body.Items = append(f.Document.Body.Items, p)
p := f.Document.Body.Items[len(f.Document.Body.Items)-1] return p
return *(**Paragraph)(unsafe.Add(unsafe.Pointer(&p), unsafe.Sizeof(uintptr(0))))
} }
// AddParagraph adds a new paragraph // AddParagraph adds a new paragraph

View File

@@ -20,8 +20,6 @@
package docx package docx
import "unsafe"
// AddTable add a new table to body by col*row // AddTable add a new table to body by col*row
// //
// unit: twips (1/20 point) // unit: twips (1/20 point)
@@ -42,7 +40,7 @@ func (f *Docx) AddTable(row int, col int) *WTable {
TableCells: cells, TableCells: cells,
} }
} }
f.Document.Body.Items = append(f.Document.Body.Items, &WTable{ tbl := &WTable{
TableProperties: &WTableProperties{ TableProperties: &WTableProperties{
Width: &WTableWidth{Type: "auto"}, Width: &WTableWidth{Type: "auto"},
TableBorders: &WTableBorders{ TableBorders: &WTableBorders{
@@ -59,11 +57,9 @@ func (f *Docx) AddTable(row int, col int) *WTable {
}, },
TableGrid: &WTableGrid{}, TableGrid: &WTableGrid{},
TableRows: trs, TableRows: trs,
}) }
f.Document.Body.Items = append(f.Document.Body.Items, tbl)
t := f.Document.Body.Items[len(f.Document.Body.Items)-1] return tbl
return *(**WTable)(unsafe.Add(unsafe.Pointer(&t), unsafe.Sizeof(uintptr(0))))
} }
// AddTableTwips add a new table to body by height and width // 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{ TableProperties: &WTableProperties{
Width: &WTableWidth{Type: "auto"}, Width: &WTableWidth{Type: "auto"},
TableBorders: &WTableBorders{ TableBorders: &WTableBorders{
@@ -118,11 +114,9 @@ func (f *Docx) AddTableTwips(rowHeights []int64, colWidths []int64) *WTable {
GridCols: grids, GridCols: grids,
}, },
TableRows: trs, TableRows: trs,
}) }
f.Document.Body.Items = append(f.Document.Body.Items, tbl)
t := f.Document.Body.Items[len(f.Document.Body.Items)-1] return tbl
return *(**WTable)(unsafe.Add(unsafe.Pointer(&t), unsafe.Sizeof(uintptr(0))))
} }
// Justification allows to set table's horizonal alignment // Justification allows to set table's horizonal alignment

View File

@@ -50,7 +50,7 @@ func TestUnmarshalPlainStructure(t *testing.T) {
t.Fatalf("We expected %d paragraphs, we got %d", tc.numParagraphs, len(doc.Body.Items)) t.Fatalf("We expected %d paragraphs, we got %d", tc.numParagraphs, len(doc.Body.Items))
} }
for i, it := range doc.Body.Items { for i, it := range doc.Body.Items {
p := it.(Paragraph) p := it.(*Paragraph)
if len(p.Children) == 0 { if len(p.Children) == 0 {
t.Fatalf("We were not able to parse paragraph %d", i) t.Fatalf("We were not able to parse paragraph %d", i)
} }