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

Method Substr

internal/buffer/line_array.go:289–307  ·  view source on GitHub ↗

Substr returns the string representation between two locations

(start, end Loc)

Source from the content-addressed store, hash-verified

287
288// Substr returns the string representation between two locations
289func (la *LineArray) Substr(start, end Loc) []byte {
290 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
291 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
292 if start.Y == end.Y {
293 src := la.lines[start.Y].data[startX:endX]
294 dest := make([]byte, len(src))
295 copy(dest, src)
296 return dest
297 }
298 str := make([]byte, 0, len(la.lines[start.Y+1].data)*(end.Y-start.Y))
299 str = append(str, la.lines[start.Y].data[startX:]...)
300 str = append(str, '\n')
301 for i := start.Y + 1; i <= end.Y-1; i++ {
302 str = append(str, la.lines[i].data...)
303 str = append(str, '\n')
304 }
305 str = append(str, la.lines[end.Y].data[:endX]...)
306 return str
307}
308
309// LinesNum returns the number of lines in the buffer
310func (la *LineArray) LinesNum() int {

Callers 7

removeMethod · 0.95
ReplaceRegexMethod · 0.80
TestSplitFunction · 0.80
TestJoinFunction · 0.80
TestInsertFunction · 0.80
WordAtMethod · 0.80
GetSelectionMethod · 0.80

Calls 1

runeToByteIndexFunction · 0.85

Tested by 3

TestSplitFunction · 0.64
TestJoinFunction · 0.64
TestInsertFunction · 0.64