github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/fix/testdata/reflect.decode.go.in (about) 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 "container/vector" 12 "encoding/base64" 13 "os" 14 "reflect" 15 "runtime" 16 "strconv" 17 "strings" 18 "unicode" 19 "utf16" 20 "utf8" 21 ) 22 23 // Unmarshal parses the JSON-encoded data and stores the result 24 // in the value pointed to by v. 25 // 26 // Unmarshal traverses the value v recursively. 27 // If an encountered value implements the Unmarshaler interface, 28 // Unmarshal calls its UnmarshalJSON method with a well-formed 29 // JSON encoding. 30 // 31 // Otherwise, Unmarshal uses the inverse of the encodings that 32 // Marshal uses, allocating maps, slices, and pointers as necessary, 33 // with the following additional rules: 34 // 35 // To unmarshal a JSON value into a nil interface value, the 36 // type stored in the interface value is one of: 37 // 38 // bool, for JSON booleans 39 // float64, for JSON numbers 40 // string, for JSON strings 41 // []interface{}, for JSON arrays 42 // map[string]interface{}, for JSON objects 43 // nil for JSON null 44 // 45 // If a JSON value is not appropriate for a given target type, 46 // or if a JSON number overflows the target type, Unmarshal 47 // skips that field and completes the unmarshalling as best it can. 48 // If no more serious errors are encountered, Unmarshal returns 49 // an UnmarshalTypeError describing the earliest such error. 50 // 51 func Unmarshal(data []byte, v interface{}) os.Error { 52 d := new(decodeState).init(data) 53 54 // Quick check for well-formedness. 55 // Avoids filling out half a data structure 56 // before discovering a JSON syntax error. 57 err := checkValid(data, &d.scan) 58 if err != nil { 59 return err 60 } 61 62 return d.unmarshal(v) 63 } 64 65 // Unmarshaler is the interface implemented by objects 66 // that can unmarshal a JSON description of themselves. 67 // The input can be assumed to be a valid JSON object 68 // encoding. UnmarshalJSON must copy the JSON data 69 // if it wishes to retain the data after returning. 70 type Unmarshaler interface { 71 UnmarshalJSON([]byte) os.Error 72 } 73 74 // An UnmarshalTypeError describes a JSON value that was 75 // not appropriate for a value of a specific Go type. 76 type UnmarshalTypeError struct { 77 Value string // description of JSON value - "bool", "array", "number -5" 78 Type reflect.Type // type of Go value it could not be assigned to 79 } 80 81 func (e *UnmarshalTypeError) String() string { 82 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() 83 } 84 85 // An UnmarshalFieldError describes a JSON object key that 86 // led to an unexported (and therefore unwritable) struct field. 87 type UnmarshalFieldError struct { 88 Key string 89 Type *reflect.StructType 90 Field reflect.StructField 91 } 92 93 func (e *UnmarshalFieldError) String() string { 94 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String() 95 } 96 97 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. 98 // (The argument to Unmarshal must be a non-nil pointer.) 99 type InvalidUnmarshalError struct { 100 Type reflect.Type 101 } 102 103 func (e *InvalidUnmarshalError) String() string { 104 if e.Type == nil { 105 return "json: Unmarshal(nil)" 106 } 107 108 if _, ok := e.Type.(*reflect.PtrType); !ok { 109 return "json: Unmarshal(non-pointer " + e.Type.String() + ")" 110 } 111 return "json: Unmarshal(nil " + e.Type.String() + ")" 112 } 113 114 func (d *decodeState) unmarshal(v interface{}) (err os.Error) { 115 defer func() { 116 if r := recover(); r != nil { 117 if _, ok := r.(runtime.Error); ok { 118 panic(r) 119 } 120 err = r.(os.Error) 121 } 122 }() 123 124 rv := reflect.NewValue(v) 125 pv, ok := rv.(*reflect.PtrValue) 126 if !ok || pv.IsNil() { 127 return &InvalidUnmarshalError{reflect.Typeof(v)} 128 } 129 130 d.scan.reset() 131 // We decode rv not pv.Elem because the Unmarshaler interface 132 // test must be applied at the top level of the value. 133 d.value(rv) 134 return d.savedError 135 } 136 137 // decodeState represents the state while decoding a JSON value. 138 type decodeState struct { 139 data []byte 140 off int // read offset in data 141 scan scanner 142 nextscan scanner // for calls to nextValue 143 savedError os.Error 144 } 145 146 // errPhase is used for errors that should not happen unless 147 // there is a bug in the JSON decoder or something is editing 148 // the data slice while the decoder executes. 149 var errPhase = os.NewError("JSON decoder out of sync - data changing underfoot?") 150 151 func (d *decodeState) init(data []byte) *decodeState { 152 d.data = data 153 d.off = 0 154 d.savedError = nil 155 return d 156 } 157 158 // error aborts the decoding by panicking with err. 159 func (d *decodeState) error(err os.Error) { 160 panic(err) 161 } 162 163 // saveError saves the first err it is called with, 164 // for reporting at the end of the unmarshal. 165 func (d *decodeState) saveError(err os.Error) { 166 if d.savedError == nil { 167 d.savedError = err 168 } 169 } 170 171 // next cuts off and returns the next full JSON value in d.data[d.off:]. 172 // The next value is known to be an object or array, not a literal. 173 func (d *decodeState) next() []byte { 174 c := d.data[d.off] 175 item, rest, err := nextValue(d.data[d.off:], &d.nextscan) 176 if err != nil { 177 d.error(err) 178 } 179 d.off = len(d.data) - len(rest) 180 181 // Our scanner has seen the opening brace/bracket 182 // and thinks we're still in the middle of the object. 183 // invent a closing brace/bracket to get it out. 184 if c == '{' { 185 d.scan.step(&d.scan, '}') 186 } else { 187 d.scan.step(&d.scan, ']') 188 } 189 190 return item 191 } 192 193 // scanWhile processes bytes in d.data[d.off:] until it 194 // receives a scan code not equal to op. 195 // It updates d.off and returns the new scan code. 196 func (d *decodeState) scanWhile(op int) int { 197 var newOp int 198 for { 199 if d.off >= len(d.data) { 200 newOp = d.scan.eof() 201 d.off = len(d.data) + 1 // mark processed EOF with len+1 202 } else { 203 c := int(d.data[d.off]) 204 d.off++ 205 newOp = d.scan.step(&d.scan, c) 206 } 207 if newOp != op { 208 break 209 } 210 } 211 return newOp 212 } 213 214 // value decodes a JSON value from d.data[d.off:] into the value. 215 // it updates d.off to point past the decoded value. 216 func (d *decodeState) value(v reflect.Value) { 217 if v == nil { 218 _, rest, err := nextValue(d.data[d.off:], &d.nextscan) 219 if err != nil { 220 d.error(err) 221 } 222 d.off = len(d.data) - len(rest) 223 224 // d.scan thinks we're still at the beginning of the item. 225 // Feed in an empty string - the shortest, simplest value - 226 // so that it knows we got to the end of the value. 227 if d.scan.step == stateRedo { 228 panic("redo") 229 } 230 d.scan.step(&d.scan, '"') 231 d.scan.step(&d.scan, '"') 232 return 233 } 234 235 switch op := d.scanWhile(scanSkipSpace); op { 236 default: 237 d.error(errPhase) 238 239 case scanBeginArray: 240 d.array(v) 241 242 case scanBeginObject: 243 d.object(v) 244 245 case scanBeginLiteral: 246 d.literal(v) 247 } 248 } 249 250 // indirect walks down v allocating pointers as needed, 251 // until it gets to a non-pointer. 252 // if it encounters an Unmarshaler, indirect stops and returns that. 253 // if wantptr is true, indirect stops at the last pointer. 254 func (d *decodeState) indirect(v reflect.Value, wantptr bool) (Unmarshaler, reflect.Value) { 255 for { 256 var isUnmarshaler bool 257 if v.Type().NumMethod() > 0 { 258 // Remember that this is an unmarshaler, 259 // but wait to return it until after allocating 260 // the pointer (if necessary). 261 _, isUnmarshaler = v.Interface().(Unmarshaler) 262 } 263 264 if iv, ok := v.(*reflect.InterfaceValue); ok && !iv.IsNil() { 265 v = iv.Elem() 266 continue 267 } 268 pv, ok := v.(*reflect.PtrValue) 269 if !ok { 270 break 271 } 272 _, isptrptr := pv.Elem().(*reflect.PtrValue) 273 if !isptrptr && wantptr && !isUnmarshaler { 274 return nil, pv 275 } 276 if pv.IsNil() { 277 pv.PointTo(reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem())) 278 } 279 if isUnmarshaler { 280 // Using v.Interface().(Unmarshaler) 281 // here means that we have to use a pointer 282 // as the struct field. We cannot use a value inside 283 // a pointer to a struct, because in that case 284 // v.Interface() is the value (x.f) not the pointer (&x.f). 285 // This is an unfortunate consequence of reflect. 286 // An alternative would be to look up the 287 // UnmarshalJSON method and return a FuncValue. 288 return v.Interface().(Unmarshaler), nil 289 } 290 v = pv.Elem() 291 } 292 return nil, v 293 } 294 295 // array consumes an array from d.data[d.off-1:], decoding into the value v. 296 // the first byte of the array ('[') has been read already. 297 func (d *decodeState) array(v reflect.Value) { 298 // Check for unmarshaler. 299 unmarshaler, pv := d.indirect(v, false) 300 if unmarshaler != nil { 301 d.off-- 302 err := unmarshaler.UnmarshalJSON(d.next()) 303 if err != nil { 304 d.error(err) 305 } 306 return 307 } 308 v = pv 309 310 // Decoding into nil interface? Switch to non-reflect code. 311 iv, ok := v.(*reflect.InterfaceValue) 312 if ok { 313 iv.Set(reflect.NewValue(d.arrayInterface())) 314 return 315 } 316 317 // Check type of target. 318 av, ok := v.(reflect.ArrayOrSliceValue) 319 if !ok { 320 d.saveError(&UnmarshalTypeError{"array", v.Type()}) 321 d.off-- 322 d.next() 323 return 324 } 325 326 sv, _ := v.(*reflect.SliceValue) 327 328 i := 0 329 for { 330 // Look ahead for ] - can only happen on first iteration. 331 op := d.scanWhile(scanSkipSpace) 332 if op == scanEndArray { 333 break 334 } 335 336 // Back up so d.value can have the byte we just read. 337 d.off-- 338 d.scan.undo(op) 339 340 // Get element of array, growing if necessary. 341 if i >= av.Cap() && sv != nil { 342 newcap := sv.Cap() + sv.Cap()/2 343 if newcap < 4 { 344 newcap = 4 345 } 346 newv := reflect.MakeSlice(sv.Type().(*reflect.SliceType), sv.Len(), newcap) 347 reflect.Copy(newv, sv) 348 sv.Set(newv) 349 } 350 if i >= av.Len() && sv != nil { 351 // Must be slice; gave up on array during i >= av.Cap(). 352 sv.SetLen(i + 1) 353 } 354 355 // Decode into element. 356 if i < av.Len() { 357 d.value(av.Elem(i)) 358 } else { 359 // Ran out of fixed array: skip. 360 d.value(nil) 361 } 362 i++ 363 364 // Next token must be , or ]. 365 op = d.scanWhile(scanSkipSpace) 366 if op == scanEndArray { 367 break 368 } 369 if op != scanArrayValue { 370 d.error(errPhase) 371 } 372 } 373 if i < av.Len() { 374 if sv == nil { 375 // Array. Zero the rest. 376 z := reflect.MakeZero(av.Type().(*reflect.ArrayType).Elem()) 377 for ; i < av.Len(); i++ { 378 av.Elem(i).SetValue(z) 379 } 380 } else { 381 sv.SetLen(i) 382 } 383 } 384 } 385 386 // matchName returns true if key should be written to a field named name. 387 func matchName(key, name string) bool { 388 return strings.ToLower(key) == strings.ToLower(name) 389 } 390 391 // object consumes an object from d.data[d.off-1:], decoding into the value v. 392 // the first byte of the object ('{') has been read already. 393 func (d *decodeState) object(v reflect.Value) { 394 // Check for unmarshaler. 395 unmarshaler, pv := d.indirect(v, false) 396 if unmarshaler != nil { 397 d.off-- 398 err := unmarshaler.UnmarshalJSON(d.next()) 399 if err != nil { 400 d.error(err) 401 } 402 return 403 } 404 v = pv 405 406 // Decoding into nil interface? Switch to non-reflect code. 407 iv, ok := v.(*reflect.InterfaceValue) 408 if ok { 409 iv.Set(reflect.NewValue(d.objectInterface())) 410 return 411 } 412 413 // Check type of target: struct or map[string]T 414 var ( 415 mv *reflect.MapValue 416 sv *reflect.StructValue 417 ) 418 switch v := v.(type) { 419 case *reflect.MapValue: 420 // map must have string type 421 t := v.Type().(*reflect.MapType) 422 if t.Key() != reflect.Typeof("") { 423 d.saveError(&UnmarshalTypeError{"object", v.Type()}) 424 break 425 } 426 mv = v 427 if mv.IsNil() { 428 mv.SetValue(reflect.MakeMap(t)) 429 } 430 case *reflect.StructValue: 431 sv = v 432 default: 433 d.saveError(&UnmarshalTypeError{"object", v.Type()}) 434 } 435 436 if mv == nil && sv == nil { 437 d.off-- 438 d.next() // skip over { } in input 439 return 440 } 441 442 for { 443 // Read opening " of string key or closing }. 444 op := d.scanWhile(scanSkipSpace) 445 if op == scanEndObject { 446 // closing } - can only happen on first iteration. 447 break 448 } 449 if op != scanBeginLiteral { 450 d.error(errPhase) 451 } 452 453 // Read string key. 454 start := d.off - 1 455 op = d.scanWhile(scanContinue) 456 item := d.data[start : d.off-1] 457 key, ok := unquote(item) 458 if !ok { 459 d.error(errPhase) 460 } 461 462 // Figure out field corresponding to key. 463 var subv reflect.Value 464 if mv != nil { 465 subv = reflect.MakeZero(mv.Type().(*reflect.MapType).Elem()) 466 } else { 467 var f reflect.StructField 468 var ok bool 469 st := sv.Type().(*reflect.StructType) 470 // First try for field with that tag. 471 if isValidTag(key) { 472 for i := 0; i < sv.NumField(); i++ { 473 f = st.Field(i) 474 if f.Tag == key { 475 ok = true 476 break 477 } 478 } 479 } 480 if !ok { 481 // Second, exact match. 482 f, ok = st.FieldByName(key) 483 } 484 if !ok { 485 // Third, case-insensitive match. 486 f, ok = st.FieldByNameFunc(func(s string) bool { return matchName(key, s) }) 487 } 488 489 // Extract value; name must be exported. 490 if ok { 491 if f.PkgPath != "" { 492 d.saveError(&UnmarshalFieldError{key, st, f}) 493 } else { 494 subv = sv.FieldByIndex(f.Index) 495 } 496 } 497 } 498 499 // Read : before value. 500 if op == scanSkipSpace { 501 op = d.scanWhile(scanSkipSpace) 502 } 503 if op != scanObjectKey { 504 d.error(errPhase) 505 } 506 507 // Read value. 508 d.value(subv) 509 510 // Write value back to map; 511 // if using struct, subv points into struct already. 512 if mv != nil { 513 mv.SetElem(reflect.NewValue(key), subv) 514 } 515 516 // Next token must be , or }. 517 op = d.scanWhile(scanSkipSpace) 518 if op == scanEndObject { 519 break 520 } 521 if op != scanObjectValue { 522 d.error(errPhase) 523 } 524 } 525 } 526 527 // literal consumes a literal from d.data[d.off-1:], decoding into the value v. 528 // The first byte of the literal has been read already 529 // (that's how the caller knows it's a literal). 530 func (d *decodeState) literal(v reflect.Value) { 531 // All bytes inside literal return scanContinue op code. 532 start := d.off - 1 533 op := d.scanWhile(scanContinue) 534 535 // Scan read one byte too far; back up. 536 d.off-- 537 d.scan.undo(op) 538 item := d.data[start:d.off] 539 540 // Check for unmarshaler. 541 wantptr := item[0] == 'n' // null 542 unmarshaler, pv := d.indirect(v, wantptr) 543 if unmarshaler != nil { 544 err := unmarshaler.UnmarshalJSON(item) 545 if err != nil { 546 d.error(err) 547 } 548 return 549 } 550 v = pv 551 552 switch c := item[0]; c { 553 case 'n': // null 554 switch v.(type) { 555 default: 556 d.saveError(&UnmarshalTypeError{"null", v.Type()}) 557 case *reflect.InterfaceValue, *reflect.PtrValue, *reflect.MapValue: 558 v.SetValue(nil) 559 } 560 561 case 't', 'f': // true, false 562 value := c == 't' 563 switch v := v.(type) { 564 default: 565 d.saveError(&UnmarshalTypeError{"bool", v.Type()}) 566 case *reflect.BoolValue: 567 v.Set(value) 568 case *reflect.InterfaceValue: 569 v.Set(reflect.NewValue(value)) 570 } 571 572 case '"': // string 573 s, ok := unquoteBytes(item) 574 if !ok { 575 d.error(errPhase) 576 } 577 switch v := v.(type) { 578 default: 579 d.saveError(&UnmarshalTypeError{"string", v.Type()}) 580 case *reflect.SliceValue: 581 if v.Type() != byteSliceType { 582 d.saveError(&UnmarshalTypeError{"string", v.Type()}) 583 break 584 } 585 b := make([]byte, base64.StdEncoding.DecodedLen(len(s))) 586 n, err := base64.StdEncoding.Decode(b, s) 587 if err != nil { 588 d.saveError(err) 589 break 590 } 591 v.Set(reflect.NewValue(b[0:n]).(*reflect.SliceValue)) 592 case *reflect.StringValue: 593 v.Set(string(s)) 594 case *reflect.InterfaceValue: 595 v.Set(reflect.NewValue(string(s))) 596 } 597 598 default: // number 599 if c != '-' && (c < '0' || c > '9') { 600 d.error(errPhase) 601 } 602 s := string(item) 603 switch v := v.(type) { 604 default: 605 d.error(&UnmarshalTypeError{"number", v.Type()}) 606 case *reflect.InterfaceValue: 607 n, err := strconv.Atof64(s) 608 if err != nil { 609 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) 610 break 611 } 612 v.Set(reflect.NewValue(n)) 613 614 case *reflect.IntValue: 615 n, err := strconv.Atoi64(s) 616 if err != nil || v.Overflow(n) { 617 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) 618 break 619 } 620 v.Set(n) 621 622 case *reflect.UintValue: 623 n, err := strconv.Atoui64(s) 624 if err != nil || v.Overflow(n) { 625 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) 626 break 627 } 628 v.Set(n) 629 630 case *reflect.FloatValue: 631 n, err := strconv.AtofN(s, v.Type().Bits()) 632 if err != nil || v.Overflow(n) { 633 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) 634 break 635 } 636 v.Set(n) 637 } 638 } 639 } 640 641 // The xxxInterface routines build up a value to be stored 642 // in an empty interface. They are not strictly necessary, 643 // but they avoid the weight of reflection in this common case. 644 645 // valueInterface is like value but returns interface{} 646 func (d *decodeState) valueInterface() interface{} { 647 switch d.scanWhile(scanSkipSpace) { 648 default: 649 d.error(errPhase) 650 case scanBeginArray: 651 return d.arrayInterface() 652 case scanBeginObject: 653 return d.objectInterface() 654 case scanBeginLiteral: 655 return d.literalInterface() 656 } 657 panic("unreachable") 658 } 659 660 // arrayInterface is like array but returns []interface{}. 661 func (d *decodeState) arrayInterface() []interface{} { 662 var v vector.Vector 663 for { 664 // Look ahead for ] - can only happen on first iteration. 665 op := d.scanWhile(scanSkipSpace) 666 if op == scanEndArray { 667 break 668 } 669 670 // Back up so d.value can have the byte we just read. 671 d.off-- 672 d.scan.undo(op) 673 674 v.Push(d.valueInterface()) 675 676 // Next token must be , or ]. 677 op = d.scanWhile(scanSkipSpace) 678 if op == scanEndArray { 679 break 680 } 681 if op != scanArrayValue { 682 d.error(errPhase) 683 } 684 } 685 return v 686 } 687 688 // objectInterface is like object but returns map[string]interface{}. 689 func (d *decodeState) objectInterface() map[string]interface{} { 690 m := make(map[string]interface{}) 691 for { 692 // Read opening " of string key or closing }. 693 op := d.scanWhile(scanSkipSpace) 694 if op == scanEndObject { 695 // closing } - can only happen on first iteration. 696 break 697 } 698 if op != scanBeginLiteral { 699 d.error(errPhase) 700 } 701 702 // Read string key. 703 start := d.off - 1 704 op = d.scanWhile(scanContinue) 705 item := d.data[start : d.off-1] 706 key, ok := unquote(item) 707 if !ok { 708 d.error(errPhase) 709 } 710 711 // Read : before value. 712 if op == scanSkipSpace { 713 op = d.scanWhile(scanSkipSpace) 714 } 715 if op != scanObjectKey { 716 d.error(errPhase) 717 } 718 719 // Read value. 720 m[key] = d.valueInterface() 721 722 // Next token must be , or }. 723 op = d.scanWhile(scanSkipSpace) 724 if op == scanEndObject { 725 break 726 } 727 if op != scanObjectValue { 728 d.error(errPhase) 729 } 730 } 731 return m 732 } 733 734 // literalInterface is like literal but returns an interface value. 735 func (d *decodeState) literalInterface() interface{} { 736 // All bytes inside literal return scanContinue op code. 737 start := d.off - 1 738 op := d.scanWhile(scanContinue) 739 740 // Scan read one byte too far; back up. 741 d.off-- 742 d.scan.undo(op) 743 item := d.data[start:d.off] 744 745 switch c := item[0]; c { 746 case 'n': // null 747 return nil 748 749 case 't', 'f': // true, false 750 return c == 't' 751 752 case '"': // string 753 s, ok := unquote(item) 754 if !ok { 755 d.error(errPhase) 756 } 757 return s 758 759 default: // number 760 if c != '-' && (c < '0' || c > '9') { 761 d.error(errPhase) 762 } 763 n, err := strconv.Atof64(string(item)) 764 if err != nil { 765 d.saveError(&UnmarshalTypeError{"number " + string(item), reflect.Typeof(0.0)}) 766 } 767 return n 768 } 769 panic("unreachable") 770 } 771 772 // getu4 decodes \uXXXX from the beginning of s, returning the hex value, 773 // or it returns -1. 774 func getu4(s []byte) int { 775 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' { 776 return -1 777 } 778 rune, err := strconv.Btoui64(string(s[2:6]), 16) 779 if err != nil { 780 return -1 781 } 782 return int(rune) 783 } 784 785 // unquote converts a quoted JSON string literal s into an actual string t. 786 // The rules are different than for Go, so cannot use strconv.Unquote. 787 func unquote(s []byte) (t string, ok bool) { 788 s, ok = unquoteBytes(s) 789 t = string(s) 790 return 791 } 792 793 func unquoteBytes(s []byte) (t []byte, ok bool) { 794 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { 795 return 796 } 797 s = s[1 : len(s)-1] 798 799 // Check for unusual characters. If there are none, 800 // then no unquoting is needed, so return a slice of the 801 // original bytes. 802 r := 0 803 for r < len(s) { 804 c := s[r] 805 if c == '\\' || c == '"' || c < ' ' { 806 break 807 } 808 if c < utf8.RuneSelf { 809 r++ 810 continue 811 } 812 rune, size := utf8.DecodeRune(s[r:]) 813 if rune == utf8.RuneError && size == 1 { 814 break 815 } 816 r += size 817 } 818 if r == len(s) { 819 return s, true 820 } 821 822 b := make([]byte, len(s)+2*utf8.UTFMax) 823 w := copy(b, s[0:r]) 824 for r < len(s) { 825 // Out of room? Can only happen if s is full of 826 // malformed UTF-8 and we're replacing each 827 // byte with RuneError. 828 if w >= len(b)-2*utf8.UTFMax { 829 nb := make([]byte, (len(b)+utf8.UTFMax)*2) 830 copy(nb, b[0:w]) 831 b = nb 832 } 833 switch c := s[r]; { 834 case c == '\\': 835 r++ 836 if r >= len(s) { 837 return 838 } 839 switch s[r] { 840 default: 841 return 842 case '"', '\\', '/', '\'': 843 b[w] = s[r] 844 r++ 845 w++ 846 case 'b': 847 b[w] = '\b' 848 r++ 849 w++ 850 case 'f': 851 b[w] = '\f' 852 r++ 853 w++ 854 case 'n': 855 b[w] = '\n' 856 r++ 857 w++ 858 case 'r': 859 b[w] = '\r' 860 r++ 861 w++ 862 case 't': 863 b[w] = '\t' 864 r++ 865 w++ 866 case 'u': 867 r-- 868 rune := getu4(s[r:]) 869 if rune < 0 { 870 return 871 } 872 r += 6 873 if utf16.IsSurrogate(rune) { 874 rune1 := getu4(s[r:]) 875 if dec := utf16.DecodeRune(rune, rune1); dec != unicode.ReplacementChar { 876 // A valid pair; consume. 877 r += 6 878 w += utf8.EncodeRune(b[w:], dec) 879 break 880 } 881 // Invalid surrogate; fall back to replacement rune. 882 rune = unicode.ReplacementChar 883 } 884 w += utf8.EncodeRune(b[w:], rune) 885 } 886 887 // Quote, control characters are invalid. 888 case c == '"', c < ' ': 889 return 890 891 // ASCII 892 case c < utf8.RuneSelf: 893 b[w] = c 894 r++ 895 w++ 896 897 // Coerce to well-formed UTF-8. 898 default: 899 rune, size := utf8.DecodeRune(s[r:]) 900 r += size 901 w += utf8.EncodeRune(b[w:], rune) 902 } 903 } 904 return b[0:w], true 905 }