package textimport ()var space = []byte(" ")// A Segment struct holds information about source positions.typeSegmentstruct {// Start is a start position of the segment. Start int// Stop is a stop position of the segment. // This value should be excluded. Stop int// Padding is a padding length of the segment. Padding int// ForceNewline is true if the segment should be ended with a newline. // Some elements(i.e. CodeBlock, FencedCodeBlock) does not trim trailing // newlines. Spec defines that EOF is treated as a newline, so we need to // add a newline to the end of the segment if it is not empty. // // i.e.: // // ```go // const test = "test" // // This code does not close the code block and ends with EOF. In this case, // we need to add a newline to the end of the last line like `const test = "test"\n`. ForceNewline bool}// NewSegment return a new Segment.func (, int) Segment {returnSegment{Start: ,Stop: ,Padding: 0, }}// NewSegmentPadding returns a new Segment with the given padding.func (, , int) Segment {returnSegment{Start: ,Stop: ,Padding: , }}// Value returns a value of the segment.func ( *Segment) ( []byte) []byte {var []byteif .Padding == 0 { = [.Start:.Stop] } else { = make([]byte, 0, .Padding+.Stop-.Start+1) = append(, bytes.Repeat(space, .Padding)...) = append(, [.Start:.Stop]...) }if .ForceNewline && len() > 0 && [len()-1] != '\n' { = append(, '\n') }return}// Len returns a length of the segment.func ( *Segment) () int {return .Stop - .Start + .Padding}// Between returns a segment between this segment and the given segment.func ( *Segment) ( Segment) Segment {if .Stop != .Stop {panic("invalid state") }returnNewSegmentPadding( .Start, .Start, .Padding-.Padding, )}// IsEmpty returns true if this segment is empty, otherwise false.func ( *Segment) () bool {return .Start >= .Stop && .Padding == 0}// TrimRightSpace returns a new segment by slicing off all trailing// space characters.func ( *Segment) ( []byte) Segment { := [.Start:.Stop] := util.TrimRightSpaceLength()if == len() {returnNewSegment(.Start, .Start) }returnNewSegmentPadding(.Start, .Stop-, .Padding)}// TrimLeftSpace returns a new segment by slicing off all leading// space characters including padding.func ( *Segment) ( []byte) Segment { := [.Start:.Stop] := util.TrimLeftSpaceLength()returnNewSegment(.Start+, .Stop)}// TrimLeftSpaceWidth returns a new segment by slicing off leading space// characters until the given width.func ( *Segment) ( int, []byte) Segment { := .Paddingfor ; > 0; -- {if == 0 {break } -- }if == 0 {returnNewSegmentPadding(.Start, .Stop, ) } := [.Start:.Stop] := .Startfor , := range {if >= .Stop-1 || <= 0 {break }if == ' ' { -- } elseif == '\t' { -= 4 } else {break } ++ }if < 0 { = * -1 }returnNewSegmentPadding(, .Stop, )}// WithStart returns a new Segment with same value except Start.func ( *Segment) ( int) Segment {returnNewSegmentPadding(, .Stop, .Padding)}// WithStop returns a new Segment with same value except Stop.func ( *Segment) ( int) Segment {returnNewSegmentPadding(.Start, , .Padding)}// ConcatPadding concats the padding to the given slice.func ( *Segment) ( []byte) []byte {if .Padding > 0 {returnappend(, bytes.Repeat(space, .Padding)...) }return}// Segments is a collection of the Segment.typeSegmentsstruct { values []Segment}// NewSegments return a new Segments.func () *Segments {return &Segments{values: nil, }}// Append appends the given segment after the tail of the collection.func ( *Segments) ( Segment) { .values = append(.values, )}// AppendAll appends all elements of given segments after the tail of the collection.func ( *Segments) ( []Segment) { .values = append(.values, ...)}// Len returns the length of the collection.func ( *Segments) () int {if .values == nil {return0 }returnlen(.values)}// At returns a segment at the given index.func ( *Segments) ( int) Segment {return .values[]}// Set sets the given Segment.func ( *Segments) ( int, Segment) { .values[] = }// SetSliced replace the collection with a subsliced value.func ( *Segments) (, int) { .values = .values[:]}// Sliced returns a subslice of the collection.func ( *Segments) (, int) []Segment {return .values[:]}// Clear delete all element of the collection.func ( *Segments) () { .values = nil}// Unshift insert the given Segment to head of the collection.func ( *Segments) ( Segment) { .values = append(.values[0:1], .values[0:]...) .values[0] = }// Value returns a string value of the collection.func ( *Segments) ( []byte) []byte {var []bytefor , := range .values { = append(, .Value()...) }return}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.