github.com/haraldrudell/parl@v0.4.176/pruntime/append-location.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pruntime
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  // AppendLocation inserts [CodeLocation.Short] at the end of s, prior to a possible newline
    14  func AppendLocation(s string, location *CodeLocation) string {
    15  	// insert code location before a possible ending newline
    16  	sNewline := ""
    17  	if strings.HasSuffix(s, "\n") {
    18  		s = s[:len(s)-1]
    19  		sNewline = "\n"
    20  	}
    21  	return fmt.Sprintf("%s %s%s", s, location.Short(), sNewline)
    22  }