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;
278 if (end + clen > len) {
280 "partial character in source, but hit end");
284 "source sequence is illegal/malformed utf-8");
300 static char *
fstrndup(
const char *ptr,
unsigned long len) {
302 if (len <= 0)
return NULL;
304 memccpy(result, ptr, 0, len);
534 state->indent_len = len;
542 state->space_len = len;
550 state->space_before_len = len;
558 state->array_nl_len = len;
566 state->object_nl_len = len;
569 state->max_nesting = 100;
572 if (
RTEST(max_nesting)) {
574 state->max_nesting =
FIX2LONG(max_nesting);
576 state->max_nesting = 0;
593 if (
RTEST(buffer_initial_length)) {
596 initial_length =
FIX2LONG(buffer_initial_length);
597 if (initial_length > 0) state->buffer_initial_length = initial_length;
601 state->allow_nan =
RTEST(tmp);
603 state->ascii_only =
RTEST(tmp);
605 state->quirks_mode =
RTEST(tmp);
684 char *indent = state->
indent;
691 long depth = ++state->
depth;
694 if (max_nesting != 0 && depth > max_nesting) {
706 for (j = 0; j < depth; j++) {
717 depth = --state->
depth;
721 for (j = 0; j < depth; j++) {
733 char *indent = state->
indent;
738 long depth = ++state->
depth;
740 if (max_nesting != 0 && depth > max_nesting) {
749 for (j = 0; j < depth; j++) {
755 state->
depth = --depth;
759 for (j = 0; j < depth; j++) {
770 #ifdef HAVE_RUBY_ENCODING_H
798 fbuffer_append_long(buffer,
FIX2LONG(obj));
804 fbuffer_append_str(buffer, tmp);
816 }
else if (
isnan(value)) {
821 fbuffer_append_str(buffer, tmp);
834 }
else if (obj ==
Qnil) {
836 }
else if (obj ==
Qfalse) {
838 }
else if (obj ==
Qtrue) {
849 fbuffer_append_str(buffer, tmp);
863 if (state->object_delim) {
869 if (state->object_delim2) {
875 if (state->space)
fbuffer_append(state->object_delim2, state->space, state->space_len);
877 if (state->array_delim) {
883 if (state->array_nl)
fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
892 return fbuffer_to_s(buffer);
903 char *p =
RSTRING_PTR(
string), *q = p + string_len - 1;
904 if (string_len < 2)
return 0;
905 for (; p < q && isspace((
unsigned char)*p); p++);
906 for (; q > p && isspace((
unsigned char)*q); q--);
907 return (*p ==
'[' && *q ==
']') || (*p ==
'{' && *q ==
'}');
951 state->max_nesting = 100;
968 if (obj == orig)
return obj;
1029 if (state->indent) {
1031 state->indent =
NULL;
1032 state->indent_len = 0;
1035 if (state->indent)
ruby_xfree(state->indent);
1037 state->indent_len = len;
1069 state->space =
NULL;
1070 state->space_len = 0;
1075 state->space_len = len;
1088 return state->space_before ?
rb_str_new(state->space_before, state->space_before_len) :
rb_str_new2(
"");
1103 if (state->space_before) {
1105 state->space_before =
NULL;
1106 state->space_before_len = 0;
1109 if (state->space_before)
ruby_xfree(state->space_before);
1111 state->space_before_len = len;
1141 if (state->object_nl) {
1143 state->object_nl =
NULL;
1146 if (state->object_nl)
ruby_xfree(state->object_nl);
1148 state->object_nl_len = len;
1176 if (state->array_nl) {
1178 state->array_nl =
NULL;
1181 if (state->array_nl)
ruby_xfree(state->array_nl);
1183 state->array_nl_len = len;
1210 return LONG2FIX(state->max_nesting);
1223 return state->max_nesting =
FIX2LONG(depth);
1269 state->quirks_mode =
RTEST(enable);
1306 return LONG2FIX(state->buffer_initial_length);
1317 long initial_length;
1320 initial_length =
FIX2LONG(buffer_initial_length);
1321 if (initial_length > 0) {
1322 state->buffer_initial_length = initial_length;
1429 #ifdef HAVE_RUBY_ENCODING_H