From 6bcf5e4ede160af2ad04f5e69636211a5ced132d Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sun, 12 Jun 2022 00:19:12 +0800 Subject: refactor: replace strings.Replace with strings.ReplaceAll (#1250) strings.ReplaceAll(s, old, new) is a wrapper function for strings.Replace(s, old, new, -1). But strings.ReplaceAll is more readable and removes the hardcoded -1. Signed-off-by: Eng Zer Jun --- calc.go | 10 +++++----- chart.go | 2 +- comment.go | 2 +- drawing.go | 2 +- lib.go | 4 ++-- picture.go | 12 ++++++------ pivotTable.go | 4 ++-- shape.go | 6 +++--- sheet.go | 4 ++-- stream.go | 2 +- styles.go | 4 ++-- table.go | 2 +- 12 files changed, 27 insertions(+), 27 deletions(-) diff --git a/calc.go b/calc.go index 35b9d74..e9a676d 100644 --- a/calc.go +++ b/calc.go @@ -1360,7 +1360,7 @@ func (f *File) parseToken(sheet string, token efp.Token, opdStack, optStack *Sta // parseReference parse reference and extract values by given reference // characters and default sheet name. func (f *File) parseReference(sheet, reference string) (arg formulaArg, err error) { - reference = strings.Replace(reference, "$", "", -1) + reference = strings.ReplaceAll(reference, "$", "") refs, cellRanges, cellRefs := list.New(), list.New(), list.New() for _, ref := range strings.Split(reference, ":") { tokens := strings.Split(ref, "!") @@ -2065,13 +2065,13 @@ func cmplx2str(num complex128, suffix string) string { c = strings.TrimSuffix(c, "+0i") c = strings.TrimSuffix(c, "-0i") c = strings.NewReplacer("+1i", "+i", "-1i", "-i").Replace(c) - c = strings.Replace(c, "i", suffix, -1) + c = strings.ReplaceAll(c, "i", suffix) return c } // str2cmplx convert complex number string characters. func str2cmplx(c string) string { - c = strings.Replace(c, "j", "i", -1) + c = strings.ReplaceAll(c, "j", "i") if c == "i" { c = "1i" } @@ -13489,7 +13489,7 @@ func (fn *formulaFuncs) SUBSTITUTE(argsList *list.List) formulaArg { text, oldText := argsList.Front().Value.(formulaArg), argsList.Front().Next().Value.(formulaArg) newText, instanceNum := argsList.Front().Next().Next().Value.(formulaArg), 0 if argsList.Len() == 3 { - return newStringFormulaArg(strings.Replace(text.Value(), oldText.Value(), newText.Value(), -1)) + return newStringFormulaArg(strings.ReplaceAll(text.Value(), oldText.Value(), newText.Value())) } instanceNumArg := argsList.Back().Value.(formulaArg).ToNumber() if instanceNumArg.Type != ArgNumber { @@ -14804,7 +14804,7 @@ func (fn *formulaFuncs) ENCODEURL(argsList *list.List) formulaArg { return newErrorFormulaArg(formulaErrorVALUE, "ENCODEURL requires 1 argument") } token := argsList.Front().Value.(formulaArg).Value() - return newStringFormulaArg(strings.Replace(url.QueryEscape(token), "+", "%20", -1)) + return newStringFormulaArg(strings.ReplaceAll(url.QueryEscape(token), "+", "%20")) } // Financial Functions diff --git a/chart.go b/chart.go index 7b7162b..7dcbe19 100644 --- a/chart.go +++ b/chart.go @@ -1001,7 +1001,7 @@ func (f *File) DeleteChart(sheet, cell string) (err error) { if ws.Drawing == nil { return } - drawingXML := strings.Replace(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl", -1) + drawingXML := strings.ReplaceAll(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl") return f.deleteDrawing(col, row, drawingXML, "Chart") } diff --git a/comment.go b/comment.go index a7c1415..0e3945d 100644 --- a/comment.go +++ b/comment.go @@ -112,7 +112,7 @@ func (f *File) AddComment(sheet, cell, format string) error { // The worksheet already has a comments relationships, use the relationships drawing ../drawings/vmlDrawing%d.vml. sheetRelationshipsDrawingVML = f.getSheetRelationshipsTargetByID(sheet, ws.LegacyDrawing.RID) commentID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingVML, "../drawings/vmlDrawing"), ".vml")) - drawingVML = strings.Replace(sheetRelationshipsDrawingVML, "..", "xl", -1) + drawingVML = strings.ReplaceAll(sheetRelationshipsDrawingVML, "..", "xl") } else { // Add first comment for given sheet. sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels" diff --git a/drawing.go b/drawing.go index d0e9135..7de0fb9 100644 --- a/drawing.go +++ b/drawing.go @@ -30,7 +30,7 @@ func (f *File) prepareDrawing(ws *xlsxWorksheet, drawingID int, sheet, drawingXM // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID) drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml")) - drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1) + drawingXML = strings.ReplaceAll(sheetRelationshipsDrawingXML, "..", "xl") } else { // Add first picture for given sheet. sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels" diff --git a/lib.go b/lib.go index 723b976..f285a40 100644 --- a/lib.go +++ b/lib.go @@ -43,7 +43,7 @@ func (f *File) ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) { if unzipSize > f.options.UnzipSizeLimit { return fileList, worksheets, newUnzipSizeLimitError(f.options.UnzipSizeLimit) } - fileName := strings.Replace(v.Name, "\\", "/", -1) + fileName := strings.ReplaceAll(v.Name, "\\", "/") if partName, ok := docPart[strings.ToLower(fileName)]; ok { fileName = partName } @@ -284,7 +284,7 @@ func CoordinatesToCellName(col, row int, abs ...bool) (string, error) { // areaRefToCoordinates provides a function to convert area reference to a // pair of coordinates. func areaRefToCoordinates(ref string) ([]int, error) { - rng := strings.Split(strings.Replace(ref, "$", "", -1), ":") + rng := strings.Split(strings.ReplaceAll(ref, "$", ""), ":") if len(rng) < 2 { return nil, ErrParameterInvalid } diff --git a/picture.go b/picture.go index 30a66d3..44f1f3b 100644 --- a/picture.go +++ b/picture.go @@ -508,12 +508,12 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte, error) { return "", nil, err } target := f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID) - drawingXML := strings.Replace(target, "..", "xl", -1) + drawingXML := strings.ReplaceAll(target, "..", "xl") if _, ok := f.Pkg.Load(drawingXML); !ok { return "", nil, err } - drawingRelationships := strings.Replace( - strings.Replace(target, "../drawings", "xl/drawings/_rels", -1), ".xml", ".xml.rels", -1) + drawingRelationships := strings.ReplaceAll( + strings.ReplaceAll(target, "../drawings", "xl/drawings/_rels"), ".xml", ".xml.rels") return f.getPicture(row, col, drawingXML, drawingRelationships) } @@ -535,7 +535,7 @@ func (f *File) DeletePicture(sheet, cell string) (err error) { if ws.Drawing == nil { return } - drawingXML := strings.Replace(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl", -1) + drawingXML := strings.ReplaceAll(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl") return f.deleteDrawing(col, row, drawingXML, "Pic") } @@ -573,7 +573,7 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string) drawRel = f.getDrawingRelationships(drawingRelationships, deTwoCellAnchor.Pic.BlipFill.Blip.Embed) if _, ok = supportedImageTypes[filepath.Ext(drawRel.Target)]; ok { ret = filepath.Base(drawRel.Target) - if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil { + if buffer, _ := f.Pkg.Load(strings.ReplaceAll(drawRel.Target, "..", "xl")); buffer != nil { buf = buffer.([]byte) } return @@ -602,7 +602,7 @@ func (f *File) getPictureFromWsDr(row, col int, drawingRelationships string, wsD anchor.Pic.BlipFill.Blip.Embed); drawRel != nil { if _, ok = supportedImageTypes[filepath.Ext(drawRel.Target)]; ok { ret = filepath.Base(drawRel.Target) - if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil { + if buffer, _ := f.Pkg.Load(strings.ReplaceAll(drawRel.Target, "..", "xl")); buffer != nil { buf = buffer.([]byte) } return diff --git a/pivotTable.go b/pivotTable.go index de671f7..10c48ce 100644 --- a/pivotTable.go +++ b/pivotTable.go @@ -141,7 +141,7 @@ func (f *File) AddPivotTable(opt *PivotTableOption) error { pivotCacheID := f.countPivotCache() + 1 sheetRelationshipsPivotTableXML := "../pivotTables/pivotTable" + strconv.Itoa(pivotTableID) + ".xml" - pivotTableXML := strings.Replace(sheetRelationshipsPivotTableXML, "..", "xl", -1) + pivotTableXML := strings.ReplaceAll(sheetRelationshipsPivotTableXML, "..", "xl") pivotCacheXML := "xl/pivotCache/pivotCacheDefinition" + strconv.Itoa(pivotCacheID) + ".xml" err = f.addPivotCache(pivotCacheXML, opt) if err != nil { @@ -206,7 +206,7 @@ func (f *File) adjustRange(rangeStr string) (string, []int, error) { if len(rng) != 2 { return "", []int{}, ErrParameterInvalid } - trimRng := strings.Replace(rng[1], "$", "", -1) + trimRng := strings.ReplaceAll(rng[1], "$", "") coordinates, err := areaRefToCoordinates(trimRng) if err != nil { return rng[0], []int{}, err diff --git a/shape.go b/shape.go index 6d86f38..ddf9e31 100644 --- a/shape.go +++ b/shape.go @@ -297,7 +297,7 @@ func (f *File) AddShape(sheet, cell, format string) error { // The worksheet already has a shape or chart relationships, use the relationships drawing ../drawings/drawing%d.xml. sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID) drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml")) - drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1) + drawingXML = strings.ReplaceAll(sheetRelationshipsDrawingXML, "..", "xl") } else { // Add first shape for given sheet. sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels" @@ -448,7 +448,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format Lang: "en-US", }, } - srgbClr := strings.Replace(strings.ToUpper(p.Font.Color), "#", "", -1) + srgbClr := strings.ReplaceAll(strings.ToUpper(p.Font.Color), "#", "") if len(srgbClr) == 6 { paragraph.R.RPr.SolidFill = &aSolidFill{ SrgbClr: &attrValString{ @@ -484,7 +484,7 @@ func setShapeRef(color string, i int) *aRef { return &aRef{ Idx: i, SrgbClr: &attrValString{ - Val: stringPtr(strings.Replace(strings.ToUpper(color), "#", "", -1)), + Val: stringPtr(strings.ReplaceAll(strings.ToUpper(color), "#", "")), }, } } diff --git a/sheet.go b/sheet.go index 47a2063..7b6e5dc 100644 --- a/sheet.go +++ b/sheet.go @@ -99,9 +99,9 @@ func (f *File) contentTypesWriter() { // and /xl/worksheets/sheet%d.xml func (f *File) getWorksheetPath(relTarget string) (path string) { path = filepath.ToSlash(strings.TrimPrefix( - strings.Replace(filepath.Clean(fmt.Sprintf("%s/%s", filepath.Dir(f.getWorkbookPath()), relTarget)), "\\", "/", -1), "/")) + strings.ReplaceAll(filepath.Clean(fmt.Sprintf("%s/%s", filepath.Dir(f.getWorkbookPath()), relTarget)), "\\", "/"), "/")) if strings.HasPrefix(relTarget, "/") { - path = filepath.ToSlash(strings.TrimPrefix(strings.Replace(filepath.Clean(relTarget), "\\", "/", -1), "/")) + path = filepath.ToSlash(strings.TrimPrefix(strings.ReplaceAll(filepath.Clean(relTarget), "\\", "/"), "/")) } return path } diff --git a/stream.go b/stream.go index e1a12be..641340e 100644 --- a/stream.go +++ b/stream.go @@ -206,7 +206,7 @@ func (sw *StreamWriter) AddTable(hCell, vCell, format string) error { } sheetRelationshipsTableXML := "../tables/table" + strconv.Itoa(tableID) + ".xml" - tableXML := strings.Replace(sheetRelationshipsTableXML, "..", "xl", -1) + tableXML := strings.ReplaceAll(sheetRelationshipsTableXML, "..", "xl") // Add first table for given sheet. sheetPath := sw.File.sheetMap[trimSheetName(sw.Sheet)] diff --git a/styles.go b/styles.go index f8f4030..0220e9c 100644 --- a/styles.go +++ b/styles.go @@ -2123,7 +2123,7 @@ func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int { if !currency { return setLangNumFmt(styleSheet, style) } - fc = strings.Replace(fc, "0.00", dp, -1) + fc = strings.ReplaceAll(fc, "0.00", dp) if style.NegRed { fc = fc + ";[Red]" + fc } @@ -3018,7 +3018,7 @@ func drawConfFmtExp(p int, ct string, format *formatConditional) *xlsxCfRule { // getPaletteColor provides a function to convert the RBG color by given // string. func getPaletteColor(color string) string { - return "FF" + strings.Replace(strings.ToUpper(color), "#", "", -1) + return "FF" + strings.ReplaceAll(strings.ToUpper(color), "#", "") } // themeReader provides a function to get the pointer to the xl/theme/theme1.xml diff --git a/table.go b/table.go index b01c1cb..413118c 100644 --- a/table.go +++ b/table.go @@ -83,7 +83,7 @@ func (f *File) AddTable(sheet, hCell, vCell, format string) error { tableID := f.countTables() + 1 sheetRelationshipsTableXML := "../tables/table" + strconv.Itoa(tableID) + ".xml" - tableXML := strings.Replace(sheetRelationshipsTableXML, "..", "xl", -1) + tableXML := strings.ReplaceAll(sheetRelationshipsTableXML, "..", "xl") // Add first table for given sheet. sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels" rID := f.addRels(sheetRels, SourceRelationshipTable, sheetRelationshipsTableXML, "") -- cgit v1.2.1