wd_newrelic_rpm 3.5.6 → 3.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/CHANGELOG +43 -3
  2. data/Gemfile +6 -2
  3. data/LICENSE +23 -0
  4. data/lib/new_relic/agent.rb +50 -3
  5. data/lib/new_relic/agent/agent.rb +40 -60
  6. data/lib/new_relic/agent/configuration/defaults.rb +9 -3
  7. data/lib/new_relic/agent/configuration/server_source.rb +4 -0
  8. data/lib/new_relic/agent/cross_app_monitor.rb +239 -0
  9. data/lib/new_relic/agent/cross_app_tracing.rb +281 -0
  10. data/lib/new_relic/agent/database.rb +28 -10
  11. data/lib/new_relic/agent/error_collector.rb +5 -0
  12. data/lib/new_relic/agent/event_listener.rb +4 -0
  13. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +58 -39
  14. data/lib/new_relic/agent/instrumentation/metric_frame.rb +16 -3
  15. data/lib/new_relic/agent/instrumentation/net.rb +13 -11
  16. data/lib/new_relic/agent/instrumentation/queue_time.rb +50 -192
  17. data/lib/new_relic/agent/instrumentation/rails4/action_controller.rb +145 -0
  18. data/lib/new_relic/agent/instrumentation/rails4/errors.rb +45 -0
  19. data/lib/new_relic/agent/instrumentation/resque.rb +10 -10
  20. data/lib/new_relic/agent/instrumentation/sinatra.rb +19 -9
  21. data/lib/new_relic/agent/new_relic_service.rb +63 -9
  22. data/lib/new_relic/agent/pipe_service.rb +8 -12
  23. data/lib/new_relic/agent/rules_engine.rb +72 -0
  24. data/lib/new_relic/agent/shim_agent.rb +0 -1
  25. data/lib/new_relic/agent/sql_sampler.rb +3 -2
  26. data/lib/new_relic/agent/stats.rb +149 -0
  27. data/lib/new_relic/agent/stats_engine.rb +9 -0
  28. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +1 -24
  29. data/lib/new_relic/agent/stats_engine/metric_stats.rb +84 -185
  30. data/lib/new_relic/agent/stats_engine/stats_hash.rb +58 -0
  31. data/lib/new_relic/agent/stats_engine/transactions.rb +10 -2
  32. data/lib/new_relic/agent/transaction_info.rb +31 -6
  33. data/lib/new_relic/agent/transaction_sample_builder.rb +19 -8
  34. data/lib/new_relic/agent/transaction_sampler.rb +17 -10
  35. data/lib/new_relic/helper.rb +32 -0
  36. data/lib/new_relic/local_environment.rb +24 -32
  37. data/lib/new_relic/okjson.rb +599 -0
  38. data/lib/new_relic/transaction_sample.rb +2 -1
  39. data/lib/new_relic/transaction_sample/segment.rb +2 -1
  40. data/lib/new_relic/version.rb +1 -1
  41. data/newrelic.yml +27 -41
  42. data/test/multiverse/suites/agent_only/Envfile +5 -1
  43. data/test/multiverse/suites/agent_only/audit_log_test.rb +2 -4
  44. data/test/multiverse/suites/agent_only/config/newrelic.yml +1 -2
  45. data/test/multiverse/suites/agent_only/{cross_process_test.rb → cross_application_tracing_test.rb} +3 -3
  46. data/test/multiverse/suites/agent_only/key_transactions_test.rb +66 -0
  47. data/test/multiverse/suites/agent_only/marshaling_test.rb +9 -22
  48. data/test/multiverse/suites/agent_only/rename_rule_test.rb +57 -0
  49. data/test/multiverse/suites/agent_only/start_up_test.rb +1 -1
  50. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +17 -6
  51. data/test/multiverse/suites/rails/error_tracing_test.rb +20 -8
  52. data/test/multiverse/suites/rails/queue_time_test.rb +2 -2
  53. data/test/multiverse/suites/resque/instrumentation_test.rb +4 -3
  54. data/test/multiverse/suites/sinatra/Envfile +2 -0
  55. data/test/multiverse/suites/sinatra/config/newrelic.yml +1 -0
  56. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +5 -5
  57. data/test/multiverse/suites/sinatra/sinatra_test.rb +77 -10
  58. data/test/new_relic/agent/agent/connect_test.rb +45 -1
  59. data/test/new_relic/agent/agent/start_worker_thread_test.rb +0 -3
  60. data/test/new_relic/agent/agent_test.rb +20 -40
  61. data/test/new_relic/agent/agent_test_controller_test.rb +27 -60
  62. data/test/new_relic/agent/busy_calculator_test.rb +1 -1
  63. data/test/new_relic/agent/configuration/server_source_test.rb +8 -3
  64. data/test/new_relic/agent/cross_app_monitor_test.rb +237 -0
  65. data/test/new_relic/agent/database_test.rb +60 -16
  66. data/test/new_relic/agent/error_collector_test.rb +28 -4
  67. data/test/new_relic/agent/event_listener_test.rb +23 -2
  68. data/test/new_relic/agent/instrumentation/browser_monitoring_timings_test.rb +1 -1
  69. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +85 -12
  70. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +95 -0
  71. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +436 -59
  72. data/test/new_relic/agent/instrumentation/queue_time_test.rb +58 -357
  73. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -5
  74. data/test/new_relic/agent/method_tracer_test.rb +4 -2
  75. data/test/new_relic/agent/new_relic_service_test.rb +108 -6
  76. data/test/new_relic/agent/pipe_channel_manager_test.rb +1 -1
  77. data/test/new_relic/agent/pipe_service_test.rb +9 -9
  78. data/test/new_relic/agent/rpm_agent_test.rb +0 -11
  79. data/test/new_relic/agent/rules_engine_test.rb +82 -0
  80. data/test/new_relic/agent/shim_agent_test.rb +0 -4
  81. data/test/new_relic/agent/sql_sampler_test.rb +7 -0
  82. data/test/new_relic/agent/stats_engine/gc_profiler_test.rb +85 -0
  83. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +110 -23
  84. data/test/new_relic/agent/stats_engine_test.rb +1 -46
  85. data/test/new_relic/agent/stats_hash_test.rb +93 -0
  86. data/test/new_relic/agent/stats_test.rb +197 -0
  87. data/test/new_relic/agent/transaction_info_test.rb +63 -11
  88. data/test/new_relic/agent/transaction_sample_builder_test.rb +10 -3
  89. data/test/new_relic/agent/transaction_sampler_test.rb +92 -80
  90. data/test/new_relic/agent/worker_loop_test.rb +1 -1
  91. data/test/new_relic/agent_test.rb +35 -5
  92. data/test/new_relic/control_test.rb +1 -1
  93. data/test/new_relic/fake_collector.rb +87 -9
  94. data/test/new_relic/helper_test.rb +24 -0
  95. data/test/new_relic/metric_data_test.rb +11 -11
  96. data/test/new_relic/metric_spec_test.rb +1 -1
  97. data/test/script/ci.sh +1 -1
  98. data/test/test_contexts.rb +0 -1
  99. data/test/test_helper.rb +21 -3
  100. metadata +34 -41
  101. data/lib/new_relic/agent/cross_process_monitoring.rb +0 -187
  102. data/lib/new_relic/stats.rb +0 -337
  103. data/test/new_relic/agent/cross_process_monitoring_test.rb +0 -190
  104. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +0 -133
  105. data/test/new_relic/fakes_sending_data.rb +0 -30
  106. data/test/new_relic/stats_test.rb +0 -421
@@ -0,0 +1,599 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2011, 2012 Keith Rarick
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ # See https://github.com/kr/okjson for updates.
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 NewRelic
31
+ module OkJson
32
+ Upstream = 'LTD7LBKLZWFF7OZK'
33
+ extend self
34
+
35
+
36
+ # Decodes a json document in string s and
37
+ # returns the corresponding ruby value.
38
+ # String s must be valid UTF-8. If you have
39
+ # a string in some other encoding, convert
40
+ # it first.
41
+ #
42
+ # String values in the resulting structure
43
+ # will be UTF-8.
44
+ def decode(s)
45
+ ts = lex(s)
46
+ v, ts = textparse(ts)
47
+ if ts.length > 0
48
+ raise Error, 'trailing garbage'
49
+ end
50
+ v
51
+ end
52
+
53
+
54
+ # Parses a "json text" in the sense of RFC 4627.
55
+ # Returns the parsed value and any trailing tokens.
56
+ # Note: this is almost the same as valparse,
57
+ # except that it does not accept atomic values.
58
+ def textparse(ts)
59
+ if ts.length < 0
60
+ raise Error, 'empty'
61
+ end
62
+
63
+ typ, _, val = ts[0]
64
+ case typ
65
+ when '{' then objparse(ts)
66
+ when '[' then arrparse(ts)
67
+ else
68
+ raise Error, "unexpected #{val.inspect}"
69
+ end
70
+ end
71
+
72
+
73
+ # Parses a "value" in the sense of RFC 4627.
74
+ # Returns the parsed value and any trailing tokens.
75
+ def valparse(ts)
76
+ if ts.length < 0
77
+ raise Error, 'empty'
78
+ end
79
+
80
+ typ, _, val = ts[0]
81
+ case typ
82
+ when '{' then objparse(ts)
83
+ when '[' then arrparse(ts)
84
+ when :val,:str then [val, ts[1..-1]]
85
+ else
86
+ raise Error, "unexpected #{val.inspect}"
87
+ end
88
+ end
89
+
90
+
91
+ # Parses an "object" in the sense of RFC 4627.
92
+ # Returns the parsed value and any trailing tokens.
93
+ def objparse(ts)
94
+ ts = eat('{', ts)
95
+ obj = {}
96
+
97
+ if ts[0][0] == '}'
98
+ return obj, ts[1..-1]
99
+ end
100
+
101
+ k, v, ts = pairparse(ts)
102
+ obj[k] = v
103
+
104
+ if ts[0][0] == '}'
105
+ return obj, ts[1..-1]
106
+ end
107
+
108
+ loop do
109
+ ts = eat(',', ts)
110
+
111
+ k, v, ts = pairparse(ts)
112
+ obj[k] = v
113
+
114
+ if ts[0][0] == '}'
115
+ return obj, ts[1..-1]
116
+ end
117
+ end
118
+ end
119
+
120
+
121
+ # Parses a "member" in the sense of RFC 4627.
122
+ # Returns the parsed values and any trailing tokens.
123
+ def pairparse(ts)
124
+ (typ, _, k), ts = ts[0], ts[1..-1]
125
+ if typ != :str
126
+ raise Error, "unexpected #{k.inspect}"
127
+ end
128
+ ts = eat(':', ts)
129
+ v, ts = valparse(ts)
130
+ [k, v, ts]
131
+ end
132
+
133
+
134
+ # Parses an "array" in the sense of RFC 4627.
135
+ # Returns the parsed value and any trailing tokens.
136
+ def arrparse(ts)
137
+ ts = eat('[', ts)
138
+ arr = []
139
+
140
+ if ts[0][0] == ']'
141
+ return arr, ts[1..-1]
142
+ end
143
+
144
+ v, ts = valparse(ts)
145
+ arr << v
146
+
147
+ if ts[0][0] == ']'
148
+ return arr, ts[1..-1]
149
+ end
150
+
151
+ loop do
152
+ ts = eat(',', ts)
153
+
154
+ v, ts = valparse(ts)
155
+ arr << v
156
+
157
+ if ts[0][0] == ']'
158
+ return arr, ts[1..-1]
159
+ end
160
+ end
161
+ end
162
+
163
+
164
+ def eat(typ, ts)
165
+ if ts[0][0] != typ
166
+ raise Error, "expected #{typ} (got #{ts[0].inspect})"
167
+ end
168
+ ts[1..-1]
169
+ end
170
+
171
+
172
+ # Scans s and returns a list of json tokens,
173
+ # excluding white space (as defined in RFC 4627).
174
+ def lex(s)
175
+ ts = []
176
+ while s.length > 0
177
+ typ, lexeme, val = tok(s)
178
+ if typ == nil
179
+ raise Error, "invalid character at #{s[0,10].inspect}"
180
+ end
181
+ if typ != :space
182
+ ts << [typ, lexeme, val]
183
+ end
184
+ s = s[lexeme.length..-1]
185
+ end
186
+ ts
187
+ end
188
+
189
+
190
+ # Scans the first token in s and
191
+ # returns a 3-element list, or nil
192
+ # if s does not begin with a valid token.
193
+ #
194
+ # The first list element is one of
195
+ # '{', '}', ':', ',', '[', ']',
196
+ # :val, :str, and :space.
197
+ #
198
+ # The second element is the lexeme.
199
+ #
200
+ # The third element is the value of the
201
+ # token for :val and :str, otherwise
202
+ # it is the lexeme.
203
+ def tok(s)
204
+ case s[0]
205
+ when ?{ then ['{', s[0,1], s[0,1]]
206
+ when ?} then ['}', s[0,1], s[0,1]]
207
+ when ?: then [':', s[0,1], s[0,1]]
208
+ when ?, then [',', s[0,1], s[0,1]]
209
+ when ?[ then ['[', s[0,1], s[0,1]]
210
+ when ?] then [']', s[0,1], s[0,1]]
211
+ when ?n then nulltok(s)
212
+ when ?t then truetok(s)
213
+ when ?f then falsetok(s)
214
+ when ?" then strtok(s)
215
+ when Spc then [:space, s[0,1], s[0,1]]
216
+ when ?\t then [:space, s[0,1], s[0,1]]
217
+ when ?\n then [:space, s[0,1], s[0,1]]
218
+ when ?\r then [:space, s[0,1], s[0,1]]
219
+ else numtok(s)
220
+ end
221
+ end
222
+
223
+
224
+ def nulltok(s); s[0,4] == 'null' ? [:val, 'null', nil] : [] end
225
+ def truetok(s); s[0,4] == 'true' ? [:val, 'true', true] : [] end
226
+ def falsetok(s); s[0,5] == 'false' ? [:val, 'false', false] : [] end
227
+
228
+
229
+ def numtok(s)
230
+ m = /-?([1-9][0-9]+|[0-9])([.][0-9]+)?([eE][+-]?[0-9]+)?/.match(s)
231
+ if m && m.begin(0) == 0
232
+ if m[3] && !m[2]
233
+ [:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
234
+ elsif m[2]
235
+ [:val, m[0], Float(m[0])]
236
+ else
237
+ [:val, m[0], Integer(m[0])]
238
+ end
239
+ else
240
+ []
241
+ end
242
+ end
243
+
244
+
245
+ def strtok(s)
246
+ m = /"([^"\\]|\\["\/\\bfnrt]|\\u[0-9a-fA-F]{4})*"/.match(s)
247
+ if ! m
248
+ raise Error, "invalid string literal at #{abbrev(s)}"
249
+ end
250
+ [:str, m[0], unquote(m[0])]
251
+ end
252
+
253
+
254
+ def abbrev(s)
255
+ t = s[0,10]
256
+ p = t['`']
257
+ t = t[0,p] if p
258
+ t = t + '...' if t.length < s.length
259
+ '`' + t + '`'
260
+ end
261
+
262
+
263
+ # Converts a quoted json string literal q into a UTF-8-encoded string.
264
+ # The rules are different than for Ruby, so we cannot use eval.
265
+ # Unquote will raise an error if q contains control characters.
266
+ def unquote(q)
267
+ q = q[1...-1]
268
+ a = q.dup # allocate a big enough string
269
+ rubydoesenc = false
270
+ # In ruby >= 1.9, a[w] is a codepoint, not a byte.
271
+ if a.class.method_defined?(:force_encoding)
272
+ a.force_encoding('UTF-8')
273
+ rubydoesenc = true
274
+ end
275
+ r, w = 0, 0
276
+ while r < q.length
277
+ c = q[r]
278
+ case true
279
+ when c == ?\\
280
+ r += 1
281
+ if r >= q.length
282
+ raise Error, "string literal ends with a \"\\\": \"#{q}\""
283
+ end
284
+
285
+ case q[r]
286
+ when ?",?\\,?/,?'
287
+ a[w] = q[r]
288
+ r += 1
289
+ w += 1
290
+ when ?b,?f,?n,?r,?t
291
+ a[w] = Unesc[q[r]]
292
+ r += 1
293
+ w += 1
294
+ when ?u
295
+ r += 1
296
+ uchar = begin
297
+ hexdec4(q[r,4])
298
+ rescue RuntimeError => e
299
+ raise Error, "invalid escape sequence \\u#{q[r,4]}: #{e}"
300
+ end
301
+ r += 4
302
+ if surrogate? uchar
303
+ if q.length >= r+6
304
+ uchar1 = hexdec4(q[r+2,4])
305
+ uchar = subst(uchar, uchar1)
306
+ if uchar != Ucharerr
307
+ # A valid pair; consume.
308
+ r += 6
309
+ end
310
+ end
311
+ end
312
+ if rubydoesenc
313
+ a[w] = '' << uchar
314
+ w += 1
315
+ else
316
+ w += ucharenc(a, w, uchar)
317
+ end
318
+ else
319
+ raise Error, "invalid escape char #{q[r]} in \"#{q}\""
320
+ end
321
+ when c == ?", c < Spc
322
+ raise Error, "invalid character in string literal \"#{q}\""
323
+ else
324
+ # Copy anything else byte-for-byte.
325
+ # Valid UTF-8 will remain valid UTF-8.
326
+ # Invalid UTF-8 will remain invalid UTF-8.
327
+ # In ruby >= 1.9, c is a codepoint, not a byte,
328
+ # in which case this is still what we want.
329
+ a[w] = c
330
+ r += 1
331
+ w += 1
332
+ end
333
+ end
334
+ a[0,w]
335
+ end
336
+
337
+
338
+ # Encodes unicode character u as UTF-8
339
+ # bytes in string a at position i.
340
+ # Returns the number of bytes written.
341
+ def ucharenc(a, i, u)
342
+ case true
343
+ when u <= Uchar1max
344
+ a[i] = (u & 0xff).chr
345
+ 1
346
+ when u <= Uchar2max
347
+ a[i+0] = (Utag2 | ((u>>6)&0xff)).chr
348
+ a[i+1] = (Utagx | (u&Umaskx)).chr
349
+ 2
350
+ when u <= Uchar3max
351
+ a[i+0] = (Utag3 | ((u>>12)&0xff)).chr
352
+ a[i+1] = (Utagx | ((u>>6)&Umaskx)).chr
353
+ a[i+2] = (Utagx | (u&Umaskx)).chr
354
+ 3
355
+ else
356
+ a[i+0] = (Utag4 | ((u>>18)&0xff)).chr
357
+ a[i+1] = (Utagx | ((u>>12)&Umaskx)).chr
358
+ a[i+2] = (Utagx | ((u>>6)&Umaskx)).chr
359
+ a[i+3] = (Utagx | (u&Umaskx)).chr
360
+ 4
361
+ end
362
+ end
363
+
364
+
365
+ def hexdec4(s)
366
+ if s.length != 4
367
+ raise Error, 'short'
368
+ end
369
+ (nibble(s[0])<<12) | (nibble(s[1])<<8) | (nibble(s[2])<<4) | nibble(s[3])
370
+ end
371
+
372
+
373
+ def subst(u1, u2)
374
+ if Usurr1 <= u1 && u1 < Usurr2 && Usurr2 <= u2 && u2 < Usurr3
375
+ return ((u1-Usurr1)<<10) | (u2-Usurr2) + Usurrself
376
+ end
377
+ return Ucharerr
378
+ end
379
+
380
+
381
+ def surrogate?(u)
382
+ Usurr1 <= u && u < Usurr3
383
+ end
384
+
385
+
386
+ def nibble(c)
387
+ case true
388
+ when ?0 <= c && c <= ?9 then c.ord - ?0.ord
389
+ when ?a <= c && c <= ?z then c.ord - ?a.ord + 10
390
+ when ?A <= c && c <= ?Z then c.ord - ?A.ord + 10
391
+ else
392
+ raise Error, "invalid hex code #{c}"
393
+ end
394
+ end
395
+
396
+
397
+ # Encodes x into a json text. It may contain only
398
+ # Array, Hash, String, Numeric, true, false, nil.
399
+ # (Note, this list excludes Symbol.)
400
+ # X itself must be an Array or a Hash.
401
+ # No other value can be encoded, and an error will
402
+ # be raised if x contains any other value, such as
403
+ # Nan, Infinity, Symbol, and Proc, or if a Hash key
404
+ # is not a String.
405
+ # Strings contained in x must be valid UTF-8.
406
+ def encode(x)
407
+ case x
408
+ when Hash then objenc(x)
409
+ when Array then arrenc(x)
410
+ else
411
+ raise Error, 'root value must be an Array or a Hash'
412
+ end
413
+ end
414
+
415
+
416
+ def valenc(x)
417
+ case x
418
+ when Hash then objenc(x)
419
+ when Array then arrenc(x)
420
+ when String then strenc(x)
421
+ when Numeric then numenc(x)
422
+ when true then "true"
423
+ when false then "false"
424
+ when nil then "null"
425
+ else
426
+ raise Error, "cannot encode #{x.class}: #{x.inspect}"
427
+ end
428
+ end
429
+
430
+
431
+ def objenc(x)
432
+ '{' + x.map{|k,v| keyenc(k) + ':' + valenc(v)}.join(',') + '}'
433
+ end
434
+
435
+
436
+ def arrenc(a)
437
+ '[' + a.map{|x| valenc(x)}.join(',') + ']'
438
+ end
439
+
440
+
441
+ def keyenc(k)
442
+ case k
443
+ when String then strenc(k)
444
+ else
445
+ raise Error, "Hash key is not a string: #{k.inspect}"
446
+ end
447
+ end
448
+
449
+
450
+ def strenc(s)
451
+ t = StringIO.new
452
+ t.putc(?")
453
+ r = 0
454
+
455
+ # In ruby >= 1.9, s[r] is a codepoint, not a byte.
456
+ rubydoesenc = s.class.method_defined?(:encoding)
457
+
458
+ while r < s.length
459
+ case s[r]
460
+ when ?" then t.print('\\"')
461
+ when ?\\ then t.print('\\\\')
462
+ when ?\b then t.print('\\b')
463
+ when ?\f then t.print('\\f')
464
+ when ?\n then t.print('\\n')
465
+ when ?\r then t.print('\\r')
466
+ when ?\t then t.print('\\t')
467
+ else
468
+ c = s[r]
469
+ case true
470
+ when rubydoesenc
471
+ begin
472
+ c.ord # will raise an error if c is invalid UTF-8
473
+ t.write(c)
474
+ rescue
475
+ t.write(Ustrerr)
476
+ end
477
+ when Spc <= c && c <= ?~
478
+ t.putc(c)
479
+ else
480
+ n = ucharcopy(t, s, r) # ensure valid UTF-8 output
481
+ r += n - 1 # r is incremented below
482
+ end
483
+ end
484
+ r += 1
485
+ end
486
+ t.putc(?")
487
+ t.string
488
+ end
489
+
490
+
491
+ def numenc(x)
492
+ if ((x.nan? || x.infinite?) rescue false)
493
+ raise Error, "Numeric cannot be represented: #{x}"
494
+ end
495
+ "#{x}"
496
+ end
497
+
498
+
499
+ # Copies the valid UTF-8 bytes of a single character
500
+ # from string s at position i to I/O object t, and
501
+ # returns the number of bytes copied.
502
+ # If no valid UTF-8 char exists at position i,
503
+ # ucharcopy writes Ustrerr and returns 1.
504
+ def ucharcopy(t, s, i)
505
+ n = s.length - i
506
+ raise Utf8Error if n < 1
507
+
508
+ c0 = s[i].ord
509
+
510
+ # 1-byte, 7-bit sequence?
511
+ if c0 < Utagx
512
+ t.putc(c0)
513
+ return 1
514
+ end
515
+
516
+ raise Utf8Error if c0 < Utag2 # unexpected continuation byte?
517
+
518
+ raise Utf8Error if n < 2 # need continuation byte
519
+ c1 = s[i+1].ord
520
+ raise Utf8Error if c1 < Utagx || Utag2 <= c1
521
+
522
+ # 2-byte, 11-bit sequence?
523
+ if c0 < Utag3
524
+ raise Utf8Error if ((c0&Umask2)<<6 | (c1&Umaskx)) <= Uchar1max
525
+ t.putc(c0)
526
+ t.putc(c1)
527
+ return 2
528
+ end
529
+
530
+ # need second continuation byte
531
+ raise Utf8Error if n < 3
532
+
533
+ c2 = s[i+2].ord
534
+ raise Utf8Error if c2 < Utagx || Utag2 <= c2
535
+
536
+ # 3-byte, 16-bit sequence?
537
+ if c0 < Utag4
538
+ u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
539
+ raise Utf8Error if u <= Uchar2max
540
+ t.putc(c0)
541
+ t.putc(c1)
542
+ t.putc(c2)
543
+ return 3
544
+ end
545
+
546
+ # need third continuation byte
547
+ raise Utf8Error if n < 4
548
+ c3 = s[i+3].ord
549
+ raise Utf8Error if c3 < Utagx || Utag2 <= c3
550
+
551
+ # 4-byte, 21-bit sequence?
552
+ if c0 < Utag5
553
+ u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
554
+ raise Utf8Error if u <= Uchar3max
555
+ t.putc(c0)
556
+ t.putc(c1)
557
+ t.putc(c2)
558
+ t.putc(c3)
559
+ return 4
560
+ end
561
+
562
+ raise Utf8Error
563
+ rescue Utf8Error
564
+ t.write(Ustrerr)
565
+ return 1
566
+ end
567
+
568
+
569
+ class Utf8Error < ::StandardError
570
+ end
571
+
572
+
573
+ class Error < ::StandardError
574
+ end
575
+
576
+
577
+ Utagx = 0x80 # 1000 0000
578
+ Utag2 = 0xc0 # 1100 0000
579
+ Utag3 = 0xe0 # 1110 0000
580
+ Utag4 = 0xf0 # 1111 0000
581
+ Utag5 = 0xF8 # 1111 1000
582
+ Umaskx = 0x3f # 0011 1111
583
+ Umask2 = 0x1f # 0001 1111
584
+ Umask3 = 0x0f # 0000 1111
585
+ Umask4 = 0x07 # 0000 0111
586
+ Uchar1max = (1<<7) - 1
587
+ Uchar2max = (1<<11) - 1
588
+ Uchar3max = (1<<16) - 1
589
+ Ucharerr = 0xFFFD # unicode "replacement char"
590
+ Ustrerr = "\xef\xbf\xbd" # unicode "replacement char"
591
+ Usurrself = 0x10000
592
+ Usurr1 = 0xd800
593
+ Usurr2 = 0xdc00
594
+ Usurr3 = 0xe000
595
+
596
+ Spc = ' '[0]
597
+ Unesc = {?b=>?\b, ?f=>?\f, ?n=>?\n, ?r=>?\r, ?t=>?\t}
598
+ end
599
+ end