1 #include "../fbuffer/fbuffer.h" 4 #ifdef HAVE_RUBY_ENCODING_H 11 #ifdef RUBY_INTEGER_UNIFICATION 57 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,
58 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,
59 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,
60 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,
61 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,
62 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,
63 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,
64 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
73 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
88 const UTF8 *srcptr = source+length;
92 case 4:
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
return 0;
93 case 3:
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
return 0;
94 case 2:
if ((a = (*--srcptr)) > 0xBF)
return 0;
98 case 0xE0:
if (a < 0xA0)
return 0;
break;
99 case 0xED:
if (a > 0x9F)
return 0;
break;
100 case 0xF0:
if (a < 0x90)
return 0;
break;
101 case 0xF4:
if (a > 0x8F)
return 0;
break;
102 default:
if (a < 0x80)
return 0;
105 case 1:
if (*source >= 0x80 && *source < 0xC2)
return 0;
107 if (*source > 0xF4)
return 0;
114 const char *digits =
"0123456789abcdef";
116 buf[2] = digits[character >> 12];
117 buf[3] = digits[(character >> 8) & 0xf];
118 buf[4] = digits[(character >> 4) & 0xf];
119 buf[5] = digits[character & 0xf];
137 char buf[6] = {
'\\',
'u' };
139 while (source < sourceEnd) {
142 if (source + extraBytesToRead >= sourceEnd) {
144 "partial character in source, but hit end");
148 "source sequence is illegal/malformed utf-8");
153 switch (extraBytesToRead) {
154 case 5: ch += *source++; ch <<= 6;
155 case 4: ch += *source++; ch <<= 6;
156 case 3: ch += *source++; ch <<= 6;
157 case 2: ch += *source++; ch <<= 6;
158 case 1: ch += *source++; ch <<= 6;
159 case 0: ch += *source++;
166 #if UNI_STRICT_CONVERSION 167 source -= (extraBytesToRead+1);
169 "source sequence is illegal/malformed utf-8");
175 if (ch >= 0x20 && ch <= 0x7f) {
211 #if UNI_STRICT_CONVERSION 212 source -= (extraBytesToRead+1);
214 "source sequence is illegal/malformed utf8");
236 const char *escape =
NULL;
239 char buf[6] = {
'\\',
'u' };
241 for (start = 0, end = 0; end <
len;) {
243 c = (
unsigned char) *p;
285 if (end + clen > len) {
287 "partial character in source, but hit end");
291 "source sequence is illegal/malformed utf-8");
309 if (len <= 0)
return NULL;
352 #ifdef RUBY_INTEGER_UNIFICATION 526 size_t size =
sizeof(*state);
538 #ifdef NEW_TYPEDDATA_WRAPPER 540 "JSON/Generator/State",
542 #ifdef RUBY_TYPED_FREE_IMMEDIATELY 553 &JSON_Generator_State_type, state);
575 state->indent_len =
len;
583 state->space_len =
len;
591 state->space_before_len =
len;
599 state->array_nl_len =
len;
607 state->object_nl_len =
len;
610 state->max_nesting = 100;
613 if (
RTEST(max_nesting)) {
615 state->max_nesting =
FIX2LONG(max_nesting);
617 state->max_nesting = 0;
634 if (
RTEST(buffer_initial_length)) {
637 initial_length =
FIX2LONG(buffer_initial_length);
638 if (initial_length > 0) state->buffer_initial_length = initial_length;
642 state->allow_nan =
RTEST(tmp);
644 state->ascii_only =
RTEST(tmp);
722 char *indent = state->
indent;
729 long depth = ++state->
depth;
732 if (max_nesting != 0 && depth > max_nesting) {
744 for (j = 0; j < depth; j++) {
755 depth = --state->
depth;
759 for (j = 0; j < depth; j++) {
771 char *indent = state->
indent;
776 long depth = ++state->
depth;
778 if (max_nesting != 0 && depth > max_nesting) {
787 for (j = 0; j < depth; j++) {
793 state->
depth = --depth;
797 for (j = 0; j < depth; j++) {
808 #ifdef HAVE_RUBY_ENCODING_H 836 fbuffer_append_long(buffer,
FIX2LONG(obj));
842 fbuffer_append_str(buffer, tmp);
845 #ifdef RUBY_INTEGER_UNIFICATION 863 }
else if (
isnan(value)) {
868 fbuffer_append_str(buffer, tmp);
881 }
else if (obj ==
Qnil) {
883 }
else if (obj ==
Qfalse) {
885 }
else if (obj ==
Qtrue) {
896 fbuffer_append_str(buffer, tmp);
910 if (state->object_delim) {
916 if (state->object_delim2) {
921 if (state->space_before)
fbuffer_append(state->object_delim2, state->space_before, state->space_before_len);
923 if (state->space)
fbuffer_append(state->object_delim2, state->space, state->space_len);
925 if (state->array_delim) {
931 if (state->array_nl)
fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
940 return fbuffer_to_s(buffer);
980 state->max_nesting = 100;
997 if (obj == orig)
return obj;
1058 if (state->indent) {
1060 state->indent =
NULL;
1061 state->indent_len = 0;
1064 if (state->indent)
ruby_xfree(state->indent);
1066 state->indent_len =
len;
1098 state->space =
NULL;
1099 state->space_len = 0;
1104 state->space_len =
len;
1117 return state->space_before ?
rb_str_new(state->space_before, state->space_before_len) :
rb_str_new2(
"");
1132 if (state->space_before) {
1134 state->space_before =
NULL;
1135 state->space_before_len = 0;
1138 if (state->space_before)
ruby_xfree(state->space_before);
1140 state->space_before_len =
len;
1170 if (state->object_nl) {
1172 state->object_nl =
NULL;
1175 if (state->object_nl)
ruby_xfree(state->object_nl);
1177 state->object_nl_len =
len;
1205 if (state->array_nl) {
1207 state->array_nl =
NULL;
1210 if (state->array_nl)
ruby_xfree(state->array_nl);
1212 state->array_nl_len =
len;
1239 return LONG2FIX(state->max_nesting);
1252 return state->max_nesting =
FIX2LONG(depth);
1312 return LONG2FIX(state->buffer_initial_length);
1323 long initial_length;
1326 initial_length =
FIX2LONG(buffer_initial_length);
1327 if (initial_length > 0) {
1328 state->buffer_initial_length = initial_length;
1386 #ifdef RUBY_INTEGER_UNIFICATION 1436 #ifdef HAVE_RUBY_ENCODING_H RUBY_EXTERN VALUE rb_cString
static void fbuffer_clear(FBuffer *fb)
static VALUE eGeneratorError
VALUE rb_ary_entry(VALUE ary, long offset)
RUBY_EXTERN VALUE rb_cFloat
static VALUE i_SAFE_STATE_PROTOTYPE
static VALUE cState_array_nl(VALUE self)
static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
#define RUBY_TYPED_FREE_IMMEDIATELY
static VALUE CRegexp_MULTILINE
static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self)
static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character)
static VALUE CEncoding_UTF_8
static unsigned int hash(str, len) register const char *str
static const char trailingBytesForUTF8[256]
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
static VALUE cState_from_state_s(VALUE self, VALUE opts)
static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length)
#define option_given_p(opts, key)
static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE mString_Extend_json_create(VALUE self, VALUE o)
static const UTF32 offsetsFromUTF8[6]
static VALUE cState_object_nl(VALUE self)
static VALUE cState_check_circular_p(VALUE self)
static VALUE cState_max_nesting(VALUE self)
VALUE rb_str_concat(VALUE, VALUE)
#define RB_OBJ_STRING(obj)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
static VALUE cState_space_before_set(VALUE self, VALUE space_before)
static const UTF32 halfBase
static VALUE mGeneratorMethods
static void fbuffer_append_char(FBuffer *fb, char newchr)
static VALUE cState_configure(VALUE self, VALUE opts)
VALUE rb_iv_get(VALUE, const char *)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_ivar_get(VALUE, ID)
void Init_generator(void)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
static void State_free(void *ptr)
static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
static VALUE cState_s_allocate(VALUE klass)
static VALUE cState_allow_nan_p(VALUE self)
static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
VALUE rb_path2class(const char *)
#define FBUFFER_INITIAL_LENGTH_DEFAULT
static VALUE cState_indent(VALUE self)
static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE cState_space_set(VALUE self, VALUE space)
static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
#define RB_TYPE_P(obj, type)
static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
VALUE rb_require(const char *)
static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE cState_generate(VALUE self, VALUE obj)
#define rb_intern_str(string)
VALUE rb_class_name(VALUE)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
VALUE rb_str_substr(VALUE, long, long)
RUBY_EXTERN VALUE rb_cObject
static VALUE cState_aset(VALUE self, VALUE name, VALUE value)
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
static VALUE cState_depth(VALUE self)
VALUE rb_str_cat2(VALUE, const char *)
static VALUE cState_indent_set(VALUE self, VALUE indent)
static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
static const rb_data_type_t JSON_Generator_State_type
static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
static VALUE cState_depth_set(VALUE self, VALUE depth)
#define MEMCPY(p1, p2, type, n)
RUBY_EXTERN int isinf(double)
static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static FBuffer * fbuffer_alloc(unsigned long initial_length)
VALUE rb_const_get(VALUE, ID)
static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE mString_to_json_raw_object(VALUE self)
static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
RUBY_EXTERN VALUE rb_cRegexp
static const int halfShift
static char * fstrndup(const char *ptr, unsigned long len)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
static VALUE cState_to_h(VALUE self)
VALUE rb_ivar_set(VALUE, ID, VALUE)
static VALUE cState_space_before(VALUE self)
unsigned char buf[MIME_BUF_SIZE]
static VALUE cState_partial_generate(VALUE self, VALUE obj)
static const UTF32 halfMask
static VALUE cState_ascii_only_p(VALUE self)
static void unicode_escape(char *buf, UTF16 character)
#define UNI_SUR_HIGH_START
static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
int rb_respond_to(VALUE, ID)
register unsigned int len
VALUE rb_define_module_under(VALUE outer, const char *name)
#define StringValueCStr(v)
static VALUE CJSON_SAFE_STATE_PROTOTYPE
static ID i_buffer_initial_length
static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static FBuffer * cState_prepare_buffer(VALUE self)
static VALUE cState_aref(VALUE self, VALUE name)
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
static VALUE mString_Extend
static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
#define GENERATE_JSON(type)
static void fbuffer_free(FBuffer *fb)
static void set_state_ivars(VALUE hash, VALUE state)
VALUE rb_hash_aref(VALUE hash, VALUE key)
static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static size_t State_memsize(const void *ptr)
#define GET_STATE_TO(self, state)
#define TypedData_Make_Struct(klass, type, data_type, sval)
#define UNI_SUR_LOW_START
static VALUE eNestingError
#define UNI_REPLACEMENT_CHAR
static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
static VALUE cState_buffer_initial_length(VALUE self)
static VALUE cState_init_copy(VALUE obj, VALUE orig)
static VALUE cState_space(VALUE self)
VALUE rb_str_intern(VALUE)
VALUE rb_define_module(const char *name)
static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
#define rb_obj_instance_variables(object)
static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE mString_included_s(VALUE self, VALUE modul)
VALUE rb_str_new(const char *, long)
VALUE rb_obj_class(VALUE)