readProcInst reads a processing instruction (until `?>`). If the processing instruction specifies an encoding, it recreates the reader with the specified encoding.
()
| 406 | // If the processing instruction specifies an encoding, it recreates |
| 407 | // the reader with the specified encoding. |
| 408 | func (d *Decoder) readProcInst() ([]byte, []byte, bool) { |
| 409 | // Discard '<?' |
| 410 | if !d.discard(len(procInstStart)) { |
| 411 | return nil, nil, false |
| 412 | } |
| 413 | |
| 414 | // Target name should follow immediately after `<?`. |
| 415 | if !d.readName() { |
| 416 | // If we couldn't read a name but there was no error, it means |
| 417 | // there was no valid target name after <?. |
| 418 | // Set an error in this case. |
| 419 | if d.err == nil { |
| 420 | d.setSyntaxErrorf("expected target name after <?") |
| 421 | } |
| 422 | return nil, nil, false |
| 423 | } |
| 424 | |
| 425 | target := d.buf.Bytes() |
| 426 | |
| 427 | // Read until '?>' |
| 428 | // We don't reset the buffer here, as we don't want target name to be overwritten. |
| 429 | for { |
| 430 | if !d.mustReadUntil('>') { |
| 431 | return nil, nil, false |
| 432 | } |
| 433 | |
| 434 | if d.buf.HasSuffix(procInstEnd) { |
| 435 | break |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // Trim the trailing '?>' |
| 440 | d.buf.Remove(len(procInstEnd)) |
| 441 | |
| 442 | // Separate the target and data |
| 443 | data := d.buf.Bytes()[len(target):] |
| 444 | |
| 445 | if bytes.Equal(target, targetXML) { |
| 446 | // Get the encoding from the processing instruction data |
| 447 | data = d.handleProcInstEncoding(data) |
| 448 | } |
| 449 | |
| 450 | return target, data, true |
| 451 | } |
| 452 | |
| 453 | // handleProcInstEncoding replaces the encoding declaration in the processing instruction data |
| 454 | // with "UTF-8" and returns the updated data. |
no test coverage detected