MCPcopy
hub / github.com/micro-editor/micro / StringWidth

Function StringWidth

internal/util/util.go:215–240  ·  view source on GitHub ↗

StringWidth returns the visual width of a byte array indexed from 0 to n (rune index) with a given tabsize

(b []byte, n, tabsize int)

Source from the content-addressed store, hash-verified

213// StringWidth returns the visual width of a byte array indexed from 0 to n (rune index)
214// with a given tabsize
215func StringWidth(b []byte, n, tabsize int) int {
216 if n <= 0 {
217 return 0
218 }
219 i := 0
220 width := 0
221 for len(b) > 0 {
222 r, _, size := DecodeCharacter(b)
223 b = b[size:]
224
225 switch r {
226 case '\t':
227 ts := tabsize - (width % tabsize)
228 width += ts
229 default:
230 width += runewidth.RuneWidth(r)
231 }
232
233 i++
234
235 if i == n {
236 return width
237 }
238 }
239 return width
240}
241
242// Min takes the min of two ints
243func Min(a, b int) int {

Callers 4

DisplayMethod · 0.92
VLocFromLocMethod · 0.92
GetVisualXMethod · 0.92
TestStringWidthFunction · 0.85

Calls 1

DecodeCharacterFunction · 0.70

Tested by 1

TestStringWidthFunction · 0.68