Path extracts a standard path or URL expression from the current list of tokens (starting at index 0), returning the path string and the number of tokens included in the path. Restricts processing to contiguous elements with no spaces! If it is not a path, returns nil string, 0
(idx0 bool)
| 24 | // Restricts processing to contiguous elements with no spaces! |
| 25 | // If it is not a path, returns nil string, 0 |
| 26 | func (tk Tokens) Path(idx0 bool) (string, int) { |
| 27 | n := len(tk) |
| 28 | if n == 0 { |
| 29 | return "", 0 |
| 30 | } |
| 31 | t0 := tk[0] |
| 32 | ispath := (t0.IsPathDelim() || t0.Tok == token.TILDE) |
| 33 | if n == 1 { |
| 34 | if ispath { |
| 35 | return t0.String(), 1 |
| 36 | } |
| 37 | return "", 0 |
| 38 | } |
| 39 | str := tk[0].String() |
| 40 | lastEnd := int(tk[0].Pos) + len(str) |
| 41 | ci := 1 |
| 42 | if !ispath { |
| 43 | lastEnd = int(tk[0].Pos) |
| 44 | ci = 0 |
| 45 | if t0.Tok != token.IDENT { |
| 46 | return "", 0 |
| 47 | } |
| 48 | tin := 1 |
| 49 | tid := t0.Str |
| 50 | tindelim := tk[tin].IsPathDelim() |
| 51 | if idx0 { |
| 52 | tindelim = tk[tin].Tok == token.QUO |
| 53 | } |
| 54 | if (int(tk[tin].Pos) > lastEnd+len(tid)) || !(tk[tin].Tok == token.COLON || tindelim) { |
| 55 | return "", 0 |
| 56 | } |
| 57 | ci += tin + 1 |
| 58 | str = tid + tk[tin].String() |
| 59 | lastEnd += len(str) |
| 60 | } |
| 61 | prevWasDelim := true |
| 62 | for { |
| 63 | if ci >= n || int(tk[ci].Pos) > lastEnd { |
| 64 | return str, ci |
| 65 | } |
| 66 | ct := tk[ci] |
| 67 | if ct.IsPathDelim() || ct.IsPathExtraDelim() { |
| 68 | prevWasDelim = true |
| 69 | str += ct.String() |
| 70 | lastEnd += len(ct.String()) |
| 71 | ci++ |
| 72 | continue |
| 73 | } |
| 74 | if ct.Tok == token.STRING { |
| 75 | prevWasDelim = true |
| 76 | str += EscapeQuotes(ct.String()) |
| 77 | lastEnd += len(ct.String()) |
| 78 | ci++ |
| 79 | continue |
| 80 | } |
| 81 | if !prevWasDelim { |
| 82 | if ct.Tok == token.ILLEGAL && ct.Str == `\` && ci+1 < n && int(tk[ci+1].Pos) == lastEnd+2 { |
| 83 | prevWasDelim = true |