tingyun_rpm 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +14 -0
  4. data/.travis.yml +4 -0
  5. data/CODE_OF_CONDUCT.md +13 -0
  6. data/Gemfile +3 -0
  7. data/Guardfile +25 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +41 -0
  10. data/cert/cacert.pem +0 -0
  11. data/lib/ting_yun/agent/agent.rb +128 -0
  12. data/lib/ting_yun/agent/class_methods.rb +21 -0
  13. data/lib/ting_yun/agent/collector/base_sampler.rb +2 -0
  14. data/lib/ting_yun/agent/collector/error_collector/error_trace_array.rb +88 -0
  15. data/lib/ting_yun/agent/collector/error_collector/noticed_error.rb +129 -0
  16. data/lib/ting_yun/agent/collector/error_collector.rb +165 -0
  17. data/lib/ting_yun/agent/collector/middle_ware_collector/cpu_sampler.rb +68 -0
  18. data/lib/ting_yun/agent/collector/middle_ware_collector/memory_sampler.rb +139 -0
  19. data/lib/ting_yun/agent/collector/middle_ware_collector/middle_ware.rb +13 -0
  20. data/lib/ting_yun/agent/collector/middle_ware_collector/sampler.rb +59 -0
  21. data/lib/ting_yun/agent/collector/middle_ware_collector.rb +80 -0
  22. data/lib/ting_yun/agent/collector/sql_sampler.rb +299 -0
  23. data/lib/ting_yun/agent/collector/stats_engine/metric_stats.rb +170 -0
  24. data/lib/ting_yun/agent/collector/stats_engine/stats_hash.rb +172 -0
  25. data/lib/ting_yun/agent/collector/stats_engine.rb +28 -0
  26. data/lib/ting_yun/agent/collector/transaction_sampler/slowest_sample_buffer.rb +25 -0
  27. data/lib/ting_yun/agent/collector/transaction_sampler/transaction_sample_buffer_base.rb +96 -0
  28. data/lib/ting_yun/agent/collector/transaction_sampler.rb +226 -0
  29. data/lib/ting_yun/agent/container_data_manager.rb +94 -0
  30. data/lib/ting_yun/agent/cross_app/cross_app_monitor.rb +131 -0
  31. data/lib/ting_yun/agent/cross_app/cross_app_tracing.rb +202 -0
  32. data/lib/ting_yun/agent/cross_app/inbound_request_monitor.rb +22 -0
  33. data/lib/ting_yun/agent/database.rb +410 -0
  34. data/lib/ting_yun/agent/datastore/metric_helper.rb +82 -0
  35. data/lib/ting_yun/agent/datastore/mongo.rb +44 -0
  36. data/lib/ting_yun/agent/datastore.rb +33 -0
  37. data/lib/ting_yun/agent/dispatcher.rb +39 -0
  38. data/lib/ting_yun/agent/event/event_listener.rb +47 -0
  39. data/lib/ting_yun/agent/event/event_loop.rb +194 -0
  40. data/lib/ting_yun/agent/instance_methods/connect.rb +164 -0
  41. data/lib/ting_yun/agent/instance_methods/container_data_manager.rb +137 -0
  42. data/lib/ting_yun/agent/instance_methods/handle_errors.rb +71 -0
  43. data/lib/ting_yun/agent/instance_methods/start.rb +219 -0
  44. data/lib/ting_yun/agent/instance_methods/start_worker_thread.rb +51 -0
  45. data/lib/ting_yun/agent/instance_methods.rb +39 -0
  46. data/lib/ting_yun/agent/method_tracer.rb +256 -0
  47. data/lib/ting_yun/agent/method_tracer_helpers.rb +85 -0
  48. data/lib/ting_yun/agent/threading/agent_thread.rb +49 -0
  49. data/lib/ting_yun/agent/transaction/attributes.rb +22 -0
  50. data/lib/ting_yun/agent/transaction/request_attributes.rb +126 -0
  51. data/lib/ting_yun/agent/transaction/trace.rb +125 -0
  52. data/lib/ting_yun/agent/transaction/trace_node.rb +110 -0
  53. data/lib/ting_yun/agent/transaction/traced_method_stack.rb +80 -0
  54. data/lib/ting_yun/agent/transaction/transaction_metrics.rb +51 -0
  55. data/lib/ting_yun/agent/transaction/transaction_sample_builder.rb +63 -0
  56. data/lib/ting_yun/agent/transaction/transaction_state.rb +112 -0
  57. data/lib/ting_yun/agent/transaction.rb +522 -0
  58. data/lib/ting_yun/agent.rb +207 -0
  59. data/lib/ting_yun/configuration/default_source.rb +638 -0
  60. data/lib/ting_yun/configuration/dotted_hash.rb +46 -0
  61. data/lib/ting_yun/configuration/environment_source.rb +116 -0
  62. data/lib/ting_yun/configuration/manager.rb +232 -0
  63. data/lib/ting_yun/configuration/manual_source.rb +14 -0
  64. data/lib/ting_yun/configuration/server_source.rb +88 -0
  65. data/lib/ting_yun/configuration/yaml_source.rb +136 -0
  66. data/lib/ting_yun/configuration.rb +9 -0
  67. data/lib/ting_yun/environment_report.rb +123 -0
  68. data/lib/ting_yun/frameworks/class_methods.rb +47 -0
  69. data/lib/ting_yun/frameworks/external.rb +15 -0
  70. data/lib/ting_yun/frameworks/instance_methods.rb +120 -0
  71. data/lib/ting_yun/frameworks/instrumentation.rb +67 -0
  72. data/lib/ting_yun/frameworks/rails.rb +63 -0
  73. data/lib/ting_yun/frameworks/rails3.rb +26 -0
  74. data/lib/ting_yun/frameworks/rails4.rb +14 -0
  75. data/lib/ting_yun/frameworks/ruby.rb +17 -0
  76. data/lib/ting_yun/frameworks/sinatra.rb +10 -0
  77. data/lib/ting_yun/frameworks.rb +34 -0
  78. data/lib/ting_yun/http/generic_request.rb +8 -0
  79. data/lib/ting_yun/http/net_http_request.rb +46 -0
  80. data/lib/ting_yun/instrumentation/active_record.rb +103 -0
  81. data/lib/ting_yun/instrumentation/middleware_proxy.rb +77 -0
  82. data/lib/ting_yun/instrumentation/middleware_tracing.rb +84 -0
  83. data/lib/ting_yun/instrumentation/mongo.rb +103 -0
  84. data/lib/ting_yun/instrumentation/mongo2.rb +37 -0
  85. data/lib/ting_yun/instrumentation/mongo_command_log_subscriber.rb +97 -0
  86. data/lib/ting_yun/instrumentation/moped.rb +95 -0
  87. data/lib/ting_yun/instrumentation/net.rb +59 -0
  88. data/lib/ting_yun/instrumentation/rack.rb +109 -0
  89. data/lib/ting_yun/instrumentation/rails3/action_controller.rb +63 -0
  90. data/lib/ting_yun/instrumentation/rails3/action_view.rb +115 -0
  91. data/lib/ting_yun/instrumentation/rails4/action_controller_subscriber.rb +124 -0
  92. data/lib/ting_yun/instrumentation/rails4/action_view_subscriber.rb +118 -0
  93. data/lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb +124 -0
  94. data/lib/ting_yun/instrumentation/rails_middleware.rb +38 -0
  95. data/lib/ting_yun/instrumentation/redis.rb +70 -0
  96. data/lib/ting_yun/instrumentation/support/active_record_helper.rb +178 -0
  97. data/lib/ting_yun/instrumentation/support/controller_instrumentation.rb +54 -0
  98. data/lib/ting_yun/instrumentation/support/database.rb +38 -0
  99. data/lib/ting_yun/instrumentation/support/event_formatter.rb +19 -0
  100. data/lib/ting_yun/instrumentation/support/evented_subscriber.rb +97 -0
  101. data/lib/ting_yun/instrumentation/support/external_error.rb +52 -0
  102. data/lib/ting_yun/instrumentation/support/metric_translator.rb +84 -0
  103. data/lib/ting_yun/instrumentation/support/mongo_formatter.rb +49 -0
  104. data/lib/ting_yun/instrumentation/support/parameter_filtering.rb +21 -0
  105. data/lib/ting_yun/instrumentation/support/queue_time.rb +76 -0
  106. data/lib/ting_yun/instrumentation/support/transaction_namer.rb +68 -0
  107. data/lib/ting_yun/instrumentation/thrift.rb +329 -0
  108. data/lib/ting_yun/logger/agent_logger.rb +196 -0
  109. data/lib/ting_yun/logger/log_once.rb +38 -0
  110. data/lib/ting_yun/logger/memory_logger.rb +56 -0
  111. data/lib/ting_yun/logger/null_logger.rb +31 -0
  112. data/lib/ting_yun/logger/startup_logger.rb +13 -0
  113. data/lib/ting_yun/logger.rb +8 -0
  114. data/lib/ting_yun/metrics/metric_data.rb +86 -0
  115. data/lib/ting_yun/metrics/metric_spec.rb +89 -0
  116. data/lib/ting_yun/metrics/stats.rb +158 -0
  117. data/lib/ting_yun/metrics.rb +12 -0
  118. data/lib/ting_yun/support/coerce.rb +86 -0
  119. data/lib/ting_yun/support/collector.rb +29 -0
  120. data/lib/ting_yun/support/exception.rb +79 -0
  121. data/lib/ting_yun/support/hash_extensions.rb +25 -0
  122. data/lib/ting_yun/support/helper.rb +54 -0
  123. data/lib/ting_yun/support/hostname.rb +13 -0
  124. data/lib/ting_yun/support/http_clients/uri_util.rb +49 -0
  125. data/lib/ting_yun/support/language_support.rb +155 -0
  126. data/lib/ting_yun/support/library_detection.rb +129 -0
  127. data/lib/ting_yun/support/local_environment.rb +185 -0
  128. data/lib/ting_yun/support/path.rb +13 -0
  129. data/lib/ting_yun/support/serialize/encodes.rb +61 -0
  130. data/lib/ting_yun/support/serialize/encoding_normalizer.rb +84 -0
  131. data/lib/ting_yun/support/serialize/json_marshaller.rb +73 -0
  132. data/lib/ting_yun/support/serialize/json_wrapper.rb +78 -0
  133. data/lib/ting_yun/support/serialize/marshaller.rb +69 -0
  134. data/lib/ting_yun/support/serialize/ok_json.rb +651 -0
  135. data/lib/ting_yun/support/system_info.rb +206 -0
  136. data/lib/ting_yun/support/timer_lib.rb +29 -0
  137. data/lib/ting_yun/support/version_number.rb +70 -0
  138. data/lib/ting_yun/ting_yun_service/connection.rb +118 -0
  139. data/lib/ting_yun/ting_yun_service/http.rb +41 -0
  140. data/lib/ting_yun/ting_yun_service/request.rb +90 -0
  141. data/lib/ting_yun/ting_yun_service/ssl.rb +45 -0
  142. data/lib/ting_yun/ting_yun_service/upload_service.rb +149 -0
  143. data/lib/ting_yun/ting_yun_service.rb +124 -0
  144. data/lib/ting_yun/version.rb +17 -0
  145. data/lib/tingyun_rpm.rb +47 -0
  146. data/tingyun_rpm.gemspec +60 -0
  147. metadata +415 -0
@@ -0,0 +1,651 @@
1
+ # encoding: utf-8
2
+
3
+ #
4
+ # Copyright 2011, 2012 Keith Rarick
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
24
+
25
+ require 'stringio'
26
+
27
+ # Some parts adapted from
28
+ # http://golang.org/src/pkg/json/decode.go and
29
+ # http://golang.org/src/pkg/utf8/utf8.go
30
+ module TingYun
31
+ module Support
32
+ module Serialize
33
+ module OkJson
34
+ Upstream = 'LTD7LBKLZWFF7OZK'
35
+ extend self
36
+
37
+
38
+ # Decodes a json document in string s and
39
+ # returns the corresponding ruby value.
40
+ # String s must be valid UTF-8. If you have
41
+ # a string in some other encoding, convert
42
+ # it first.
43
+ #
44
+ # String values in the resulting structure
45
+ # will be UTF-8.
46
+ def decode(s)
47
+ ts = lex(s)
48
+ v, ts = textparse(ts)
49
+ if ts.length > 0
50
+ raise Error, 'trailing garbage'
51
+ end
52
+ v
53
+ end
54
+
55
+
56
+ # Parses a "json text" in the sense of RFC 4627.
57
+ # Returns the parsed value and any trailing tokens.
58
+ # Note: this is almost the same as valparse,
59
+ # except that it does not accept atomic values.
60
+ def textparse(ts)
61
+ if ts.length < 0
62
+ raise Error, 'empty'
63
+ end
64
+
65
+ typ, _, val = ts[0]
66
+ case typ
67
+ when '{' then
68
+ objparse(ts)
69
+ when '[' then
70
+ arrparse(ts)
71
+ else
72
+ raise Error, "unexpected #{val.inspect}"
73
+ end
74
+ end
75
+
76
+
77
+ # Parses a "value" in the sense of RFC 4627.
78
+ # Returns the parsed value and any trailing tokens.
79
+ def valparse(ts)
80
+ if ts.length < 0
81
+ raise Error, 'empty'
82
+ end
83
+
84
+ typ, _, val = ts[0]
85
+ case typ
86
+ when '{' then
87
+ objparse(ts)
88
+ when '[' then
89
+ arrparse(ts)
90
+ when :val, :str then
91
+ [val, ts[1..-1]]
92
+ else
93
+ raise Error, "unexpected #{val.inspect}"
94
+ end
95
+ end
96
+
97
+
98
+ # Parses an "object" in the sense of RFC 4627.
99
+ # Returns the parsed value and any trailing tokens.
100
+ def objparse(ts)
101
+ ts = eat('{', ts)
102
+ obj = {}
103
+
104
+ if ts[0][0] == '}'
105
+ return obj, ts[1..-1]
106
+ end
107
+
108
+ k, v, ts = pairparse(ts)
109
+ obj[k] = v
110
+
111
+ if ts[0][0] == '}'
112
+ return obj, ts[1..-1]
113
+ end
114
+
115
+ loop do
116
+ ts = eat(',', ts)
117
+
118
+ k, v, ts = pairparse(ts)
119
+ obj[k] = v
120
+
121
+ if ts[0][0] == '}'
122
+ return obj, ts[1..-1]
123
+ end
124
+ end
125
+ end
126
+
127
+
128
+ # Parses a "member" in the sense of RFC 4627.
129
+ # Returns the parsed values and any trailing tokens.
130
+ def pairparse(ts)
131
+ (typ, _, k), ts = ts[0], ts[1..-1]
132
+ if typ != :str
133
+ raise Error, "unexpected #{k.inspect}"
134
+ end
135
+ ts = eat(':', ts)
136
+ v, ts = valparse(ts)
137
+ [k, v, ts]
138
+ end
139
+
140
+
141
+ # Parses an "array" in the sense of RFC 4627.
142
+ # Returns the parsed value and any trailing tokens.
143
+ def arrparse(ts)
144
+ ts = eat('[', ts)
145
+ arr = []
146
+
147
+ if ts[0][0] == ']'
148
+ return arr, ts[1..-1]
149
+ end
150
+
151
+ v, ts = valparse(ts)
152
+ arr << v
153
+
154
+ if ts[0][0] == ']'
155
+ return arr, ts[1..-1]
156
+ end
157
+
158
+ loop do
159
+ ts = eat(',', ts)
160
+
161
+ v, ts = valparse(ts)
162
+ arr << v
163
+
164
+ if ts[0][0] == ']'
165
+ return arr, ts[1..-1]
166
+ end
167
+ end
168
+ end
169
+
170
+
171
+ def eat(typ, ts)
172
+ if ts[0][0] != typ
173
+ raise Error, "expected #{typ} (got #{ts[0].inspect})"
174
+ end
175
+ ts[1..-1]
176
+ end
177
+
178
+
179
+ # Scans s and returns a list of json tokens,
180
+ # excluding white space (as defined in RFC 4627).
181
+ def lex(s)
182
+ ts = []
183
+ while s.length > 0
184
+ typ, lexeme, val = tok(s)
185
+ if typ == nil
186
+ raise Error, "invalid character at #{s[0, 10].inspect}"
187
+ end
188
+ if typ != :space
189
+ ts << [typ, lexeme, val]
190
+ end
191
+ s = s[lexeme.length..-1]
192
+ end
193
+ ts
194
+ end
195
+
196
+
197
+ # Scans the first token in s and
198
+ # returns a 3-element list, or nil
199
+ # if s does not begin with a valid token.
200
+ #
201
+ # The first list element is one of
202
+ # '{', '}', ':', ',', '[', ']',
203
+ # :val, :str, and :space.
204
+ #
205
+ # The second element is the lexeme.
206
+ #
207
+ # The third element is the value of the
208
+ # token for :val and :str, otherwise
209
+ # it is the lexeme.
210
+ def tok(s)
211
+ case s[0]
212
+ when ?{ then
213
+ ['{', s[0, 1], s[0, 1]]
214
+ when ?} then
215
+ ['}', s[0, 1], s[0, 1]]
216
+ when ?: then
217
+ [':', s[0, 1], s[0, 1]]
218
+ when ?, then
219
+ [',', s[0, 1], s[0, 1]]
220
+ when ?[ then
221
+ ['[', s[0, 1], s[0, 1]]
222
+ when ?] then
223
+ [']', s[0, 1], s[0, 1]]
224
+ when ?n then
225
+ nulltok(s)
226
+ when ?t then
227
+ truetok(s)
228
+ when ?f then
229
+ falsetok(s)
230
+ when ?" then
231
+ strtok(s)
232
+ when Spc then
233
+ [:space, s[0, 1], s[0, 1]]
234
+ when ?\t then
235
+ [:space, s[0, 1], s[0, 1]]
236
+ when ?\n then
237
+ [:space, s[0, 1], s[0, 1]]
238
+ when ?\r then
239
+ [:space, s[0, 1], s[0, 1]]
240
+ else
241
+ numtok(s)
242
+ end
243
+ end
244
+
245
+
246
+ def nulltok(s)
247
+ ; s[0, 4] == 'null' ? [:val, 'null', nil] : []
248
+ end
249
+
250
+ def truetok(s)
251
+ ; s[0, 4] == 'true' ? [:val, 'true', true] : []
252
+ end
253
+
254
+ def falsetok(s)
255
+ ; s[0, 5] == 'false' ? [:val, 'false', false] : []
256
+ end
257
+
258
+
259
+ def numtok(s)
260
+ m = /-?([1-9][0-9]+|[0-9])([.][0-9]+)?([eE][+-]?[0-9]+)?/.match(s)
261
+ if m && m.begin(0) == 0
262
+ if m[3] && !m[2]
263
+ [:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
264
+ elsif m[2]
265
+ [:val, m[0], Float(m[0])]
266
+ else
267
+ [:val, m[0], Integer(m[0])]
268
+ end
269
+ else
270
+ []
271
+ end
272
+ end
273
+
274
+
275
+ def strtok(s)
276
+ m = /"([^"\\]|\\["\/\\bfnrt]|\\u[0-9a-fA-F]{4})*"/.match(s)
277
+ if !m
278
+ raise Error, "invalid string literal at #{abbrev(s)}"
279
+ end
280
+ [:str, m[0], unquote(m[0])]
281
+ end
282
+
283
+
284
+ def abbrev(s)
285
+ t = s[0, 10]
286
+ p = t['`']
287
+ t = t[0, p] if p
288
+ t = t + '...' if t.length < s.length
289
+ '`' + t + '`'
290
+ end
291
+
292
+
293
+ # Converts a quoted json string literal q into a UTF-8-encoded string.
294
+ # The rules are different than for Ruby, so we cannot use eval.
295
+ # Unquote will raise an error if q contains control characters.
296
+ def unquote(q)
297
+ q = q[1...-1]
298
+ a = q.dup # allocate a big enough string
299
+ rubydoesenc = false
300
+ # In ruby >= 1.9, a[w] is a codepoint, not a byte.
301
+ if a.class.method_defined?(:force_encoding)
302
+ a.force_encoding('UTF-8')
303
+ rubydoesenc = true
304
+ end
305
+ r, w = 0, 0
306
+ while r < q.length
307
+ c = q[r]
308
+ case true
309
+ when c == ?\\
310
+ r += 1
311
+ if r >= q.length
312
+ raise Error, "string literal ends with a \"\\\": \"#{q}\""
313
+ end
314
+
315
+ case q[r]
316
+ when ?", ?\\, ?/, ?'
317
+ a[w] = q[r]
318
+ r += 1
319
+ w += 1
320
+ when ?b, ?f, ?n, ?r, ?t
321
+ a[w] = Unesc[q[r]]
322
+ r += 1
323
+ w += 1
324
+ when ?u
325
+ r += 1
326
+ uchar = begin
327
+ hexdec4(q[r, 4])
328
+ rescue RuntimeError => e
329
+ raise Error, "invalid escape sequence \\u#{q[r, 4]}: #{e}"
330
+ end
331
+ r += 4
332
+ if surrogate? uchar
333
+ if q.length >= r+6
334
+ uchar1 = hexdec4(q[r+2, 4])
335
+ uchar = subst(uchar, uchar1)
336
+ if uchar != Ucharerr
337
+ # A valid pair; consume.
338
+ r += 6
339
+ end
340
+ end
341
+ end
342
+ if rubydoesenc
343
+ a[w] = '' << uchar
344
+ w += 1
345
+ else
346
+ w += ucharenc(a, w, uchar)
347
+ end
348
+ else
349
+ raise Error, "invalid escape char #{q[r]} in \"#{q}\""
350
+ end
351
+ when c == ?", c < Spc
352
+ raise Error, "invalid character in string literal \"#{q}\""
353
+ else
354
+ # Copy anything else byte-for-byte.
355
+ # Valid UTF-8 will remain valid UTF-8.
356
+ # Invalid UTF-8 will remain invalid UTF-8.
357
+ # In ruby >= 1.9, c is a codepoint, not a byte,
358
+ # in which case this is still what we want.
359
+ a[w] = c
360
+ r += 1
361
+ w += 1
362
+ end
363
+ end
364
+ a[0, w]
365
+ end
366
+
367
+
368
+ # Encodes unicode character u as UTF-8
369
+ # bytes in string a at position i.
370
+ # Returns the number of bytes written.
371
+ def ucharenc(a, i, u)
372
+ case true
373
+ when u <= Uchar1max
374
+ a[i] = (u & 0xff).chr
375
+ 1
376
+ when u <= Uchar2max
377
+ a[i+0] = (Utag2 | ((u>>6)&0xff)).chr
378
+ a[i+1] = (Utagx | (u&Umaskx)).chr
379
+ 2
380
+ when u <= Uchar3max
381
+ a[i+0] = (Utag3 | ((u>>12)&0xff)).chr
382
+ a[i+1] = (Utagx | ((u>>6)&Umaskx)).chr
383
+ a[i+2] = (Utagx | (u&Umaskx)).chr
384
+ 3
385
+ else
386
+ a[i+0] = (Utag4 | ((u>>18)&0xff)).chr
387
+ a[i+1] = (Utagx | ((u>>12)&Umaskx)).chr
388
+ a[i+2] = (Utagx | ((u>>6)&Umaskx)).chr
389
+ a[i+3] = (Utagx | (u&Umaskx)).chr
390
+ 4
391
+ end
392
+ end
393
+
394
+
395
+ def hexdec4(s)
396
+ if s.length != 4
397
+ raise Error, 'short'
398
+ end
399
+ (nibble(s[0])<<12) | (nibble(s[1])<<8) | (nibble(s[2])<<4) | nibble(s[3])
400
+ end
401
+
402
+
403
+ def subst(u1, u2)
404
+ if Usurr1 <= u1 && u1 < Usurr2 && Usurr2 <= u2 && u2 < Usurr3
405
+ return ((u1-Usurr1)<<10) | (u2-Usurr2) + Usurrself
406
+ end
407
+ return Ucharerr
408
+ end
409
+
410
+
411
+ def surrogate?(u)
412
+ Usurr1 <= u && u < Usurr3
413
+ end
414
+
415
+
416
+ def nibble(c)
417
+ case true
418
+ when ?0 <= c && c <= ?9 then
419
+ c.ord - ?0.ord
420
+ when ?a <= c && c <= ?z then
421
+ c.ord - ?a.ord + 10
422
+ when ?A <= c && c <= ?Z then
423
+ c.ord - ?A.ord + 10
424
+ else
425
+ raise Error, "invalid hex code #{c}"
426
+ end
427
+ end
428
+
429
+
430
+ # Encodes x into a json text. It may contain only
431
+ # Array, Hash, String, Numeric, true, false, nil.
432
+ # (Note, this list excludes Symbol.)
433
+ # X itself must be an Array or a Hash.
434
+ # No other value can be encoded, and an error will
435
+ # be raised if x contains any other value, such as
436
+ # Nan, Infinity, Symbol, and Proc, or if a Hash key
437
+ # is not a String.
438
+ # Strings contained in x must be valid UTF-8.
439
+ def encode(x)
440
+ case x
441
+ when Hash then
442
+ objenc(x)
443
+ when Array then
444
+ arrenc(x)
445
+ else
446
+ raise Error, 'root value must be an Array or a Hash'
447
+ end
448
+ end
449
+
450
+
451
+ def valenc(x)
452
+ case x
453
+ when Hash then
454
+ objenc(x)
455
+ when Array then
456
+ arrenc(x)
457
+ when String then
458
+ strenc(x)
459
+ when Numeric then
460
+ numenc(x)
461
+ when true then
462
+ "true"
463
+ when false then
464
+ "false"
465
+ when nil then
466
+ "null"
467
+ else
468
+ raise Error, "cannot encode #{x.class}: #{x.inspect}"
469
+ end
470
+ end
471
+
472
+
473
+ def objenc(x)
474
+ '{' + x.map { |k, v| keyenc(k) + ':' + valenc(v) }.join(',') + '}'
475
+ end
476
+
477
+
478
+ def arrenc(a)
479
+ '[' + a.map { |x| valenc(x) }.join(',') + ']'
480
+ end
481
+
482
+
483
+ def keyenc(k)
484
+ case k
485
+ when String then
486
+ strenc(k)
487
+ else
488
+ raise Error, "Hash key is not a string: #{k.inspect}"
489
+ end
490
+ end
491
+
492
+
493
+ def strenc(s)
494
+ t = StringIO.new
495
+ t.putc(?")
496
+ r = 0
497
+
498
+ # In ruby >= 1.9, s[r] is a codepoint, not a byte.
499
+ rubydoesenc = s.class.method_defined?(:encoding)
500
+
501
+ while r < s.length
502
+ case s[r]
503
+ when ?" then
504
+ t.print('\\"')
505
+ when ?\\ then
506
+ t.print('\\\\')
507
+ when ?\b then
508
+ t.print('\\b')
509
+ when ?\f then
510
+ t.print('\\f')
511
+ when ?\n then
512
+ t.print('\\n')
513
+ when ?\r then
514
+ t.print('\\r')
515
+ when ?\t then
516
+ t.print('\\t')
517
+ else
518
+ c = s[r]
519
+ case true
520
+ when rubydoesenc
521
+ begin
522
+ c.ord # will raise an error if c is invalid UTF-8
523
+ t.write(c)
524
+ rescue
525
+ t.write(Ustrerr)
526
+ end
527
+ when Spc <= c && c <= ?~
528
+ t.putc(c)
529
+ else
530
+ n = ucharcopy(t, s, r) # ensure valid UTF-8 output
531
+ r += n - 1 # r is incremented below
532
+ end
533
+ end
534
+ r += 1
535
+ end
536
+ t.putc(?")
537
+ t.string
538
+ end
539
+
540
+
541
+ def numenc(x)
542
+ if ((x.nan? || x.infinite?) rescue false)
543
+ raise Error, "Numeric cannot be represented: #{x}"
544
+ end
545
+ "#{x}"
546
+ end
547
+
548
+
549
+ # Copies the valid UTF-8 bytes of a single character
550
+ # from string s at position i to I/O object t, and
551
+ # returns the number of bytes copied.
552
+ # If no valid UTF-8 char exists at position i,
553
+ # ucharcopy writes Ustrerr and returns 1.
554
+ def ucharcopy(t, s, i)
555
+ n = s.length - i
556
+ raise Utf8Error if n < 1
557
+
558
+ c0 = s[i].ord
559
+
560
+ # 1-byte, 7-bit sequence?
561
+ if c0 < Utagx
562
+ t.putc(c0)
563
+ return 1
564
+ end
565
+
566
+ raise Utf8Error if c0 < Utag2 # unexpected continuation byte?
567
+
568
+ raise Utf8Error if n < 2 # need continuation byte
569
+ c1 = s[i+1].ord
570
+ raise Utf8Error if c1 < Utagx || Utag2 <= c1
571
+
572
+ # 2-byte, 11-bit sequence?
573
+ if c0 < Utag3
574
+ raise Utf8Error if ((c0&Umask2)<<6 | (c1&Umaskx)) <= Uchar1max
575
+ t.putc(c0)
576
+ t.putc(c1)
577
+ return 2
578
+ end
579
+
580
+ # need second continuation byte
581
+ raise Utf8Error if n < 3
582
+
583
+ c2 = s[i+2].ord
584
+ raise Utf8Error if c2 < Utagx || Utag2 <= c2
585
+
586
+ # 3-byte, 16-bit sequence?
587
+ if c0 < Utag4
588
+ u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
589
+ raise Utf8Error if u <= Uchar2max
590
+ t.putc(c0)
591
+ t.putc(c1)
592
+ t.putc(c2)
593
+ return 3
594
+ end
595
+
596
+ # need third continuation byte
597
+ raise Utf8Error if n < 4
598
+ c3 = s[i+3].ord
599
+ raise Utf8Error if c3 < Utagx || Utag2 <= c3
600
+
601
+ # 4-byte, 21-bit sequence?
602
+ if c0 < Utag5
603
+ u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
604
+ raise Utf8Error if u <= Uchar3max
605
+ t.putc(c0)
606
+ t.putc(c1)
607
+ t.putc(c2)
608
+ t.putc(c3)
609
+ return 4
610
+ end
611
+
612
+ raise Utf8Error
613
+ rescue Utf8Error
614
+ t.write(Ustrerr)
615
+ return 1
616
+ end
617
+
618
+
619
+ class Utf8Error < ::StandardError
620
+ end
621
+
622
+
623
+ class Error < ::StandardError
624
+ end
625
+
626
+
627
+ Utagx = 0x80 # 1000 0000
628
+ Utag2 = 0xc0 # 1100 0000
629
+ Utag3 = 0xe0 # 1110 0000
630
+ Utag4 = 0xf0 # 1111 0000
631
+ Utag5 = 0xF8 # 1111 1000
632
+ Umaskx = 0x3f # 0011 1111
633
+ Umask2 = 0x1f # 0001 1111
634
+ Umask3 = 0x0f # 0000 1111
635
+ Umask4 = 0x07 # 0000 0111
636
+ Uchar1max = (1<<7) - 1
637
+ Uchar2max = (1<<11) - 1
638
+ Uchar3max = (1<<16) - 1
639
+ Ucharerr = 0xFFFD # unicode "replacement char"
640
+ Ustrerr = "\xef\xbf\xbd" # unicode "replacement char"
641
+ Usurrself = 0x10000
642
+ Usurr1 = 0xd800
643
+ Usurr2 = 0xdc00
644
+ Usurr3 = 0xe000
645
+
646
+ Spc = ' '[0]
647
+ Unesc = {?b => ?\b, ?f => ?\f, ?n => ?\n, ?r => ?\r, ?t => ?\t}
648
+ end
649
+ end
650
+ end
651
+ end