@@ -77,9 +77,9 @@ func checkCommentForPeriod(c comment) *Issue {
77
77
// Make a replacement. Use `pos.line` to get an original line from
78
78
// attached lines. Use `iss.Pos.Column` because it's a position in
79
79
// the original line.
80
- original := [] rune ( c .lines [pos .line - 1 ])
81
- iss .Replacement = string ( original [:iss .Pos .Column - 1 ]) + "." +
82
- string ( original [iss .Pos .Column - 1 :])
80
+ original := c .lines [pos .line - 1 ]
81
+ iss .Replacement = original [:iss .Pos .Column - 1 ] + "." +
82
+ original [iss .Pos .Column - 1 :]
83
83
84
84
// Save replacement to raw lines to be able to combine it with
85
85
// further replacements
@@ -118,9 +118,11 @@ func checkCommentForCapital(c comment) []Issue {
118
118
// Make a replacement. Use `pos.line` to get an original line from
119
119
// attached lines. Use `iss.Pos.Column` because it's a position in
120
120
// the original line.
121
- rep := []rune (c .lines [pos .line - 1 ])
122
- rep [iss .Pos .Column - 1 ] = unicode .ToTitle (rep [iss .Pos .Column - 1 ])
123
- iss .Replacement = string (rep )
121
+ line := c .lines [pos .line - 1 ]
122
+ col := byteToRuneColumn (line , iss .Pos .Column ) - 1
123
+ rep := string (unicode .ToTitle ([]rune (line )[col ])) // capital letter
124
+ iss .Replacement = line [:iss .Pos .Column - 1 ] + rep +
125
+ line [iss .Pos .Column - 1 + len (rep ):]
124
126
125
127
// Save replacement to raw lines to be able to combine it with
126
128
// further replacements
@@ -158,7 +160,7 @@ func checkPeriod(comment string) (pos position, ok bool) {
158
160
return position {}, true
159
161
}
160
162
161
- pos .column = len ([] rune ( line ) ) + 1
163
+ pos .column = len (line ) + 1
162
164
return pos , false
163
165
}
164
166
@@ -209,7 +211,10 @@ func checkCapital(comment string, skipFirst bool) (pp []position) {
209
211
continue
210
212
}
211
213
if state == endOfSentence && unicode .IsLower (r ) {
212
- pp = append (pp , position {line : pos .line , column : pos .column })
214
+ pp = append (pp , position {
215
+ line : pos .line ,
216
+ column : runeToByteColumn (comment , pos .column ),
217
+ })
213
218
}
214
219
state = empty
215
220
}
@@ -267,3 +272,22 @@ func hasSuffix(s string, suffixes []string) bool {
267
272
}
268
273
return false
269
274
}
275
+
276
+ // The following two functions convert byte and rune indexes.
277
+ //
278
+ // Example:
279
+ // text: a b c Ш e f
280
+ // runes: 1 2 3 4 5 6
281
+ // bytes: 0 1 2 3 5 6
282
+ // The reason of the difference is that the size of "Ш" is 2 bytes.
283
+ // NOTE: Works only for 1-based indexes (line columns).
284
+
285
+ // byteToRuneColumn converts byte index inside the string to rune index.
286
+ func byteToRuneColumn (s string , i int ) int {
287
+ return len ([]rune (s [:i - 1 ])) + 1
288
+ }
289
+
290
+ // runeToByteColumn converts rune index inside the string to byte index.
291
+ func runeToByteColumn (s string , i int ) int {
292
+ return len (string ([]rune (s )[:i - 1 ])) + 1
293
+ }
0 commit comments