Determine the input stream encoding by checking the BOM symbol. If no BOM is found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure.
(parser *yaml_parser_t)
| 45 | // Determine the input stream encoding by checking the BOM symbol. If no BOM is |
| 46 | // found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure. |
| 47 | func yaml_parser_determine_encoding(parser *yaml_parser_t) bool { |
| 48 | // Ensure that we had enough bytes in the raw buffer. |
| 49 | for !parser.eof && len(parser.raw_buffer)-parser.raw_buffer_pos < 3 { |
| 50 | if !yaml_parser_update_raw_buffer(parser) { |
| 51 | return false |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Determine the encoding. |
| 56 | buf := parser.raw_buffer |
| 57 | pos := parser.raw_buffer_pos |
| 58 | avail := len(buf) - pos |
| 59 | if avail >= 2 && buf[pos] == bom_UTF16LE[0] && buf[pos+1] == bom_UTF16LE[1] { |
| 60 | parser.encoding = yaml_UTF16LE_ENCODING |
| 61 | parser.raw_buffer_pos += 2 |
| 62 | parser.offset += 2 |
| 63 | } else if avail >= 2 && buf[pos] == bom_UTF16BE[0] && buf[pos+1] == bom_UTF16BE[1] { |
| 64 | parser.encoding = yaml_UTF16BE_ENCODING |
| 65 | parser.raw_buffer_pos += 2 |
| 66 | parser.offset += 2 |
| 67 | } else if avail >= 3 && buf[pos] == bom_UTF8[0] && buf[pos+1] == bom_UTF8[1] && buf[pos+2] == bom_UTF8[2] { |
| 68 | parser.encoding = yaml_UTF8_ENCODING |
| 69 | parser.raw_buffer_pos += 3 |
| 70 | parser.offset += 3 |
| 71 | } else { |
| 72 | parser.encoding = yaml_UTF8_ENCODING |
| 73 | } |
| 74 | return true |
| 75 | } |
| 76 | |
| 77 | // Update the raw buffer. |
| 78 | func yaml_parser_update_raw_buffer(parser *yaml_parser_t) bool { |
no test coverage detected