cuelang.org/go@v0.13.0/encoding/jsonschema/pointer.go (about) 1 package jsonschema 2 3 import "strings" 4 5 // TODO this file contains functionality that mimics the JSON Pointer functionality 6 // in https://pkg.go.dev/github.com/go-json-experiment/json/jsontext#Pointer; 7 // perhaps use it when it moves into the stdlib as json/v2. 8 9 var ( 10 jsonPtrEsc = strings.NewReplacer("~", "~0", "/", "~1") 11 jsonPtrUnesc = strings.NewReplacer("~0", "~", "~1", "/") 12 ) 13 14 // TODO(go1.23) func jsonPointerFromTokens(tokens iter.Seq[string]) string 15 func jsonPointerFromTokens(tokens func(func(string) bool)) string { 16 var buf strings.Builder 17 // TODO for tok := range tokens { 18 tokens(func(tok string) bool { 19 buf.WriteByte('/') 20 buf.WriteString(jsonPtrEsc.Replace(tok)) 21 return true 22 }) 23 return buf.String() 24 } 25 26 // TODO(go1.23) func jsonPointerTokens(p string) iter.Seq[string] 27 func jsonPointerTokens(p string) func(func(string) bool) { 28 return func(yield func(string) bool) { 29 needUnesc := strings.IndexByte(p, '~') >= 0 30 for len(p) > 0 { 31 p = strings.TrimPrefix(p, "/") 32 i := min(uint(strings.IndexByte(p, '/')), uint(len(p))) 33 var ok bool 34 if needUnesc { 35 ok = yield(jsonPtrUnesc.Replace(p[:i])) 36 } else { 37 ok = yield(p[:i]) 38 } 39 if !ok { 40 return 41 } 42 p = p[i:] 43 } 44 } 45 }