1 #include "../fbuffer/fbuffer.h"
4 #ifdef HAVE_RUBY_ENCODING_H
51 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
52 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
53 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
54 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
55 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
56 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
57 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
58 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
67 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
82 const UTF8 *srcptr = source+length;
86 case 4:
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
return 0;
87 case 3:
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
return 0;
88 case 2:
if ((a = (*--srcptr)) > 0xBF)
return 0;
92 case 0xE0:
if (a < 0xA0)
return 0;
break;
93 case 0xED:
if (a > 0x9F)
return 0;
break;
94 case 0xF0:
if (a < 0x90)
return 0;
break;
95 case 0xF4:
if (a > 0x8F)
return 0;
break;
96 default:
if (a < 0x80)
return 0;
99 case 1:
if (*source >= 0x80 && *source < 0xC2)
return 0;
101 if (*source > 0xF4)
return 0;
108 const char *digits =
"0123456789abcdef";
110 buf[2] = digits[character >> 12];
111 buf[3] = digits[(character >> 8) & 0xf];
112 buf[4] = digits[(character >> 4) & 0xf];
113 buf[5] = digits[character & 0xf];
131 char buf[6] = {
'\\',
'u' };
133 while (source < sourceEnd) {
136 if (source + extraBytesToRead >= sourceEnd) {
138 "partial character in source, but hit end");
142 "source sequence is illegal/malformed utf-8");
147 switch (extraBytesToRead) {
148 case 5: ch += *source++; ch <<= 6;
149 case 4: ch += *source++; ch <<= 6;
150 case 3: ch += *source++; ch <<= 6;
151 case 2: ch += *source++; ch <<= 6;
152 case 1: ch += *source++; ch <<= 6;
153 case 0: ch += *source++;
160 #if UNI_STRICT_CONVERSION
161 source -= (extraBytesToRead+1);
163 "source sequence is illegal/malformed utf-8");
169 if (ch >= 0x20 && ch <= 0x7f) {
205 #if UNI_STRICT_CONVERSION
206 source -= (extraBytesToRead+1);
208 "source sequence is illegal/malformed utf8");
228 unsigned long len =
RSTRING_LEN(
string), start = 0, end = 0;
229 const char *escape =
NULL;
232 char buf[6] = {
'\\',
'u' };
234 for (start = 0, end = 0; end < len;) {
236 c = (
unsigned char) *
p;
289 static char *
fstrndup(
const char *ptr,
unsigned long len) {
291 if (len <= 0)
return NULL;
293 memccpy(result, ptr, 0, len);
526 state->indent_len = len;
534 state->space_len = len;
542 state->space_before_len = len;
550 state->array_nl_len = len;
558 state->object_nl_len = len;
561 state->max_nesting = 100;
564 if (
RTEST(max_nesting)) {
566 state->max_nesting =
FIX2LONG(max_nesting);
568 state->max_nesting = 0;
585 if (
RTEST(buffer_initial_length)) {
588 initial_length =
FIX2LONG(buffer_initial_length);
589 if (initial_length > 0) state->buffer_initial_length = initial_length;
593 state->allow_nan =
RTEST(tmp);
595 state->ascii_only =
RTEST(tmp);
597 state->quirks_mode =
RTEST(tmp);
676 char *indent = state->
indent;
683 long depth = ++state->
depth;
686 if (max_nesting != 0 && depth > max_nesting) {
698 for (j = 0; j < depth; j++) {
709 depth = --state->
depth;
713 for (j = 0; j < depth; j++) {
725 char *indent = state->
indent;
730 long depth = ++state->
depth;
732 if (max_nesting != 0 && depth > max_nesting) {
741 for (j = 0; j < depth; j++) {
747 state->
depth = --depth;
751 for (j = 0; j < depth; j++) {
762 #ifdef HAVE_RUBY_ENCODING_H
790 fbuffer_append_long(buffer,
FIX2LONG(obj));
796 fbuffer_append_str(buffer, tmp);
808 }
else if (
isnan(value)) {
813 fbuffer_append_str(buffer, tmp);
826 }
else if (obj ==
Qnil) {
828 }
else if (obj ==
Qfalse) {
830 }
else if (obj ==
Qtrue) {
841 fbuffer_append_str(buffer, tmp);
855 if (state->object_delim) {
861 if (state->object_delim2) {
867 if (state->space)
fbuffer_append(state->object_delim2, state->space, state->space_len);
869 if (state->array_delim) {
875 if (state->array_nl)
fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
884 return fbuffer_to_s(buffer);
896 if (string_len < 2)
return 0;
897 for (; p < q && isspace(*p); p++);
898 for (; q > p && isspace(*q); q--);
899 return (*p ==
'[' && *q ==
']') || (*p ==
'{' && *q ==
'}');
943 state->max_nesting = 100;
1020 if (state->indent) {
1022 state->indent =
NULL;
1023 state->indent_len = 0;
1026 if (state->indent)
ruby_xfree(state->indent);
1028 state->indent_len = len;
1060 state->space =
NULL;
1061 state->space_len = 0;
1066 state->space_len = len;
1079 return state->space_before ?
rb_str_new(state->space_before, state->space_before_len) :
rb_str_new2(
"");
1094 if (state->space_before) {
1096 state->space_before =
NULL;
1097 state->space_before_len = 0;
1100 if (state->space_before)
ruby_xfree(state->space_before);
1102 state->space_before_len = len;
1132 if (state->object_nl) {
1134 state->object_nl =
NULL;
1137 if (state->object_nl)
ruby_xfree(state->object_nl);
1139 state->object_nl_len = len;
1167 if (state->array_nl) {
1169 state->array_nl =
NULL;
1172 if (state->array_nl)
ruby_xfree(state->array_nl);
1174 state->array_nl_len = len;
1201 return LONG2FIX(state->max_nesting);
1214 return state->max_nesting =
FIX2LONG(depth);
1260 state->quirks_mode =
RTEST(enable);
1297 return LONG2FIX(state->buffer_initial_length);
1308 long initial_length;
1311 initial_length =
FIX2LONG(buffer_initial_length);
1312 if (initial_length > 0) {
1313 state->buffer_initial_length = initial_length;
1420 #ifdef HAVE_RUBY_ENCODING_H