1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-05 07:40:24 +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
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

View File

@@ -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

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))
}
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)
}