Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced fmt methods with faster alternatives #105

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions dbase/database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dbase

import (
"errors"
"fmt"
"path"
"path/filepath"
Expand All @@ -16,10 +17,10 @@ type Database struct {
// The database file must be a DBC file and the tables must be DBF files and in the same directory as the database
func OpenDatabase(config *Config) (*Database, error) {
if config == nil {
return nil, newError("dbase-io-opendatabase-1", fmt.Errorf("missing config"))
return nil, newError("dbase-io-opendatabase-1", errors.New("missing config"))
}
if len(strings.TrimSpace(config.Filename)) == 0 {
return nil, newError("dbase-io-opendatabase-2", fmt.Errorf("missing filename"))
return nil, newError("dbase-io-opendatabase-2", errors.New("missing filename"))
}
if strings.ToUpper(filepath.Ext(config.Filename)) != string(DBC) {
return nil, newError("dbase-io-opendatabase-3", fmt.Errorf("invalid file name: %v", config.Filename))
Expand Down Expand Up @@ -47,7 +48,7 @@ func OpenDatabase(config *Config) (*Database, error) {
}
tableName, ok := objectName.(string)
if !ok {
return nil, newError("dbase-io-opendatabase-8", fmt.Errorf("table name is not a string"))
return nil, newError("dbase-io-opendatabase-8", errors.New("table name is not a string"))
}
tableName = strings.Trim(tableName, " ")
if tableName == "" {
Expand Down
3 changes: 2 additions & 1 deletion dbase/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbase

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -238,7 +239,7 @@ func (file *File) BytesToRow(data []byte) (*Row, error) {
// a row should start with te delete flag, a space ACTIVE(0x20) or DELETED(0x2A)
rec.Deleted = Marker(data[0]) == Deleted
if !rec.Deleted && Marker(data[0]) != Active {
return nil, newError("dbase-table-bytestorow-2", fmt.Errorf("invalid row data, no delete flag found at beginning of row"))
return nil, newError("dbase-table-bytestorow-2", errors.New("invalid row data, no delete flag found at beginning of row"))
}
// deleted flag already read
offset := uint16(1)
Expand Down
9 changes: 5 additions & 4 deletions dbase/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"fmt"
"math"
"strconv"
"time"
)

Expand Down Expand Up @@ -268,8 +269,8 @@ func (file *File) getFloatRepresentation(field *Field, skipSpacing bool) ([]byte
}
var bin []byte
if b == float64(int64(b)) {
// if the value is an integer, store as integer
bin = []byte(fmt.Sprintf("%d", int64(b)))
// if the value has no decimals, store as integer
bin = []byte(strconv.FormatInt(int64(b), 10))
} else {
// if the value is a float, store as float
expression := fmt.Sprintf("%%.%df", field.column.Decimals)
Expand Down Expand Up @@ -431,8 +432,8 @@ func (file *File) getNumericRepresentation(field *Field, skipSpacing bool) ([]by
f, fok := field.value.(float64)
if fok {
if f == float64(int64(f)) {
// if the value is an integer, store as integer
bin = []byte(fmt.Sprintf("%d", int64(f)))
// if the value has no decimals, store as integer
bin = []byte(strconv.FormatInt(int64(f), 10))
} else {
// if the value is a float, store as float
expression := fmt.Sprintf("%%.%df", field.column.Decimals)
Expand Down
9 changes: 5 additions & 4 deletions dbase/io_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dbase
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"path/filepath"
Expand All @@ -21,7 +22,7 @@ type GenericIO struct {

func (g GenericIO) OpenTable(config *Config) (*File, error) {
if config == nil {
return nil, newError("dbase-io-generic-opentable-1", fmt.Errorf("missing configuration"))
return nil, newError("dbase-io-generic-opentable-1", errors.New("missing configuration"))
}
debugf("Opening table from custom io interface - Untested: %v - Trim spaces: %v - ValidateCodepage: %v - InterpretCodepage: %v", config.Untested, config.TrimSpaces, config.ValidateCodePage, config.InterpretCodePage)
fileName := filepath.Clean(config.Filename)
Expand Down Expand Up @@ -71,7 +72,7 @@ func (g GenericIO) OpenTable(config *Config) (*File, error) {
// If the FPT file does not exist an error is returned.
if MemoFlag.Defined(file.header.TableFlags) {
if file.relatedHandle == nil {
return nil, newError("dbase-io-generic-opentable-6", fmt.Errorf("no related handle defined"))
return nil, newError("dbase-io-generic-opentable-6", errors.New("no related handle defined"))
}
err = file.ReadMemoHeader()
if err != nil {
Expand Down Expand Up @@ -431,7 +432,7 @@ func (g GenericIO) ReadNullFlag(file *File, position uint64, column *Column) (bo
return false, false, newError("dbase-io-generic-readnullflag-1", err)
}
if file.nullFlagColumn == nil || (column.DataType != byte(Varchar) && column.DataType != byte(Varbinary)) {
return false, false, newError("dbase-io-generic-readnullflag-2", fmt.Errorf("null flag column missing or not a varchar/varbinary field"))
return false, false, newError("dbase-io-generic-readnullflag-2", errors.New("null flag column missing or not a varchar/varbinary field"))
}
nullFlagPosition := file.table.nullFlagPosition(column)
position = uint64(file.header.FirstRow) + position*uint64(file.header.RowLength) + uint64(file.nullFlagColumn.Position)
Expand Down Expand Up @@ -519,7 +520,7 @@ func (g GenericIO) WriteRow(file *File, row *Row) error {

func (g GenericIO) Search(file *File, field *Field, exactMatch bool) ([]*Row, error) {
if field.column.DataType == 'M' {
return nil, newError("dbase-io-generic-search-1", fmt.Errorf("searching memo fields is not supported"))
return nil, newError("dbase-io-generic-search-1", errors.New("searching memo fields is not supported"))
}
handle, err := g.getHandle(file)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions dbase/io_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package dbase
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"os"
"path"
Expand All @@ -25,7 +26,7 @@ type WindowsIO struct{}

func (w WindowsIO) OpenTable(config *Config) (*File, error) {
if config == nil || len(strings.TrimSpace(config.Filename)) == 0 {
return nil, newError("dbase-io-windows-opentable-1", fmt.Errorf("missing configuration or filename"))
return nil, newError("dbase-io-windows-opentable-1", errors.New("missing configuration or filename"))
}
debugf("Opening table: %s - Read-only: %v - Exclusive: %v - Untested: %v - Trim spaces: %v - Write lock: %v - ValidateCodepage: %v - InterpretCodepage: %v", config.Filename, config.ReadOnly, config.Exclusive, config.Untested, config.TrimSpaces, config.WriteLock, config.ValidateCodePage, config.InterpretCodePage)
var err error
Expand Down Expand Up @@ -161,7 +162,7 @@ func (w WindowsIO) Create(file *File) error {
file.config.Filename = strings.ToUpper(strings.TrimSpace(file.config.Filename))
// Check for valid file name
if len(file.config.Filename) == 0 {
return newError("dbase-io-windows-create-1", fmt.Errorf("missing filename"))
return newError("dbase-io-windows-create-1", errors.New("missing filename"))
}
dbfname, err := windows.UTF16FromString(file.config.Filename)
if err != nil {
Expand All @@ -170,7 +171,7 @@ func (w WindowsIO) Create(file *File) error {
// Check if file exists already
_, err = windows.GetFileAttributes(&dbfname[0])
if err == nil {
return newError("dbase-io-windows-create-3", fmt.Errorf("file already exists"))
return newError("dbase-io-windows-create-3", errors.New("file already exists"))
}
// Create the file
debugf("Creating file: %s", file.config.Filename)
Expand Down Expand Up @@ -388,7 +389,7 @@ func (w WindowsIO) ReadNullFlag(file *File, position uint64, column *Column) (bo
return false, false, newError("dbase-io-windows-readnullflag-1", err)
}
if file.nullFlagColumn == nil || (column.DataType != byte(Varchar) && column.DataType != byte(Varbinary)) {
return false, false, newError("dbase-io-windows-readnullflag-2", fmt.Errorf("null flag column is nil or column is not varchar or varbinary"))
return false, false, newError("dbase-io-windows-readnullflag-2", errors.New("null flag column is nil or column is not varchar or varbinary"))
}
nullFlagPosition := file.table.nullFlagPosition(column)
pos := uint64(file.header.FirstRow) + position*uint64(file.header.RowLength) + uint64(file.nullFlagColumn.Position)
Expand Down Expand Up @@ -688,7 +689,7 @@ func (w WindowsIO) WriteRow(file *File, row *Row) (err error) {

func (w WindowsIO) Search(file *File, field *Field, exactMatch bool) ([]*Row, error) {
if field.column.DataType == 'M' {
return nil, newError("dbase-io-windows-search-1", fmt.Errorf("searching memo fields is not supported"))
return nil, newError("dbase-io-windows-search-1", errors.New("searching memo fields is not supported"))
}
handle, err := w.getHandle(file)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions dbase/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (row *Row) StringValueByName(name string) (string, error) {
if ok {
return string(sanitizeString(bslice)), nil
}
return "", newError("dbase-table-stringvaluebyname-2", fmt.Errorf("value is not a string"))
return "", newError("dbase-table-stringvaluebyname-2", errors.New("value is not a string"))
}
return "", nil
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func (row *Row) IntValueByName(name string) (int64, error) {
if ok {
return intVal, nil
}
return 0, newError("dbase-table-intvaluebyname-2", fmt.Errorf("value is not an int"))
return 0, newError("dbase-table-intvaluebyname-2", errors.New("value is not an int"))
}
return 0, nil
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (row *Row) FloatValueByName(name string) (float64, error) {
if ok {
return floatVal, nil
}
return 0, newError("dbase-table-floatvaluebyname-2", fmt.Errorf("value is not a float"))
return 0, newError("dbase-table-floatvaluebyname-2", errors.New("value is not a float"))
}
return 0, nil
}
Expand All @@ -197,7 +197,7 @@ func (row *Row) BoolValueByName(name string) (bool, error) {
if ok {
return boolVal, nil
}
return false, newError("dbase-table-boolvaluebyname-2", fmt.Errorf("value is not a bool"))
return false, newError("dbase-table-boolvaluebyname-2", errors.New("value is not a bool"))
}

return false, nil
Expand Down Expand Up @@ -226,7 +226,7 @@ func (row *Row) TimeValueByName(name string) (time.Time, error) {
if ok {
return timeVal, nil
}
return time.Time{}, newError("dbase-table-timevaluebyname-2", fmt.Errorf("value is not a time"))
return time.Time{}, newError("dbase-table-timevaluebyname-2", errors.New("value is not a time"))
}
return time.Time{}, nil
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func (row *Row) BytesValueByName(name string) ([]byte, error) {
if ok {
return []byte(strVal), nil
}
return nil, newError("dbase-table-bytesvaluebyname-2", fmt.Errorf("value is not a byte slice"))
return nil, newError("dbase-table-bytesvaluebyname-2", errors.New("value is not a byte slice"))
}
return nil, nil
}
Expand Down Expand Up @@ -462,7 +462,7 @@ func (c *Column) Reflect() (reflect.Type, error) {
// SetValue allows to change the field value
func (field *Field) SetValue(value interface{}) error {
if field == nil {
return newError("dbase-table-setvalue-1", fmt.Errorf("field is not defined by table"))
return newError("dbase-table-setvalue-1", errors.New("field is not defined by table"))
}
field.value = value
return nil
Expand Down
Loading