cf-runtime 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cfruntime/okjson.rb +596 -0
- data/lib/cfruntime/properties.rb +4 -4
- data/lib/cfruntime/version.rb +1 -1
- metadata +162 -175
@@ -0,0 +1,596 @@
|
|
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 CFRuntime::OkJson
|
31
|
+
extend self
|
32
|
+
|
33
|
+
|
34
|
+
# Decodes a json document in string s and
|
35
|
+
# returns the corresponding ruby value.
|
36
|
+
# String s must be valid UTF-8. If you have
|
37
|
+
# a string in some other encoding, convert
|
38
|
+
# it first.
|
39
|
+
#
|
40
|
+
# String values in the resulting structure
|
41
|
+
# will be UTF-8.
|
42
|
+
def decode(s)
|
43
|
+
ts = lex(s)
|
44
|
+
v, ts = textparse(ts)
|
45
|
+
if ts.length > 0
|
46
|
+
raise Error, 'trailing garbage'
|
47
|
+
end
|
48
|
+
v
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
# Parses a "json text" in the sense of RFC 4627.
|
53
|
+
# Returns the parsed value and any trailing tokens.
|
54
|
+
# Note: this is almost the same as valparse,
|
55
|
+
# except that it does not accept atomic values.
|
56
|
+
def textparse(ts)
|
57
|
+
if ts.length < 0
|
58
|
+
raise Error, 'empty'
|
59
|
+
end
|
60
|
+
|
61
|
+
typ, _, val = ts[0]
|
62
|
+
case typ
|
63
|
+
when '{' then objparse(ts)
|
64
|
+
when '[' then arrparse(ts)
|
65
|
+
else
|
66
|
+
raise Error, "unexpected #{val.inspect}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
# Parses a "value" in the sense of RFC 4627.
|
72
|
+
# Returns the parsed value and any trailing tokens.
|
73
|
+
def valparse(ts)
|
74
|
+
if ts.length < 0
|
75
|
+
raise Error, 'empty'
|
76
|
+
end
|
77
|
+
|
78
|
+
typ, _, val = ts[0]
|
79
|
+
case typ
|
80
|
+
when '{' then objparse(ts)
|
81
|
+
when '[' then arrparse(ts)
|
82
|
+
when :val,:str then [val, ts[1..-1]]
|
83
|
+
else
|
84
|
+
raise Error, "unexpected #{val.inspect}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
# Parses an "object" in the sense of RFC 4627.
|
90
|
+
# Returns the parsed value and any trailing tokens.
|
91
|
+
def objparse(ts)
|
92
|
+
ts = eat('{', ts)
|
93
|
+
obj = {}
|
94
|
+
|
95
|
+
if ts[0][0] == '}'
|
96
|
+
return obj, ts[1..-1]
|
97
|
+
end
|
98
|
+
|
99
|
+
k, v, ts = pairparse(ts)
|
100
|
+
obj[k] = v
|
101
|
+
|
102
|
+
if ts[0][0] == '}'
|
103
|
+
return obj, ts[1..-1]
|
104
|
+
end
|
105
|
+
|
106
|
+
loop do
|
107
|
+
ts = eat(',', ts)
|
108
|
+
|
109
|
+
k, v, ts = pairparse(ts)
|
110
|
+
obj[k] = v
|
111
|
+
|
112
|
+
if ts[0][0] == '}'
|
113
|
+
return obj, ts[1..-1]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
# Parses a "member" in the sense of RFC 4627.
|
120
|
+
# Returns the parsed values and any trailing tokens.
|
121
|
+
def pairparse(ts)
|
122
|
+
(typ, _, k), ts = ts[0], ts[1..-1]
|
123
|
+
if typ != :str
|
124
|
+
raise Error, "unexpected #{k.inspect}"
|
125
|
+
end
|
126
|
+
ts = eat(':', ts)
|
127
|
+
v, ts = valparse(ts)
|
128
|
+
[k, v, ts]
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
# Parses an "array" in the sense of RFC 4627.
|
133
|
+
# Returns the parsed value and any trailing tokens.
|
134
|
+
def arrparse(ts)
|
135
|
+
ts = eat('[', ts)
|
136
|
+
arr = []
|
137
|
+
|
138
|
+
if ts[0][0] == ']'
|
139
|
+
return arr, ts[1..-1]
|
140
|
+
end
|
141
|
+
|
142
|
+
v, ts = valparse(ts)
|
143
|
+
arr << v
|
144
|
+
|
145
|
+
if ts[0][0] == ']'
|
146
|
+
return arr, ts[1..-1]
|
147
|
+
end
|
148
|
+
|
149
|
+
loop do
|
150
|
+
ts = eat(',', ts)
|
151
|
+
|
152
|
+
v, ts = valparse(ts)
|
153
|
+
arr << v
|
154
|
+
|
155
|
+
if ts[0][0] == ']'
|
156
|
+
return arr, ts[1..-1]
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
def eat(typ, ts)
|
163
|
+
if ts[0][0] != typ
|
164
|
+
raise Error, "expected #{typ} (got #{ts[0].inspect})"
|
165
|
+
end
|
166
|
+
ts[1..-1]
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
# Scans s and returns a list of json tokens,
|
171
|
+
# excluding white space (as defined in RFC 4627).
|
172
|
+
def lex(s)
|
173
|
+
ts = []
|
174
|
+
while s.length > 0
|
175
|
+
typ, lexeme, val = tok(s)
|
176
|
+
if typ == nil
|
177
|
+
raise Error, "invalid character at #{s[0,10].inspect}"
|
178
|
+
end
|
179
|
+
if typ != :space
|
180
|
+
ts << [typ, lexeme, val]
|
181
|
+
end
|
182
|
+
s = s[lexeme.length..-1]
|
183
|
+
end
|
184
|
+
ts
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
# Scans the first token in s and
|
189
|
+
# returns a 3-element list, or nil
|
190
|
+
# if s does not begin with a valid token.
|
191
|
+
#
|
192
|
+
# The first list element is one of
|
193
|
+
# '{', '}', ':', ',', '[', ']',
|
194
|
+
# :val, :str, and :space.
|
195
|
+
#
|
196
|
+
# The second element is the lexeme.
|
197
|
+
#
|
198
|
+
# The third element is the value of the
|
199
|
+
# token for :val and :str, otherwise
|
200
|
+
# it is the lexeme.
|
201
|
+
def tok(s)
|
202
|
+
case s[0]
|
203
|
+
when ?{ then ['{', s[0,1], s[0,1]]
|
204
|
+
when ?} then ['}', s[0,1], s[0,1]]
|
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 ?n then nulltok(s)
|
210
|
+
when ?t then truetok(s)
|
211
|
+
when ?f then falsetok(s)
|
212
|
+
when ?" then strtok(s)
|
213
|
+
when Spc then [:space, s[0,1], s[0,1]]
|
214
|
+
when ?\t then [:space, s[0,1], s[0,1]]
|
215
|
+
when ?\n then [:space, s[0,1], s[0,1]]
|
216
|
+
when ?\r then [:space, s[0,1], s[0,1]]
|
217
|
+
else numtok(s)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
def nulltok(s); s[0,4] == 'null' ? [:val, 'null', nil] : [] end
|
223
|
+
def truetok(s); s[0,4] == 'true' ? [:val, 'true', true] : [] end
|
224
|
+
def falsetok(s); s[0,5] == 'false' ? [:val, 'false', false] : [] end
|
225
|
+
|
226
|
+
|
227
|
+
def numtok(s)
|
228
|
+
m = /-?([1-9][0-9]+|[0-9])([.][0-9]+)?([eE][+-]?[0-9]+)?/.match(s)
|
229
|
+
if m && m.begin(0) == 0
|
230
|
+
if m[3] && !m[2]
|
231
|
+
[:val, m[0], Integer(m[1])*(10**Integer(m[3][1..-1]))]
|
232
|
+
elsif m[2]
|
233
|
+
[:val, m[0], Float(m[0])]
|
234
|
+
else
|
235
|
+
[:val, m[0], Integer(m[0])]
|
236
|
+
end
|
237
|
+
else
|
238
|
+
[]
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
|
243
|
+
def strtok(s)
|
244
|
+
m = /"([^"\\]|\\["\/\\bfnrt]|\\u[0-9a-fA-F]{4})*"/.match(s)
|
245
|
+
if ! m
|
246
|
+
raise Error, "invalid string literal at #{abbrev(s)}"
|
247
|
+
end
|
248
|
+
[:str, m[0], unquote(m[0])]
|
249
|
+
end
|
250
|
+
|
251
|
+
|
252
|
+
def abbrev(s)
|
253
|
+
t = s[0,10]
|
254
|
+
p = t['`']
|
255
|
+
t = t[0,p] if p
|
256
|
+
t = t + '...' if t.length < s.length
|
257
|
+
'`' + t + '`'
|
258
|
+
end
|
259
|
+
|
260
|
+
|
261
|
+
# Converts a quoted json string literal q into a UTF-8-encoded string.
|
262
|
+
# The rules are different than for Ruby, so we cannot use eval.
|
263
|
+
# Unquote will raise an error if q contains control characters.
|
264
|
+
def unquote(q)
|
265
|
+
q = q[1...-1]
|
266
|
+
a = q.dup # allocate a big enough string
|
267
|
+
rubydoesenc = false
|
268
|
+
# In ruby >= 1.9, a[w] is a codepoint, not a byte.
|
269
|
+
if a.class.method_defined?(:force_encoding)
|
270
|
+
a.force_encoding('UTF-8')
|
271
|
+
rubydoesenc = true
|
272
|
+
end
|
273
|
+
r, w = 0, 0
|
274
|
+
while r < q.length
|
275
|
+
c = q[r]
|
276
|
+
case true
|
277
|
+
when c == ?\\
|
278
|
+
r += 1
|
279
|
+
if r >= q.length
|
280
|
+
raise Error, "string literal ends with a \"\\\": \"#{q}\""
|
281
|
+
end
|
282
|
+
|
283
|
+
case q[r]
|
284
|
+
when ?",?\\,?/,?'
|
285
|
+
a[w] = q[r]
|
286
|
+
r += 1
|
287
|
+
w += 1
|
288
|
+
when ?b,?f,?n,?r,?t
|
289
|
+
a[w] = Unesc[q[r]]
|
290
|
+
r += 1
|
291
|
+
w += 1
|
292
|
+
when ?u
|
293
|
+
r += 1
|
294
|
+
uchar = begin
|
295
|
+
hexdec4(q[r,4])
|
296
|
+
rescue RuntimeError => e
|
297
|
+
raise Error, "invalid escape sequence \\u#{q[r,4]}: #{e}"
|
298
|
+
end
|
299
|
+
r += 4
|
300
|
+
if surrogate? uchar
|
301
|
+
if q.length >= r+6
|
302
|
+
uchar1 = hexdec4(q[r+2,4])
|
303
|
+
uchar = subst(uchar, uchar1)
|
304
|
+
if uchar != Ucharerr
|
305
|
+
# A valid pair; consume.
|
306
|
+
r += 6
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
310
|
+
if rubydoesenc
|
311
|
+
a[w] = '' << uchar
|
312
|
+
w += 1
|
313
|
+
else
|
314
|
+
w += ucharenc(a, w, uchar)
|
315
|
+
end
|
316
|
+
else
|
317
|
+
raise Error, "invalid escape char #{q[r]} in \"#{q}\""
|
318
|
+
end
|
319
|
+
when c == ?", c < Spc
|
320
|
+
raise Error, "invalid character in string literal \"#{q}\""
|
321
|
+
else
|
322
|
+
# Copy anything else byte-for-byte.
|
323
|
+
# Valid UTF-8 will remain valid UTF-8.
|
324
|
+
# Invalid UTF-8 will remain invalid UTF-8.
|
325
|
+
# In ruby >= 1.9, c is a codepoint, not a byte,
|
326
|
+
# in which case this is still what we want.
|
327
|
+
a[w] = c
|
328
|
+
r += 1
|
329
|
+
w += 1
|
330
|
+
end
|
331
|
+
end
|
332
|
+
a[0,w]
|
333
|
+
end
|
334
|
+
|
335
|
+
|
336
|
+
# Encodes unicode character u as UTF-8
|
337
|
+
# bytes in string a at position i.
|
338
|
+
# Returns the number of bytes written.
|
339
|
+
def ucharenc(a, i, u)
|
340
|
+
case true
|
341
|
+
when u <= Uchar1max
|
342
|
+
a[i] = (u & 0xff).chr
|
343
|
+
1
|
344
|
+
when u <= Uchar2max
|
345
|
+
a[i+0] = (Utag2 | ((u>>6)&0xff)).chr
|
346
|
+
a[i+1] = (Utagx | (u&Umaskx)).chr
|
347
|
+
2
|
348
|
+
when u <= Uchar3max
|
349
|
+
a[i+0] = (Utag3 | ((u>>12)&0xff)).chr
|
350
|
+
a[i+1] = (Utagx | ((u>>6)&Umaskx)).chr
|
351
|
+
a[i+2] = (Utagx | (u&Umaskx)).chr
|
352
|
+
3
|
353
|
+
else
|
354
|
+
a[i+0] = (Utag4 | ((u>>18)&0xff)).chr
|
355
|
+
a[i+1] = (Utagx | ((u>>12)&Umaskx)).chr
|
356
|
+
a[i+2] = (Utagx | ((u>>6)&Umaskx)).chr
|
357
|
+
a[i+3] = (Utagx | (u&Umaskx)).chr
|
358
|
+
4
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
|
363
|
+
def hexdec4(s)
|
364
|
+
if s.length != 4
|
365
|
+
raise Error, 'short'
|
366
|
+
end
|
367
|
+
(nibble(s[0])<<12) | (nibble(s[1])<<8) | (nibble(s[2])<<4) | nibble(s[3])
|
368
|
+
end
|
369
|
+
|
370
|
+
|
371
|
+
def subst(u1, u2)
|
372
|
+
if Usurr1 <= u1 && u1 < Usurr2 && Usurr2 <= u2 && u2 < Usurr3
|
373
|
+
return ((u1-Usurr1)<<10) | (u2-Usurr2) + Usurrself
|
374
|
+
end
|
375
|
+
return Ucharerr
|
376
|
+
end
|
377
|
+
|
378
|
+
|
379
|
+
def surrogate?(u)
|
380
|
+
Usurr1 <= u && u < Usurr3
|
381
|
+
end
|
382
|
+
|
383
|
+
|
384
|
+
def nibble(c)
|
385
|
+
case true
|
386
|
+
when ?0 <= c && c <= ?9 then c.ord - ?0.ord
|
387
|
+
when ?a <= c && c <= ?z then c.ord - ?a.ord + 10
|
388
|
+
when ?A <= c && c <= ?Z then c.ord - ?A.ord + 10
|
389
|
+
else
|
390
|
+
raise Error, "invalid hex code #{c}"
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
|
395
|
+
# Encodes x into a json text. It may contain only
|
396
|
+
# Array, Hash, String, Numeric, true, false, nil.
|
397
|
+
# (Note, this list excludes Symbol.)
|
398
|
+
# X itself must be an Array or a Hash.
|
399
|
+
# No other value can be encoded, and an error will
|
400
|
+
# be raised if x contains any other value, such as
|
401
|
+
# Nan, Infinity, Symbol, and Proc, or if a Hash key
|
402
|
+
# is not a String.
|
403
|
+
# Strings contained in x must be valid UTF-8.
|
404
|
+
def encode(x)
|
405
|
+
case x
|
406
|
+
when Hash then objenc(x)
|
407
|
+
when Array then arrenc(x)
|
408
|
+
else
|
409
|
+
raise Error, 'root value must be an Array or a Hash'
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
|
414
|
+
def valenc(x)
|
415
|
+
case x
|
416
|
+
when Hash then objenc(x)
|
417
|
+
when Array then arrenc(x)
|
418
|
+
when String then strenc(x)
|
419
|
+
when Numeric then numenc(x)
|
420
|
+
when true then "true"
|
421
|
+
when false then "false"
|
422
|
+
when nil then "null"
|
423
|
+
else
|
424
|
+
raise Error, "cannot encode #{x.class}: #{x.inspect}"
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
|
429
|
+
def objenc(x)
|
430
|
+
'{' + x.map{|k,v| keyenc(k) + ':' + valenc(v)}.join(',') + '}'
|
431
|
+
end
|
432
|
+
|
433
|
+
|
434
|
+
def arrenc(a)
|
435
|
+
'[' + a.map{|x| valenc(x)}.join(',') + ']'
|
436
|
+
end
|
437
|
+
|
438
|
+
|
439
|
+
def keyenc(k)
|
440
|
+
case k
|
441
|
+
when String then strenc(k)
|
442
|
+
else
|
443
|
+
raise Error, "Hash key is not a string: #{k.inspect}"
|
444
|
+
end
|
445
|
+
end
|
446
|
+
|
447
|
+
|
448
|
+
def strenc(s)
|
449
|
+
t = StringIO.new
|
450
|
+
t.putc(?")
|
451
|
+
r = 0
|
452
|
+
|
453
|
+
# In ruby >= 1.9, s[r] is a codepoint, not a byte.
|
454
|
+
rubydoesenc = s.class.method_defined?(:encoding)
|
455
|
+
|
456
|
+
while r < s.length
|
457
|
+
case s[r]
|
458
|
+
when ?" then t.print('\\"')
|
459
|
+
when ?\\ then t.print('\\\\')
|
460
|
+
when ?\b then t.print('\\b')
|
461
|
+
when ?\f then t.print('\\f')
|
462
|
+
when ?\n then t.print('\\n')
|
463
|
+
when ?\r then t.print('\\r')
|
464
|
+
when ?\t then t.print('\\t')
|
465
|
+
else
|
466
|
+
c = s[r]
|
467
|
+
case true
|
468
|
+
when rubydoesenc
|
469
|
+
begin
|
470
|
+
c.ord # will raise an error if c is invalid UTF-8
|
471
|
+
t.write(c)
|
472
|
+
rescue
|
473
|
+
t.write(Ustrerr)
|
474
|
+
end
|
475
|
+
when Spc <= c && c <= ?~
|
476
|
+
t.putc(c)
|
477
|
+
else
|
478
|
+
n = ucharcopy(t, s, r) # ensure valid UTF-8 output
|
479
|
+
r += n - 1 # r is incremented below
|
480
|
+
end
|
481
|
+
end
|
482
|
+
r += 1
|
483
|
+
end
|
484
|
+
t.putc(?")
|
485
|
+
t.string
|
486
|
+
end
|
487
|
+
|
488
|
+
|
489
|
+
def numenc(x)
|
490
|
+
if ((x.nan? || x.infinite?) rescue false)
|
491
|
+
raise Error, "Numeric cannot be represented: #{x}"
|
492
|
+
end
|
493
|
+
"#{x}"
|
494
|
+
end
|
495
|
+
|
496
|
+
|
497
|
+
# Copies the valid UTF-8 bytes of a single character
|
498
|
+
# from string s at position i to I/O object t, and
|
499
|
+
# returns the number of bytes copied.
|
500
|
+
# If no valid UTF-8 char exists at position i,
|
501
|
+
# ucharcopy writes Ustrerr and returns 1.
|
502
|
+
def ucharcopy(t, s, i)
|
503
|
+
n = s.length - i
|
504
|
+
raise Utf8Error if n < 1
|
505
|
+
|
506
|
+
c0 = s[i].ord
|
507
|
+
|
508
|
+
# 1-byte, 7-bit sequence?
|
509
|
+
if c0 < Utagx
|
510
|
+
t.putc(c0)
|
511
|
+
return 1
|
512
|
+
end
|
513
|
+
|
514
|
+
raise Utf8Error if c0 < Utag2 # unexpected continuation byte?
|
515
|
+
|
516
|
+
raise Utf8Error if n < 2 # need continuation byte
|
517
|
+
c1 = s[i+1].ord
|
518
|
+
raise Utf8Error if c1 < Utagx || Utag2 <= c1
|
519
|
+
|
520
|
+
# 2-byte, 11-bit sequence?
|
521
|
+
if c0 < Utag3
|
522
|
+
raise Utf8Error if ((c0&Umask2)<<6 | (c1&Umaskx)) <= Uchar1max
|
523
|
+
t.putc(c0)
|
524
|
+
t.putc(c1)
|
525
|
+
return 2
|
526
|
+
end
|
527
|
+
|
528
|
+
# need second continuation byte
|
529
|
+
raise Utf8Error if n < 3
|
530
|
+
|
531
|
+
c2 = s[i+2].ord
|
532
|
+
raise Utf8Error if c2 < Utagx || Utag2 <= c2
|
533
|
+
|
534
|
+
# 3-byte, 16-bit sequence?
|
535
|
+
if c0 < Utag4
|
536
|
+
u = (c0&Umask3)<<12 | (c1&Umaskx)<<6 | (c2&Umaskx)
|
537
|
+
raise Utf8Error if u <= Uchar2max
|
538
|
+
t.putc(c0)
|
539
|
+
t.putc(c1)
|
540
|
+
t.putc(c2)
|
541
|
+
return 3
|
542
|
+
end
|
543
|
+
|
544
|
+
# need third continuation byte
|
545
|
+
raise Utf8Error if n < 4
|
546
|
+
c3 = s[i+3].ord
|
547
|
+
raise Utf8Error if c3 < Utagx || Utag2 <= c3
|
548
|
+
|
549
|
+
# 4-byte, 21-bit sequence?
|
550
|
+
if c0 < Utag5
|
551
|
+
u = (c0&Umask4)<<18 | (c1&Umaskx)<<12 | (c2&Umaskx)<<6 | (c3&Umaskx)
|
552
|
+
raise Utf8Error if u <= Uchar3max
|
553
|
+
t.putc(c0)
|
554
|
+
t.putc(c1)
|
555
|
+
t.putc(c2)
|
556
|
+
t.putc(c3)
|
557
|
+
return 4
|
558
|
+
end
|
559
|
+
|
560
|
+
raise Utf8Error
|
561
|
+
rescue Utf8Error
|
562
|
+
t.write(Ustrerr)
|
563
|
+
return 1
|
564
|
+
end
|
565
|
+
|
566
|
+
|
567
|
+
class Utf8Error < ::StandardError
|
568
|
+
end
|
569
|
+
|
570
|
+
|
571
|
+
class Error < ::StandardError
|
572
|
+
end
|
573
|
+
|
574
|
+
|
575
|
+
Utagx = 0x80 # 1000 0000
|
576
|
+
Utag2 = 0xc0 # 1100 0000
|
577
|
+
Utag3 = 0xe0 # 1110 0000
|
578
|
+
Utag4 = 0xf0 # 1111 0000
|
579
|
+
Utag5 = 0xF8 # 1111 1000
|
580
|
+
Umaskx = 0x3f # 0011 1111
|
581
|
+
Umask2 = 0x1f # 0001 1111
|
582
|
+
Umask3 = 0x0f # 0000 1111
|
583
|
+
Umask4 = 0x07 # 0000 0111
|
584
|
+
Uchar1max = (1<<7) - 1
|
585
|
+
Uchar2max = (1<<11) - 1
|
586
|
+
Uchar3max = (1<<16) - 1
|
587
|
+
Ucharerr = 0xFFFD # unicode "replacement char"
|
588
|
+
Ustrerr = "\xef\xbf\xbd" # unicode "replacement char"
|
589
|
+
Usurrself = 0x10000
|
590
|
+
Usurr1 = 0xd800
|
591
|
+
Usurr2 = 0xdc00
|
592
|
+
Usurr3 = 0xe000
|
593
|
+
|
594
|
+
Spc = ' '[0]
|
595
|
+
Unesc = {?b=>?\b, ?f=>?\f, ?n=>?\n, ?r=>?\r, ?t=>?\t}
|
596
|
+
end
|
data/lib/cfruntime/properties.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module CFRuntime
|
2
2
|
|
3
|
-
require 'crack/json'
|
4
3
|
require 'uri'
|
4
|
+
require File.join(File.dirname(__FILE__), 'okjson')
|
5
5
|
|
6
6
|
class CloudApp
|
7
7
|
|
@@ -30,7 +30,7 @@ module CFRuntime
|
|
30
30
|
def service_props(service_name)
|
31
31
|
registered_svcs = {}
|
32
32
|
if ENV['VCAP_SERVICES']
|
33
|
-
svcs =
|
33
|
+
svcs = CFRuntime::OkJson.decode(ENV['VCAP_SERVICES'])
|
34
34
|
else
|
35
35
|
svcs = {}
|
36
36
|
end
|
@@ -91,7 +91,7 @@ module CFRuntime
|
|
91
91
|
def service_names
|
92
92
|
service_names = []
|
93
93
|
if ENV['VCAP_SERVICES']
|
94
|
-
|
94
|
+
CFRuntime::OkJson.decode(ENV['VCAP_SERVICES']).each do |key,list|
|
95
95
|
list.each do |svc|
|
96
96
|
service_names << svc["name"]
|
97
97
|
end
|
@@ -106,7 +106,7 @@ module CFRuntime
|
|
106
106
|
def service_names_of_type(type)
|
107
107
|
service_names = []
|
108
108
|
if ENV['VCAP_SERVICES']
|
109
|
-
|
109
|
+
CFRuntime::OkJson.decode(ENV['VCAP_SERVICES']).each do |key,list|
|
110
110
|
label, version = key.split('-')
|
111
111
|
list.each do |svc|
|
112
112
|
if label == type
|
data/lib/cfruntime/version.rb
CHANGED
metadata
CHANGED
@@ -1,227 +1,223 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: cf-runtime
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- VMware
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
12
|
+
date: 2012-07-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: redis
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
18
|
+
requirements:
|
26
19
|
- - ~>
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
|
30
|
-
- 0
|
31
|
-
- 3
|
32
|
-
- 1
|
33
|
-
version: 0.3.1
|
34
|
-
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: redis
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.0'
|
22
|
+
type: :development
|
38
23
|
prerelease: false
|
39
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
25
|
none: false
|
41
|
-
requirements:
|
26
|
+
requirements:
|
42
27
|
- - ~>
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
|
45
|
-
|
46
|
-
- 2
|
47
|
-
- 0
|
48
|
-
version: "2.0"
|
49
|
-
type: :development
|
50
|
-
version_requirements: *id002
|
51
|
-
- !ruby/object:Gem::Dependency
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
52
31
|
name: amqp
|
53
|
-
|
54
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
55
33
|
none: false
|
56
|
-
requirements:
|
34
|
+
requirements:
|
57
35
|
- - ~>
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
|
60
|
-
segments:
|
61
|
-
- 0
|
62
|
-
- 8
|
63
|
-
version: "0.8"
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0.8'
|
64
38
|
type: :development
|
65
|
-
version_requirements: *id003
|
66
|
-
- !ruby/object:Gem::Dependency
|
67
|
-
name: carrot
|
68
39
|
prerelease: false
|
69
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
41
|
none: false
|
71
|
-
requirements:
|
42
|
+
requirements:
|
72
43
|
- - ~>
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0.8'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: carrot
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.0'
|
79
54
|
type: :development
|
80
|
-
version_requirements: *id004
|
81
|
-
- !ruby/object:Gem::Dependency
|
82
|
-
name: mongo
|
83
55
|
prerelease: false
|
84
|
-
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
57
|
none: false
|
86
|
-
requirements:
|
58
|
+
requirements:
|
87
59
|
- - ~>
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: mongo
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
94
69
|
version: 1.2.0
|
95
70
|
type: :development
|
96
|
-
version_requirements: *id005
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: pg
|
99
71
|
prerelease: false
|
100
|
-
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
101
73
|
none: false
|
102
|
-
requirements:
|
74
|
+
requirements:
|
103
75
|
- - ~>
|
104
|
-
- !ruby/object:Gem::Version
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.2.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: pg
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
110
85
|
version: 0.11.0
|
111
86
|
type: :development
|
112
|
-
version_requirements: *id006
|
113
|
-
- !ruby/object:Gem::Dependency
|
114
|
-
name: mysql2
|
115
87
|
prerelease: false
|
116
|
-
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 0.11.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: mysql2
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
117
97
|
none: false
|
118
|
-
requirements:
|
98
|
+
requirements:
|
119
99
|
- - ~>
|
120
|
-
- !ruby/object:Gem::Version
|
121
|
-
hash: 25
|
122
|
-
segments:
|
123
|
-
- 0
|
124
|
-
- 2
|
125
|
-
- 7
|
100
|
+
- !ruby/object:Gem::Version
|
126
101
|
version: 0.2.7
|
127
102
|
type: :development
|
128
|
-
version_requirements: *id007
|
129
|
-
- !ruby/object:Gem::Dependency
|
130
|
-
name: rake
|
131
103
|
prerelease: false
|
132
|
-
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 0.2.7
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: rake
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
133
113
|
none: false
|
134
|
-
requirements:
|
114
|
+
requirements:
|
135
115
|
- - ~>
|
136
|
-
- !ruby/object:Gem::Version
|
137
|
-
hash: 63
|
138
|
-
segments:
|
139
|
-
- 0
|
140
|
-
- 9
|
141
|
-
- 2
|
116
|
+
- !ruby/object:Gem::Version
|
142
117
|
version: 0.9.2
|
143
118
|
type: :development
|
144
|
-
version_requirements: *id008
|
145
|
-
- !ruby/object:Gem::Dependency
|
146
|
-
name: rcov
|
147
119
|
prerelease: false
|
148
|
-
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
121
|
none: false
|
150
|
-
requirements:
|
122
|
+
requirements:
|
151
123
|
- - ~>
|
152
|
-
- !ruby/object:Gem::Version
|
153
|
-
|
154
|
-
|
155
|
-
- 0
|
156
|
-
- 9
|
157
|
-
- 10
|
158
|
-
version: 0.9.10
|
159
|
-
type: :development
|
160
|
-
version_requirements: *id009
|
161
|
-
- !ruby/object:Gem::Dependency
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 0.9.2
|
126
|
+
- !ruby/object:Gem::Dependency
|
162
127
|
name: rack-test
|
163
|
-
|
164
|
-
requirement: &id010 !ruby/object:Gem::Requirement
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
165
129
|
none: false
|
166
|
-
requirements:
|
130
|
+
requirements:
|
167
131
|
- - ~>
|
168
|
-
- !ruby/object:Gem::Version
|
169
|
-
hash: 5
|
170
|
-
segments:
|
171
|
-
- 0
|
172
|
-
- 6
|
173
|
-
- 1
|
132
|
+
- !ruby/object:Gem::Version
|
174
133
|
version: 0.6.1
|
175
134
|
type: :development
|
176
|
-
version_requirements: *id010
|
177
|
-
- !ruby/object:Gem::Dependency
|
178
|
-
name: rspec
|
179
135
|
prerelease: false
|
180
|
-
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ~>
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: 0.6.1
|
142
|
+
- !ruby/object:Gem::Dependency
|
143
|
+
name: rspec
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
181
145
|
none: false
|
182
|
-
requirements:
|
146
|
+
requirements:
|
183
147
|
- - ~>
|
184
|
-
- !ruby/object:Gem::Version
|
185
|
-
hash: 23
|
186
|
-
segments:
|
187
|
-
- 2
|
188
|
-
- 6
|
189
|
-
- 0
|
148
|
+
- !ruby/object:Gem::Version
|
190
149
|
version: 2.6.0
|
191
150
|
type: :development
|
192
|
-
|
193
|
-
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ~>
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: 2.6.0
|
158
|
+
- !ruby/object:Gem::Dependency
|
194
159
|
name: ci_reporter
|
160
|
+
requirement: !ruby/object:Gem::Requirement
|
161
|
+
none: false
|
162
|
+
requirements:
|
163
|
+
- - ~>
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: 1.6.5
|
166
|
+
type: :development
|
195
167
|
prerelease: false
|
196
|
-
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
197
169
|
none: false
|
198
|
-
requirements:
|
170
|
+
requirements:
|
199
171
|
- - ~>
|
200
|
-
- !ruby/object:Gem::Version
|
201
|
-
hash: 5
|
202
|
-
segments:
|
203
|
-
- 1
|
204
|
-
- 6
|
205
|
-
- 5
|
172
|
+
- !ruby/object:Gem::Version
|
206
173
|
version: 1.6.5
|
174
|
+
- !ruby/object:Gem::Dependency
|
175
|
+
name: simplecov
|
176
|
+
requirement: !ruby/object:Gem::Requirement
|
177
|
+
none: false
|
178
|
+
requirements:
|
179
|
+
- - ~>
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: 0.6.1
|
182
|
+
type: :development
|
183
|
+
prerelease: false
|
184
|
+
version_requirements: !ruby/object:Gem::Requirement
|
185
|
+
none: false
|
186
|
+
requirements:
|
187
|
+
- - ~>
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: 0.6.1
|
190
|
+
- !ruby/object:Gem::Dependency
|
191
|
+
name: simplecov-rcov
|
192
|
+
requirement: !ruby/object:Gem::Requirement
|
193
|
+
none: false
|
194
|
+
requirements:
|
195
|
+
- - ~>
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
version: 0.2.3
|
207
198
|
type: :development
|
208
|
-
|
199
|
+
prerelease: false
|
200
|
+
version_requirements: !ruby/object:Gem::Requirement
|
201
|
+
none: false
|
202
|
+
requirements:
|
203
|
+
- - ~>
|
204
|
+
- !ruby/object:Gem::Version
|
205
|
+
version: 0.2.3
|
209
206
|
description: Cloud Foundry Runtime Library
|
210
207
|
email: support@vmware.com
|
211
208
|
executables: []
|
212
|
-
|
213
209
|
extensions: []
|
214
|
-
|
215
|
-
extra_rdoc_files:
|
210
|
+
extra_rdoc_files:
|
216
211
|
- README.md
|
217
212
|
- LICENSE
|
218
|
-
files:
|
213
|
+
files:
|
219
214
|
- LICENSE
|
220
215
|
- README.md
|
221
216
|
- lib/cfruntime/amqp.rb
|
222
217
|
- lib/cfruntime/carrot.rb
|
223
218
|
- lib/cfruntime/mongodb.rb
|
224
219
|
- lib/cfruntime/mysql.rb
|
220
|
+
- lib/cfruntime/okjson.rb
|
225
221
|
- lib/cfruntime/postgres.rb
|
226
222
|
- lib/cfruntime/properties.rb
|
227
223
|
- lib/cfruntime/redis.rb
|
@@ -229,38 +225,29 @@ files:
|
|
229
225
|
- lib/cfruntime.rb
|
230
226
|
homepage: http://vmware.com
|
231
227
|
licenses: []
|
232
|
-
|
233
228
|
post_install_message:
|
234
|
-
rdoc_options:
|
229
|
+
rdoc_options:
|
235
230
|
- -N
|
236
231
|
- --tab-width=2
|
237
232
|
- --exclude='cf-runtime.gemspec|spec'
|
238
|
-
require_paths:
|
233
|
+
require_paths:
|
239
234
|
- lib
|
240
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
235
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
241
236
|
none: false
|
242
|
-
requirements:
|
243
|
-
- -
|
244
|
-
- !ruby/object:Gem::Version
|
245
|
-
|
246
|
-
|
247
|
-
- 0
|
248
|
-
version: "0"
|
249
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
237
|
+
requirements:
|
238
|
+
- - ! '>='
|
239
|
+
- !ruby/object:Gem::Version
|
240
|
+
version: '0'
|
241
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
250
242
|
none: false
|
251
|
-
requirements:
|
252
|
-
- -
|
253
|
-
- !ruby/object:Gem::Version
|
254
|
-
|
255
|
-
segments:
|
256
|
-
- 0
|
257
|
-
version: "0"
|
243
|
+
requirements:
|
244
|
+
- - ! '>='
|
245
|
+
- !ruby/object:Gem::Version
|
246
|
+
version: '0'
|
258
247
|
requirements: []
|
259
|
-
|
260
248
|
rubyforge_project:
|
261
|
-
rubygems_version: 1.8.
|
249
|
+
rubygems_version: 1.8.21
|
262
250
|
signing_key:
|
263
251
|
specification_version: 3
|
264
252
|
summary: Cloud Foundry Runtime Library
|
265
253
|
test_files: []
|
266
|
-
|