github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/internal/util/shorten.go (about)

     1  package util
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/hirochachacha/plua/internal/version"
     7  )
     8  
     9  func Shorten(s string) string {
    10  	if len(s) == 0 {
    11  		return ""
    12  	}
    13  
    14  	switch s[0] {
    15  	case '=':
    16  		s = s[1:]
    17  		if len(s) >= version.LUA_IDSIZE {
    18  			return s[:version.LUA_IDSIZE-1]
    19  		}
    20  		return s
    21  	case '@':
    22  		s = s[1:]
    23  		if len(s) >= version.LUA_IDSIZE {
    24  			return "..." + s[len(s)-version.LUA_IDSIZE+4:]
    25  		}
    26  		return s
    27  	default:
    28  		i := strings.IndexRune(s, '\n')
    29  		if i == -1 {
    30  			s = "[string \"" + s
    31  
    32  			if len(s) >= version.LUA_IDSIZE-3 {
    33  				return s[:version.LUA_IDSIZE-6] + "...\"]"
    34  			}
    35  			return s + "\"]"
    36  		}
    37  
    38  		s = "[string \"" + s[:i]
    39  
    40  		if len(s) >= version.LUA_IDSIZE-3 {
    41  			return s[:version.LUA_IDSIZE-6] + "...\"]"
    42  		}
    43  
    44  		return s + "...\"]"
    45  	}
    46  }