Source file src/encoding/json/decode.go

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Represents JSON data structure using native Go types: booleans, floats,
     6  // strings, arrays, and maps.
     7  
     8  //go:build !goexperiment.jsonv2
     9  
    10  package json
    11  
    12  import (
    13  	"encoding"
    14  	"encoding/base64"
    15  	"fmt"
    16  	"reflect"
    17  	"strconv"
    18  	"strings"
    19  	"unicode"
    20  	"unicode/utf16"
    21  	"unicode/utf8"
    22  	_ "unsafe" // for linkname
    23  )
    24  
    25  // Unmarshal parses the JSON-encoded data and stores the result
    26  // in the value pointed to by v. If v is nil or not a pointer,
    27  // Unmarshal returns an [InvalidUnmarshalError].
    28  //
    29  // Unmarshal uses the inverse of the encodings that
    30  // [Marshal] uses, allocating maps, slices, and pointers as necessary,
    31  // with the following additional rules:
    32  //
    33  // To unmarshal JSON into a pointer, Unmarshal first handles the case of
    34  // the JSON being the JSON literal null. In that case, Unmarshal sets
    35  // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into
    36  // the value pointed at by the pointer. If the pointer is nil, Unmarshal
    37  // allocates a new value for it to point to.
    38  //
    39  // To unmarshal JSON into a value implementing [Unmarshaler],
    40  // Unmarshal calls that value's [Unmarshaler.UnmarshalJSON] method, including
    41  // when the input is a JSON null.
    42  // Otherwise, if the value implements [encoding.TextUnmarshaler]
    43  // and the input is a JSON quoted string, Unmarshal calls
    44  // [encoding.TextUnmarshaler.UnmarshalText] with the unquoted form of the string.
    45  //
    46  // To unmarshal JSON into a struct, Unmarshal matches incoming object
    47  // keys to the keys used by [Marshal] (either the struct field name or its tag),
    48  // preferring an exact match but also accepting a case-insensitive match. By
    49  // default, object keys which don't have a corresponding struct field are
    50  // ignored (see [Decoder.DisallowUnknownFields] for an alternative).
    51  //
    52  // To unmarshal JSON into an interface value,
    53  // Unmarshal stores one of these in the interface value:
    54  //
    55  //   - bool, for JSON booleans
    56  //   - float64, for JSON numbers
    57  //   - string, for JSON strings
    58  //   - []any, for JSON arrays
    59  //   - map[string]any, for JSON objects
    60  //   - nil for JSON null
    61  //
    62  // To unmarshal a JSON array into a slice, Unmarshal resets the slice length
    63  // to zero and then appends each element to the slice.
    64  // As a special case, to unmarshal an empty JSON array into a slice,
    65  // Unmarshal replaces the slice with a new empty slice.
    66  //
    67  // To unmarshal a JSON array into a Go array, Unmarshal decodes
    68  // JSON array elements into corresponding Go array elements.
    69  // If the Go array is smaller than the JSON array,
    70  // the additional JSON array elements are discarded.
    71  // If the JSON array is smaller than the Go array,
    72  // the additional Go array elements are set to zero values.
    73  //
    74  // To unmarshal a JSON object into a map, Unmarshal first establishes a map to
    75  // use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal
    76  // reuses the existing map, keeping existing entries. Unmarshal then stores
    77  // key-value pairs from the JSON object into the map. The map's key type must
    78  // either be any string type, an integer, or implement [encoding.TextUnmarshaler].
    79  //
    80  // If the JSON-encoded data contain a syntax error, Unmarshal returns a [SyntaxError].
    81  //
    82  // If a JSON value is not appropriate for a given target type,
    83  // or if a JSON number overflows the target type, Unmarshal
    84  // skips that field and completes the unmarshaling as best it can.
    85  // If no more serious errors are encountered, Unmarshal returns
    86  // an [UnmarshalTypeError] describing the earliest such error. In any
    87  // case, it's not guaranteed that all the remaining fields following
    88  // the problematic one will be unmarshaled into the target object.
    89  //
    90  // The JSON null value unmarshals into an interface, map, pointer, or slice
    91  // by setting that Go value to nil. Because null is often used in JSON to mean
    92  // “not present,” unmarshaling a JSON null into any other Go type has no effect
    93  // on the value and produces no error.
    94  //
    95  // When unmarshaling quoted strings, invalid UTF-8 or
    96  // invalid UTF-16 surrogate pairs are not treated as an error.
    97  // Instead, they are replaced by the Unicode replacement
    98  // character U+FFFD.
    99  func Unmarshal(data []byte, v any) error {
   100  	// Check for well-formedness.
   101  	// Avoids filling out half a data structure
   102  	// before discovering a JSON syntax error.
   103  	var d decodeState
   104  	err := checkValid(data, &d.scan)
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	d.init(data)
   110  	return d.unmarshal(v)
   111  }
   112  
   113  // Unmarshaler is the interface implemented by types
   114  // that can unmarshal a JSON description of themselves.
   115  // The input can be assumed to be a valid encoding of
   116  // a JSON value. UnmarshalJSON must copy the JSON data
   117  // if it wishes to retain the data after returning.
   118  type Unmarshaler interface {
   119  	UnmarshalJSON([]byte) error
   120  }
   121  
   122  // An UnmarshalTypeError describes a JSON value that was
   123  // not appropriate for a value of a specific Go type.
   124  type UnmarshalTypeError struct {
   125  	Value  string       // description of JSON value - "bool", "array", "number -5"
   126  	Type   reflect.Type // type of Go value it could not be assigned to
   127  	Offset int64        // error occurred after reading Offset bytes
   128  	Struct string       // name of the struct type containing the field
   129  	Field  string       // the full path from root node to the field, include embedded struct
   130  }
   131  
   132  func (e *UnmarshalTypeError) Error() string {
   133  	if e.Struct != "" || e.Field != "" {
   134  		return "json: cannot unmarshal " + e.Value + " into Go struct field " + e.Struct + "." + e.Field + " of type " + e.Type.String()
   135  	}
   136  	return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
   137  }
   138  
   139  // An UnmarshalFieldError describes a JSON object key that
   140  // led to an unexported (and therefore unwritable) struct field.
   141  //
   142  // Deprecated: No longer used; kept for compatibility.
   143  type UnmarshalFieldError struct {
   144  	Key   string
   145  	Type  reflect.Type
   146  	Field reflect.StructField
   147  }
   148  
   149  func (e *UnmarshalFieldError) Error() string {
   150  	return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String()
   151  }
   152  
   153  // An InvalidUnmarshalError describes an invalid argument passed to [Unmarshal].
   154  // (The argument to [Unmarshal] must be a non-nil pointer.)
   155  type InvalidUnmarshalError struct {
   156  	Type reflect.Type
   157  }
   158  
   159  func (e *InvalidUnmarshalError) Error() string {
   160  	if e.Type == nil {
   161  		return "json: Unmarshal(nil)"
   162  	}
   163  
   164  	if e.Type.Kind() != reflect.Pointer {
   165  		return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
   166  	}
   167  	return "json: Unmarshal(nil " + e.Type.String() + ")"
   168  }
   169  
   170  func (d *decodeState) unmarshal(v any) error {
   171  	rv := reflect.ValueOf(v)
   172  	if rv.Kind() != reflect.Pointer || rv.IsNil() {
   173  		return &InvalidUnmarshalError{reflect.TypeOf(v)}
   174  	}
   175  
   176  	d.scan.reset()
   177  	d.scanWhile(scanSkipSpace)
   178  	// We decode rv not rv.Elem because the Unmarshaler interface
   179  	// test must be applied at the top level of the value.
   180  	err := d.value(rv)
   181  	if err != nil {
   182  		return d.addErrorContext(err)
   183  	}
   184  	return d.savedError
   185  }
   186  
   187  // A Number represents a JSON number literal.
   188  type Number string
   189  
   190  // String returns the literal text of the number.
   191  func (n Number) String() string { return string(n) }
   192  
   193  // Float64 returns the number as a float64.
   194  func (n Number) Float64() (float64, error) {
   195  	return strconv.ParseFloat(string(n), 64)
   196  }
   197  
   198  // Int64 returns the number as an int64.
   199  func (n Number) Int64() (int64, error) {
   200  	return strconv.ParseInt(string(n), 10, 64)
   201  }
   202  
   203  // An errorContext provides context for type errors during decoding.
   204  type errorContext struct {
   205  	Struct     reflect.Type
   206  	FieldStack []string
   207  }
   208  
   209  // decodeState represents the state while decoding a JSON value.
   210  type decodeState struct {
   211  	data                  []byte
   212  	off                   int // next read offset in data
   213  	opcode                int // last read result
   214  	scan                  scanner
   215  	errorContext          *errorContext
   216  	savedError            error
   217  	useNumber             bool
   218  	disallowUnknownFields bool
   219  }
   220  
   221  // readIndex returns the position of the last byte read.
   222  func (d *decodeState) readIndex() int {
   223  	return d.off - 1
   224  }
   225  
   226  // phasePanicMsg is used as a panic message when we end up with something that
   227  // shouldn't happen. It can indicate a bug in the JSON decoder, or that
   228  // something is editing the data slice while the decoder executes.
   229  const phasePanicMsg = "JSON decoder out of sync - data changing underfoot?"
   230  
   231  func (d *decodeState) init(data []byte) *decodeState {
   232  	d.data = data
   233  	d.off = 0
   234  	d.savedError = nil
   235  	if d.errorContext != nil {
   236  		d.errorContext.Struct = nil
   237  		// Reuse the allocated space for the FieldStack slice.
   238  		d.errorContext.FieldStack = d.errorContext.FieldStack[:0]
   239  	}
   240  	return d
   241  }
   242  
   243  // saveError saves the first err it is called with,
   244  // for reporting at the end of the unmarshal.
   245  func (d *decodeState) saveError(err error) {
   246  	if d.savedError == nil {
   247  		d.savedError = d.addErrorContext(err)
   248  	}
   249  }
   250  
   251  // addErrorContext returns a new error enhanced with information from d.errorContext
   252  func (d *decodeState) addErrorContext(err error) error {
   253  	if d.errorContext != nil && (d.errorContext.Struct != nil || len(d.errorContext.FieldStack) > 0) {
   254  		switch err := err.(type) {
   255  		case *UnmarshalTypeError:
   256  			err.Struct = d.errorContext.Struct.Name()
   257  			fieldStack := d.errorContext.FieldStack
   258  			if err.Field != "" {
   259  				fieldStack = append(fieldStack, err.Field)
   260  			}
   261  			err.Field = strings.Join(fieldStack, ".")
   262  		}
   263  	}
   264  	return err
   265  }
   266  
   267  // skip scans to the end of what was started.
   268  func (d *decodeState) skip() {
   269  	s, data, i := &d.scan, d.data, d.off
   270  	depth := len(s.parseState)
   271  	for {
   272  		op := s.step(s, data[i])
   273  		i++
   274  		if len(s.parseState) < depth {
   275  			d.off = i
   276  			d.opcode = op
   277  			return
   278  		}
   279  	}
   280  }
   281  
   282  // scanNext processes the byte at d.data[d.off].
   283  func (d *decodeState) scanNext() {
   284  	if d.off < len(d.data) {
   285  		d.opcode = d.scan.step(&d.scan, d.data[d.off])
   286  		d.off++
   287  	} else {
   288  		d.opcode = d.scan.eof()
   289  		d.off = len(d.data) + 1 // mark processed EOF with len+1
   290  	}
   291  }
   292  
   293  // scanWhile processes bytes in d.data[d.off:] until it
   294  // receives a scan code not equal to op.
   295  func (d *decodeState) scanWhile(op int) {
   296  	s, data, i := &d.scan, d.data, d.off
   297  	for i < len(data) {
   298  		newOp := s.step(s, data[i])
   299  		i++
   300  		if newOp != op {
   301  			d.opcode = newOp
   302  			d.off = i
   303  			return
   304  		}
   305  	}
   306  
   307  	d.off = len(data) + 1 // mark processed EOF with len+1
   308  	d.opcode = d.scan.eof()
   309  }
   310  
   311  // rescanLiteral is similar to scanWhile(scanContinue), but it specialises the
   312  // common case where we're decoding a literal. The decoder scans the input
   313  // twice, once for syntax errors and to check the length of the value, and the
   314  // second to perform the decoding.
   315  //
   316  // Only in the second step do we use decodeState to tokenize literals, so we
   317  // know there aren't any syntax errors. We can take advantage of that knowledge,
   318  // and scan a literal's bytes much more quickly.
   319  func (d *decodeState) rescanLiteral() {
   320  	data, i := d.data, d.off
   321  Switch:
   322  	switch data[i-1] {
   323  	case '"': // string
   324  		for ; i < len(data); i++ {
   325  			switch data[i] {
   326  			case '\\':
   327  				i++ // escaped char
   328  			case '"':
   329  				i++ // tokenize the closing quote too
   330  				break Switch
   331  			}
   332  		}
   333  	case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-': // number
   334  		for ; i < len(data); i++ {
   335  			switch data[i] {
   336  			case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   337  				'.', 'e', 'E', '+', '-':
   338  			default:
   339  				break Switch
   340  			}
   341  		}
   342  	case 't': // true
   343  		i += len("rue")
   344  	case 'f': // false
   345  		i += len("alse")
   346  	case 'n': // null
   347  		i += len("ull")
   348  	}
   349  	if i < len(data) {
   350  		d.opcode = stateEndValue(&d.scan, data[i])
   351  	} else {
   352  		d.opcode = scanEnd
   353  	}
   354  	d.off = i + 1
   355  }
   356  
   357  // value consumes a JSON value from d.data[d.off-1:], decoding into v, and
   358  // reads the following byte ahead. If v is invalid, the value is discarded.
   359  // The first byte of the value has been read already.
   360  func (d *decodeState) value(v reflect.Value) error {
   361  	switch d.opcode {
   362  	default:
   363  		panic(phasePanicMsg)
   364  
   365  	case scanBeginArray:
   366  		if v.IsValid() {
   367  			if err := d.array(v); err != nil {
   368  				return err
   369  			}
   370  		} else {
   371  			d.skip()
   372  		}
   373  		d.scanNext()
   374  
   375  	case scanBeginObject:
   376  		if v.IsValid() {
   377  			if err := d.object(v); err != nil {
   378  				return err
   379  			}
   380  		} else {
   381  			d.skip()
   382  		}
   383  		d.scanNext()
   384  
   385  	case scanBeginLiteral:
   386  		// All bytes inside literal return scanContinue op code.
   387  		start := d.readIndex()
   388  		d.rescanLiteral()
   389  
   390  		if v.IsValid() {
   391  			if err := d.literalStore(d.data[start:d.readIndex()], v, false); err != nil {
   392  				return err
   393  			}
   394  		}
   395  	}
   396  	return nil
   397  }
   398  
   399  type unquotedValue struct{}
   400  
   401  // valueQuoted is like value but decodes a
   402  // quoted string literal or literal null into an interface value.
   403  // If it finds anything other than a quoted string literal or null,
   404  // valueQuoted returns unquotedValue{}.
   405  func (d *decodeState) valueQuoted() any {
   406  	switch d.opcode {
   407  	default:
   408  		panic(phasePanicMsg)
   409  
   410  	case scanBeginArray, scanBeginObject:
   411  		d.skip()
   412  		d.scanNext()
   413  
   414  	case scanBeginLiteral:
   415  		v := d.literalInterface()
   416  		switch v.(type) {
   417  		case nil, string:
   418  			return v
   419  		}
   420  	}
   421  	return unquotedValue{}
   422  }
   423  
   424  // indirect walks down v allocating pointers as needed,
   425  // until it gets to a non-pointer.
   426  // If it encounters an Unmarshaler, indirect stops and returns that.
   427  // If decodingNull is true, indirect stops at the first settable pointer so it
   428  // can be set to nil.
   429  func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
   430  	// Issue #24153 indicates that it is generally not a guaranteed property
   431  	// that you may round-trip a reflect.Value by calling Value.Addr().Elem()
   432  	// and expect the value to still be settable for values derived from
   433  	// unexported embedded struct fields.
   434  	//
   435  	// The logic below effectively does this when it first addresses the value
   436  	// (to satisfy possible pointer methods) and continues to dereference
   437  	// subsequent pointers as necessary.
   438  	//
   439  	// After the first round-trip, we set v back to the original value to
   440  	// preserve the original RW flags contained in reflect.Value.
   441  	v0 := v
   442  	haveAddr := false
   443  
   444  	// If v is a named type and is addressable,
   445  	// start with its address, so that if the type has pointer methods,
   446  	// we find them.
   447  	if v.Kind() != reflect.Pointer && v.Type().Name() != "" && v.CanAddr() {
   448  		haveAddr = true
   449  		v = v.Addr()
   450  	}
   451  	for {
   452  		// Load value from interface, but only if the result will be
   453  		// usefully addressable.
   454  		if v.Kind() == reflect.Interface && !v.IsNil() {
   455  			e := v.Elem()
   456  			if e.Kind() == reflect.Pointer && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Pointer) {
   457  				haveAddr = false
   458  				v = e
   459  				continue
   460  			}
   461  		}
   462  
   463  		if v.Kind() != reflect.Pointer {
   464  			break
   465  		}
   466  
   467  		if decodingNull && v.CanSet() {
   468  			break
   469  		}
   470  
   471  		// Prevent infinite loop if v is an interface pointing to its own address:
   472  		//     var v any
   473  		//     v = &v
   474  		if v.Elem().Kind() == reflect.Interface && v.Elem().Elem().Equal(v) {
   475  			v = v.Elem()
   476  			break
   477  		}
   478  		if v.IsNil() {
   479  			v.Set(reflect.New(v.Type().Elem()))
   480  		}
   481  		if v.Type().NumMethod() > 0 && v.CanInterface() {
   482  			if u, ok := v.Interface().(Unmarshaler); ok {
   483  				return u, nil, reflect.Value{}
   484  			}
   485  			if !decodingNull {
   486  				if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
   487  					return nil, u, reflect.Value{}
   488  				}
   489  			}
   490  		}
   491  
   492  		if haveAddr {
   493  			v = v0 // restore original value after round-trip Value.Addr().Elem()
   494  			haveAddr = false
   495  		} else {
   496  			v = v.Elem()
   497  		}
   498  	}
   499  	return nil, nil, v
   500  }
   501  
   502  // array consumes an array from d.data[d.off-1:], decoding into v.
   503  // The first byte of the array ('[') has been read already.
   504  func (d *decodeState) array(v reflect.Value) error {
   505  	// Check for unmarshaler.
   506  	u, ut, pv := indirect(v, false)
   507  	if u != nil {
   508  		start := d.readIndex()
   509  		d.skip()
   510  		return u.UnmarshalJSON(d.data[start:d.off])
   511  	}
   512  	if ut != nil {
   513  		d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
   514  		d.skip()
   515  		return nil
   516  	}
   517  	v = pv
   518  
   519  	// Check type of target.
   520  	switch v.Kind() {
   521  	case reflect.Interface:
   522  		if v.NumMethod() == 0 {
   523  			// Decoding into nil interface? Switch to non-reflect code.
   524  			ai := d.arrayInterface()
   525  			v.Set(reflect.ValueOf(ai))
   526  			return nil
   527  		}
   528  		// Otherwise it's invalid.
   529  		fallthrough
   530  	default:
   531  		d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
   532  		d.skip()
   533  		return nil
   534  	case reflect.Array, reflect.Slice:
   535  		break
   536  	}
   537  
   538  	i := 0
   539  	for {
   540  		// Look ahead for ] - can only happen on first iteration.
   541  		d.scanWhile(scanSkipSpace)
   542  		if d.opcode == scanEndArray {
   543  			break
   544  		}
   545  
   546  		// Expand slice length, growing the slice if necessary.
   547  		if v.Kind() == reflect.Slice {
   548  			if i >= v.Cap() {
   549  				v.Grow(1)
   550  			}
   551  			if i >= v.Len() {
   552  				v.SetLen(i + 1)
   553  			}
   554  		}
   555  
   556  		if i < v.Len() {
   557  			// Decode into element.
   558  			if err := d.value(v.Index(i)); err != nil {
   559  				return err
   560  			}
   561  		} else {
   562  			// Ran out of fixed array: skip.
   563  			if err := d.value(reflect.Value{}); err != nil {
   564  				return err
   565  			}
   566  		}
   567  		i++
   568  
   569  		// Next token must be , or ].
   570  		if d.opcode == scanSkipSpace {
   571  			d.scanWhile(scanSkipSpace)
   572  		}
   573  		if d.opcode == scanEndArray {
   574  			break
   575  		}
   576  		if d.opcode != scanArrayValue {
   577  			panic(phasePanicMsg)
   578  		}
   579  	}
   580  
   581  	if i < v.Len() {
   582  		if v.Kind() == reflect.Array {
   583  			for ; i < v.Len(); i++ {
   584  				v.Index(i).SetZero() // zero remainder of array
   585  			}
   586  		} else {
   587  			v.SetLen(i) // truncate the slice
   588  		}
   589  	}
   590  	if i == 0 && v.Kind() == reflect.Slice {
   591  		v.Set(reflect.MakeSlice(v.Type(), 0, 0))
   592  	}
   593  	return nil
   594  }
   595  
   596  var nullLiteral = []byte("null")
   597  var textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
   598  
   599  // object consumes an object from d.data[d.off-1:], decoding into v.
   600  // The first byte ('{') of the object has been read already.
   601  func (d *decodeState) object(v reflect.Value) error {
   602  	// Check for unmarshaler.
   603  	u, ut, pv := indirect(v, false)
   604  	if u != nil {
   605  		start := d.readIndex()
   606  		d.skip()
   607  		return u.UnmarshalJSON(d.data[start:d.off])
   608  	}
   609  	if ut != nil {
   610  		d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)})
   611  		d.skip()
   612  		return nil
   613  	}
   614  	v = pv
   615  	t := v.Type()
   616  
   617  	// Decoding into nil interface? Switch to non-reflect code.
   618  	if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
   619  		oi := d.objectInterface()
   620  		v.Set(reflect.ValueOf(oi))
   621  		return nil
   622  	}
   623  
   624  	var fields structFields
   625  
   626  	// Check type of target:
   627  	//   struct or
   628  	//   map[T1]T2 where T1 is string, an integer type,
   629  	//             or an encoding.TextUnmarshaler
   630  	switch v.Kind() {
   631  	case reflect.Map:
   632  		// Map key must either have string kind, have an integer kind,
   633  		// or be an encoding.TextUnmarshaler.
   634  		switch t.Key().Kind() {
   635  		case reflect.String,
   636  			reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
   637  			reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   638  		default:
   639  			if !reflect.PointerTo(t.Key()).Implements(textUnmarshalerType) {
   640  				d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)})
   641  				d.skip()
   642  				return nil
   643  			}
   644  		}
   645  		if v.IsNil() {
   646  			v.Set(reflect.MakeMap(t))
   647  		}
   648  	case reflect.Struct:
   649  		fields = cachedTypeFields(t)
   650  		// ok
   651  	default:
   652  		d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)})
   653  		d.skip()
   654  		return nil
   655  	}
   656  
   657  	var mapElem reflect.Value
   658  	var origErrorContext errorContext
   659  	if d.errorContext != nil {
   660  		origErrorContext = *d.errorContext
   661  	}
   662  
   663  	for {
   664  		// Read opening " of string key or closing }.
   665  		d.scanWhile(scanSkipSpace)
   666  		if d.opcode == scanEndObject {
   667  			// closing } - can only happen on first iteration.
   668  			break
   669  		}
   670  		if d.opcode != scanBeginLiteral {
   671  			panic(phasePanicMsg)
   672  		}
   673  
   674  		// Read key.
   675  		start := d.readIndex()
   676  		d.rescanLiteral()
   677  		item := d.data[start:d.readIndex()]
   678  		key, ok := unquoteBytes(item)
   679  		if !ok {
   680  			panic(phasePanicMsg)
   681  		}
   682  
   683  		// Figure out field corresponding to key.
   684  		var subv reflect.Value
   685  		destring := false // whether the value is wrapped in a string to be decoded first
   686  
   687  		if v.Kind() == reflect.Map {
   688  			elemType := t.Elem()
   689  			if !mapElem.IsValid() {
   690  				mapElem = reflect.New(elemType).Elem()
   691  			} else {
   692  				mapElem.SetZero()
   693  			}
   694  			subv = mapElem
   695  		} else {
   696  			f := fields.byExactName[string(key)]
   697  			if f == nil {
   698  				f = fields.byFoldedName[string(foldName(key))]
   699  			}
   700  			if f != nil {
   701  				subv = v
   702  				destring = f.quoted
   703  				if d.errorContext == nil {
   704  					d.errorContext = new(errorContext)
   705  				}
   706  				for i, ind := range f.index {
   707  					if subv.Kind() == reflect.Pointer {
   708  						if subv.IsNil() {
   709  							// If a struct embeds a pointer to an unexported type,
   710  							// it is not possible to set a newly allocated value
   711  							// since the field is unexported.
   712  							//
   713  							// See https://golang.org/issue/21357
   714  							if !subv.CanSet() {
   715  								d.saveError(fmt.Errorf("json: cannot set embedded pointer to unexported struct: %v", subv.Type().Elem()))
   716  								// Invalidate subv to ensure d.value(subv) skips over
   717  								// the JSON value without assigning it to subv.
   718  								subv = reflect.Value{}
   719  								destring = false
   720  								break
   721  							}
   722  							subv.Set(reflect.New(subv.Type().Elem()))
   723  						}
   724  						subv = subv.Elem()
   725  					}
   726  					if i < len(f.index)-1 {
   727  						d.errorContext.FieldStack = append(
   728  							d.errorContext.FieldStack,
   729  							subv.Type().Field(ind).Name,
   730  						)
   731  					}
   732  					subv = subv.Field(ind)
   733  				}
   734  				d.errorContext.Struct = t
   735  				d.errorContext.FieldStack = append(d.errorContext.FieldStack, f.name)
   736  			} else if d.disallowUnknownFields {
   737  				d.saveError(fmt.Errorf("json: unknown field %q", key))
   738  			}
   739  		}
   740  
   741  		// Read : before value.
   742  		if d.opcode == scanSkipSpace {
   743  			d.scanWhile(scanSkipSpace)
   744  		}
   745  		if d.opcode != scanObjectKey {
   746  			panic(phasePanicMsg)
   747  		}
   748  		d.scanWhile(scanSkipSpace)
   749  
   750  		if destring {
   751  			switch qv := d.valueQuoted().(type) {
   752  			case nil:
   753  				if err := d.literalStore(nullLiteral, subv, false); err != nil {
   754  					return err
   755  				}
   756  			case string:
   757  				if err := d.literalStore([]byte(qv), subv, true); err != nil {
   758  					return err
   759  				}
   760  			default:
   761  				d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type()))
   762  			}
   763  		} else {
   764  			if err := d.value(subv); err != nil {
   765  				return err
   766  			}
   767  		}
   768  
   769  		// Write value back to map;
   770  		// if using struct, subv points into struct already.
   771  		if v.Kind() == reflect.Map {
   772  			kt := t.Key()
   773  			var kv reflect.Value
   774  			if reflect.PointerTo(kt).Implements(textUnmarshalerType) {
   775  				kv = reflect.New(kt)
   776  				if err := d.literalStore(item, kv, true); err != nil {
   777  					return err
   778  				}
   779  				kv = kv.Elem()
   780  			} else {
   781  				switch kt.Kind() {
   782  				case reflect.String:
   783  					kv = reflect.New(kt).Elem()
   784  					kv.SetString(string(key))
   785  				case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   786  					s := string(key)
   787  					n, err := strconv.ParseInt(s, 10, 64)
   788  					if err != nil || kt.OverflowInt(n) {
   789  						d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)})
   790  						break
   791  					}
   792  					kv = reflect.New(kt).Elem()
   793  					kv.SetInt(n)
   794  				case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   795  					s := string(key)
   796  					n, err := strconv.ParseUint(s, 10, 64)
   797  					if err != nil || kt.OverflowUint(n) {
   798  						d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)})
   799  						break
   800  					}
   801  					kv = reflect.New(kt).Elem()
   802  					kv.SetUint(n)
   803  				default:
   804  					panic("json: Unexpected key type") // should never occur
   805  				}
   806  			}
   807  			if kv.IsValid() {
   808  				v.SetMapIndex(kv, subv)
   809  			}
   810  		}
   811  
   812  		// Next token must be , or }.
   813  		if d.opcode == scanSkipSpace {
   814  			d.scanWhile(scanSkipSpace)
   815  		}
   816  		if d.errorContext != nil {
   817  			// Reset errorContext to its original state.
   818  			// Keep the same underlying array for FieldStack, to reuse the
   819  			// space and avoid unnecessary allocs.
   820  			d.errorContext.FieldStack = d.errorContext.FieldStack[:len(origErrorContext.FieldStack)]
   821  			d.errorContext.Struct = origErrorContext.Struct
   822  		}
   823  		if d.opcode == scanEndObject {
   824  			break
   825  		}
   826  		if d.opcode != scanObjectValue {
   827  			panic(phasePanicMsg)
   828  		}
   829  	}
   830  	return nil
   831  }
   832  
   833  // convertNumber converts the number literal s to a float64 or a Number
   834  // depending on the setting of d.useNumber.
   835  func (d *decodeState) convertNumber(s string) (any, error) {
   836  	if d.useNumber {
   837  		return Number(s), nil
   838  	}
   839  	f, err := strconv.ParseFloat(s, 64)
   840  	if err != nil {
   841  		return nil, &UnmarshalTypeError{Value: "number " + s, Type: reflect.TypeFor[float64](), Offset: int64(d.off)}
   842  	}
   843  	return f, nil
   844  }
   845  
   846  var numberType = reflect.TypeFor[Number]()
   847  
   848  // literalStore decodes a literal stored in item into v.
   849  //
   850  // fromQuoted indicates whether this literal came from unwrapping a
   851  // string from the ",string" struct tag option. this is used only to
   852  // produce more helpful error messages.
   853  func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) error {
   854  	// Check for unmarshaler.
   855  	if len(item) == 0 {
   856  		// Empty string given.
   857  		d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   858  		return nil
   859  	}
   860  	isNull := item[0] == 'n' // null
   861  	u, ut, pv := indirect(v, isNull)
   862  	if u != nil {
   863  		return u.UnmarshalJSON(item)
   864  	}
   865  	if ut != nil {
   866  		if item[0] != '"' {
   867  			if fromQuoted {
   868  				d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   869  				return nil
   870  			}
   871  			val := "number"
   872  			switch item[0] {
   873  			case 'n':
   874  				val = "null"
   875  			case 't', 'f':
   876  				val = "bool"
   877  			}
   878  			d.saveError(&UnmarshalTypeError{Value: val, Type: v.Type(), Offset: int64(d.readIndex())})
   879  			return nil
   880  		}
   881  		s, ok := unquoteBytes(item)
   882  		if !ok {
   883  			if fromQuoted {
   884  				return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
   885  			}
   886  			panic(phasePanicMsg)
   887  		}
   888  		return ut.UnmarshalText(s)
   889  	}
   890  
   891  	v = pv
   892  
   893  	switch c := item[0]; c {
   894  	case 'n': // null
   895  		// The main parser checks that only true and false can reach here,
   896  		// but if this was a quoted string input, it could be anything.
   897  		if fromQuoted && string(item) != "null" {
   898  			d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   899  			break
   900  		}
   901  		switch v.Kind() {
   902  		case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice:
   903  			v.SetZero()
   904  			// otherwise, ignore null for primitives/string
   905  		}
   906  	case 't', 'f': // true, false
   907  		value := item[0] == 't'
   908  		// The main parser checks that only true and false can reach here,
   909  		// but if this was a quoted string input, it could be anything.
   910  		if fromQuoted && string(item) != "true" && string(item) != "false" {
   911  			d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   912  			break
   913  		}
   914  		switch v.Kind() {
   915  		default:
   916  			if fromQuoted {
   917  				d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   918  			} else {
   919  				d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())})
   920  			}
   921  		case reflect.Bool:
   922  			v.SetBool(value)
   923  		case reflect.Interface:
   924  			if v.NumMethod() == 0 {
   925  				v.Set(reflect.ValueOf(value))
   926  			} else {
   927  				d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())})
   928  			}
   929  		}
   930  
   931  	case '"': // string
   932  		s, ok := unquoteBytes(item)
   933  		if !ok {
   934  			if fromQuoted {
   935  				return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
   936  			}
   937  			panic(phasePanicMsg)
   938  		}
   939  		switch v.Kind() {
   940  		default:
   941  			d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
   942  		case reflect.Slice:
   943  			if v.Type().Elem().Kind() != reflect.Uint8 {
   944  				d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
   945  				break
   946  			}
   947  			b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
   948  			n, err := base64.StdEncoding.Decode(b, s)
   949  			if err != nil {
   950  				d.saveError(err)
   951  				break
   952  			}
   953  			v.SetBytes(b[:n])
   954  		case reflect.String:
   955  			t := string(s)
   956  			if v.Type() == numberType && !isValidNumber(t) {
   957  				return fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item)
   958  			}
   959  			v.SetString(t)
   960  		case reflect.Interface:
   961  			if v.NumMethod() == 0 {
   962  				v.Set(reflect.ValueOf(string(s)))
   963  			} else {
   964  				d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
   965  			}
   966  		}
   967  
   968  	default: // number
   969  		if c != '-' && (c < '0' || c > '9') {
   970  			if fromQuoted {
   971  				return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
   972  			}
   973  			panic(phasePanicMsg)
   974  		}
   975  		switch v.Kind() {
   976  		default:
   977  			if v.Kind() == reflect.String && v.Type() == numberType {
   978  				// s must be a valid number, because it's
   979  				// already been tokenized.
   980  				v.SetString(string(item))
   981  				break
   982  			}
   983  			if fromQuoted {
   984  				return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
   985  			}
   986  			d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())})
   987  		case reflect.Interface:
   988  			n, err := d.convertNumber(string(item))
   989  			if err != nil {
   990  				d.saveError(err)
   991  				break
   992  			}
   993  			if v.NumMethod() != 0 {
   994  				d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())})
   995  				break
   996  			}
   997  			v.Set(reflect.ValueOf(n))
   998  
   999  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1000  			n, err := strconv.ParseInt(string(item), 10, 64)
  1001  			if err != nil || v.OverflowInt(n) {
  1002  				d.saveError(&UnmarshalTypeError{Value: "number " + string(item), Type: v.Type(), Offset: int64(d.readIndex())})
  1003  				break
  1004  			}
  1005  			v.SetInt(n)
  1006  
  1007  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1008  			n, err := strconv.ParseUint(string(item), 10, 64)
  1009  			if err != nil || v.OverflowUint(n) {
  1010  				d.saveError(&UnmarshalTypeError{Value: "number " + string(item), Type: v.Type(), Offset: int64(d.readIndex())})
  1011  				break
  1012  			}
  1013  			v.SetUint(n)
  1014  
  1015  		case reflect.Float32, reflect.Float64:
  1016  			n, err := strconv.ParseFloat(string(item), v.Type().Bits())
  1017  			if err != nil || v.OverflowFloat(n) {
  1018  				d.saveError(&UnmarshalTypeError{Value: "number " + string(item), Type: v.Type(), Offset: int64(d.readIndex())})
  1019  				break
  1020  			}
  1021  			v.SetFloat(n)
  1022  		}
  1023  	}
  1024  	return nil
  1025  }
  1026  
  1027  // The xxxInterface routines build up a value to be stored
  1028  // in an empty interface. They are not strictly necessary,
  1029  // but they avoid the weight of reflection in this common case.
  1030  
  1031  // valueInterface is like value but returns any.
  1032  func (d *decodeState) valueInterface() (val any) {
  1033  	switch d.opcode {
  1034  	default:
  1035  		panic(phasePanicMsg)
  1036  	case scanBeginArray:
  1037  		val = d.arrayInterface()
  1038  		d.scanNext()
  1039  	case scanBeginObject:
  1040  		val = d.objectInterface()
  1041  		d.scanNext()
  1042  	case scanBeginLiteral:
  1043  		val = d.literalInterface()
  1044  	}
  1045  	return
  1046  }
  1047  
  1048  // arrayInterface is like array but returns []any.
  1049  func (d *decodeState) arrayInterface() []any {
  1050  	var v = make([]any, 0)
  1051  	for {
  1052  		// Look ahead for ] - can only happen on first iteration.
  1053  		d.scanWhile(scanSkipSpace)
  1054  		if d.opcode == scanEndArray {
  1055  			break
  1056  		}
  1057  
  1058  		v = append(v, d.valueInterface())
  1059  
  1060  		// Next token must be , or ].
  1061  		if d.opcode == scanSkipSpace {
  1062  			d.scanWhile(scanSkipSpace)
  1063  		}
  1064  		if d.opcode == scanEndArray {
  1065  			break
  1066  		}
  1067  		if d.opcode != scanArrayValue {
  1068  			panic(phasePanicMsg)
  1069  		}
  1070  	}
  1071  	return v
  1072  }
  1073  
  1074  // objectInterface is like object but returns map[string]any.
  1075  func (d *decodeState) objectInterface() map[string]any {
  1076  	m := make(map[string]any)
  1077  	for {
  1078  		// Read opening " of string key or closing }.
  1079  		d.scanWhile(scanSkipSpace)
  1080  		if d.opcode == scanEndObject {
  1081  			// closing } - can only happen on first iteration.
  1082  			break
  1083  		}
  1084  		if d.opcode != scanBeginLiteral {
  1085  			panic(phasePanicMsg)
  1086  		}
  1087  
  1088  		// Read string key.
  1089  		start := d.readIndex()
  1090  		d.rescanLiteral()
  1091  		item := d.data[start:d.readIndex()]
  1092  		key, ok := unquote(item)
  1093  		if !ok {
  1094  			panic(phasePanicMsg)
  1095  		}
  1096  
  1097  		// Read : before value.
  1098  		if d.opcode == scanSkipSpace {
  1099  			d.scanWhile(scanSkipSpace)
  1100  		}
  1101  		if d.opcode != scanObjectKey {
  1102  			panic(phasePanicMsg)
  1103  		}
  1104  		d.scanWhile(scanSkipSpace)
  1105  
  1106  		// Read value.
  1107  		m[key] = d.valueInterface()
  1108  
  1109  		// Next token must be , or }.
  1110  		if d.opcode == scanSkipSpace {
  1111  			d.scanWhile(scanSkipSpace)
  1112  		}
  1113  		if d.opcode == scanEndObject {
  1114  			break
  1115  		}
  1116  		if d.opcode != scanObjectValue {
  1117  			panic(phasePanicMsg)
  1118  		}
  1119  	}
  1120  	return m
  1121  }
  1122  
  1123  // literalInterface consumes and returns a literal from d.data[d.off-1:] and
  1124  // it reads the following byte ahead. The first byte of the literal has been
  1125  // read already (that's how the caller knows it's a literal).
  1126  func (d *decodeState) literalInterface() any {
  1127  	// All bytes inside literal return scanContinue op code.
  1128  	start := d.readIndex()
  1129  	d.rescanLiteral()
  1130  
  1131  	item := d.data[start:d.readIndex()]
  1132  
  1133  	switch c := item[0]; c {
  1134  	case 'n': // null
  1135  		return nil
  1136  
  1137  	case 't', 'f': // true, false
  1138  		return c == 't'
  1139  
  1140  	case '"': // string
  1141  		s, ok := unquote(item)
  1142  		if !ok {
  1143  			panic(phasePanicMsg)
  1144  		}
  1145  		return s
  1146  
  1147  	default: // number
  1148  		if c != '-' && (c < '0' || c > '9') {
  1149  			panic(phasePanicMsg)
  1150  		}
  1151  		n, err := d.convertNumber(string(item))
  1152  		if err != nil {
  1153  			d.saveError(err)
  1154  		}
  1155  		return n
  1156  	}
  1157  }
  1158  
  1159  // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
  1160  // or it returns -1.
  1161  func getu4(s []byte) rune {
  1162  	if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
  1163  		return -1
  1164  	}
  1165  	var r rune
  1166  	for _, c := range s[2:6] {
  1167  		switch {
  1168  		case '0' <= c && c <= '9':
  1169  			c = c - '0'
  1170  		case 'a' <= c && c <= 'f':
  1171  			c = c - 'a' + 10
  1172  		case 'A' <= c && c <= 'F':
  1173  			c = c - 'A' + 10
  1174  		default:
  1175  			return -1
  1176  		}
  1177  		r = r*16 + rune(c)
  1178  	}
  1179  	return r
  1180  }
  1181  
  1182  // unquote converts a quoted JSON string literal s into an actual string t.
  1183  // The rules are different than for Go, so cannot use strconv.Unquote.
  1184  func unquote(s []byte) (t string, ok bool) {
  1185  	s, ok = unquoteBytes(s)
  1186  	t = string(s)
  1187  	return
  1188  }
  1189  
  1190  // unquoteBytes should be an internal detail,
  1191  // but widely used packages access it using linkname.
  1192  // Notable members of the hall of shame include:
  1193  //   - github.com/bytedance/sonic
  1194  //
  1195  // Do not remove or change the type signature.
  1196  // See go.dev/issue/67401.
  1197  //
  1198  //go:linkname unquoteBytes
  1199  func unquoteBytes(s []byte) (t []byte, ok bool) {
  1200  	if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
  1201  		return
  1202  	}
  1203  	s = s[1 : len(s)-1]
  1204  
  1205  	// Check for unusual characters. If there are none,
  1206  	// then no unquoting is needed, so return a slice of the
  1207  	// original bytes.
  1208  	r := 0
  1209  	for r < len(s) {
  1210  		c := s[r]
  1211  		if c == '\\' || c == '"' || c < ' ' {
  1212  			break
  1213  		}
  1214  		if c < utf8.RuneSelf {
  1215  			r++
  1216  			continue
  1217  		}
  1218  		rr, size := utf8.DecodeRune(s[r:])
  1219  		if rr == utf8.RuneError && size == 1 {
  1220  			break
  1221  		}
  1222  		r += size
  1223  	}
  1224  	if r == len(s) {
  1225  		return s, true
  1226  	}
  1227  
  1228  	b := make([]byte, len(s)+2*utf8.UTFMax)
  1229  	w := copy(b, s[0:r])
  1230  	for r < len(s) {
  1231  		// Out of room? Can only happen if s is full of
  1232  		// malformed UTF-8 and we're replacing each
  1233  		// byte with RuneError.
  1234  		if w >= len(b)-2*utf8.UTFMax {
  1235  			nb := make([]byte, (len(b)+utf8.UTFMax)*2)
  1236  			copy(nb, b[0:w])
  1237  			b = nb
  1238  		}
  1239  		switch c := s[r]; {
  1240  		case c == '\\':
  1241  			r++
  1242  			if r >= len(s) {
  1243  				return
  1244  			}
  1245  			switch s[r] {
  1246  			default:
  1247  				return
  1248  			case '"', '\\', '/', '\'':
  1249  				b[w] = s[r]
  1250  				r++
  1251  				w++
  1252  			case 'b':
  1253  				b[w] = '\b'
  1254  				r++
  1255  				w++
  1256  			case 'f':
  1257  				b[w] = '\f'
  1258  				r++
  1259  				w++
  1260  			case 'n':
  1261  				b[w] = '\n'
  1262  				r++
  1263  				w++
  1264  			case 'r':
  1265  				b[w] = '\r'
  1266  				r++
  1267  				w++
  1268  			case 't':
  1269  				b[w] = '\t'
  1270  				r++
  1271  				w++
  1272  			case 'u':
  1273  				r--
  1274  				rr := getu4(s[r:])
  1275  				if rr < 0 {
  1276  					return
  1277  				}
  1278  				r += 6
  1279  				if utf16.IsSurrogate(rr) {
  1280  					rr1 := getu4(s[r:])
  1281  					if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
  1282  						// A valid pair; consume.
  1283  						r += 6
  1284  						w += utf8.EncodeRune(b[w:], dec)
  1285  						break
  1286  					}
  1287  					// Invalid surrogate; fall back to replacement rune.
  1288  					rr = unicode.ReplacementChar
  1289  				}
  1290  				w += utf8.EncodeRune(b[w:], rr)
  1291  			}
  1292  
  1293  		// Quote, control characters are invalid.
  1294  		case c == '"', c < ' ':
  1295  			return
  1296  
  1297  		// ASCII
  1298  		case c < utf8.RuneSelf:
  1299  			b[w] = c
  1300  			r++
  1301  			w++
  1302  
  1303  		// Coerce to well-formed UTF-8.
  1304  		default:
  1305  			rr, size := utf8.DecodeRune(s[r:])
  1306  			r += size
  1307  			w += utf8.EncodeRune(b[w:], rr)
  1308  		}
  1309  	}
  1310  	return b[0:w], true
  1311  }
  1312  

View as plain text