Skip to content

Commit 81a8972

Browse files
authored
Optimize string allocations in the qlbridge stringutils (#34)
1 parent 118e8fc commit 81a8972

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

expr/stringutil.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,24 @@ func LeftRight(val string) (string, string, bool) {
2828
}
2929
switch by := val[0]; by {
3030
case '`':
31-
vals := strings.Split(val, "`.`")
32-
if len(vals) == 1 {
31+
i := strings.Index(val, "`.`")
32+
if i == -1 {
3333
return "", IdentityTrim(val), false
34-
} else if len(vals) == 2 {
35-
return IdentityTrim(vals[0]), IdentityTrim(vals[1]), true
3634
}
37-
// wat, no idea what this is
38-
return "", val, false
35+
return IdentityTrim(val[0:i]), IdentityTrim(val[i+3:]), true
3936
case '[':
40-
vals := strings.Split(val, "].[")
41-
if len(vals) == 1 {
37+
i := strings.Index(val, "].[")
38+
if i == -1 {
4239
return "", IdentityTrim(val), false
43-
} else if len(vals) == 2 {
44-
return IdentityTrim(vals[0]), IdentityTrim(vals[1]), true
4540
}
46-
// wat, no idea what this is
47-
return "", val, false
41+
return IdentityTrim(val[0:i]), IdentityTrim(val[i+3:]), true
4842
default:
49-
vals := strings.SplitN(val, ".", 2)
50-
if len(vals) == 1 {
43+
i := strings.Index(val, ".")
44+
if i == -1 {
5145
return "", val, false
52-
} else if len(vals) == 2 {
53-
return IdentityTrim(vals[0]), IdentityTrim(vals[1]), true
5446
}
47+
return IdentityTrim(val[0:i]), IdentityTrim(val[i+1:]), true
5548
}
56-
57-
return "", val, false
5849
}
5950

6051
// IdentityTrim trims the leading/trailing identity quote marks ` or []
@@ -86,7 +77,8 @@ func IdentityMaybeEscapeBuf(buf *bytes.Buffer, quote byte, ident string) {
8677
}
8778

8879
// IdentityMaybeQuoteStrict Quote an identity if need be (has illegal characters or spaces)
89-
// First character MUST be alpha (not numeric or any other character)
80+
//
81+
// First character MUST be alpha (not numeric or any other character)
9082
func IdentityMaybeQuoteStrictBuf(buf *bytes.Buffer, quote byte, ident string) {
9183

9284
if len(ident) == 0 {
@@ -158,10 +150,9 @@ func escapeQuote(buf *bytes.Buffer, quote rune, val string) {
158150

159151
// LiteralQuoteEscape escape string that may need characters escaped
160152
//
161-
// LiteralQuoteEscape("'","item's") => 'item''s'
162-
// LiteralQuoteEscape(`"`,"item's") => "item's"
163-
// LiteralQuoteEscape(`"`,`item"s`) => "item""s"
164-
//
153+
// LiteralQuoteEscape("'","item's") => 'item''s'
154+
// LiteralQuoteEscape(`"`,"item's") => "item's"
155+
// LiteralQuoteEscape(`"`,`item"s`) => "item""s"
165156
func LiteralQuoteEscape(quote rune, literal string) string {
166157
if len(literal) > 1 {
167158
quoteb := byte(quote)
@@ -177,10 +168,9 @@ func LiteralQuoteEscape(quote rune, literal string) string {
177168

178169
// LiteralQuoteEscapeBuf escape string that may need characters escaped
179170
//
180-
// LiteralQuoteEscapeBuf("'","item's") => 'item''s'
181-
// LiteralQuoteEscapeBuf(`"`,"item's") => "item's"
182-
// LiteralQuoteEscapeBuf(`"`,`item"s`) => "item""s"
183-
//
171+
// LiteralQuoteEscapeBuf("'","item's") => 'item''s'
172+
// LiteralQuoteEscapeBuf(`"`,"item's") => "item's"
173+
// LiteralQuoteEscapeBuf(`"`,`item"s`) => "item""s"
184174
func LiteralQuoteEscapeBuf(buf *bytes.Buffer, quote rune, literal string) {
185175
if len(literal) > 1 {
186176
quoteb := byte(quote)
@@ -201,8 +191,7 @@ func LiteralQuoteEscapeBuf(buf *bytes.Buffer, quote rune, literal string) {
201191

202192
// StringEscape escape string that may need characters escaped
203193
//
204-
// StringEscape("'","item's") => "item''s"
205-
//
194+
// StringEscape("'","item's") => "item''s"
206195
func StringEscape(quote rune, literal string) string {
207196
var buf bytes.Buffer
208197
escapeQuote(&buf, quote, literal)
@@ -211,8 +200,7 @@ func StringEscape(quote rune, literal string) string {
211200

212201
// StringUnEscape remove escaping on string that may need characters escaped
213202
//
214-
// StringUnEscape(`"`,`item"s`) => "item""s", true
215-
//
203+
// StringUnEscape(`"`,`item"s`) => "item""s", true
216204
func StringUnEscape(quote rune, val string) (string, bool) {
217205
var buf bytes.Buffer
218206
prevEscape, hasEscape := false, false

0 commit comments

Comments
 (0)