3 #ifdef HAVE_RUBY_ENCODING_H
4 static VALUE CEncoding_UTF_8;
5 static ID i_encoding, i_encode;
49 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,
50 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,
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 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,
56 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
65 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
80 const UTF8 *srcptr = source+length;
84 case 4:
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
return 0;
85 case 3:
if ((a = (*--srcptr)) < 0x80 || a > 0xBF)
return 0;
86 case 2:
if ((a = (*--srcptr)) > 0xBF)
return 0;
90 case 0xE0:
if (a < 0xA0)
return 0;
break;
91 case 0xED:
if (a > 0x9F)
return 0;
break;
92 case 0xF0:
if (a < 0x90)
return 0;
break;
93 case 0xF4:
if (a > 0x8F)
return 0;
break;
94 default:
if (a < 0x80)
return 0;
97 case 1:
if (*source >= 0x80 && *source < 0xC2)
return 0;
99 if (*source > 0xF4)
return 0;
106 const char *digits =
"0123456789abcdef";
108 buf[2] = digits[character >> 12];
109 buf[3] = digits[(character >> 8) & 0xf];
110 buf[4] = digits[(character >> 4) & 0xf];
111 buf[5] = digits[character & 0xf];
129 char buf[6] = {
'\\',
'u' };
131 while (source < sourceEnd) {
134 if (source + extraBytesToRead >= sourceEnd) {
136 "partial character in source, but hit end");
140 "source sequence is illegal/malformed utf-8");
145 switch (extraBytesToRead) {
146 case 5: ch += *source++; ch <<= 6;
147 case 4: ch += *source++; ch <<= 6;
148 case 3: ch += *source++; ch <<= 6;
149 case 2: ch += *source++; ch <<= 6;
150 case 1: ch += *source++; ch <<= 6;
151 case 0: ch += *source++;
158 #if UNI_STRICT_CONVERSION
159 source -= (extraBytesToRead+1);
161 "source sequence is illegal/malformed utf-8");
167 if (ch >= 0x20 && ch <= 0x7f) {
203 #if UNI_STRICT_CONVERSION
204 source -= (extraBytesToRead+1);
206 "source sequence is illegal/malformed utf8");
227 const char *escape =
NULL;
230 char buf[6] = {
'\\',
'u' };
232 for (start = 0, end = 0; end <
len;) {
234 c = (
unsigned char) *
p;
289 if (len <= 0)
return NULL;
291 memccpy(result, ptr, 0, len);
300 memset((
void *) fb, 0,
sizeof(
FBuffer));
308 assert(initial_length > 0);
310 memset((
void *) fb, 0,
sizeof(
FBuffer));
328 unsigned long required;
335 for (required = fb->
capa; requested > required - fb->
len; required <<= 1);
337 if (required > fb->
capa) {
365 *(fb->
ptr + fb->
len) = newchr;
373 while (end > start) {
374 c = *end, *end-- = *start, *start++ = c;
380 static char digits[] =
"0123456789";
384 if (sign < 0) number = -number;
385 do *tmp++ = digits[number % 10];
while (number /= 10);
386 if (sign < 0) *tmp++ =
'-';
394 unsigned long len =
fltoa(number, buf);
400 unsigned long len = fb->
len;
676 state->max_nesting = 19;
679 if (
RTEST(max_nesting)) {
683 state->max_nesting = 0;
748 char *indent = state->
indent;
755 long depth = ++state->
depth;
758 if (max_nesting != 0 && depth > max_nesting) {
770 for (j = 0; j < depth; j++) {
781 depth = --state->
depth;
785 for (j = 0; j < depth; j++) {
797 char *indent = state->
indent;
802 long depth = ++state->
depth;
804 if (max_nesting != 0 && depth > max_nesting) {
813 for (j = 0; j < depth; j++) {
819 state->
depth = --depth;
823 for (j = 0; j < depth; j++) {
834 #ifdef HAVE_RUBY_ENCODING_H
835 obj =
rb_funcall(obj, i_encode, 1, CEncoding_UTF_8);
880 }
else if (
isnan(value)) {
898 }
else if (obj ==
Qnil) {
900 }
else if (obj ==
Qfalse) {
902 }
else if (obj ==
Qtrue) {
926 if (
state->object_delim) {
932 if (
state->object_delim2) {
940 if (
state->array_delim) {
978 if (!
state->quirks_mode) {
979 args[0] =
rb_str_new2(
"\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z");
1011 state->max_nesting = 19;
1087 if (
state->indent) {
1090 state->indent_len = 0;
1128 state->space_len = 0;
1161 if (
state->space_before) {
1164 state->space_before_len = 0;
1199 if (
state->object_nl) {
1234 if (
state->array_nl) {
1452 #ifdef HAVE_RUBY_ENCODING_H
RUBY_EXTERN VALUE rb_cString
#define RSTRING_LEN(string)
static void fbuffer_free(FBuffer *fb)
static VALUE fbuffer_to_s(FBuffer *fb)
static VALUE eGeneratorError
static JSON_Generator_State * State_allocate()
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)
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 FBuffer * fbuffer_alloc()
static const char trailingBytesForUTF8[256]
#define Data_Get_Struct(obj, type, sval)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
static VALUE cState_from_state_s(VALUE self, VALUE opts)
static void fbuffer_append_long(FBuffer *fb, long number)
static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
#define RFLOAT_VALUE(val)
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)
static void fbuffer_clear(FBuffer *fb)
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 VALUE cState_configure(VALUE self, VALUE opts)
#define RSTRING_PTR(string)
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,...)
static VALUE cState_quirks_mode_p(VALUE self)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
static void freverse(char *start, char *end)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
static void fbuffer_append_char(FBuffer *fb, char newchr)
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
#define RARRAY_LEN(ARRAY)
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 *)
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)
#define Data_Wrap_Struct(klass, mark, free, sval)
static FBuffer * fbuffer_dup(FBuffer *fb)
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
#define MEMZERO(p, type, n)
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)
VALUE rb_class_new_instance(int, VALUE *, VALUE)
VALUE rb_class_name(VALUE)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
unsigned long initial_length
RUBY_EXTERN VALUE rb_cObject
static VALUE cState_depth(VALUE self)
static VALUE cState_indent_set(VALUE self, VALUE indent)
static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
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)
VALUE rb_const_get(VALUE, ID)
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
#define REALLOC_N(var, type, n)
static char * fstrndup(const char *ptr, unsigned long len)
#define RB_GC_GUARD(object)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
static VALUE cState_to_h(VALUE self)
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)
register unsigned int len
#define UNI_SUR_HIGH_START
static void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
int rb_respond_to(VALUE, ID)
VALUE rb_define_module_under(VALUE outer, const char *name)
#define StringValueCStr(v)
static VALUE CJSON_SAFE_STATE_PROTOTYPE
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)
static VALUE cState_quirks_mode_set(VALUE self, VALUE enable)
static void fbuffer_append_str(FBuffer *fb, VALUE str)
static VALUE mString_Extend
static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
#define GENERATE_JSON(type)
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)
#define UNI_SUR_LOW_START
static VALUE eNestingError
#define UNI_REPLACEMENT_CHAR
#define assert(condition)
#define StringValuePtr(v)
static VALUE cState_init_copy(VALUE obj, VALUE orig)
static VALUE cState_space(VALUE self)
static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
#define FBUFFER_INITIAL_LENGTH
VALUE rb_define_module(const char *name)
static long fltoa(long number, char *buf)
static FBuffer * fbuffer_alloc_with_length(unsigned long initial_length)
RUBY_EXTERN VALUE rb_cFixnum
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)
VALUE rb_str_new2(const char *)
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)
static void State_free(JSON_Generator_State *state)