github.com/haraldrudell/parl@v0.4.176/pbytes/trim-newline.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  // Package pbytes provides byte-slice related functions TrimNewline.
     7  package pbytes
     8  
     9  import "bytes"
    10  
    11  // newlinc-character variants
    12  var pbNewlines = [][]byte{
    13  	[]byte("\r\n"), // Windows
    14  	[]byte("\n"),   // Unix-like and macOS
    15  	[]byte("\r"),   // legacy macOS
    16  }
    17  
    18  // TrimNewline trims Unix-like obsolete macOS and Windows newlines
    19  func TrimNewline(in []byte) (out []byte) {
    20  	for _, nl := range pbNewlines {
    21  		if out = bytes.TrimSuffix(in, nl); len(in) != len(out) {
    22  			return
    23  		}
    24  	}
    25  	out = in
    26  
    27  	return
    28  }