rbjq 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rbjq/ops.rb ADDED
@@ -0,0 +1,449 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbjq
4
+ # Core value operations: indexing, slicing, paths and arithmetic.
5
+ module Ops
6
+ module_function
7
+
8
+ def tostring(v)
9
+ v.is_a?(String) ? v : Encoder.dump(v)
10
+ end
11
+
12
+ MAX_PATH_DEPTH = 10_000
13
+ MAX_ARRAY_INDEX = 536_870_911
14
+
15
+ def index_error(t, k)
16
+ raise JQError, "Cannot index #{Value.type_name(t)} with #{Value.type_name(k)} (#{Value.dump_trunc(k)})"
17
+ end
18
+
19
+ # A path component that selects a slice: {"start": n|null, "end": n|null}
20
+ def slice_key?(k)
21
+ k.is_a?(Hash)
22
+ end
23
+
24
+ def check_slice_key(k)
25
+ return if k.key?('start') && k.key?('end')
26
+
27
+ raise JQError, 'Array/string slice indices must be integers'
28
+ end
29
+
30
+ def index(t, k)
31
+ case t
32
+ when Hash
33
+ return t[k] if k.is_a?(String)
34
+
35
+ index_error(t, k)
36
+ when Array
37
+ case k
38
+ when Integer
39
+ k += t.length if k.negative?
40
+ k.negative? ? nil : t[k]
41
+ when Float
42
+ return nil if k.nan? || k.infinite?
43
+
44
+ i = k.floor
45
+ i += t.length if i.negative?
46
+ i.negative? ? nil : t[i]
47
+ when Hash
48
+ check_slice_key(k)
49
+ slice(t, k['start'], k['end'])
50
+ when Array
51
+ indices_of(t, k)
52
+ else
53
+ index_error(t, k)
54
+ end
55
+ when nil
56
+ case k
57
+ when String, Integer, Float, Hash, Array, nil then nil
58
+ else index_error(t, k)
59
+ end
60
+ when String
61
+ if k.is_a?(Hash)
62
+ check_slice_key(k)
63
+ slice(t, k['start'], k['end'])
64
+ else
65
+ index_error(t, k)
66
+ end
67
+ else
68
+ index_error(t, k)
69
+ end
70
+ end
71
+
72
+ def indices_of(arr, sub)
73
+ return [] if sub.empty?
74
+
75
+ out = []
76
+ (0..(arr.length - sub.length)).each do |i|
77
+ out << i if sub.each_with_index.all? { |e, j| Value.equal?(arr[i + j], e) }
78
+ end
79
+ out
80
+ end
81
+
82
+ def slice_bounds(len, from, to)
83
+ unless (from.nil? || Value.number?(from)) && (to.nil? || Value.number?(to))
84
+ raise JQError, 'Start and end indices of an array slice must be numbers'
85
+ end
86
+
87
+ # NaN bounds behave like omitted ones
88
+ ds = from.nil? || (from.is_a?(Float) && from.nan?) ? 0.0 : from.to_f
89
+ de = to.nil? || (to.is_a?(Float) && to.nan?) ? len.to_f : to.to_f
90
+ ds += len if ds.negative?
91
+ de += len if de.negative?
92
+ ds = ds.clamp(0.0, len.to_f)
93
+ de = de.clamp(0.0, len.to_f)
94
+ s = ds.floor
95
+ e = de.ceil
96
+ e = s if e < s
97
+ [s, e]
98
+ end
99
+
100
+ def slice(t, from, to)
101
+ case t
102
+ when nil then nil
103
+ when Array
104
+ s, e = slice_bounds(t.length, from, to)
105
+ t[s...e]
106
+ when String
107
+ s, e = slice_bounds(t.length, from, to)
108
+ t[s...e]
109
+ else
110
+ index_error(t, { 'start' => from, 'end' => to })
111
+ end
112
+ end
113
+
114
+ def check_path(path)
115
+ raise JQError, 'Path must be specified as an array' unless path.is_a?(Array)
116
+ raise JQError, 'Path too deep' if path.size > MAX_PATH_DEPTH
117
+ end
118
+
119
+ def getpath(t, path)
120
+ check_path(path)
121
+ path.each do |k|
122
+ return nil if t.nil?
123
+
124
+ t = index(t, k)
125
+ end
126
+ t
127
+ end
128
+
129
+ # Returns a copy of `t` whose element at key `k` is replaced by `v`.
130
+ def set_child(t, k, v)
131
+ case k
132
+ when String
133
+ index_error(t, k) unless t.nil? || t.is_a?(Hash)
134
+
135
+ h = t.nil? ? {} : t.dup
136
+ h[k] = v
137
+ h
138
+ when Integer, Float
139
+ index_error(t, k) unless t.nil? || t.is_a?(Array)
140
+ raise JQError, 'Cannot set array element at NaN index' if k.is_a?(Float) && k.nan?
141
+
142
+ a = t.nil? ? [] : t.dup
143
+ idx = k.to_f.floor
144
+ if idx.negative?
145
+ idx += a.length
146
+ raise JQError, 'Out of bounds negative array index' if idx.negative?
147
+ end
148
+ raise JQError, 'Array index too large' if idx > MAX_ARRAY_INDEX
149
+
150
+ a[idx] = v
151
+ a
152
+ when Hash
153
+ case t
154
+ when nil, Array
155
+ check_slice_key(k)
156
+ a = t.nil? ? [] : t
157
+ s, e = slice_bounds(a.length, k['start'], k['end'])
158
+ raise JQError, 'A slice of an array can only be assigned another array' unless v.is_a?(Array)
159
+
160
+ a[0...s] + v + a[e..]
161
+ when String
162
+ raise JQError, 'Cannot update string slices'
163
+ else
164
+ index_error(t, k)
165
+ end
166
+ when Array
167
+ raise JQError, 'Cannot update field at array index of array' if t.is_a?(Array)
168
+
169
+ index_error(t, k)
170
+ else
171
+ index_error(t, k)
172
+ end
173
+ end
174
+
175
+ # Iterative, so that paths of up to MAX_PATH_DEPTH components work with
176
+ # the default Ruby stack.
177
+ def setpath(t, path, v)
178
+ check_path(path)
179
+ return v if path.empty?
180
+
181
+ # walk down, remembering each container
182
+ containers = [t]
183
+ cur = t
184
+ path[0...-1].each do |k|
185
+ cur = setpath_child(cur, k)
186
+ containers << cur
187
+ end
188
+ # rebuild bottom-up
189
+ (path.size - 1).downto(0) do |i|
190
+ v = set_child(containers[i], path[i], v)
191
+ end
192
+ v
193
+ end
194
+
195
+ # The current value at `k` while descending for setpath
196
+ def setpath_child(t, k)
197
+ return nil if t.nil?
198
+
199
+ if k.is_a?(Hash) && (t.is_a?(Array) || t.nil?)
200
+ check_slice_key(k)
201
+ s, e = slice_bounds(t.length, k['start'], k['end'])
202
+ return t[s...e]
203
+ end
204
+ # validate the key against the container type the same way set_child does
205
+ case k
206
+ when String then index_error(t, k) unless t.is_a?(Hash)
207
+ when Integer, Float then index_error(t, k) unless t.is_a?(Array)
208
+ when Array then raise JQError, 'Cannot update field at array index of array' if t.is_a?(Array)
209
+ end
210
+ index(t, k)
211
+ end
212
+
213
+ def delpaths(t, paths)
214
+ raise JQError, 'Paths must be specified as an array' unless paths.is_a?(Array)
215
+
216
+ normalized = paths.flat_map do |p|
217
+ raise JQError, "Path must be specified as array, not #{Value.type_name(p)}" unless p.is_a?(Array)
218
+
219
+ check_path(p)
220
+ normalize_path(t, p)
221
+ end
222
+ sorted = Value.sort(normalized)
223
+ sorted = sorted.each_with_index.reject { |q, i| i.positive? && Value.equal?(q, sorted[i - 1]) }.map(&:first)
224
+ sorted.reverse_each { |p| t = delpath(t, p) }
225
+ t
226
+ end
227
+
228
+ # Resolve negative array indices against the value and expand a trailing
229
+ # slice into its indices, so that deleting several paths at once removes
230
+ # the elements they referred to in the original value (like jq).
231
+ # Paths that point outside an array are dropped.
232
+ def normalize_path(t, path)
233
+ out = []
234
+ cur = t
235
+ path.each_with_index do |k, i|
236
+ last = i == path.size - 1
237
+ if cur.is_a?(Array) && Value.number?(k)
238
+ return [] if k.is_a?(Float) && k.nan?
239
+
240
+ k = k.to_f.floor
241
+ k += cur.length if k.negative?
242
+ return [] if k.negative? || k >= cur.length
243
+ elsif cur.is_a?(Array) && k.is_a?(Hash) && last
244
+ check_slice_key(k)
245
+ s, e = slice_bounds(cur.length, k['start'], k['end'])
246
+ return (s...e).map { |j| out + [j] }
247
+ end
248
+ out << k
249
+ break if last
250
+
251
+ cur = cur.nil? ? nil : index(cur, k)
252
+ end
253
+ [out]
254
+ end
255
+
256
+ def delete_child(t, k)
257
+ case t
258
+ when Hash
259
+ raise JQError, "Cannot delete #{Value.type_name(k)} field of object" unless k.is_a?(String)
260
+
261
+ h = t.dup
262
+ h.delete(k)
263
+ h
264
+ when Array
265
+ case k
266
+ when Integer, Float
267
+ idx = k.to_f.floor
268
+ idx += t.length if idx.negative?
269
+ raise JQError, 'Out of bounds negative array index' if idx.negative?
270
+ return t if idx >= t.length
271
+
272
+ a = t.dup
273
+ a.delete_at(idx)
274
+ a
275
+ when Hash
276
+ check_slice_key(k)
277
+ s, e = slice_bounds(t.length, k['start'], k['end'])
278
+ t[0...s] + t[e..]
279
+ else
280
+ raise JQError, "Cannot delete #{Value.type_name(k)} element of array"
281
+ end
282
+ else
283
+ raise JQError, "Cannot delete fields from #{Value.type_name(t)}"
284
+ end
285
+ end
286
+
287
+ # Iterative delete of one path
288
+ def delpath(t, path)
289
+ return nil if path.empty?
290
+
291
+ containers = [t]
292
+ cur = t
293
+ path[0...-1].each do |k|
294
+ return t if cur.nil?
295
+
296
+ cur = index(cur, k)
297
+ containers << cur
298
+ end
299
+ return t if cur.nil?
300
+
301
+ v = delete_child(cur, path[-1])
302
+ (path.size - 2).downto(0) do |i|
303
+ k = path[i]
304
+ parent = containers[i]
305
+ v = if k.is_a?(Hash)
306
+ s, e = slice_bounds(parent.length, k['start'], k['end'])
307
+ parent[0...s] + v + parent[e..]
308
+ else
309
+ set_child(parent, k, v)
310
+ end
311
+ end
312
+ v
313
+ end
314
+
315
+ # ------------------------------------------------------------ arithmetic
316
+
317
+ def binop(op, l, r)
318
+ case op
319
+ when :+ then add(l, r)
320
+ when :- then subtract(l, r)
321
+ when :* then multiply(l, r)
322
+ when :/ then divide(l, r)
323
+ when :% then modulo(l, r)
324
+ when :== then Value.equal?(l, r)
325
+ when :'!=' then !Value.equal?(l, r)
326
+ when :< then Value.compare(l, r).negative?
327
+ when :<= then Value.compare(l, r) <= 0
328
+ when :> then Value.compare(l, r).positive?
329
+ when :>= then Value.compare(l, r) >= 0
330
+ else raise JQError, "unknown operator #{op}"
331
+ end
332
+ end
333
+
334
+ def add(l, r)
335
+ return r if l.nil?
336
+ return l if r.nil?
337
+
338
+ if Value.number?(l) && Value.number?(r)
339
+ Value.normalize_number(dbl(l) + dbl(r))
340
+ elsif l.is_a?(String) && r.is_a?(String)
341
+ l + r
342
+ elsif l.is_a?(Array) && r.is_a?(Array)
343
+ l + r
344
+ elsif l.is_a?(Hash) && r.is_a?(Hash)
345
+ l.merge(r)
346
+ else
347
+ raise JQError, "#{Value.desc(l)} and #{Value.desc(r)} cannot be added"
348
+ end
349
+ end
350
+
351
+ def subtract(l, r)
352
+ if Value.number?(l) && Value.number?(r)
353
+ Value.normalize_number(dbl(l) - dbl(r))
354
+ elsif l.is_a?(Array) && r.is_a?(Array)
355
+ l.reject { |x| r.any? { |y| Value.equal?(x, y) } }
356
+ else
357
+ raise JQError, "#{Value.desc(l)} and #{Value.desc(r)} cannot be subtracted"
358
+ end
359
+ end
360
+
361
+ def multiply(l, r)
362
+ if Value.number?(l) && Value.number?(r)
363
+ Value.normalize_number(dbl(l) * dbl(r))
364
+ elsif (l.is_a?(String) && Value.number?(r)) || (Value.number?(l) && r.is_a?(String))
365
+ s, n = l.is_a?(String) ? [l, r] : [r, l]
366
+ return nil if n.is_a?(Float) && n.nan?
367
+ return nil if n.negative?
368
+
369
+ n = n.infinite? ? 2**31 : n.floor if n.is_a?(Float)
370
+ raise JQError, 'Repeat string result too long' if s.bytesize * n > 2_147_483_647
371
+
372
+ s * n
373
+ elsif l.is_a?(Hash) && r.is_a?(Hash)
374
+ deep_merge(l, r, 0)
375
+ else
376
+ raise JQError, "#{Value.desc(l)} and #{Value.desc(r)} cannot be multiplied"
377
+ end
378
+ end
379
+
380
+ def deep_merge(a, b, depth)
381
+ raise JQError, 'Object merge too deep' if depth > Value::MAX_DEPTH
382
+
383
+ out = a.dup
384
+ b.each do |k, v|
385
+ out[k] = if out[k].is_a?(Hash) && v.is_a?(Hash)
386
+ deep_merge(out[k], v, depth + 1)
387
+ else
388
+ v
389
+ end
390
+ end
391
+ out
392
+ end
393
+
394
+ def divide(l, r)
395
+ if Value.number?(l) && Value.number?(r)
396
+ raise JQError, "#{Value.desc(l)} and #{Value.desc(r)} cannot be divided because the divisor is zero" if r.zero?
397
+
398
+ Value.normalize_number(l.to_f / r)
399
+ elsif l.is_a?(String) && r.is_a?(String)
400
+ split_string(l, r)
401
+ else
402
+ raise JQError, "#{Value.desc(l)} and #{Value.desc(r)} cannot be divided"
403
+ end
404
+ end
405
+
406
+ def split_string(s, sep)
407
+ return [] if s.empty?
408
+ return s.chars if sep.empty?
409
+
410
+ s.split(Regexp.new(Regexp.escape(sep)), -1)
411
+ end
412
+
413
+ INTMAX_MAX = (2**63) - 1
414
+ INTMAX_MIN = -2**63
415
+
416
+ # jq computes on doubles: integers that a double cannot represent exactly
417
+ # are converted before the operation, not after.
418
+ def dbl(n)
419
+ n.is_a?(Integer) && n.abs > Value::MAX_SAFE ? n.to_f : n
420
+ end
421
+
422
+ # Like jq's dtoi: double -> intmax_t, clamped to the int64 range.
423
+ def to_intmax(n)
424
+ n = dbl(n)
425
+ return n if n.is_a?(Integer)
426
+ return 0 if n.nan?
427
+ return INTMAX_MAX if n >= 9.223372036854775807e18
428
+ return INTMAX_MIN if n <= -9.223372036854775808e18
429
+
430
+ n.truncate
431
+ end
432
+
433
+ def modulo(l, r)
434
+ unless Value.number?(l) && Value.number?(r)
435
+ raise JQError, "#{Value.desc(l)} and #{Value.desc(r)} cannot be divided (remainder)"
436
+ end
437
+ return Float::NAN if (l.is_a?(Float) && l.nan?) || (r.is_a?(Float) && r.nan?)
438
+
439
+ a = to_intmax(l)
440
+ b = to_intmax(r)
441
+ if b.zero?
442
+ raise JQError,
443
+ "#{Value.desc(l)} and #{Value.desc(r)} cannot be divided (remainder) because the divisor is zero"
444
+ end
445
+
446
+ Value.normalize_number(a.remainder(b))
447
+ end
448
+ end
449
+ end