Ruby  1.9.3p547(2014-05-14revision45962)
generator.h
Go to the documentation of this file.
1 #ifndef _GENERATOR_H_
2 #define _GENERATOR_H_
3 
4 #include <string.h>
5 #include <assert.h>
6 #include <math.h>
7 
8 #include "ruby.h"
9 
10 #if HAVE_RUBY_RE_H
11 #include "ruby/re.h"
12 #endif
13 
14 #if HAVE_RE_H
15 #include "re.h"
16 #endif
17 
18 #ifdef HAVE_RUBY_ENCODING_H
19 #include "ruby/encoding.h"
20 #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
21 #else
22 #define FORCE_UTF8(obj)
23 #endif
24 
25 #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
26 
27 #ifndef RHASH_SIZE
28 #define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
29 #endif
30 
31 #ifndef RFLOAT_VALUE
32 #define RFLOAT_VALUE(val) (RFLOAT(val)->value)
33 #endif
34 
35 #ifndef RARRAY_PTR
36 #define RARRAY_PTR(ARRAY) RARRAY(ARRAY)->ptr
37 #endif
38 #ifndef RARRAY_LEN
39 #define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
40 #endif
41 #ifndef RSTRING_PTR
42 #define RSTRING_PTR(string) RSTRING(string)->ptr
43 #endif
44 #ifndef RSTRING_LEN
45 #define RSTRING_LEN(string) RSTRING(string)->len
46 #endif
47 
48 /* We don't need to guard objects for rbx, so let's do nothing at all. */
49 #ifndef RB_GC_GUARD
50 #define RB_GC_GUARD(object)
51 #endif
52 
53 /* fbuffer implementation */
54 
55 typedef struct FBufferStruct {
56  unsigned long initial_length;
57  char *ptr;
58  unsigned long len;
59  unsigned long capa;
60 } FBuffer;
61 
62 #define FBUFFER_INITIAL_LENGTH 4096
63 
64 #define FBUFFER_PTR(fb) (fb->ptr)
65 #define FBUFFER_LEN(fb) (fb->len)
66 #define FBUFFER_CAPA(fb) (fb->capa)
67 #define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
68 
69 static char *fstrndup(const char *ptr, unsigned long len);
70 static FBuffer *fbuffer_alloc();
71 static FBuffer *fbuffer_alloc_with_length(unsigned long initial_length);
72 static void fbuffer_free(FBuffer *fb);
73 static void fbuffer_clear(FBuffer *fb);
74 static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
75 static void fbuffer_append_long(FBuffer *fb, long number);
76 static void fbuffer_append_char(FBuffer *fb, char newchr);
77 static FBuffer *fbuffer_dup(FBuffer *fb);
78 static VALUE fbuffer_to_s(FBuffer *fb);
79 
80 /* unicode defintions */
81 
82 #define UNI_STRICT_CONVERSION 1
83 
84 typedef unsigned long UTF32; /* at least 32 bits */
85 typedef unsigned short UTF16; /* at least 16 bits */
86 typedef unsigned char UTF8; /* typically 8 bits */
87 
88 #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
89 #define UNI_MAX_BMP (UTF32)0x0000FFFF
90 #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
91 #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
92 #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
93 
94 #define UNI_SUR_HIGH_START (UTF32)0xD800
95 #define UNI_SUR_HIGH_END (UTF32)0xDBFF
96 #define UNI_SUR_LOW_START (UTF32)0xDC00
97 #define UNI_SUR_LOW_END (UTF32)0xDFFF
98 
99 static const int halfShift = 10; /* used for shifting by 10 bits */
100 
101 static const UTF32 halfBase = 0x0010000UL;
102 static const UTF32 halfMask = 0x3FFUL;
103 
104 static unsigned char isLegalUTF8(const UTF8 *source, unsigned long length);
105 static void unicode_escape(char *buf, UTF16 character);
106 static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
107 static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
108 static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string);
109 
110 /* ruby api and some helpers */
111 
113  char *indent;
115  char *space;
116  long space_len;
119  char *object_nl;
121  char *array_nl;
127  char allow_nan;
130  long depth;
132 
133 #define GET_STATE(self) \
134  JSON_Generator_State *state; \
135  Data_Get_Struct(self, JSON_Generator_State, state)
136 
137 #define GENERATE_JSON(type) \
138  FBuffer *buffer; \
139  VALUE Vstate; \
140  JSON_Generator_State *state; \
141  \
142  rb_scan_args(argc, argv, "01", &Vstate); \
143  Vstate = cState_from_state_s(cState, Vstate); \
144  Data_Get_Struct(Vstate, JSON_Generator_State, state); \
145  buffer = cState_prepare_buffer(Vstate); \
146  generate_json_##type(buffer, Vstate, state, self); \
147  return fbuffer_to_s(buffer)
148 
149 static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
150 static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
151 static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
152 static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
153 static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
154 static VALUE mString_included_s(VALUE self, VALUE modul);
155 static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
157 static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self);
159 static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
160 static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
161 static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
162 static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
165 static VALUE cState_s_allocate(VALUE klass);
166 static VALUE cState_configure(VALUE self, VALUE opts);
167 static VALUE cState_to_h(VALUE self);
168 static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
169 static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
170 static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
171 static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
172 static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
173 static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
174 static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
175 static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
176 static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
177 static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
178 static VALUE cState_partial_generate(VALUE self, VALUE obj);
179 static VALUE cState_generate(VALUE self, VALUE obj);
180 static VALUE cState_initialize(int argc, VALUE *argv, VALUE self);
181 static VALUE cState_from_state_s(VALUE self, VALUE opts);
182 static VALUE cState_indent(VALUE self);
183 static VALUE cState_indent_set(VALUE self, VALUE indent);
184 static VALUE cState_space(VALUE self);
185 static VALUE cState_space_set(VALUE self, VALUE space);
186 static VALUE cState_space_before(VALUE self);
187 static VALUE cState_space_before_set(VALUE self, VALUE space_before);
188 static VALUE cState_object_nl(VALUE self);
189 static VALUE cState_object_nl_set(VALUE self, VALUE object_nl);
190 static VALUE cState_array_nl(VALUE self);
191 static VALUE cState_array_nl_set(VALUE self, VALUE array_nl);
192 static VALUE cState_max_nesting(VALUE self);
193 static VALUE cState_max_nesting_set(VALUE self, VALUE depth);
194 static VALUE cState_allow_nan_p(VALUE self);
195 static VALUE cState_ascii_only_p(VALUE self);
196 static VALUE cState_depth(VALUE self);
197 static VALUE cState_depth_set(VALUE self, VALUE depth);
198 static FBuffer *cState_prepare_buffer(VALUE self);
199 
200 #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 FBuffer * fbuffer_dup(FBuffer *fb)
static VALUE mString_to_json_raw_object(VALUE self)
unsigned long capa
Definition: generator.h:59
static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
unsigned char UTF8
Definition: generator.h:86
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:101
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:85
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 FBuffer * fbuffer_alloc_with_length(unsigned long initial_length)
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)
unsigned long initial_length
Definition: generator.h:56
static void fbuffer_free(FBuffer *fb)
static VALUE fbuffer_to_s(FBuffer *fb)
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
unsigned long UTF32
Definition: generator.h:84
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:120
static VALUE cState_allow_nan_p(VALUE self)
static VALUE cState_from_state_s(VALUE self, VALUE opts)
static void fbuffer_append_long(FBuffer *fb, long number)
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:99
static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
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)
static void fbuffer_clear(FBuffer *fb)
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:3913
static VALUE cState_generate(VALUE self, VALUE obj)
unsigned long VALUE
Definition: ruby.h:88
static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
static const UTF32 halfMask
Definition: generator.h:102
static void fbuffer_append_char(FBuffer *fb, char newchr)
static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
struct FBufferStruct FBuffer
register unsigned int len
Definition: name2ctype.h:22210
char * ptr
Definition: generator.h:57
static VALUE mString_to_json_raw(int argc, VALUE *argv, VALUE self)
unsigned long len
Definition: generator.h:58
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 FBuffer * fbuffer_alloc()
static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self)
static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
state
Definition: gb18030.c:213
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:121