@@ -359,7 +359,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment
359
359
return
360
360
}
361
361
362
- if pos .Line == p .last .Line && (prev == nil || prev .Text [1 ] != '/' ) {
362
+ if pos .Line == p .last .Line && (prev == nil || len ( prev . Text ) > 1 && prev .Text [1 ] != '/' ) {
363
363
// comment on the same line as last item:
364
364
// separate with at least one separator
365
365
hasSep := false
@@ -457,7 +457,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment
457
457
458
458
// make sure there is at least one line break
459
459
// if the previous comment was a line comment
460
- if n == 0 && prev != nil && prev .Text [1 ] == '/' {
460
+ if n == 0 && prev != nil && len ( prev . Text ) > 1 && prev .Text [1 ] == '/' {
461
461
n = 1
462
462
}
463
463
@@ -567,7 +567,7 @@ func stripCommonPrefix(lines []string) {
567
567
// for the opening /*, assume up to 3 blanks or a tab. This
568
568
// whitespace may be found as suffix in the common prefix.
569
569
first := lines [0 ]
570
- if isBlank (first [2 :]) {
570
+ if len ( first ) > 2 && isBlank (first [2 :]) {
571
571
// no comment text on the first line:
572
572
// reduce prefix by up to 3 blanks or a tab
573
573
// if present - this keeps comment text indented
@@ -594,8 +594,12 @@ func stripCommonPrefix(lines []string) {
594
594
suffix = suffix [2 :n ]
595
595
} else {
596
596
// otherwise assume two blanks
597
- suffix [0 ], suffix [1 ] = ' ' , ' '
598
- suffix = suffix [0 :n ]
597
+ if len (suffix ) > 1 {
598
+ suffix [0 ], suffix [1 ] = ' ' , ' '
599
+ }
600
+ if len (suffix ) > n {
601
+ suffix = suffix [0 :n ]
602
+ }
599
603
}
600
604
// Shorten the computed common prefix by the length of
601
605
// suffix, if it is found as suffix of the prefix.
@@ -643,7 +647,7 @@ func (p *printer) writeComment(comment *ast.Comment) {
643
647
}
644
648
645
649
// shortcut common case of //-style comments
646
- if text [1 ] == '/' {
650
+ if len ( text ) > 1 && text [1 ] == '/' {
647
651
p .writeString (pos , trimRight (text ), true )
648
652
return
649
653
}
@@ -756,7 +760,7 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro
756
760
// to track whether we're inside an expression or statement and
757
761
// use that information to decide more directly.
758
762
needsLinebreak := false
759
- if p .mode & noExtraBlank == 0 &&
763
+ if p .mode & noExtraBlank == 0 && len ( last . Text ) > 1 &&
760
764
last .Text [1 ] == '*' {
761
765
if line := p .lineFor (last .Pos ()); (line == 0 || line == next .Line ) &&
762
766
tok != token .COMMA &&
@@ -771,7 +775,7 @@ func (p *printer) intersperseComments(next token.Position, tok token.Token) (wro
771
775
}
772
776
// Ensure that there is a line break after a //-style comment,
773
777
// before EOF, and before a closing '}' unless explicitly disabled.
774
- if last .Text [1 ] == '/' ||
778
+ if len ( last . Text ) > 1 && last .Text [1 ] == '/' ||
775
779
tok == token .EOF ||
776
780
tok == token .RBRACE && p .mode & noExtraLinebreak == 0 {
777
781
needsLinebreak = true
0 commit comments