github.com/jmigpin/editor@v1.6.0/util/parseutil/reslocparser/resloc.go (about)

     1  package reslocparser
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"github.com/jmigpin/editor/util/parseutil"
     9  )
    10  
    11  type ResLoc struct {
    12  	Path   string // raw path
    13  	Line   int    // 0 is nil
    14  	Column int    // 0 is nil
    15  	Offset int    // <=-1 is nil // TODO: file:#123?
    16  
    17  	PathSep rune
    18  	Escape  rune
    19  
    20  	Scheme string // ex: "file://", useful to know when to translate to another path separator
    21  	Volume string
    22  
    23  	Pos, End int // contains reverse expansion
    24  }
    25  
    26  func (rl *ResLoc) ClearFilename1() string {
    27  	s := rl.Path
    28  
    29  	// commented: don't remove escapes to allow stringify to destinguish between other params (in case ":" was escaped)
    30  	//s = parseutil.RemoveEscapes(s, rl.escape)
    31  
    32  	s = parseutil.CleanMultiplePathSeps(s, rl.PathSep)
    33  
    34  	if rl.Scheme == "file://" {
    35  		// bypass first slash
    36  		if rl.Volume != "" {
    37  			rs := []rune(s)
    38  			if rs[0] == '/' {
    39  				s = string(rs[1:])
    40  			}
    41  		}
    42  		// replace slashes
    43  		sep := '/'
    44  		if rl.PathSep != sep {
    45  			s = strings.Replace(s, string(sep), string(rl.PathSep), -1)
    46  		}
    47  	}
    48  
    49  	//if rl.separator2 != 0 {
    50  	//}
    51  	//u, err := strconv.Unquote("\"" + s + "\"")
    52  	//if err == nil {
    53  	//	s = u
    54  	//}
    55  
    56  	if u, err := url.QueryUnescape(s); err == nil {
    57  		s = u
    58  	}
    59  
    60  	return s
    61  }
    62  
    63  func (rl *ResLoc) Stringify1() string {
    64  	s := rl.ClearFilename1()
    65  	if rl.Line > 0 {
    66  		s += fmt.Sprintf(":%d", rl.Line)
    67  	}
    68  	if rl.Column > 0 {
    69  		s += fmt.Sprintf(":%d", rl.Column)
    70  	}
    71  	return s
    72  }