MCPcopy Index your code
hub / github.com/segmentio/encoding / decodeDuration

Method decodeDuration

json/decode.go:438–479  ·  view source on GitHub ↗
(b []byte, p unsafe.Pointer)

Source from the content-addressed store, hash-verified

436}
437
438func (d decoder) decodeDuration(b []byte, p unsafe.Pointer) ([]byte, error) {
439 if hasNullPrefix(b) {
440 return b[4:], nil
441 }
442
443 // in order to inter-operate with the stdlib, we must be able to interpret
444 // durations passed as integer values. there's some discussion about being
445 // flexible on how durations are formatted, but for the time being, it's
446 // been punted to go2 at the earliest: https://github.com/golang/go/issues/4712
447 if len(b) > 0 && b[0] != '"' {
448 v, r, err := d.parseInt(b, durationType)
449 if err != nil {
450 return d.inputError(b, int32Type)
451 }
452
453 if v < math.MinInt64 || v > math.MaxInt64 {
454 return r, unmarshalOverflow(b[:len(b)-len(r)], int32Type)
455 }
456
457 *(*time.Duration)(p) = time.Duration(v)
458 return r, nil
459 }
460
461 if len(b) < 2 || b[0] != '"' {
462 return d.inputError(b, durationType)
463 }
464
465 i := bytes.IndexByte(b[1:], '"') + 1
466 if i <= 0 {
467 return d.inputError(b, durationType)
468 }
469
470 s := b[1:i] // trim quotes
471
472 v, err := time.ParseDuration(*(*string)(unsafe.Pointer(&s)))
473 if err != nil {
474 return d.inputError(b, durationType)
475 }
476
477 *(*time.Duration)(p) = v
478 return b[i+1:], nil
479}
480
481func (d decoder) decodeTime(b []byte, p unsafe.Pointer) ([]byte, error) {
482 if hasNullPrefix(b) {

Callers

nothing calls this directly

Calls 4

parseIntMethod · 0.95
inputErrorMethod · 0.95
hasNullPrefixFunction · 0.85
unmarshalOverflowFunction · 0.85

Tested by

no test coverage detected