Ruby  2.0.0p648(2015-12-16revision53162)
generator.h
Go to the documentation of this file.
1 #ifndef _GENERATOR_H_
2 #define _GENERATOR_H_
3 
4 #include <math.h>
5 #include <ctype.h>
6 
7 #include "ruby.h"
8 
9 #ifdef HAVE_RUBY_RE_H
10 #include "ruby/re.h"
11 #else
12 #include "re.h"
13 #endif
14 
15 #ifndef rb_intern_str
16 #define rb_intern_str(string) SYM2ID(rb_str_intern(string))
17 #endif
18 
19 #ifndef rb_obj_instance_variables
20 #define rb_obj_instance_variables(object) rb_funcall(object, rb_intern("instance_variables"), 0)
21 #endif
22 
23 #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
24 
25 /* unicode defintions */
26 
27 #define UNI_STRICT_CONVERSION 1
28 
29 typedef unsigned long UTF32; /* at least 32 bits */
30 typedef unsigned short UTF16; /* at least 16 bits */
31 typedef unsigned char UTF8; /* typically 8 bits */
32 
33 #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
34 #define UNI_MAX_BMP (UTF32)0x0000FFFF
35 #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
36 #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
37 #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
38 
39 #define UNI_SUR_HIGH_START (UTF32)0xD800
40 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
41 #define UNI_SUR_LOW_START (UTF32)0xDC00
42 #define UNI_SUR_LOW_END (UTF32)0xDFFF
43 
44 static const int halfShift = 10; /* used for shifting by 10 bits */
45 
46 static const UTF32 halfBase = 0x0010000UL;
47 static const UTF32 halfMask = 0x3FFUL;
48 
49 static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length);
50 static void unicode_escape(char *buf, UTF16 character);
51 static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
52 static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
53 static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string);
54 static char *fstrndup(const char *ptr, unsigned long len);
55 
56 /* ruby api and some helpers */
57 
59  char *indent;
60  long indent_len;
61  char *space;
62  long space_len;
63  char *space_before;
65  char *object_nl;
67  char *array_nl;
73  char allow_nan;
74  char ascii_only;
76  long depth;
79 
80 #define GET_STATE(self) \
81  JSON_Generator_State *state; \
82  Data_Get_Struct(self, JSON_Generator_State, state)
83 
84 #define GENERATE_JSON(type) \
85  FBuffer *buffer; \
86  VALUE Vstate; \
87  JSON_Generator_State *state; \
88  \
89  rb_scan_args(argc, argv, "01", &Vstate); \
90  Vstate = cState_from_state_s(cState, Vstate); \
91  Data_Get_Struct(Vstate, JSON_Generator_State, state); \
92  buffer = cState_prepare_buffer(Vstate); \
93  generate_json_##type(buffer, Vstate, state, self); \
94  return fbuffer_to_s(buffer)
95 
96 static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
97 static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
98 static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
99 static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
100 static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
101 static VALUE mString_included_s(VALUE self, VALUE modul);
102 static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
104 static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self);
106 static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
107 static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
108 static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
109 static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
110 static void State_free(JSON_Generator_State *state);
112 static VALUE cState_s_allocate(VALUE klass);
113 static VALUE cState_configure(VALUE self, VALUE opts);
114 static VALUE cState_to_h(VALUE self);
115 static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
116 static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
117 static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
118 static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
119 static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
120 static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
121 static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
122 static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
123 static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
124 static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
125 static VALUE cState_partial_generate(VALUE self, VALUE obj);
126 static VALUE cState_generate(VALUE self, VALUE obj);
127 static VALUE cState_initialize(int argc, VALUE *argv, VALUE self);
128 static VALUE cState_from_state_s(VALUE self, VALUE opts);
129 static VALUE cState_indent(VALUE self);
130 static VALUE cState_indent_set(VALUE self, VALUE indent);
131 static VALUE cState_space(VALUE self);
132 static VALUE cState_space_set(VALUE self, VALUE space);
133 static VALUE cState_space_before(VALUE self);
134 static VALUE cState_space_before_set(VALUE self, VALUE space_before);
135 static VALUE cState_object_nl(VALUE self);
136 static VALUE cState_object_nl_set(VALUE self, VALUE object_nl);
137 static VALUE cState_array_nl(VALUE self);
138 static VALUE cState_array_nl_set(VALUE self, VALUE array_nl);
139 static VALUE cState_max_nesting(VALUE self);
140 static VALUE cState_max_nesting_set(VALUE self, VALUE depth);
141 static VALUE cState_allow_nan_p(VALUE self);
142 static VALUE cState_ascii_only_p(VALUE self);
143 static VALUE cState_depth(VALUE self);
144 static VALUE cState_depth_set(VALUE self, VALUE depth);
145 static FBuffer *cState_prepare_buffer(VALUE self);
146 
147 #endif
static VALUE cState_depth(VALUE self)
static VALUE cState_partial_generate(VALUE self, VALUE obj)
static VALUE cState_indent_set(VALUE self, VALUE indent)
static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
static VALUE cState_depth_set(VALUE self, VALUE depth)
struct JSON_Generator_StateStruct JSON_Generator_State
static VALUE cState_space(VALUE self)
static VALUE cState_space_set(VALUE self, VALUE space)
static VALUE mString_to_json_raw_object(VALUE self)
static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
unsigned char UTF8
Definition: generator.h:31
static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE cState_to_h(VALUE self)
static const UTF32 halfBase
Definition: generator.h:46
static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
static VALUE cState_ascii_only_p(VALUE self)
static void unicode_escape(char *buf, UTF16 character)
static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self)
static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
unsigned short UTF16
Definition: generator.h:30
static FBuffer * cState_prepare_buffer(VALUE self)
static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
static char * fstrndup(const char *ptr, unsigned long len)
static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
static VALUE cState_space_before(VALUE self)
static void State_free(JSON_Generator_State *state)
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
unsigned long UTF32
Definition: generator.h:29
static JSON_Generator_State * State_allocate()
static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character)
int argc
Definition: ruby.c:130
static VALUE cState_allow_nan_p(VALUE self)
static VALUE cState_from_state_s(VALUE self, VALUE opts)
static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length)
static VALUE mString_Extend_json_create(VALUE self, VALUE o)
static const int halfShift
Definition: generator.h:44
static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE mString_to_json(int argc, VALUE *argv, VALUE self)
static VALUE cState_object_nl(VALUE self)
static VALUE cState_max_nesting(VALUE self)
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
static VALUE cState_generate(VALUE self, VALUE obj)
unsigned long VALUE
Definition: ruby.h:104
static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static const UTF32 halfMask
Definition: generator.h:47
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self)
static VALUE cState_indent(VALUE self)
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE mString_included_s(VALUE self, VALUE modul)
static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE cState_space_before_set(VALUE self, VALUE space_before)
static VALUE cState_array_nl(VALUE self)
static VALUE cState_s_allocate(VALUE klass)
static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self)
static VALUE cState_configure(VALUE self, VALUE opts)
char ** argv
Definition: ruby.c:131