|
25 | 25 | #define MSGPACK_PACKER_IO_FLUSH_THRESHOLD_TO_WRITE_STRING_BODY (1024)
|
26 | 26 | #endif
|
27 | 27 |
|
| 28 | +#ifndef UNREACHABLE_RETURN |
| 29 | +// Ruby 2.5 |
| 30 | +#define UNREACHABLE_RETURN() return |
| 31 | +#endif |
| 32 | + |
28 | 33 | struct msgpack_packer_t;
|
29 | 34 | typedef struct msgpack_packer_t msgpack_packer_t;
|
30 | 35 |
|
@@ -404,27 +409,33 @@ static inline bool msgpack_packer_is_utf8_compat_string(VALUE v, int encindex)
|
404 | 409 | {
|
405 | 410 | return encindex == msgpack_rb_encindex_utf8
|
406 | 411 | || encindex == msgpack_rb_encindex_usascii
|
407 |
| - || (rb_enc_asciicompat(rb_enc_from_index(encindex)) && ENC_CODERANGE_ASCIIONLY(v)); |
| 412 | + || ENC_CODERANGE_ASCIIONLY(v); |
408 | 413 | }
|
409 | 414 |
|
410 | 415 | static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE v)
|
411 | 416 | {
|
412 |
| - /* actual return type of RSTRING_LEN is long */ |
413 |
| - unsigned long len = RSTRING_LEN(v); |
414 |
| - if(len > 0xffffffffUL) { |
415 |
| - // TODO rb_eArgError? |
416 |
| - rb_raise(rb_eArgError, "size of string is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL); |
| 417 | + long len = RSTRING_LEN(v); |
| 418 | + |
| 419 | + if(RB_UNLIKELY(len > 0xffffffffL)) { |
| 420 | + rb_raise(rb_eArgError, "size of string is too long to pack: %lu bytes should be <= %ld", len, 0xffffffffL); |
| 421 | + UNREACHABLE_RETURN(); |
| 422 | + } |
| 423 | + |
| 424 | + if (RB_UNLIKELY(pk->compatibility_mode)) { |
| 425 | + msgpack_packer_write_raw_header(pk, (unsigned int)len); |
| 426 | + msgpack_buffer_append_string(PACKER_BUFFER_(pk), v); |
| 427 | + return; |
417 | 428 | }
|
418 | 429 |
|
419 |
| - int encindex = ENCODING_GET(v); |
420 |
| - if(msgpack_packer_is_binary(v, encindex) && !pk->compatibility_mode) { |
| 430 | + int encindex = ENCODING_GET_INLINED(v); |
| 431 | + if(msgpack_packer_is_binary(v, encindex)) { |
421 | 432 | /* write ASCII-8BIT string using Binary type */
|
422 | 433 | msgpack_packer_write_bin_header(pk, (unsigned int)len);
|
423 | 434 | msgpack_buffer_append_string(PACKER_BUFFER_(pk), v);
|
424 | 435 | } else {
|
425 | 436 | /* write UTF-8, US-ASCII, or 7bit-safe ascii-compatible string using String type directly */
|
426 | 437 | /* in compatibility mode, packer packs String values as is */
|
427 |
| - if(!pk->compatibility_mode && !msgpack_packer_is_utf8_compat_string(v, encindex)) { |
| 438 | + if(RB_UNLIKELY(!msgpack_packer_is_utf8_compat_string(v, encindex))) { |
428 | 439 | /* transcode other strings to UTF-8 and write using String type */
|
429 | 440 | VALUE enc = rb_enc_from_encoding(rb_utf8_encoding()); /* rb_enc_from_encoding_index is not extern */
|
430 | 441 | v = rb_str_encode(v, enc, 0, Qnil);
|
|
0 commit comments