fix the bug of convert eList to segment list

This commit is contained in:
lionsoul2014 2026-04-23 17:33:23 +08:00
parent a603a75ab7
commit 69d267d85d
1 changed files with 44 additions and 8 deletions

View File

@ -238,7 +238,7 @@ func (e *Editor) PutSegment(seg *Segment, cb func(newSeg *Segment, oldList []*Se
// print for debug // print for debug
// for i, s := range eList { // for i, s := range eList {
// fmt.Printf("ele %d: %s\n", i, s.Value.(*Segment)) // fmt.Printf("find -> %d: %s\n", i, s.Value.(*Segment))
// } // }
// segment split // segment split
@ -256,13 +256,8 @@ func (e *Editor) PutSegment(seg *Segment, cb func(newSeg *Segment, oldList []*Se
if cb == nil { if cb == nil {
sList = append(sList, seg) sList = append(sList, seg)
} else { } else {
var tsList []*Segment
for _, ele := range eList {
tsList = append(tsList, ele.Value.(*Segment))
}
// call the callback and append the segments // call the callback and append the segments
sList = append(sList, cb(seg, tsList)...) sList = append(sList, cb(seg, e._getSegments(seg, eList))...)
} }
// check and do the tailing segment append // check and do the tailing segment append
@ -281,7 +276,7 @@ func (e *Editor) PutSegment(seg *Segment, cb func(newSeg *Segment, oldList []*Se
// print for debug // print for debug
// for i, s := range sList { // for i, s := range sList {
// fmt.Printf("%d: %s\n", i, s) // fmt.Printf("replace -> %d: %s\n", i, s)
// } // }
// delete all the in-range segments and // delete all the in-range segments and
@ -309,6 +304,47 @@ func (e *Editor) PutSegment(seg *Segment, cb func(newSeg *Segment, oldList []*Se
return oldRows, newRows, nil return oldRows, newRows, nil
} }
func (e *Editor) _getSegments(seg *Segment, eList []*list.Element) []*Segment {
var sList []*Segment
var eLen = len(eList)
switch eLen {
case 0:
return sList
case 1:
fSeg := eList[0].Value.(*Segment)
return append(sList, &Segment{
StartIP: seg.StartIP,
EndIP: seg.EndIP,
Region: fSeg.Region,
})
}
var maxIdx = eLen - 1
// append the first segment
fSeg := eList[0].Value.(*Segment)
sList = append(sList, &Segment{
StartIP: seg.StartIP,
EndIP: fSeg.EndIP,
Region: fSeg.Region,
})
// append the middle segments
for i := 1; i < maxIdx; i++ {
sList = append(sList, eList[i].Value.(*Segment))
}
// append the last segment
lSeg := eList[maxIdx].Value.(*Segment)
sList = append(sList, &Segment{
StartIP: lSeg.StartIP,
EndIP: seg.EndIP,
Region: lSeg.Region,
})
return sList
}
func (e *Editor) PutFile(src string, cb func(newSeg *Segment, oldList []*Segment) []*Segment) (int, int, error) { func (e *Editor) PutFile(src string, cb func(newSeg *Segment, oldList []*Segment) []*Segment) (int, int, error) {
handle, err := os.OpenFile(src, os.O_RDONLY, 0600) handle, err := os.OpenFile(src, os.O_RDONLY, 0600)
if err != nil { if err != nil {