@@ -18,32 +18,48 @@ import (
1818 "strconv"
1919)
2020
21+ // Decode decodes a JSON Pointer token by replacing each encoded slash ("~1")
22+ // with '/' (%x2F) and each encoded tilde ("~0") with '~' (%x7E).
2123func Decode (token string ) string {
2224 return decoder .Replace (token )
2325}
2426
27+ // Encode encodes a string to a token of a JSON Pointer by replacing each '~'
28+ // (%x7E) with "~0" and '/' (%x2F) with "~1".
2529func Encode (token string ) string {
2630 return encoder .Replace (token )
2731}
2832
33+ // Token is a segment of a JSON Pointer, divided by '/' (%x2F).
2934type Token string
3035
36+ // Bytes returns the decoded Bytes of t
3137func (t Token ) Bytes () []byte {
32- return []byte (t )
38+ return []byte (t . String () )
3339}
3440
41+ // String returns the decoded value of t
3542func (t Token ) String () string {
3643 return decoder .Replace (string (t ))
3744}
3845
46+ // Int64 attempts to parse t as an int64. If t can be parsed as an int64 then
47+ // the value is returned. If t can not be parsed as an int64 then an error is
48+ // returned.
3949func (t Token ) Int64 () (int64 , error ) {
4050 return strconv .ParseInt (t .String (), 10 , 64 )
4151}
4252
53+ // Uint64 attempts to parse t as an uint64. If t can be parsed as an uint64 then
54+ // the value is returned. If t can not be parsed as an uint64 then an error is
55+ // returned.
4356func (t Token ) Uint64 () (uint64 , error ) {
4457 return strconv .ParseUint (t .String (), 10 , 64 )
4558}
4659
60+ // Int attempts to parse t as an int. If t can be parsed as an int then
61+ // the value is returned. If t can not be parsed as an int then an error is
62+ // returned.
4763func (t Token ) Int () (int , error ) {
4864 return strconv .Atoi (t .String ())
4965}
@@ -52,27 +68,6 @@ func (t Token) ptr() JSONPointer {
5268 return JSONPointer (t )
5369}
5470
55- // Tokens is a slice of Tokens.
56- type Tokens []Token
57-
58- // Strings returns ts as a slice of strings
59- func (ts Tokens ) Strings () []string {
60- s := make ([]string , len (ts ))
61- for i , t := range ts {
62- s [i ] = t .String ()
63- }
64- return s
65- }
66-
67- // Stringers returns ts as a slice of fmt.Stringers
68- func (ts Tokens ) Stringers () []fmt.Stringer {
69- s := make ([]fmt.Stringer , len (ts ))
70- for i , t := range ts {
71- s [i ] = t
72- }
73- return s
74- }
75-
7671// Index parses t for an index value. If t can be parsed as an int, is equal to
7772// or greater than 0 and less than or equal to next then the value is returned.
7873// If t is equal to "-" then next is returned. If neither condition is true, -1
@@ -103,3 +98,24 @@ func (t Token) Index(next int) (int, error) {
10398 }
10499 return i , nil
105100}
101+
102+ // Tokens is a slice of Tokens.
103+ type Tokens []Token
104+
105+ // Strings returns ts as a slice of strings
106+ func (ts Tokens ) Strings () []string {
107+ s := make ([]string , len (ts ))
108+ for i , t := range ts {
109+ s [i ] = t .String ()
110+ }
111+ return s
112+ }
113+
114+ // Stringers returns ts as a slice of fmt.Stringers
115+ func (ts Tokens ) Stringers () []fmt.Stringer {
116+ s := make ([]fmt.Stringer , len (ts ))
117+ for i , t := range ts {
118+ s [i ] = t
119+ }
120+ return s
121+ }
0 commit comments