jsonata 2.2.2.1 → 2.3.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.
- checksums.yaml +4 -4
- data/.rubocop_local.yml +5 -0
- data/AGENTS.md +14 -3
- data/README.md +15 -5
- data/lib/jsonata/datetime.rb +573 -125
- data/lib/jsonata/evaluator.rb +200 -90
- data/lib/jsonata/functions.rb +237 -205
- data/lib/jsonata/parser.rb +179 -33
- data/lib/jsonata/signature.rb +1 -1
- data/lib/jsonata/utils.rb +83 -11
- data/lib/jsonata/version.rb +1 -1
- metadata +6 -6
data/lib/jsonata/datetime.rb
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
#
|
|
12
12
|
# JavaScript Date objects are represented by UTC Ruby Time objects.
|
|
13
13
|
|
|
14
|
+
require 'set'
|
|
15
|
+
|
|
14
16
|
module Jsonata
|
|
15
17
|
module DateTime
|
|
16
18
|
FEW = %w[Zero One Two Three Four Five Six Seven Eight Nine Ten
|
|
@@ -21,10 +23,15 @@ module Jsonata
|
|
|
21
23
|
MAGNITUDES = %w[Thousand Million Billion Trillion].freeze
|
|
22
24
|
|
|
23
25
|
# converts a number into english words
|
|
24
|
-
# @param value
|
|
25
|
-
# @param ordinal
|
|
26
|
-
# @
|
|
26
|
+
# @param value [String] the value to format
|
|
27
|
+
# @param ordinal [Boolean] ordinal or cardinal form
|
|
28
|
+
# @return [String] representation in words
|
|
27
29
|
def number_to_words(value, ordinal)
|
|
30
|
+
if value.kind_of?(Float) && value.nan?
|
|
31
|
+
# a component of a date that is out of range - formatted as an empty string, as in roman numerals and letters
|
|
32
|
+
return ''
|
|
33
|
+
end
|
|
34
|
+
|
|
28
35
|
lookup = nil
|
|
29
36
|
lookup = lambda do |num, prev, ord|
|
|
30
37
|
words = +''
|
|
@@ -87,10 +94,19 @@ module Jsonata
|
|
|
87
94
|
word_values["#{lword}th"] = val
|
|
88
95
|
end
|
|
89
96
|
end.freeze
|
|
97
|
+
# the words that can make up a number when parsing, in upper case and grouped by their first letter.
|
|
98
|
+
# The order of the words in each group matters - it's the order that they are tried in (see the words cursor in matchParts)
|
|
99
|
+
WORDS_BY_INITIAL = {}.tap do |words_by_initial|
|
|
100
|
+
(Utils.keys(WORD_VALUES) + ['and']).each do |word|
|
|
101
|
+
upper = word.upcase
|
|
102
|
+
initial = upper[0]
|
|
103
|
+
words_by_initial[initial] = (words_by_initial[initial] || []) + [upper]
|
|
104
|
+
end
|
|
105
|
+
end.freeze
|
|
90
106
|
|
|
91
107
|
# Converts a number in english words to numeric value
|
|
92
|
-
# @param text
|
|
93
|
-
# @
|
|
108
|
+
# @param text [String] the number in words
|
|
109
|
+
# @return [Numeric] the numeric value
|
|
94
110
|
def words_to_number(text)
|
|
95
111
|
parts = text.split(/,\s|\sand\s|[\s\\-]/, -1)
|
|
96
112
|
values = parts.map { |part| WORD_VALUES[part] }
|
|
@@ -126,21 +142,32 @@ module Jsonata
|
|
|
126
142
|
[1, 'i']
|
|
127
143
|
].freeze
|
|
128
144
|
|
|
145
|
+
# the largest number that is formatted in roman numerals - ten thousand Ms. Section 4.6.1 allows an upper bound, and
|
|
146
|
+
# larger numbers are formatted using the format token 1
|
|
147
|
+
MAX_ROMAN = 1e7
|
|
148
|
+
|
|
129
149
|
ROMAN_VALUES = {'M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50, 'X' => 10, 'V' => 5, 'I' => 1}.freeze
|
|
130
150
|
|
|
131
151
|
# converts a number to roman numerals
|
|
132
|
-
# @param value
|
|
133
|
-
# @
|
|
152
|
+
# @param value [Numeric] the number
|
|
153
|
+
# @return [String] the number in roman numerals
|
|
134
154
|
def decimal_to_roman(value)
|
|
155
|
+
roman = +''
|
|
135
156
|
ROMAN_NUMERALS.each do |numeral|
|
|
136
|
-
|
|
157
|
+
# (jsonata-js: Math.floor(value / numeral[0]), which is NaN, and so not > 0, when value is NaN)
|
|
158
|
+
quotient = value.fdiv(numeral[0])
|
|
159
|
+
count = quotient.nan? ? 0 : quotient.floor
|
|
160
|
+
if count.positive?
|
|
161
|
+
roman << (numeral[1] * count)
|
|
162
|
+
value -= count * numeral[0]
|
|
163
|
+
end
|
|
137
164
|
end
|
|
138
|
-
|
|
165
|
+
roman
|
|
139
166
|
end
|
|
140
167
|
|
|
141
168
|
# converts roman numerals to a number
|
|
142
|
-
# @param roman
|
|
143
|
-
# @
|
|
169
|
+
# @param roman [String] roman number
|
|
170
|
+
# @return [Numeric] the numeric value
|
|
144
171
|
def roman_to_decimal(roman)
|
|
145
172
|
decimal = 0
|
|
146
173
|
max = 1
|
|
@@ -158,9 +185,9 @@ module Jsonata
|
|
|
158
185
|
end
|
|
159
186
|
|
|
160
187
|
# converts a number to spreadsheet style letters
|
|
161
|
-
# @param value
|
|
162
|
-
# @param a_char
|
|
163
|
-
# @
|
|
188
|
+
# @param value [Numeric] the number
|
|
189
|
+
# @param a_char [String] the character representing the start of the sequence, e.g. 'A'
|
|
190
|
+
# @return [String] the letters
|
|
164
191
|
def decimal_to_letters(value, a_char)
|
|
165
192
|
letters = []
|
|
166
193
|
a_code = a_char.ord
|
|
@@ -172,9 +199,9 @@ module Jsonata
|
|
|
172
199
|
end
|
|
173
200
|
|
|
174
201
|
# converts spreadsheet style letters to a number
|
|
175
|
-
# @param letters
|
|
176
|
-
# @param a_char
|
|
177
|
-
# @
|
|
202
|
+
# @param letters [String] the letters
|
|
203
|
+
# @param a_char [String] the character representing the start of the sequence, e.g. 'A'
|
|
204
|
+
# @return [Numeric] the numeric value
|
|
178
205
|
def letters_to_decimal(letters, a_char)
|
|
179
206
|
a_code = a_char.ord
|
|
180
207
|
decimal = 0
|
|
@@ -186,9 +213,9 @@ module Jsonata
|
|
|
186
213
|
|
|
187
214
|
# Formats an integer as specified by the XPath fn:format-integer function
|
|
188
215
|
# See https://www.w3.org/TR/xpath-functions-31/#func-format-integer
|
|
189
|
-
# @param value
|
|
190
|
-
# @param picture
|
|
191
|
-
# @
|
|
216
|
+
# @param value [Numeric] the number to be formatted
|
|
217
|
+
# @param picture [String] the picture string that specifies the format
|
|
218
|
+
# @return [String] the formatted number
|
|
192
219
|
def format_integer(value, picture)
|
|
193
220
|
return nil if value.nil?
|
|
194
221
|
|
|
@@ -213,9 +240,9 @@ module Jsonata
|
|
|
213
240
|
}.freeze
|
|
214
241
|
|
|
215
242
|
# formats an integer using a preprocessed representation of the picture string
|
|
216
|
-
# @param value
|
|
217
|
-
# @param format
|
|
218
|
-
# @
|
|
243
|
+
# @param value [Numeric] the number to be formatted
|
|
244
|
+
# @param format [Object] the preprocessed representation of the pucture string
|
|
245
|
+
# @return [String] the formatted number
|
|
219
246
|
def _format_integer(value, format)
|
|
220
247
|
formatted_integer = nil
|
|
221
248
|
negative = value.negative?
|
|
@@ -224,8 +251,13 @@ module Jsonata
|
|
|
224
251
|
when FORMATS[:LETTERS]
|
|
225
252
|
formatted_integer = decimal_to_letters(value, format[:case] == TCASE[:UPPER] ? 'A' : 'a')
|
|
226
253
|
when FORMATS[:ROMAN]
|
|
227
|
-
|
|
228
|
-
|
|
254
|
+
if value > MAX_ROMAN
|
|
255
|
+
# there's no numeral larger than M, so a large number would be a long run of them
|
|
256
|
+
formatted_integer = Utils.decimal_digits_to_string(Utils.decimal_digits(value), 0)
|
|
257
|
+
else
|
|
258
|
+
formatted_integer = decimal_to_roman(value)
|
|
259
|
+
formatted_integer = formatted_integer.upcase if format[:case] == TCASE[:UPPER]
|
|
260
|
+
end
|
|
229
261
|
when FORMATS[:WORDS]
|
|
230
262
|
formatted_integer = number_to_words(value, format[:ordinal])
|
|
231
263
|
if format[:case] == TCASE[:UPPER]
|
|
@@ -234,7 +266,7 @@ module Jsonata
|
|
|
234
266
|
formatted_integer = formatted_integer.downcase
|
|
235
267
|
end
|
|
236
268
|
when FORMATS[:DECIMAL]
|
|
237
|
-
formatted_integer = Utils.
|
|
269
|
+
formatted_integer = Utils.decimal_digits_to_string(Utils.decimal_digits(value), 0)
|
|
238
270
|
# TODO use functionPad
|
|
239
271
|
pad_length = format[:mandatory_digits] - formatted_integer.length
|
|
240
272
|
if pad_length.positive?
|
|
@@ -283,8 +315,8 @@ module Jsonata
|
|
|
283
315
|
0xFF10].freeze
|
|
284
316
|
|
|
285
317
|
# preprocesses the picture string
|
|
286
|
-
# @param picture
|
|
287
|
-
# @
|
|
318
|
+
# @param picture [String] picture string
|
|
319
|
+
# @return [Hash] analysed picture
|
|
288
320
|
def analyse_integer_picture(picture)
|
|
289
321
|
format = {
|
|
290
322
|
:type => 'integer',
|
|
@@ -384,8 +416,9 @@ module Jsonata
|
|
|
384
416
|
# find the greatest common divisor of all the positions
|
|
385
417
|
factor = indexes.reduce { |a, b| a.gcd(b) }
|
|
386
418
|
# is every position separated by this divisor? If so, it's regular
|
|
419
|
+
positions = Set.new(indexes)
|
|
387
420
|
(1..indexes.length).each do |index|
|
|
388
|
-
return 0 unless
|
|
421
|
+
return 0 unless positions.include?(index * factor)
|
|
389
422
|
end
|
|
390
423
|
factor
|
|
391
424
|
end
|
|
@@ -419,35 +452,76 @@ module Jsonata
|
|
|
419
452
|
'C' => 'n', 'E' => 'n'
|
|
420
453
|
}.freeze
|
|
421
454
|
|
|
455
|
+
# the largest width modifier allowed in a date/time picture string. The width of a component is padded to the minimum
|
|
456
|
+
# width, so a large width modifier in a short picture string would produce a long string from a small expression
|
|
457
|
+
MAX_WIDTH_MODIFIER = 100
|
|
458
|
+
|
|
459
|
+
# matching a timestamp takes time and memory proportional to the number of positions in it (its length plus one)
|
|
460
|
+
# times the number of parts in the picture string. Limit that to one million (1e6), which is far more than any
|
|
461
|
+
# real timestamp needs
|
|
462
|
+
MAX_MATCH_SIZE = 1e6
|
|
463
|
+
|
|
464
|
+
# The format of fractional seconds (Section 9.8.4.5), and the least and most digits that they are formatted with
|
|
465
|
+
# @param marker_spec [Object] the analysed marker
|
|
466
|
+
# @return [Hash] the format
|
|
467
|
+
def fractional_seconds_format(marker_spec)
|
|
468
|
+
picture = Utils.string_to_array(marker_spec[:presentation1])
|
|
469
|
+
# Append an empty modifier so semicolons in the primary token remain grouping signs.
|
|
470
|
+
format = analyse_integer_picture("#{marker_spec[:presentation1]};")
|
|
471
|
+
min_digits = format[:mandatory_digits]
|
|
472
|
+
max_digits = min_digits + format[:optional_digits]
|
|
473
|
+
if marker_spec[:width]
|
|
474
|
+
min_digits = [min_digits, marker_spec[:width][:min]].max unless marker_spec[:width][:min].nil?
|
|
475
|
+
max_digits = [max_digits, min_digits].max
|
|
476
|
+
max_digits = [max_digits, marker_spec[:width][:max]].max unless marker_spec[:width][:max].nil?
|
|
477
|
+
elsif picture.length == 1
|
|
478
|
+
max_digits = 3 # a single digit allows the full millisecond precision
|
|
479
|
+
end
|
|
480
|
+
{:picture => picture, :format => format, :min_digits => min_digits, :max_digits => max_digits}
|
|
481
|
+
end
|
|
482
|
+
|
|
422
483
|
# Section 9.8.4.1 the format specifier is an array of string literals and variable markers
|
|
423
484
|
# analyse the date-time picture string
|
|
424
|
-
# @param picture
|
|
425
|
-
# @
|
|
426
|
-
|
|
485
|
+
# @param picture [String] picture string
|
|
486
|
+
# @param max_length [Integer, nil] when parsing, the length of the timestamp
|
|
487
|
+
# @return [Hash, nil] the analysed string, or nil if the timestamp is too short to match
|
|
488
|
+
# the picture string, or too long to match it in reasonable time
|
|
489
|
+
def analyse_date_time_picture(picture, max_length = nil)
|
|
427
490
|
spec = []
|
|
428
491
|
format = {
|
|
429
492
|
:type => 'datetime',
|
|
430
493
|
:parts => spec
|
|
431
494
|
}
|
|
495
|
+
# a timestamp needs all the characters of each literal, and at least one character for each marker
|
|
496
|
+
min_length = 0
|
|
497
|
+
cannot_match = lambda do
|
|
498
|
+
!max_length.nil? && (min_length > max_length || spec.length * (max_length + 1) > MAX_MATCH_SIZE)
|
|
499
|
+
end
|
|
500
|
+
# (Ruby specific) positions in the picture string are byte offsets, as indexing a non-ASCII Ruby string by
|
|
501
|
+
# character takes time proportional to the index. '[' and ']' are ASCII, so can't be part of another character
|
|
502
|
+
bytes = picture.b
|
|
432
503
|
add_literal = lambda do |start, end_pos|
|
|
433
504
|
if end_pos > start
|
|
434
|
-
literal = picture
|
|
505
|
+
literal = picture.byteslice(start, end_pos - start)
|
|
435
506
|
# replace any doubled ]] with single ]
|
|
436
507
|
# what if there are instances of single ']' ? - the spec doesn't say
|
|
437
508
|
literal = literal.gsub(']]', ']')
|
|
438
509
|
spec.push({:type => 'literal', :value => literal})
|
|
510
|
+
min_length += literal.length
|
|
439
511
|
end
|
|
440
512
|
end
|
|
441
513
|
|
|
442
514
|
start = 0
|
|
443
515
|
pos = 0
|
|
444
|
-
|
|
445
|
-
|
|
516
|
+
# the whole picture string is analysed, so that it's an error if it's invalid, however long the timestamp is
|
|
517
|
+
while pos < bytes.bytesize
|
|
518
|
+
if bytes.getbyte(pos) == 0x5B # [
|
|
446
519
|
# check it's not a doubled [[
|
|
447
|
-
if
|
|
520
|
+
if bytes.getbyte(pos + 1) == 0x5B
|
|
448
521
|
# literal [
|
|
449
522
|
add_literal.call(start, pos)
|
|
450
523
|
spec.push({:type => 'literal', :value => '['})
|
|
524
|
+
min_length += 1
|
|
451
525
|
pos += 2
|
|
452
526
|
start = pos
|
|
453
527
|
next
|
|
@@ -457,14 +531,14 @@ module Jsonata
|
|
|
457
531
|
add_literal.call(start, pos)
|
|
458
532
|
start = pos
|
|
459
533
|
# search forward to closing ]
|
|
460
|
-
pos =
|
|
534
|
+
pos = bytes.index(']', start)
|
|
461
535
|
# TODO handle error case if pos === -1
|
|
462
536
|
if pos.nil?
|
|
463
537
|
# error - no closing bracket
|
|
464
538
|
raise Jsonata::Error.new('D3135')
|
|
465
539
|
end
|
|
466
540
|
|
|
467
|
-
marker = picture
|
|
541
|
+
marker = picture.byteslice(start + 1, pos - start - 1)
|
|
468
542
|
# whitespace within a variable marker is ignored (i.e. remove it)
|
|
469
543
|
marker = marker.gsub(/\s+/, '')
|
|
470
544
|
defn = {
|
|
@@ -486,7 +560,10 @@ module Jsonata
|
|
|
486
560
|
nil
|
|
487
561
|
else
|
|
488
562
|
# TODO validate wm is an unsigned int
|
|
489
|
-
Utils.parse_int(wm)
|
|
563
|
+
width = Utils.parse_int(wm)
|
|
564
|
+
raise Jsonata::Error.new('D3142', :value => wm) if !width.nil? && width > MAX_WIDTH_MODIFIER
|
|
565
|
+
|
|
566
|
+
width
|
|
490
567
|
end
|
|
491
568
|
end
|
|
492
569
|
if dash.nil?
|
|
@@ -553,9 +630,12 @@ module Jsonata
|
|
|
553
630
|
defn[:n] = w if w >= 2
|
|
554
631
|
end
|
|
555
632
|
end
|
|
556
|
-
# if the previous part is also an integer with no intervening markup, then its width for parsing must be
|
|
633
|
+
# if the previous part is also an integer with no intervening markup, then its width for parsing must be
|
|
634
|
+
# precisely defined - unless this part can't start with a digit, so there's no doubt where the previous one ends.
|
|
635
|
+
# Fractional seconds are always parsed as digits, whatever their presentation
|
|
557
636
|
previous_part = spec[-1]
|
|
558
|
-
if previous_part && previous_part[:integer_format]
|
|
637
|
+
if previous_part && previous_part[:integer_format] &&
|
|
638
|
+
(defn[:integer_format][:primary] == FORMATS[:DECIMAL] || defn[:component] == 'f')
|
|
559
639
|
previous_part[:integer_format][:parse_width] = previous_part[:integer_format][:mandatory_digits]
|
|
560
640
|
end
|
|
561
641
|
end
|
|
@@ -563,12 +643,14 @@ module Jsonata
|
|
|
563
643
|
defn[:integer_format] = analyse_integer_picture(defn[:presentation1])
|
|
564
644
|
end
|
|
565
645
|
spec.push(defn)
|
|
646
|
+
min_length += 1
|
|
566
647
|
start = pos + 1
|
|
567
648
|
end
|
|
568
|
-
pos
|
|
649
|
+
# (jsonata-js: pos++) skip to the next '[', which is the same as stepping over the other characters one by one
|
|
650
|
+
pos = bytes.index('[', pos + 1) || bytes.bytesize
|
|
569
651
|
end
|
|
570
652
|
add_literal.call(start, pos)
|
|
571
|
-
format
|
|
653
|
+
cannot_match.call ? nil : format
|
|
572
654
|
end
|
|
573
655
|
|
|
574
656
|
DAYS = ['', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'].freeze
|
|
@@ -599,7 +681,13 @@ module Jsonata
|
|
|
599
681
|
end
|
|
600
682
|
|
|
601
683
|
# Equivalent of new Date(millis)
|
|
684
|
+
# (Ruby specific) Returns nil for an Invalid Date, i.e. when millis is NaN or beyond the JS time range of
|
|
685
|
+
# +/-8.64e15 ms; the date getters of an Invalid Date return NaN (see get_date_time_fragment).
|
|
686
|
+
# @param millis [Numeric] millis since the epoch
|
|
687
|
+
# @return [Time, nil] the UTC date, or nil for an Invalid Date
|
|
602
688
|
def self.new_date(millis)
|
|
689
|
+
return nil if (millis.kind_of?(Float) && millis.nan?) || millis.abs > 8.64e15
|
|
690
|
+
|
|
603
691
|
Time.at(Rational(millis.truncate, 1000), :in => 'UTC')
|
|
604
692
|
end
|
|
605
693
|
|
|
@@ -641,6 +729,16 @@ module Jsonata
|
|
|
641
729
|
end
|
|
642
730
|
|
|
643
731
|
def get_date_time_fragment(date, component)
|
|
732
|
+
# (Ruby specific) the getters of an Invalid Date return NaN, from which every numeric component is NaN
|
|
733
|
+
if date.nil?
|
|
734
|
+
return case component
|
|
735
|
+
when 'P' then 'am' # NaN >= 12 is false
|
|
736
|
+
when 'C', 'E' then 'ISO'
|
|
737
|
+
when 'Z', 'z' then nil
|
|
738
|
+
else Float::NAN
|
|
739
|
+
end
|
|
740
|
+
end
|
|
741
|
+
|
|
644
742
|
component_value = nil
|
|
645
743
|
case component
|
|
646
744
|
when 'Y' # year
|
|
@@ -749,10 +847,10 @@ module Jsonata
|
|
|
749
847
|
ISO8601_PICTURE = '[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01].[f001][Z01:01t]'
|
|
750
848
|
|
|
751
849
|
# formats the date/time as specified by the XPath fn:format-dateTime function
|
|
752
|
-
# @param millis
|
|
753
|
-
# @param picture
|
|
754
|
-
# @param timezone
|
|
755
|
-
# @
|
|
850
|
+
# @param millis [Numeric] the timestamp to be formatted, in millis since the epoch
|
|
851
|
+
# @param picture [String] the picture string that specifies the format
|
|
852
|
+
# @param timezone [String] the timezone to use
|
|
853
|
+
# @return [String] the formatted timestamp
|
|
756
854
|
def format_date_time(millis, picture, timezone)
|
|
757
855
|
offset_hours = 0
|
|
758
856
|
offset_minutes = 0
|
|
@@ -760,9 +858,9 @@ module Jsonata
|
|
|
760
858
|
unless timezone.nil?
|
|
761
859
|
# parse the hour and minute offsets
|
|
762
860
|
# assume for now the format supplied is +hhmm
|
|
763
|
-
offset = Utils.parse_int(timezone) ||
|
|
764
|
-
offset_hours = (offset / 100.0).floor
|
|
861
|
+
offset = Utils.parse_int(timezone) || Float::NAN # (jsonata-js: parseInt returns NaN)
|
|
765
862
|
offset_minutes = offset.remainder(100)
|
|
863
|
+
offset_hours = (offset - offset_minutes) / 100
|
|
766
864
|
end
|
|
767
865
|
|
|
768
866
|
format_component = lambda do |date, marker_spec|
|
|
@@ -775,13 +873,19 @@ module Jsonata
|
|
|
775
873
|
component_value = marker_spec[:n].nil? ? Float::NAN : component_value % (10**marker_spec[:n])
|
|
776
874
|
end
|
|
777
875
|
if marker_spec[:names]
|
|
876
|
+
# (Ruby specific) a NaN index raises in Ruby, whereas it is undefined in JS
|
|
877
|
+
nan = component_value.kind_of?(Float) && component_value.nan?
|
|
778
878
|
if marker_spec[:component] == 'M' || marker_spec[:component] == 'x'
|
|
779
|
-
component_value = MONTHS[component_value - 1]
|
|
879
|
+
component_value = nan ? nil : MONTHS[component_value - 1]
|
|
780
880
|
elsif marker_spec[:component] == 'F'
|
|
781
|
-
component_value = DAYS[component_value]
|
|
881
|
+
component_value = nan ? nil : DAYS[component_value]
|
|
782
882
|
else
|
|
783
883
|
raise Jsonata::Error.new('D3133', :value => marker_spec[:component])
|
|
784
884
|
end
|
|
885
|
+
if component_value.nil?
|
|
886
|
+
# the date is out of range - formatted as an empty string, as in roman numerals and letters
|
|
887
|
+
component_value = ''
|
|
888
|
+
end
|
|
785
889
|
if marker_spec[:names] == TCASE[:UPPER]
|
|
786
890
|
component_value = component_value.upcase
|
|
787
891
|
elsif marker_spec[:names] == TCASE[:LOWER]
|
|
@@ -795,25 +899,55 @@ module Jsonata
|
|
|
795
899
|
component_value = _format_integer(component_value, marker_spec[:integer_format])
|
|
796
900
|
end
|
|
797
901
|
elsif marker_spec[:component] == 'f'
|
|
798
|
-
#
|
|
799
|
-
|
|
902
|
+
# Section 9.8.4.5 Formatting Fractional Seconds
|
|
903
|
+
raise Jsonata::Error.new('D3133', :value => marker_spec[:component]) if marker_spec[:names]
|
|
904
|
+
|
|
905
|
+
if marker_spec[:integer_format][:primary] != FORMATS[:DECIMAL] ||
|
|
906
|
+
(component_value.kind_of?(Float) && component_value.nan?)
|
|
907
|
+
component_value = _format_integer(component_value, marker_spec[:integer_format])
|
|
908
|
+
else
|
|
909
|
+
fractional_format = fractional_seconds_format(marker_spec)
|
|
910
|
+
picture = fractional_format[:picture]
|
|
911
|
+
format = fractional_format[:format]
|
|
912
|
+
min_digits = fractional_format[:min_digits]
|
|
913
|
+
max_digits = fractional_format[:max_digits]
|
|
914
|
+
|
|
915
|
+
fraction = "00#{component_value}"[-3..].sub(/0+\z/, '')
|
|
916
|
+
fraction += '0' * (min_digits - fraction.length) if fraction.length < min_digits
|
|
917
|
+
# Pad and truncate on the right, without rounding into the next second.
|
|
918
|
+
digits = Utils.string_to_array(fraction[0, max_digits]).map do |digit|
|
|
919
|
+
(format[:zero_code] + digit.to_i).chr(Encoding::UTF_8)
|
|
920
|
+
end
|
|
921
|
+
# Count grouping positions from the left. The output cannot exceed the
|
|
922
|
+
# adjusted picture's width, so separators never need to be extrapolated.
|
|
923
|
+
position = 0
|
|
924
|
+
picture.each do |character|
|
|
925
|
+
code = character.ord
|
|
926
|
+
if character == '#' || code.between?(format[:zero_code], format[:zero_code] + 9)
|
|
927
|
+
position += 1
|
|
928
|
+
elsif position < digits.length
|
|
929
|
+
digits[position] = character + digits[position]
|
|
930
|
+
end
|
|
931
|
+
end
|
|
932
|
+
component_value = digits.join
|
|
933
|
+
end
|
|
800
934
|
elsif marker_spec[:component] == 'Z' || marker_spec[:component] == 'z'
|
|
801
935
|
# Section 9.8.4.6 Formatting timezones
|
|
802
936
|
offset = (offset_hours * 100) + offset_minutes
|
|
803
937
|
if marker_spec[:integer_format][:regular]
|
|
804
|
-
component_value = _format_integer(offset, marker_spec[:integer_format])
|
|
938
|
+
component_value = _format_integer(offset.abs, marker_spec[:integer_format])
|
|
805
939
|
else
|
|
806
940
|
num_digits = marker_spec[:integer_format][:mandatory_digits]
|
|
807
941
|
if [1, 2].include?(num_digits)
|
|
808
|
-
component_value = _format_integer(offset_hours, marker_spec[:integer_format])
|
|
809
|
-
component_value += ":#{format_integer(offset_minutes, '00')}" if offset_minutes != 0
|
|
942
|
+
component_value = _format_integer(offset_hours.abs, marker_spec[:integer_format])
|
|
943
|
+
component_value += ":#{format_integer(offset_minutes.abs, '00')}" if offset_minutes != 0
|
|
810
944
|
elsif [3, 4].include?(num_digits)
|
|
811
|
-
component_value = _format_integer(offset, marker_spec[:integer_format])
|
|
945
|
+
component_value = _format_integer(offset.abs, marker_spec[:integer_format])
|
|
812
946
|
else
|
|
813
947
|
raise Jsonata::Error.new('D3134', :value => num_digits)
|
|
814
948
|
end
|
|
815
949
|
end
|
|
816
|
-
component_value =
|
|
950
|
+
component_value = (offset.negative? ? '-' : '+') + component_value
|
|
817
951
|
component_value = "GMT#{component_value}" if marker_spec[:component] == 'z'
|
|
818
952
|
component_value = 'Z' if offset.zero? && marker_spec[:presentation2] == 't'
|
|
819
953
|
elsif marker_spec[:component] == 'P'
|
|
@@ -847,55 +981,125 @@ module Jsonata
|
|
|
847
981
|
result
|
|
848
982
|
end
|
|
849
983
|
|
|
850
|
-
#
|
|
851
|
-
#
|
|
852
|
-
# @
|
|
853
|
-
|
|
984
|
+
# Convert the digits of a number from a decimal digit family to ASCII digits. Any other characters are left as they
|
|
985
|
+
# are, so that parseInt reads (or stops at) them as it would anyway
|
|
986
|
+
# @param digits [String] the number
|
|
987
|
+
# @param zero_code [Integer] the code point of the zero digit of the family
|
|
988
|
+
# @return [String] the number in ASCII digits
|
|
989
|
+
def to_ascii_digits(digits, zero_code)
|
|
990
|
+
# (jsonata-js: converts in chunks, since String.fromCodePoint can't take an unlimited number of arguments)
|
|
991
|
+
digits.each_codepoint.map do |code|
|
|
992
|
+
code.between?(zero_code, zero_code + 9) ? code - zero_code + 0x30 : code
|
|
993
|
+
end.pack('U*')
|
|
994
|
+
end
|
|
995
|
+
|
|
996
|
+
# Remove the grouping separators of an integer picture string from a number, in one pass over the number
|
|
997
|
+
# @param digits [String] the number
|
|
998
|
+
# @param format_spec [Object] the analysed integer picture string
|
|
999
|
+
# @return [String] the number without its grouping separators
|
|
1000
|
+
def remove_grouping_separators(digits, format_spec)
|
|
1001
|
+
separators = format_spec[:regular] ? [format_spec[:grouping_separators]] : format_spec[:grouping_separators]
|
|
1002
|
+
return digits if separators.empty?
|
|
1003
|
+
|
|
1004
|
+
characters = Set.new(separators.map { |separator| separator[:character] })
|
|
1005
|
+
# (jsonata-js: steps through the code points, copying the text between the separators)
|
|
1006
|
+
digits.each_char.reject { |char| characters.include?(char) }.join
|
|
1007
|
+
end
|
|
1008
|
+
|
|
1009
|
+
# sets of characters for matching parts of a timestamp - in upper case, since timestamps are matched case-insensitively
|
|
1010
|
+
DIGIT_CHARS = '0123456789'
|
|
1011
|
+
LETTER_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
1012
|
+
ROMAN_CHARS = 'MDCLXVI'
|
|
1013
|
+
WORD_SEPARATORS = '-, '
|
|
1014
|
+
ORDINAL_SUFFIXES = %w[TH ST ND RD].freeze
|
|
1015
|
+
|
|
1016
|
+
# Generate a matcher to parse integers or timestamps
|
|
1017
|
+
# @param format_spec [Object] object representing the format
|
|
1018
|
+
# @return [Object] matcher
|
|
1019
|
+
def generate_matcher(format_spec)
|
|
854
1020
|
matcher = {}
|
|
855
1021
|
if format_spec[:type] == 'datetime'
|
|
856
1022
|
matcher[:type] = 'datetime'
|
|
1023
|
+
# each part matches one of:
|
|
1024
|
+
# literal - the given text
|
|
1025
|
+
# words - a number in words
|
|
1026
|
+
# chars - a run of these characters, either the longest that lets the rest of the timestamp match,
|
|
1027
|
+
# or a fixed number of them (width), and optionally followed by an ordinal suffix (ordinal).
|
|
1028
|
+
# A timezone has a prefix before the run, and the run can be split by a separator (hours and minutes)
|
|
857
1029
|
matcher[:parts] = format_spec[:parts].map do |part|
|
|
858
1030
|
res = {}
|
|
859
1031
|
if part[:type] == 'literal'
|
|
860
|
-
res[:
|
|
1032
|
+
res[:literal] = canonicalize(part[:value])
|
|
861
1033
|
elsif part[:component] == 'Z' || part[:component] == 'z'
|
|
862
1034
|
# timezone
|
|
863
1035
|
separator = nil
|
|
864
1036
|
separator = part[:integer_format][:grouping_separators] unless part[:integer_format][:grouping_separators].kind_of?(Array)
|
|
865
|
-
|
|
866
|
-
res[:
|
|
867
|
-
res[:
|
|
868
|
-
|
|
1037
|
+
# the possible characters at each position of the prefix
|
|
1038
|
+
res[:prefix] = part[:component] == 'z' ? ['G', 'M', 'T', '-+'] : ['-+']
|
|
1039
|
+
res[:chars] = DIGIT_CHARS
|
|
1040
|
+
if separator
|
|
1041
|
+
res[:separator] = canonicalize(separator[:character])
|
|
1042
|
+
elsif part[:integer_format][:mandatory_digits] <= 2
|
|
1043
|
+
# [Z0] and [Z01] format any minutes as two digits after a colon
|
|
1044
|
+
res[:minutes] = true
|
|
1045
|
+
end
|
|
1046
|
+
# if directly followed by another number in digits, then the number of digits (after any separator) is fixed,
|
|
1047
|
+
# if the timezone is always formatted with that many: the minutes after a separator, and the hours in [Z01]
|
|
1048
|
+
# or the hours and minutes in [Z0001]. Otherwise, it's matched like any other number of digits
|
|
1049
|
+
if Utils.truthy?(part[:integer_format][:parse_width])
|
|
1050
|
+
if separator
|
|
1051
|
+
res[:width] = separator[:position]
|
|
1052
|
+
elsif part[:integer_format][:mandatory_digits] == 2 || part[:integer_format][:mandatory_digits] == 4
|
|
1053
|
+
res[:width] = part[:integer_format][:mandatory_digits]
|
|
1054
|
+
end
|
|
1055
|
+
end
|
|
869
1056
|
res[:parse] = lambda do |value|
|
|
870
1057
|
value = value[3..] if part[:component] == 'z' # remove the leading GMT
|
|
1058
|
+
# the sign applies to both the hours and the minutes, including offsets such as -00:30
|
|
1059
|
+
sign = value[0] == '-' ? -1 : 1
|
|
1060
|
+
value = value[1..]
|
|
871
1061
|
offset_hours = 0
|
|
872
1062
|
offset_minutes = 0
|
|
873
1063
|
if separator
|
|
874
|
-
|
|
875
|
-
|
|
1064
|
+
# the separator could be the same as the sign, and it is matched case-insensitively
|
|
1065
|
+
position = canonicalize(value).index(canonicalize(separator[:character]))
|
|
1066
|
+
offset_hours = Utils.parse_int(value[0, position])
|
|
1067
|
+
offset_minutes = Utils.parse_int(value[(position + separator[:character].length)..])
|
|
1068
|
+
elsif value.include?(':')
|
|
1069
|
+
# the minutes after a colon, from [Z0] or [Z01]
|
|
1070
|
+
offset_hours = Utils.parse_int(value[0, value.index(':')])
|
|
1071
|
+
offset_minutes = Utils.parse_int(value[(value.index(':') + 1)..])
|
|
876
1072
|
else
|
|
877
1073
|
# depends on number of digits
|
|
878
|
-
numdigits = value.length
|
|
1074
|
+
numdigits = value.length
|
|
879
1075
|
if numdigits <= 2
|
|
880
1076
|
# just hour offset
|
|
881
1077
|
offset_hours = Utils.parse_int(value)
|
|
882
1078
|
else
|
|
883
|
-
offset_hours = Utils.parse_int(value[0,
|
|
884
|
-
offset_minutes = Utils.parse_int(value[
|
|
1079
|
+
offset_hours = Utils.parse_int(value[0, numdigits - 2])
|
|
1080
|
+
offset_minutes = Utils.parse_int(value[(numdigits - 2)..])
|
|
885
1081
|
end
|
|
886
1082
|
end
|
|
887
|
-
(offset_hours * 60) + offset_minutes
|
|
1083
|
+
sign * ((offset_hours * 60) + offset_minutes)
|
|
888
1084
|
end
|
|
889
1085
|
elsif part[:component] == 'f'
|
|
890
|
-
res[:
|
|
1086
|
+
res[:chars] = DIGIT_CHARS
|
|
1087
|
+
# if directly followed by another number in digits, then the number of digits is fixed, if the fractional
|
|
1088
|
+
# seconds are always formatted with the same number of digits. Otherwise, they're matched like any other
|
|
1089
|
+
# number of digits
|
|
1090
|
+
if part[:integer_format] && Utils.truthy?(part[:integer_format][:parse_width]) &&
|
|
1091
|
+
part[:integer_format][:primary] == FORMATS[:DECIMAL]
|
|
1092
|
+
format = fractional_seconds_format(part)
|
|
1093
|
+
res[:width] = format[:min_digits] if format[:min_digits] == format[:max_digits]
|
|
1094
|
+
end
|
|
891
1095
|
res[:parse] = lambda do |value|
|
|
892
1096
|
Float("0.#{value[0, 3]}") * 1000
|
|
893
1097
|
end
|
|
894
1098
|
elsif part[:integer_format]
|
|
895
|
-
res =
|
|
1099
|
+
res = generate_matcher(part[:integer_format])
|
|
896
1100
|
else
|
|
897
1101
|
# must be a month or day name
|
|
898
|
-
res[:
|
|
1102
|
+
res[:chars] = LETTER_CHARS
|
|
899
1103
|
lookup = {}
|
|
900
1104
|
if part[:component] == 'M' || part[:component] == 'x'
|
|
901
1105
|
# months
|
|
@@ -918,7 +1122,8 @@ module Jsonata
|
|
|
918
1122
|
end
|
|
919
1123
|
end
|
|
920
1124
|
elsif part[:component] == 'P'
|
|
921
|
-
lookup
|
|
1125
|
+
lookup['am'] = lookup['AM'] = 0
|
|
1126
|
+
lookup['pm'] = lookup['PM'] = 1
|
|
922
1127
|
else
|
|
923
1128
|
# unsupported 'name' option for this component
|
|
924
1129
|
raise Jsonata::Error.new('D3133', :value => part[:component])
|
|
@@ -934,31 +1139,24 @@ module Jsonata
|
|
|
934
1139
|
|
|
935
1140
|
case format_spec[:primary]
|
|
936
1141
|
when FORMATS[:LETTERS]
|
|
937
|
-
matcher[:
|
|
1142
|
+
matcher[:chars] = LETTER_CHARS
|
|
938
1143
|
matcher[:parse] = lambda do |value|
|
|
939
1144
|
letters_to_decimal(value, is_upper ? 'A' : 'a')
|
|
940
1145
|
end
|
|
941
1146
|
when FORMATS[:ROMAN]
|
|
942
|
-
matcher[:
|
|
1147
|
+
matcher[:chars] = ROMAN_CHARS
|
|
943
1148
|
matcher[:parse] = lambda do |value|
|
|
944
1149
|
roman_to_decimal(is_upper ? value : value.upcase)
|
|
945
1150
|
end
|
|
946
1151
|
when FORMATS[:WORDS]
|
|
947
|
-
matcher[:
|
|
1152
|
+
matcher[:words] = true
|
|
948
1153
|
matcher[:parse] = lambda do |value|
|
|
949
1154
|
words_to_number(value.downcase)
|
|
950
1155
|
end
|
|
951
1156
|
when FORMATS[:DECIMAL]
|
|
952
|
-
matcher[:
|
|
953
|
-
matcher[:
|
|
954
|
-
|
|
955
|
-
else
|
|
956
|
-
'+'
|
|
957
|
-
end
|
|
958
|
-
if format_spec[:ordinal]
|
|
959
|
-
# ordinals
|
|
960
|
-
matcher[:regex] << '(?:th|st|nd|rd)'
|
|
961
|
-
end
|
|
1157
|
+
matcher[:chars] = DIGIT_CHARS
|
|
1158
|
+
matcher[:width] = format_spec[:parse_width]
|
|
1159
|
+
matcher[:ordinal] = format_spec[:ordinal]
|
|
962
1160
|
matcher[:parse] = lambda do |value|
|
|
963
1161
|
digits = value
|
|
964
1162
|
if format_spec[:ordinal]
|
|
@@ -966,17 +1164,8 @@ module Jsonata
|
|
|
966
1164
|
digits = value[0...-2]
|
|
967
1165
|
end
|
|
968
1166
|
# strip out the separators
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
else
|
|
972
|
-
format_spec[:grouping_separators].each do |sep|
|
|
973
|
-
digits = digits.split(sep[:character], -1).join
|
|
974
|
-
end
|
|
975
|
-
end
|
|
976
|
-
if format_spec[:zero_code] != 0x30
|
|
977
|
-
# apply offset
|
|
978
|
-
digits = digits.each_char.map { |char| (char.ord - format_spec[:zero_code] + 0x30).chr(Encoding::UTF_8) }.join
|
|
979
|
-
end
|
|
1167
|
+
digits = remove_grouping_separators(digits, format_spec)
|
|
1168
|
+
digits = to_ascii_digits(digits, format_spec[:zero_code]) if format_spec[:zero_code] != 0x30
|
|
980
1169
|
Utils.parse_int(digits)
|
|
981
1170
|
end
|
|
982
1171
|
when FORMATS[:SEQUENCE]
|
|
@@ -987,32 +1176,291 @@ module Jsonata
|
|
|
987
1176
|
matcher
|
|
988
1177
|
end
|
|
989
1178
|
|
|
1179
|
+
# Converts a string to the form used to compare strings case-insensitively, in the same way as a regex with
|
|
1180
|
+
# the 'i' flag: each character in upper case, unless that would change its length or make a non-ASCII character ASCII
|
|
1181
|
+
# @param str [String] the string to convert
|
|
1182
|
+
# @return [String] the converted string, which has the same length
|
|
1183
|
+
def canonicalize(str)
|
|
1184
|
+
return str.upcase if str.ascii_only?
|
|
1185
|
+
|
|
1186
|
+
# (jsonata-js: builds the result in an array of code units, to save memory)
|
|
1187
|
+
str.each_char.map do |ch|
|
|
1188
|
+
# (Ruby specific) a lone surrogate (invalid UTF-8) is left as it is
|
|
1189
|
+
next ch unless ch.valid_encoding?
|
|
1190
|
+
|
|
1191
|
+
upper = ch.upcase
|
|
1192
|
+
upper.length == 1 && !(ch.ord >= 128 && upper.ord < 128) ? upper : ch
|
|
1193
|
+
end.join
|
|
1194
|
+
end
|
|
1195
|
+
|
|
1196
|
+
# flags recorded for each part of the picture string at each position in the timestamp
|
|
1197
|
+
FAILED = 1 # the rest of the picture string can't be matched from this part at this position
|
|
1198
|
+
VISITED = 2 # a number in words has already been continued from this position (see the words cursor)
|
|
1199
|
+
|
|
1200
|
+
# Match a timestamp against the parts of a picture string, finding the same match as the regex made by
|
|
1201
|
+
# concatenating a capturing group for each part. Like a backtracking regex engine, it tries the possible matches
|
|
1202
|
+
# of each part in turn, in the same order, until the rest of the timestamp matches the rest of the parts.
|
|
1203
|
+
# Unlike a regex, it never tries to match the rest of the parts from the same part and position twice, so it takes
|
|
1204
|
+
# time proportional to the length of the timestamp times the number of parts, however the picture string is
|
|
1205
|
+
# constructed. On a timestamp that doesn't match, a regex can take time that grows exponentially with the number
|
|
1206
|
+
# of parts that can match the same characters (a ReDoS attack). The timestamp's length plus one, times the number of
|
|
1207
|
+
# parts, must be no more than maxMatchSize, which analyseDateTimePicture checks when parsing
|
|
1208
|
+
# @param parts [Array] the parts of the picture string, as generated by generateMatcher
|
|
1209
|
+
# @param timestamp [String] the timestamp to match
|
|
1210
|
+
# @return [Array, nil] the text matching each part, or null if the timestamp doesn't match
|
|
1211
|
+
def match_parts(parts, timestamp)
|
|
1212
|
+
count = parts.length
|
|
1213
|
+
stride = timestamp.length + 1
|
|
1214
|
+
if count.zero?
|
|
1215
|
+
return timestamp.empty? ? [] : nil
|
|
1216
|
+
end
|
|
1217
|
+
|
|
1218
|
+
text = canonicalize(timestamp)
|
|
1219
|
+
# (Ruby specific) the characters of the text, since indexing a non-ASCII Ruby string by character takes time
|
|
1220
|
+
# proportional to the index
|
|
1221
|
+
text_chars = Utils.string_to_array(text)
|
|
1222
|
+
length = text_chars.length
|
|
1223
|
+
memo = Array.new(stride * count, 0)
|
|
1224
|
+
run_ends = {}
|
|
1225
|
+
# (Ruby specific) keyed by the literal String object, since hashing a long Ruby string takes time proportional to
|
|
1226
|
+
# its length every time
|
|
1227
|
+
literal_starts = {}.compare_by_identity
|
|
1228
|
+
# for each part that matches a range of positions, the lowest position tried, plus one (zero if none tried yet)
|
|
1229
|
+
lowest_tried = Array.new(stride * count, 0)
|
|
1230
|
+
|
|
1231
|
+
# whether the literal text occurs at the given position. All the positions where it occurs are found the first
|
|
1232
|
+
# time, in one pass over the timestamp (Knuth-Morris-Pratt), so that each test takes constant time however long
|
|
1233
|
+
# the literal is
|
|
1234
|
+
literal_at = lambda do |literal, pos|
|
|
1235
|
+
starts = literal_starts[literal]
|
|
1236
|
+
unless starts
|
|
1237
|
+
literal_chars = Utils.string_to_array(literal) # (Ruby specific)
|
|
1238
|
+
# the length of the longest proper prefix of the literal that is also a suffix of each prefix of it
|
|
1239
|
+
overlap = Array.new(literal_chars.length, 0)
|
|
1240
|
+
kk = 0
|
|
1241
|
+
(1...literal_chars.length).each do |ii|
|
|
1242
|
+
kk = overlap[kk - 1] while kk.positive? && literal_chars[ii] != literal_chars[kk]
|
|
1243
|
+
kk += 1 if literal_chars[ii] == literal_chars[kk]
|
|
1244
|
+
overlap[ii] = kk
|
|
1245
|
+
end
|
|
1246
|
+
starts = Array.new(stride, 0)
|
|
1247
|
+
kk = 0
|
|
1248
|
+
(0...length).each do |ii|
|
|
1249
|
+
kk = overlap[kk - 1] while kk.positive? && text_chars[ii] != literal_chars[kk]
|
|
1250
|
+
kk += 1 if text_chars[ii] == literal_chars[kk]
|
|
1251
|
+
if kk == literal_chars.length
|
|
1252
|
+
starts[ii - kk + 1] = 1
|
|
1253
|
+
kk = overlap[kk - 1]
|
|
1254
|
+
end
|
|
1255
|
+
end
|
|
1256
|
+
literal_starts[literal] = starts
|
|
1257
|
+
end
|
|
1258
|
+
starts[pos] == 1
|
|
1259
|
+
end
|
|
1260
|
+
|
|
1261
|
+
# the position after the run of characters from the given set that starts at the given position
|
|
1262
|
+
run_end = lambda do |chars, pos|
|
|
1263
|
+
ends = run_ends[chars]
|
|
1264
|
+
unless ends
|
|
1265
|
+
ends = Array.new(stride, 0)
|
|
1266
|
+
ends[length] = length
|
|
1267
|
+
(length - 1).downto(0) do |ii|
|
|
1268
|
+
ends[ii] = chars.include?(text_chars[ii]) ? ends[ii + 1] : ii
|
|
1269
|
+
end
|
|
1270
|
+
run_ends[chars] = ends
|
|
1271
|
+
end
|
|
1272
|
+
ends[pos]
|
|
1273
|
+
end
|
|
1274
|
+
|
|
1275
|
+
starts_with = lambda do |str, pos|
|
|
1276
|
+
text_chars[pos, str.length]&.join == str
|
|
1277
|
+
end
|
|
1278
|
+
|
|
1279
|
+
# the words (or separator) that a number in words can continue with at the given position, in the order they are tried
|
|
1280
|
+
next_words = lambda do |pos|
|
|
1281
|
+
ch = text_chars[pos]
|
|
1282
|
+
WORDS_BY_INITIAL[ch] || (!ch.nil? && WORD_SEPARATORS.include?(ch) ? [ch] : [])
|
|
1283
|
+
end
|
|
1284
|
+
|
|
1285
|
+
# a cursor for each part, which steps through the positions where that part can end, given where it starts,
|
|
1286
|
+
# in the order that a regex would try them. Only one cursor per part is ever in use at a time
|
|
1287
|
+
cursors = parts.each_with_index.map do |part, index|
|
|
1288
|
+
base = index * stride
|
|
1289
|
+
if part[:words]
|
|
1290
|
+
# a number in words matches like the regex (?:word1|word2|...|[-, ])+ which tries to continue with
|
|
1291
|
+
# each word in turn before ending at the current position. So the possible ends are tried in the
|
|
1292
|
+
# order of a depth first search through the words, but each position is only searched from once,
|
|
1293
|
+
# since the ends found by searching from the same position again would already have failed
|
|
1294
|
+
positions = []
|
|
1295
|
+
options = []
|
|
1296
|
+
tried = []
|
|
1297
|
+
depth = 0
|
|
1298
|
+
push = lambda do |pos|
|
|
1299
|
+
positions[depth] = pos
|
|
1300
|
+
options[depth] = next_words.call(pos)
|
|
1301
|
+
tried[depth] = 0
|
|
1302
|
+
depth += 1
|
|
1303
|
+
end
|
|
1304
|
+
next({
|
|
1305
|
+
:start => lambda do |pos|
|
|
1306
|
+
depth = 0
|
|
1307
|
+
push.call(pos) if memo[base + pos].nobits?(VISITED)
|
|
1308
|
+
end,
|
|
1309
|
+
:next => lambda do
|
|
1310
|
+
while depth.positive?
|
|
1311
|
+
top = depth - 1
|
|
1312
|
+
if tried[top] < options[top].length
|
|
1313
|
+
word = options[top][tried[top]]
|
|
1314
|
+
tried[top] += 1
|
|
1315
|
+
pos = positions[top] + word.length
|
|
1316
|
+
if starts_with.call(word, positions[top]) && memo[base + pos].nobits?(VISITED)
|
|
1317
|
+
memo[base + pos] |= VISITED
|
|
1318
|
+
push.call(pos)
|
|
1319
|
+
end
|
|
1320
|
+
else
|
|
1321
|
+
depth -= 1
|
|
1322
|
+
if top.positive?
|
|
1323
|
+
return positions[top] # the number has to have at least one word
|
|
1324
|
+
end
|
|
1325
|
+
end
|
|
1326
|
+
end
|
|
1327
|
+
-1
|
|
1328
|
+
end
|
|
1329
|
+
})
|
|
1330
|
+
end
|
|
1331
|
+
# otherwise the part can end at a single position, or anywhere in a range of positions which are tried from
|
|
1332
|
+
# the highest to the lowest (like a greedy regex). Ranges that end at the same position share the record of
|
|
1333
|
+
# the lowest position tried, since the higher positions would already have failed
|
|
1334
|
+
low = 0
|
|
1335
|
+
high = -1
|
|
1336
|
+
key = nil
|
|
1337
|
+
first = -1
|
|
1338
|
+
only = lambda do |end_pos|
|
|
1339
|
+
low = high = end_pos
|
|
1340
|
+
end
|
|
1341
|
+
{
|
|
1342
|
+
:start => lambda do |pos|
|
|
1343
|
+
# no possible ends, unless found below
|
|
1344
|
+
low = 0
|
|
1345
|
+
high = -1
|
|
1346
|
+
key = nil
|
|
1347
|
+
first = -1
|
|
1348
|
+
unless part[:literal].nil?
|
|
1349
|
+
only.call(pos + part[:literal].length) if literal_at.call(part[:literal], pos)
|
|
1350
|
+
return
|
|
1351
|
+
end
|
|
1352
|
+
from = pos
|
|
1353
|
+
if part[:prefix]
|
|
1354
|
+
ii = 0
|
|
1355
|
+
while ii < part[:prefix].length
|
|
1356
|
+
return if from == length || !part[:prefix][ii].include?(text_chars[from])
|
|
1357
|
+
|
|
1358
|
+
from += 1
|
|
1359
|
+
ii += 1
|
|
1360
|
+
end
|
|
1361
|
+
end
|
|
1362
|
+
if part[:separator]
|
|
1363
|
+
# the separator isn't one of the characters before it, so they must all be before it
|
|
1364
|
+
head = run_end.call(part[:chars], from)
|
|
1365
|
+
return if head == from || !starts_with.call(part[:separator], head)
|
|
1366
|
+
|
|
1367
|
+
from = head + part[:separator].length
|
|
1368
|
+
end
|
|
1369
|
+
end_pos = run_end.call(part[:chars], from)
|
|
1370
|
+
return if end_pos == from
|
|
1371
|
+
|
|
1372
|
+
if Utils.truthy?(part[:width])
|
|
1373
|
+
# a width that isn't a positive whole number (e.g. from the width modifier [Y,1--2]) can't match anything
|
|
1374
|
+
return if !part[:width].positive? || from + part[:width] > end_pos
|
|
1375
|
+
|
|
1376
|
+
end_pos = from + part[:width]
|
|
1377
|
+
end
|
|
1378
|
+
if part[:minutes] && text_chars[end_pos] == ':' && run_end.call(DIGIT_CHARS, end_pos + 1) >= end_pos + 3
|
|
1379
|
+
# the optional minutes of a timezone (two digits after a colon) are tried before the hours on their
|
|
1380
|
+
# own, as the regex (?::[0-9]{2})? would
|
|
1381
|
+
first = end_pos + 3
|
|
1382
|
+
end
|
|
1383
|
+
if !Utils.truthy?(part[:width]) && !part[:ordinal]
|
|
1384
|
+
key = base + end_pos
|
|
1385
|
+
low = from + 1
|
|
1386
|
+
high = lowest_tried[key].zero? ? end_pos : [end_pos, lowest_tried[key] - 2].min
|
|
1387
|
+
return
|
|
1388
|
+
end
|
|
1389
|
+
# a fixed number of characters, or all of them if followed by an ordinal suffix (which isn't one of them)
|
|
1390
|
+
if !part[:ordinal]
|
|
1391
|
+
only.call(end_pos)
|
|
1392
|
+
elsif ORDINAL_SUFFIXES.include?(text_chars[end_pos, 2]&.join)
|
|
1393
|
+
only.call(end_pos + 2)
|
|
1394
|
+
end
|
|
1395
|
+
end,
|
|
1396
|
+
:next => lambda do
|
|
1397
|
+
if first != -1
|
|
1398
|
+
end_pos = first
|
|
1399
|
+
first = -1
|
|
1400
|
+
return end_pos
|
|
1401
|
+
end
|
|
1402
|
+
return -1 if high < low
|
|
1403
|
+
|
|
1404
|
+
lowest_tried[key] = high + 1 unless key.nil?
|
|
1405
|
+
high -= 1
|
|
1406
|
+
high + 1 # (jsonata-js: return high--)
|
|
1407
|
+
end
|
|
1408
|
+
}
|
|
1409
|
+
end
|
|
1410
|
+
|
|
1411
|
+
# search for the end of each part in turn, backtracking to the previous part when the current one can't be matched
|
|
1412
|
+
starts = []
|
|
1413
|
+
ends = []
|
|
1414
|
+
depth = 0
|
|
1415
|
+
enter = lambda do |index, pos|
|
|
1416
|
+
starts[index] = pos
|
|
1417
|
+
cursors[index][:start].call(pos)
|
|
1418
|
+
depth = index + 1
|
|
1419
|
+
end
|
|
1420
|
+
enter.call(0, 0)
|
|
1421
|
+
while depth.positive?
|
|
1422
|
+
index = depth - 1
|
|
1423
|
+
end_pos = cursors[index][:next].call
|
|
1424
|
+
if end_pos == -1
|
|
1425
|
+
memo[(index * stride) + starts[index]] |= FAILED
|
|
1426
|
+
depth -= 1
|
|
1427
|
+
elsif index == count - 1
|
|
1428
|
+
if end_pos == length
|
|
1429
|
+
ends[index] = end_pos
|
|
1430
|
+
timestamp_chars = Utils.string_to_array(timestamp) # (Ruby specific)
|
|
1431
|
+
return starts.each_with_index.map { |start, ii| timestamp_chars[start...ends[ii]].join }
|
|
1432
|
+
end
|
|
1433
|
+
elsif memo[((index + 1) * stride) + end_pos].nobits?(FAILED)
|
|
1434
|
+
ends[index] = end_pos
|
|
1435
|
+
enter.call(index + 1, end_pos)
|
|
1436
|
+
end
|
|
1437
|
+
end
|
|
1438
|
+
nil
|
|
1439
|
+
end
|
|
1440
|
+
|
|
990
1441
|
# parse a string containing an integer as specified by the picture string
|
|
991
|
-
# @param value
|
|
992
|
-
# @param picture
|
|
993
|
-
# @
|
|
1442
|
+
# @param value [String] the string to parse
|
|
1443
|
+
# @param picture [String] the picture string
|
|
1444
|
+
# @return [Numeric] the parsed number
|
|
994
1445
|
def parse_integer(value, picture)
|
|
995
1446
|
return nil if value.nil?
|
|
996
1447
|
|
|
997
1448
|
format_spec = analyse_integer_picture(picture)
|
|
998
|
-
match_spec =
|
|
999
|
-
#
|
|
1000
|
-
# const matcher = new RegExp(fullRegex);
|
|
1001
|
-
# TODO validate input based on the matcher regex
|
|
1449
|
+
match_spec = generate_matcher(format_spec)
|
|
1450
|
+
# TODO validate input using matchParts
|
|
1002
1451
|
match_spec[:parse].call(value)
|
|
1003
1452
|
end
|
|
1004
1453
|
|
|
1005
1454
|
# parse a string containing a timestamp as specified by the picture string
|
|
1006
|
-
# @param timestamp
|
|
1007
|
-
# @param picture
|
|
1008
|
-
# @
|
|
1455
|
+
# @param timestamp [String] the string to parse
|
|
1456
|
+
# @param picture [String] the picture string
|
|
1457
|
+
# @return [Numeric] the parsed timestamp in millis since the epoch
|
|
1009
1458
|
def parse_date_time(timestamp, picture)
|
|
1010
|
-
format_spec = analyse_date_time_picture(picture)
|
|
1011
|
-
|
|
1012
|
-
full_regex = "\\A#{match_spec[:parts].map { |part| "(#{part[:regex]})" }.join}\\z"
|
|
1459
|
+
format_spec = analyse_date_time_picture(picture, timestamp.length)
|
|
1460
|
+
return nil if format_spec.nil?
|
|
1013
1461
|
|
|
1014
|
-
|
|
1015
|
-
info =
|
|
1462
|
+
match_spec = generate_matcher(format_spec)
|
|
1463
|
+
info = match_parts(match_spec[:parts], timestamp)
|
|
1016
1464
|
return nil if info.nil?
|
|
1017
1465
|
|
|
1018
1466
|
# validate what we've just parsed - do we have enough information to create a timestamp?
|
|
@@ -1062,8 +1510,8 @@ module Jsonata
|
|
|
1062
1510
|
tm_b = 47 # binary 101111
|
|
1063
1511
|
|
|
1064
1512
|
components = {}
|
|
1065
|
-
(
|
|
1066
|
-
mpart = match_spec[:parts][i
|
|
1513
|
+
(0...info.size).each do |i|
|
|
1514
|
+
mpart = match_spec[:parts][i]
|
|
1067
1515
|
components[mpart[:component]] = mpart[:parse].call(info[i]) if mpart[:parse]
|
|
1068
1516
|
end
|
|
1069
1517
|
|
|
@@ -1206,9 +1654,9 @@ module Jsonata
|
|
|
1206
1654
|
|
|
1207
1655
|
# Converts an ISO 8601 timestamp to milliseconds since the epoch
|
|
1208
1656
|
#
|
|
1209
|
-
# @param timestamp
|
|
1210
|
-
# @param picture
|
|
1211
|
-
# @
|
|
1657
|
+
# @param timestamp [String] the timestamp to be converted
|
|
1658
|
+
# @param picture [String] the picture string defining the format of the timestamp (defaults to ISO 8601)
|
|
1659
|
+
# @return [Numeric] milliseconds since the epoch
|
|
1212
1660
|
def to_millis(timestamp, picture)
|
|
1213
1661
|
# undefined inputs always return undefined
|
|
1214
1662
|
return nil if timestamp.nil?
|
|
@@ -1223,10 +1671,10 @@ module Jsonata
|
|
|
1223
1671
|
end
|
|
1224
1672
|
|
|
1225
1673
|
# Converts milliseconds since the epoch to an ISO 8601 timestamp
|
|
1226
|
-
# @param millis
|
|
1227
|
-
# @param picture
|
|
1228
|
-
# @param timezone
|
|
1229
|
-
# @
|
|
1674
|
+
# @param millis [Numeric] milliseconds since the epoch to be converted
|
|
1675
|
+
# @param picture [String] the picture string defining the format of the timestamp (defaults to ISO 8601)
|
|
1676
|
+
# @param timezone [String] the timezone to format the timestamp in (defaults to UTC)
|
|
1677
|
+
# @return [String] the formatted timestamp
|
|
1230
1678
|
def from_millis(millis, picture, timezone)
|
|
1231
1679
|
# undefined inputs always return undefined
|
|
1232
1680
|
return nil if millis.nil?
|