github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/bytestrings/bytestrings.go (about)

     1  package bytestrings
     2  
     3  import (
     4  	"strings"
     5  	"unsafe"
     6  )
     7  
     8  // NextNonEmptyLine returns the next non-empty line and the remaining text.
     9  func NextNonEmptyLine[T ~[]byte | ~string](text T) (T, T) {
    10  	for {
    11  		lfIndex := strings.IndexByte(*(*string)(unsafe.Pointer(&text)), '\n')
    12  		if lfIndex == -1 {
    13  			return text, text[len(text):]
    14  		}
    15  		line := text[:lfIndex]
    16  		text = text[lfIndex+1:]
    17  		if lfIndex == 0 {
    18  			continue
    19  		}
    20  		if line[len(line)-1] == '\r' {
    21  			line = line[:len(line)-1]
    22  		}
    23  		if len(line) == 0 {
    24  			continue
    25  		}
    26  		return line, text
    27  	}
    28  }