mirror of
https://github.com/willnorris/imageproxy.git
synced 2025-03-11 02:19:14 -05:00
define constants for option strings
this prevents extra allocations in ParseOptions and cleans up the code a bit, but doesn't actually improve performance in any meaningful way.
This commit is contained in:
parent
c79633735f
commit
0980ea64a0
2 changed files with 28 additions and 12 deletions
35
data.go
35
data.go
|
@ -23,6 +23,14 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
optFit = "fit"
|
||||||
|
optFlipVertical = "fv"
|
||||||
|
optFlipHorizontal = "fh"
|
||||||
|
optRotatePrefix = "r"
|
||||||
|
optSizeDelimiter = "x"
|
||||||
|
)
|
||||||
|
|
||||||
// URLError reports a malformed URL error.
|
// URLError reports a malformed URL error.
|
||||||
type URLError struct {
|
type URLError struct {
|
||||||
Message string
|
Message string
|
||||||
|
@ -55,18 +63,18 @@ var emptyOptions = Options{}
|
||||||
|
|
||||||
func (o Options) String() string {
|
func (o Options) String() string {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
fmt.Fprintf(buf, "%vx%v", o.Width, o.Height)
|
fmt.Fprintf(buf, "%v%s%v", o.Width, optSizeDelimiter, o.Height)
|
||||||
if o.Fit {
|
if o.Fit {
|
||||||
buf.WriteString(",fit")
|
fmt.Fprintf(buf, ",%s", optFit)
|
||||||
}
|
}
|
||||||
if o.Rotate != 0 {
|
if o.Rotate != 0 {
|
||||||
fmt.Fprintf(buf, ",r%d", o.Rotate)
|
fmt.Fprintf(buf, ",%s%d", string(optRotatePrefix), o.Rotate)
|
||||||
}
|
}
|
||||||
if o.FlipVertical {
|
if o.FlipVertical {
|
||||||
buf.WriteString(",fv")
|
fmt.Fprintf(buf, ",%s", optFlipVertical)
|
||||||
}
|
}
|
||||||
if o.FlipHorizontal {
|
if o.FlipHorizontal {
|
||||||
buf.WriteString(",fh")
|
fmt.Fprintf(buf, ",%s", optFlipHorizontal)
|
||||||
}
|
}
|
||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
@ -127,16 +135,19 @@ func ParseOptions(str string) Options {
|
||||||
|
|
||||||
for _, opt := range strings.Split(str, ",") {
|
for _, opt := range strings.Split(str, ",") {
|
||||||
switch {
|
switch {
|
||||||
case opt == "fit":
|
case len(opt) == 0:
|
||||||
|
break
|
||||||
|
case opt == optFit:
|
||||||
options.Fit = true
|
options.Fit = true
|
||||||
case opt == "fv":
|
case opt == optFlipVertical:
|
||||||
options.FlipVertical = true
|
options.FlipVertical = true
|
||||||
case opt == "fh":
|
case opt == optFlipHorizontal:
|
||||||
options.FlipHorizontal = true
|
options.FlipHorizontal = true
|
||||||
case len(opt) > 2 && opt[:1] == "r":
|
case strings.HasPrefix(opt, optRotatePrefix):
|
||||||
options.Rotate, _ = strconv.Atoi(opt[1:])
|
value := strings.TrimPrefix(opt, optRotatePrefix)
|
||||||
case strings.ContainsRune(opt, 'x'):
|
options.Rotate, _ = strconv.Atoi(value)
|
||||||
size := strings.SplitN(opt, "x", 2)
|
case strings.Contains(opt, optSizeDelimiter):
|
||||||
|
size := strings.SplitN(opt, optSizeDelimiter, 2)
|
||||||
if w := size[0]; w != "" {
|
if w := size[0]; w != "" {
|
||||||
options.Width, _ = strconv.ParseFloat(w, 64)
|
options.Width, _ = strconv.ParseFloat(w, 64)
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,10 @@ func TestOptions_String(t *testing.T) {
|
||||||
Options{1, 2, true, 90, true, true},
|
Options{1, 2, true, 90, true, true},
|
||||||
"1x2,fit,r90,fv,fh",
|
"1x2,fit,r90,fv,fh",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Options{0.15, 1.3, false, 45, false, false},
|
||||||
|
"0.15x1.3,r45",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
|
@ -48,6 +52,7 @@ func TestParseOptions(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{"", emptyOptions},
|
{"", emptyOptions},
|
||||||
{"x", emptyOptions},
|
{"x", emptyOptions},
|
||||||
|
{"r", emptyOptions},
|
||||||
{"0", emptyOptions},
|
{"0", emptyOptions},
|
||||||
{",,,,", emptyOptions},
|
{",,,,", emptyOptions},
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue