Skip to content

Commit

Permalink
Removed Parser struct and init regex
Browse files Browse the repository at this point in the history
This is a breaking change.

Parser struct removed and variables DisableCheck, reTimeDecimal and reDuration are global.

init function initialize reTimeDecimal and reDuration.
  • Loading branch information
xhit committed Jul 2, 2020
1 parent ccda8ef commit 3c666fd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 42 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Santiago De la Cruz
Copyright (c) 2020 Santiago De la Cruz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ import (

func main() {

//Initialize the parser
s2dParser := str2duration.NewStr2DurationParser()

/*
If DisableCheck is true then when input string is
is invalid the time.Duration returned is always 0s and err is always nil.
By default DisableCheck is false.
*/

//s2dParser.DisableCheck = true
//str2duration.DisableCheck = true

for i, tt := range []struct {
dur string
Expand Down Expand Up @@ -80,7 +77,7 @@ func main() {
{"2D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},

} {
durationFromString, err := s2dParser.Str2Duration(tt.dur)
durationFromString, err := str2duration.Str2Duration(tt.dur)
if err != nil {
panic(err)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/xhit/go-str2duration

go 1.13
go 1.14
43 changes: 16 additions & 27 deletions str2duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,22 @@ const (
)

/*
Parser struct contains flag
for DisableCheck the speed up performance disabling aditional checks
DisableCheck speed up performance disabling aditional checks
in the input string. If DisableCheck is true then when input string is
is invalid the time.Duration returned is always 0s and err is always nil.
By default DisableCheck is false.
Parser also contains compiled regex for performance.
*/
type Parser struct {
reTimeDecimal *regexp.Regexp
reDuration *regexp.Regexp
DisableCheck bool
}
var DisableCheck bool
var reTimeDecimal *regexp.Regexp
var reDuration *regexp.Regexp

//NewStr2DurationParser returns unexported struct with compiled regex for performance
func NewStr2DurationParser() Parser {
var reTimeDecimal = regexp.MustCompile(`(?i)(\d+)(?:(?:\.)(\d+))?((?:[mµn])?s)$`)
var reDuration = regexp.MustCompile(`(?i)^(?:(\d+)(?:w))?(?:(\d+)(?:d))?(?:(\d+)(?:h))?(?:(\d{1,2})(?:m))?(?:(\d+)(?:s))?(?:(\d+)(?:ms))?(?:(\d+)(?:µs))?(?:(\d+)(?:ns))?$`)

return Parser{
reTimeDecimal: reTimeDecimal,
reDuration: reDuration,
}
func init() {
reTimeDecimal = regexp.MustCompile(`(?i)(\d+)(?:(?:\.)(\d+))?((?:[mµn])?s)$`)
reDuration = regexp.MustCompile(`(?i)^(?:(\d+)(?:w))?(?:(\d+)(?:d))?(?:(\d+)(?:h))?(?:(\d{1,2})(?:m))?(?:(\d+)(?:s))?(?:(\d+)(?:ms))?(?:(\d+)(?:µs))?(?:(\d+)(?:ns))?$`)
}

//Str2Duration returns time.Duration from string input
func (p *Parser) Str2Duration(str string) (time.Duration, error) {
func Str2Duration(str string) (time.Duration, error) {

var err error
/*
Expand All @@ -49,22 +38,22 @@ func (p *Parser) Str2Duration(str string) (time.Duration, error) {
string then that time is formatted in nanoseconds, this example returns 1000000001ns
*/
if strings.Contains(str, ".") {
str, err = p.decimalTimeToNano(str)
str, err = decimalTimeToNano(str)
if err != nil {
return time.Duration(0), err
}
}

if !p.DisableCheck {
if !p.reDuration.MatchString(str) {
if !DisableCheck {
if !reDuration.MatchString(str) {
return time.Duration(0), errors.New("invalid input duration string")
}
}

var du time.Duration

//errors ignored because regex
for _, match := range p.reDuration.FindAllStringSubmatch(str, -1) {
for _, match := range reDuration.FindAllStringSubmatch(str, -1) {

//weeks
if len(match[1]) > 0 {
Expand Down Expand Up @@ -118,17 +107,17 @@ func (p *Parser) Str2Duration(str string) (time.Duration, error) {
return du, nil
}

func (p *Parser) decimalTimeToNano(str string) (string, error) {
func decimalTimeToNano(str string) (string, error) {

var dotPart, dotTime, dotTimeDecimal, dotUnit string

if !p.DisableCheck {
if !p.reTimeDecimal.MatchString(str) {
if !DisableCheck {
if !reTimeDecimal.MatchString(str) {
return "", errors.New("invalid input duration string")
}
}

var t = p.reTimeDecimal.FindAllStringSubmatch(str, -1)
var t = reTimeDecimal.FindAllStringSubmatch(str, -1)

dotPart = t[0][0]
dotTime = t[0][1]
Expand Down
12 changes: 4 additions & 8 deletions str2duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (

func TestParseString(t *testing.T) {

str2duration := NewStr2DurationParser()

str2duration.DisableCheck = false
DisableCheck = false

for i, tt := range []struct {
dur string
Expand Down Expand Up @@ -48,7 +46,7 @@ func TestParseString(t *testing.T) {
{"2.3D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
{"2D3S3.66.SMS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
} {
durationFromString, err := str2duration.Str2Duration(tt.dur)
durationFromString, err := Str2Duration(tt.dur)
if err != nil {
t.Logf("index %d -> in: %s returned: %s\tnot equal to %s", i, tt.dur, err.Error(), tt.expected.String())

Expand All @@ -61,9 +59,7 @@ func TestParseString(t *testing.T) {
//TestParseDuration test if string returned by a duration is equal to string returned with the package
func TestParseDuration(t *testing.T) {

str2duration := NewStr2DurationParser()

str2duration.DisableCheck = true
DisableCheck = true

for i, duration := range []time.Duration{
time.Duration(time.Hour + time.Minute + time.Second + time.Millisecond + time.Microsecond + time.Nanosecond),
Expand Down Expand Up @@ -96,7 +92,7 @@ func TestParseDuration(t *testing.T) {
time.Duration(61 * time.Second),
time.Duration(time.Microsecond + 16*time.Nanosecond),
} {
durationFromString, _ := str2duration.Str2Duration(duration.String())
durationFromString, _ := Str2Duration(duration.String())
if duration.String() != durationFromString.String() {
t.Errorf("index %d -> %s not equal to %s", i, duration.String(), durationFromString.String())
}
Expand Down

0 comments on commit 3c666fd

Please sign in to comment.