ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.66 by root, Sun Nov 29 21:32:01 2020 UTC vs.
Revision 1.69 by root, Mon Nov 30 20:38:25 2020 UTC

103 103
104// known forced types, also hardcoded in CBOR.pm 104// known forced types, also hardcoded in CBOR.pm
105enum 105enum
106{ 106{
107 AS_CBOR = 0, 107 AS_CBOR = 0,
108 AS_INT = 1,
108 AS_BYTES = 1, 109 AS_BYTES = 2,
109 AS_TEXT = 2, 110 AS_TEXT = 3,
110 AS_FLOAT16 = 3, 111 AS_FLOAT16 = 4,
111 AS_FLOAT32 = 4, 112 AS_FLOAT32 = 5,
112 AS_FLOAT64 = 5, 113 AS_FLOAT64 = 6,
113 // possibly future enhancements: float, integer 114 // possibly future enhancements: (generic) float, (generic) string
114}; 115};
115 116
116#define F_SHRINK 0x00000001UL 117#define F_SHRINK 0x00000001UL
117#define F_ALLOW_UNKNOWN 0x00000002UL 118#define F_ALLOW_UNKNOWN 0x00000002UL
118#define F_ALLOW_SHARING 0x00000004UL 119#define F_ALLOW_SHARING 0x00000004UL
287 *enc->cur++ = len; 288 *enc->cur++ = len;
288 } 289 }
289} 290}
290 291
291// encodes a perl value into a CBOR integer 292// encodes a perl value into a CBOR integer
292ecb_inline 293ecb_inline void
293encode_int (enc_t *enc, SV *sv) 294encode_int (enc_t *enc, SV *sv)
294{ 295{
295 if (SvIsUV (sv)) 296 if (SvIsUV (sv))
296 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); 297 encode_uint (enc, MAJOR_POS_INT, SvUVX (sv));
297 else if (SvIVX (sv) >= 0) 298 else if (SvIVX (sv) >= 0)
417 memcpy (enc->cur, &fp, 8); 418 memcpy (enc->cur, &fp, 8);
418 enc->cur += 8; 419 enc->cur += 8;
419} 420}
420 421
421ecb_inline void 422ecb_inline void
423encode_bool (enc_t *enc, int istrue)
424{
425 encode_ch (enc, istrue ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE);
426}
427
428ecb_inline void
422encode_forced (enc_t *enc, UV type, SV *sv) 429encode_forced (enc_t *enc, UV type, SV *sv)
423{ 430{
424 switch (type) 431 switch (type)
425 { 432 {
426 case AS_CBOR: 433 case AS_CBOR:
447 STRLEN len; 454 STRLEN len;
448 char *str = SvPVutf8 (sv, len); 455 char *str = SvPVutf8 (sv, len);
449 encode_strref (enc, 1, 1, str, len); 456 encode_strref (enc, 1, 1, str, len);
450 } 457 }
451 break; 458 break;
459
460 case AS_INT: encode_int (enc, sv); break;
452 461
453 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break; 462 case AS_FLOAT16: encode_float16 (enc, SvNV (sv)); break;
454 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break; 463 case AS_FLOAT32: encode_float32 (enc, SvNV (sv)); break;
455 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break; 464 case AS_FLOAT64: encode_float64 (enc, SvNV (sv)); break;
456 465
545 554
546 HV *stash = SvSTASH (sv); 555 HV *stash = SvSTASH (sv);
547 556
548 if (stash == boolean_stash) 557 if (stash == boolean_stash)
549 { 558 {
550 encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); 559 encode_bool (enc, SvIV (sv));
551 return; 560 return;
552 } 561 }
553 else if (stash == error_stash) 562 else if (stash == error_stash)
554 { 563 {
555 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); 564 encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF);
714 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); 723 encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len);
715 } 724 }
716 else if (SvNOKp (sv)) 725 else if (SvNOKp (sv))
717 encode_nv (enc, sv); 726 encode_nv (enc, sv);
718 else if (SvIOKp (sv)) 727 else if (SvIOKp (sv))
719 encode_int (e,v sv); 728 encode_int (enc, sv);
720 else if (SvROK (sv)) 729 else if (SvROK (sv))
721 encode_rv (enc, SvRV (sv)); 730 encode_rv (enc, SvRV (sv));
722 else if (!SvOK (sv)) 731 else if (!SvOK (sv))
723 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); 732 encode_ch (enc, MAJOR_MISC | SIMPLE_NULL);
724 else if (enc->cbor.flags & F_ALLOW_UNKNOWN) 733 else if (enc->cbor.flags & F_ALLOW_UNKNOWN)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines