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

View as plain text