github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/encoding/json/shims/shims.go (about)

     1  // This package provides shims over Go 1.2{2,3} APIs
     2  // which are missing from Go 1.22, and used by the Go 1.24 encoding/json package.
     3  //
     4  // Inside the vendored package, all shim code has comments that begin look like
     5  // // SHIM(...): ...
     6  package shims
     7  
     8  import (
     9  	"encoding/base64"
    10  	"reflect"
    11  	"slices"
    12  )
    13  
    14  const EscapeHTMLByDefault = true
    15  
    16  type OverflowableType struct{ reflect.Type }
    17  
    18  func (t OverflowableType) OverflowInt(x int64) bool {
    19  	k := t.Kind()
    20  	switch k {
    21  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    22  		bitSize := t.Size() * 8
    23  		trunc := (x << (64 - bitSize)) >> (64 - bitSize)
    24  		return x != trunc
    25  	}
    26  	panic("reflect: OverflowInt of non-int type " + t.String())
    27  }
    28  
    29  func (t OverflowableType) OverflowUint(x uint64) bool {
    30  	k := t.Kind()
    31  	switch k {
    32  	case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    33  		bitSize := t.Size() * 8
    34  		trunc := (x << (64 - bitSize)) >> (64 - bitSize)
    35  		return x != trunc
    36  	}
    37  	panic("reflect: OverflowUint of non-uint type " + t.String())
    38  }
    39  
    40  // Original src code from Go 1.23 go/src/reflect/type.go (taken 1/9/25)
    41  /*
    42  
    43  func (t *rtype) OverflowInt(x int64) bool {
    44  	k := t.Kind()
    45  	switch k {
    46  	case Int, Int8, Int16, Int32, Int64:
    47  		bitSize := t.Size() * 8
    48  		trunc := (x << (64 - bitSize)) >> (64 - bitSize)
    49  		return x != trunc
    50  	}
    51  	panic("reflect: OverflowInt of non-int type " + t.String())
    52  }
    53  
    54  func (t *rtype) OverflowUint(x uint64) bool {
    55  	k := t.Kind()
    56  	switch k {
    57  	case Uint, Uintptr, Uint8, Uint16, Uint32, Uint64:
    58  		bitSize := t.Size() * 8
    59  		trunc := (x << (64 - bitSize)) >> (64 - bitSize)
    60  		return x != trunc
    61  	}
    62  	panic("reflect: OverflowUint of non-uint type " + t.String())
    63  }
    64  
    65  */
    66  
    67  // TypeFor returns the [Type] that represents the type argument T.
    68  func TypeFor[T any]() reflect.Type {
    69  	var v T
    70  	if t := reflect.TypeOf(v); t != nil {
    71  		return t // optimize for T being a non-interface kind
    72  	}
    73  	return reflect.TypeOf((*T)(nil)).Elem() // only for an interface kind
    74  }
    75  
    76  // Original src code from Go 1.23 go/src/reflect/type.go (taken 1/9/25)
    77  /*
    78  
    79  // TypeFor returns the [Type] that represents the type argument T.
    80  func TypeFor[T any]() Type {
    81  	var v T
    82  	if t := TypeOf(v); t != nil {
    83  		return t // optimize for T being a non-interface kind
    84  	}
    85  	return TypeOf((*T)(nil)).Elem() // only for an interface kind
    86  }
    87  
    88  */
    89  
    90  type AppendableStdEncoding struct{ *base64.Encoding }
    91  
    92  // AppendEncode appends the base64 encoded src to dst
    93  // and returns the extended buffer.
    94  func (enc AppendableStdEncoding) AppendEncode(dst, src []byte) []byte {
    95  	n := enc.EncodedLen(len(src))
    96  	dst = slices.Grow(dst, n)
    97  	enc.Encode(dst[len(dst):][:n], src)
    98  	return dst[:len(dst)+n]
    99  }
   100  
   101  // Original src code from Go 1.23.4 go/src/encoding/base64/base64.go (taken 1/9/25)
   102  /*
   103  
   104  // AppendEncode appends the base64 encoded src to dst
   105  // and returns the extended buffer.
   106  func (enc *Encoding) AppendEncode(dst, src []byte) []byte {
   107  	n := enc.EncodedLen(len(src))
   108  	dst = slices.Grow(dst, n)
   109  	enc.Encode(dst[len(dst):][:n], src)
   110  	return dst[:len(dst)+n]
   111  }
   112  
   113  */