Skip to content

Commit ef134b9

Browse files
Detect when in a string literal (#47)
* [tests] Add failing test case for quoting issue #46 * [asmfmt.go] Detect when in a string literal Fixes #46
1 parent b745a68 commit ef134b9

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

asmfmt.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,18 @@ func newStatement(s string, defs map[string]struct{}) *statement {
325325
st := statement{}
326326

327327
// Fix where a comment start if any
328+
// We need to make sure that the comment isn't embedded in a string literal
328329
startcom := strings.Index(s, "//")
330+
startstr := strings.Index(s, "\"")
331+
for endstr := 0; startcom > startstr && startstr > endstr; {
332+
// This does not check for any escaping (i.e. "\"")
333+
endstr = startstr + 1 + strings.Index(s[startstr+1:], "\"")
334+
startcom = endstr + strings.Index(s[endstr:], "//")
335+
if startcom < endstr {
336+
startcom = 0
337+
}
338+
startstr = endstr + 1 + strings.Index(s[endstr+1:], "\"")
339+
}
329340
if startcom > 0 {
330341
st.comment = strings.TrimSpace(s[startcom+2:])
331342
s = strings.TrimSpace(s[:startcom])
@@ -417,11 +428,25 @@ func (st *statement) setParams(s string) {
417428
runes := []rune(s)
418429
last := '\n'
419430
inComment := false
431+
inStringLiteral := false
432+
inCharLiteral := false
420433
out := make([]rune, 0, len(runes))
421434
for _, r := range runes {
422435
switch r {
436+
case '"':
437+
if last != '\\' && inStringLiteral {
438+
inStringLiteral = false
439+
} else if last != '\\' && !inStringLiteral {
440+
inStringLiteral = true
441+
}
442+
case '\'':
443+
if last != '\\' && inCharLiteral {
444+
inCharLiteral = false
445+
} else if last != '\\' && !inCharLiteral {
446+
inCharLiteral = true
447+
}
423448
case ',':
424-
if inComment {
449+
if inComment || inStringLiteral || inCharLiteral {
425450
break
426451
}
427452
c := strings.TrimSpace(string(out))
@@ -443,11 +468,12 @@ func (st *statement) setParams(s string) {
443468
r = ' '
444469
}
445470
case ';':
446-
if !inComment {
447-
out = []rune(strings.TrimSpace(string(out)) + "; ")
448-
last = r
449-
continue
471+
if inComment || inStringLiteral || inCharLiteral {
472+
break
450473
}
474+
out = []rune(strings.TrimSpace(string(out)) + "; ")
475+
last = r
476+
continue
451477
}
452478
if last == ';' && unicode.IsSpace(r) {
453479
continue

testdata/quoting.golden

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
db ","
2+
db ";"
3+
db "http://example.com"
4+
db "2 strings", "http://example.com"
5+
db "3 strings", "2//slash groups", "http://example.com"
6+
db ','
7+
db ';'

testdata/quoting.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
db ","
2+
db ";"
3+
db "http://example.com"
4+
db "2 strings", "http://example.com"
5+
db "3 strings", "2//slash groups", "http://example.com"
6+
db ','
7+
db ';'

0 commit comments

Comments
 (0)