ParseBytes is like Parse, but parses from a byte slice.
(s []byte)
| 249 | |
| 250 | // ParseBytes is like Parse, but parses from a byte slice. |
| 251 | func ParseBytes(s []byte) (ref Ref, ok bool) { |
| 252 | i := bytes.IndexByte(s, '-') |
| 253 | if i < 0 { |
| 254 | return |
| 255 | } |
| 256 | name := s[:i] // e.g. "sha1", "sha224" |
| 257 | hex := s[i+1:] |
| 258 | meta, ok := metaFromBytes(name) |
| 259 | if !ok { |
| 260 | return parseUnknown(string(name), string(hex)) |
| 261 | } |
| 262 | if len(hex) != meta.size*2 { |
| 263 | ok = false |
| 264 | return |
| 265 | } |
| 266 | dt, ok := meta.ctorb(hex) |
| 267 | if !ok { |
| 268 | return |
| 269 | } |
| 270 | return Ref{dt}, true |
| 271 | } |
| 272 | |
| 273 | // ParseOrZero parses as a blobref. If s is invalid, a zero Ref is |
| 274 | // returned which can be tested with the Valid method. |