jdogo 3.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +23 -0
- data/LICENSE +202 -0
- data/README.md +108 -0
- data/lib/jdogo/decoder.rb +710 -0
- data/lib/jdogo/encoder.rb +662 -0
- data/lib/jdogo/legacy.rb +210 -0
- data/lib/jdogo/number.rb +117 -0
- data/lib/jdogo/readable.rb +604 -0
- data/lib/jdogo/text.rb +281 -0
- data/lib/jdogo/time_column.rb +96 -0
- data/lib/jdogo/value.rb +321 -0
- data/lib/jdogo/version.rb +5 -0
- data/lib/jdogo.rb +159 -0
- metadata +61 -0
data/lib/jdogo/legacy.rb
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JDogo
|
|
4
|
+
# Readers for older layouts. JDogo 1.x text ('|'-separated rows) and the old typed layout
|
|
5
|
+
# ('!JDOGO/2 @mode TYPED') must still load (spec 2.1 rule 10.3). This library reads them and never
|
|
6
|
+
# writes them.
|
|
7
|
+
#
|
|
8
|
+
# @api private
|
|
9
|
+
class Legacy
|
|
10
|
+
TYPED_DOC_MARKER = "!JDOGO/2 @doc"
|
|
11
|
+
TYPED_V1_HEADER = "!JDOGO/2 @mode TYPED"
|
|
12
|
+
|
|
13
|
+
V1_OBJECT = /\{[^{}:"]*=/ # 1.x object literal: {key=value}
|
|
14
|
+
V1_NUMBER = /\A-?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?\z/
|
|
15
|
+
# Unicode white space, as Go's strings.TrimSpace and strings.Fields see it.
|
|
16
|
+
SPACE = "[\\t\\n\\v\\f\\r \\u0085\\u00A0\\u1680\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]"
|
|
17
|
+
TRIM_RE = /\A#{SPACE}+|#{SPACE}+\z/o
|
|
18
|
+
FIELDS_RE = /#{SPACE}+/o
|
|
19
|
+
ATOI = /\A[+-]?[0-9]+\z/
|
|
20
|
+
|
|
21
|
+
def self.trim_space(s) = s.gsub(TRIM_RE, "")
|
|
22
|
+
|
|
23
|
+
# looks_v1?: a marker only the 1.x format writes: a '@' header with '|'-separated rows, a
|
|
24
|
+
# {key=value} object, or the headers '@', '@empty[]' and '@nulls[n]'.
|
|
25
|
+
def self.looks_v1?(text)
|
|
26
|
+
s = text.sub(/\A[ \t\r\n]+/, "")
|
|
27
|
+
return false unless s.start_with?("@")
|
|
28
|
+
|
|
29
|
+
first, rest = s.split("\n", 2)
|
|
30
|
+
rest ||= ""
|
|
31
|
+
first = first.delete_suffix("\r")
|
|
32
|
+
return true if first == "@" || first.start_with?("@empty[]", "@nulls[")
|
|
33
|
+
|
|
34
|
+
rest.include?("|") || V1_OBJECT.match?(rest)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def initialize(max_depth, max_collection)
|
|
38
|
+
@max_depth = max_depth
|
|
39
|
+
@max_collection = max_collection
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def check_size(n)
|
|
43
|
+
return if n <= @max_collection
|
|
44
|
+
|
|
45
|
+
raise LimitError, "Collection size #{n} exceeds the maximum of #{@max_collection}"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# non_empty_lines splits on line endings and drops empty lines, as the 1.x readers do.
|
|
49
|
+
def self.non_empty_lines(s) = s.split(/\r?\n/, -1).reject(&:empty?)
|
|
50
|
+
|
|
51
|
+
# decode_v1 reads JDogo 1.x compact text.
|
|
52
|
+
def decode_v1(text)
|
|
53
|
+
s = Legacy.trim_space(text)
|
|
54
|
+
return nil if s.empty? || s == "null" || s == "~"
|
|
55
|
+
return [] if s == "@empty[]"
|
|
56
|
+
return nulls(s[7...-1]) if s.start_with?("@nulls[") && s.end_with?("]")
|
|
57
|
+
return scalar_or_list(s) unless s.start_with?("@")
|
|
58
|
+
|
|
59
|
+
lines = Legacy.non_empty_lines(s)
|
|
60
|
+
unless lines.drop(1).any? { |l| l.include?("|") }
|
|
61
|
+
begin
|
|
62
|
+
return Decoder.decode(s, @max_depth, @max_collection)
|
|
63
|
+
rescue Error
|
|
64
|
+
nil # read as 1.x below
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
names = Legacy.split_escaped(lines[0][1..], ",").map { |n| Legacy.trim_space(n) }
|
|
68
|
+
rows(names, lines.drop(1))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def nulls(count)
|
|
72
|
+
c = Legacy.trim_space(count)
|
|
73
|
+
return [] unless ATOI.match?(c)
|
|
74
|
+
|
|
75
|
+
n = c.to_i
|
|
76
|
+
return [] if n.negative? || n > Num::INT64_MAX
|
|
77
|
+
|
|
78
|
+
check_size(n)
|
|
79
|
+
Array.new(n)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def scalar_or_list(s)
|
|
83
|
+
return unescape(s, 1) unless s.start_with?("[") && s.end_with?("]")
|
|
84
|
+
|
|
85
|
+
inner = Legacy.trim_space(s[1...-1])
|
|
86
|
+
return [] if inner.empty?
|
|
87
|
+
|
|
88
|
+
parts = Legacy.split_escaped(inner, ",")
|
|
89
|
+
check_size(parts.size)
|
|
90
|
+
parts.map { |p| unescape(Legacy.trim_space(p), 1) }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# rows reads '|'-separated rows under the given field names: one row is an object, several a list.
|
|
94
|
+
def rows(names, lines)
|
|
95
|
+
check_size(lines.size)
|
|
96
|
+
out = lines.map do |line|
|
|
97
|
+
next nil if line == "null"
|
|
98
|
+
|
|
99
|
+
cells = Legacy.split_escaped(line, "|")
|
|
100
|
+
names.each_with_index.to_h { |name, i| [name, unescape(cells[i] || "~", 1)] }
|
|
101
|
+
end
|
|
102
|
+
case out.size
|
|
103
|
+
when 0 then []
|
|
104
|
+
when 1 then out[0]
|
|
105
|
+
else out
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# unescape reads one 1.x cell.
|
|
110
|
+
def unescape(tok, depth)
|
|
111
|
+
raise LimitError, "Maximum recursion depth exceeded: #{depth} > #{@max_depth}" if depth > @max_depth
|
|
112
|
+
|
|
113
|
+
case tok
|
|
114
|
+
when "", "~", "null" then return nil
|
|
115
|
+
when '""' then return ""
|
|
116
|
+
when "true" then return true
|
|
117
|
+
when "false" then return false
|
|
118
|
+
end
|
|
119
|
+
if V1_NUMBER.match?(tok)
|
|
120
|
+
return Integer(tok, 10) if Num::INTEGER_TOKEN.match?(tok)
|
|
121
|
+
|
|
122
|
+
return Num.parse_float(tok) || raise(ParseError.new(0, "number out of range: #{tok[0, 40]}"))
|
|
123
|
+
end
|
|
124
|
+
if tok.start_with?("[") && tok.end_with?("]")
|
|
125
|
+
inner = Legacy.trim_space(tok[1...-1])
|
|
126
|
+
return [] if inner.empty?
|
|
127
|
+
|
|
128
|
+
return Legacy.split_escaped(inner, ",").map { |p| unescape(Legacy.trim_space(p), depth + 1) }
|
|
129
|
+
end
|
|
130
|
+
if tok.start_with?("{") && tok.end_with?("}")
|
|
131
|
+
inner = Legacy.trim_space(tok[1...-1])
|
|
132
|
+
out = {}
|
|
133
|
+
return out if inner.empty?
|
|
134
|
+
|
|
135
|
+
Legacy.split_escaped(inner, ",").each do |p|
|
|
136
|
+
k, val = p.split("=", 2)
|
|
137
|
+
if val.nil?
|
|
138
|
+
out[Legacy.trim_space(p)] = nil
|
|
139
|
+
else
|
|
140
|
+
out[Legacy.trim_space(k)] = unescape(Legacy.trim_space(val), depth + 1)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
return out
|
|
144
|
+
end
|
|
145
|
+
# '\|', '\\', '\,' and any other escaped character stand for themselves
|
|
146
|
+
tok.gsub(/\\(.)/m) do
|
|
147
|
+
case Regexp.last_match(1)
|
|
148
|
+
when "n" then "\n"
|
|
149
|
+
when "r" then "\r"
|
|
150
|
+
else Regexp.last_match(1)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# split_escaped splits on delim outside brackets and braces, keeping backslash escapes in the parts.
|
|
156
|
+
def self.split_escaped(s, delim)
|
|
157
|
+
parts = []
|
|
158
|
+
d = delim.ord
|
|
159
|
+
start = 0
|
|
160
|
+
bracket = 0
|
|
161
|
+
brace = 0
|
|
162
|
+
i = 0
|
|
163
|
+
n = s.bytesize
|
|
164
|
+
while i < n
|
|
165
|
+
c = s.getbyte(i)
|
|
166
|
+
case c
|
|
167
|
+
when 92 then i += 1 # '\\'
|
|
168
|
+
when 91 then bracket += 1
|
|
169
|
+
when 93 then bracket = [0, bracket - 1].max
|
|
170
|
+
when 123 then brace += 1
|
|
171
|
+
when 125 then brace = [0, brace - 1].max
|
|
172
|
+
else
|
|
173
|
+
if c == d && bracket.zero? && brace.zero?
|
|
174
|
+
parts << s.byteslice(start, i - start)
|
|
175
|
+
start = i + 1
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
i += 1
|
|
179
|
+
end
|
|
180
|
+
parts << s.byteslice(start, [n - start, 0].max)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# ---------------------------------------------------------------- typed
|
|
184
|
+
|
|
185
|
+
# decode_typed reads typed JDogo: '!JDOGO/2 @doc' and a document, as every SDK writes it, or the
|
|
186
|
+
# older '!JDOGO/2 @mode TYPED' layout.
|
|
187
|
+
def decode_typed(text)
|
|
188
|
+
marker = ["#{TYPED_DOC_MARKER}\n", "#{TYPED_DOC_MARKER}\r\n"].find { |m| text.start_with?(m) }
|
|
189
|
+
return Decoder.decode(text.byteslice(marker.bytesize..), @max_depth, @max_collection) if marker
|
|
190
|
+
|
|
191
|
+
s = Legacy.trim_space(text)
|
|
192
|
+
s = Legacy.trim_space(s.delete_prefix(TYPED_V1_HEADER)) if s.start_with?(TYPED_V1_HEADER)
|
|
193
|
+
return nil if s.empty? || s == "null" || s == "~"
|
|
194
|
+
return [] if s.start_with?("@list empty")
|
|
195
|
+
|
|
196
|
+
if s.start_with?("@list nulls")
|
|
197
|
+
_, count = s.split("count=", 2)
|
|
198
|
+
words = count.to_s.split(FIELDS_RE).reject(&:empty?)
|
|
199
|
+
return words.empty? ? [] : nulls(words[0])
|
|
200
|
+
end
|
|
201
|
+
lines = Legacy.non_empty_lines(s)
|
|
202
|
+
return scalar_or_list(Legacy.trim_space(lines.drop(1).join("\n"))) if lines[0].start_with?("@type ")
|
|
203
|
+
return unescape(s, 1) unless lines[0].start_with?("@fields ")
|
|
204
|
+
|
|
205
|
+
specs = Legacy.split_escaped(Legacy.trim_space(lines[0].delete_prefix("@fields ")), ",")
|
|
206
|
+
names = specs.map { |spec| Legacy.trim_space(Legacy.trim_space(spec).split(":", 2)[0].to_s) }
|
|
207
|
+
rows(names, lines.drop(1))
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
data/lib/jdogo/number.rb
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JDogo
|
|
4
|
+
# Number spellings. Float#to_s gives the shortest digits that read back as the same float, in its
|
|
5
|
+
# own layout ('1.0e-07'); the ECMAScript and Java spellings are built from those digits.
|
|
6
|
+
#
|
|
7
|
+
# @api private
|
|
8
|
+
module Num
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
INT64_MIN = -(2**63)
|
|
12
|
+
INT64_MAX = (2**63) - 1
|
|
13
|
+
|
|
14
|
+
def int64?(x) = x >= INT64_MIN && x <= INT64_MAX
|
|
15
|
+
|
|
16
|
+
# neg_zero?: -0.0.
|
|
17
|
+
def neg_zero?(x) = x.zero? && (1.0 / x).negative?
|
|
18
|
+
|
|
19
|
+
# shortest_digits returns the shortest decimal digits that read back as |x|, and the exponent n with
|
|
20
|
+
# |x| = 0.d1d2... x 10^n. x must be finite and not zero.
|
|
21
|
+
def shortest_digits(x)
|
|
22
|
+
mant, exp = x.abs.to_s.split("e")
|
|
23
|
+
ip, fp = mant.split(".")
|
|
24
|
+
digits = ip + fp.to_s
|
|
25
|
+
n = ip.length + exp.to_i
|
|
26
|
+
stripped = digits.sub(/\A0+/, "")
|
|
27
|
+
n -= digits.length - stripped.length
|
|
28
|
+
[stripped.sub(/0+\z/, ""), n]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# es_number spells a finite float as ECMAScript's Number.prototype.toString does: a plain decimal
|
|
32
|
+
# when 1e-7 <= |x| < 1e21, otherwise d.ddde+N / d.ddde-N (spec 2.3 rule 1.3).
|
|
33
|
+
def es_number(x)
|
|
34
|
+
return "0" if x.zero?
|
|
35
|
+
|
|
36
|
+
# Fast path: Float#to_s writes '123.456' or '0.0001' (shortest digits, no exponent) for most
|
|
37
|
+
# numbers, which is also the ECMAScript spelling; a whole number only drops its '.0'.
|
|
38
|
+
s = x.to_s
|
|
39
|
+
unless s.include?("e")
|
|
40
|
+
return s.end_with?(".0") ? s[0...-2] : s
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
ds, n = shortest_digits(x)
|
|
44
|
+
k = ds.length
|
|
45
|
+
sign = x.negative? ? "-" : ""
|
|
46
|
+
if k <= n && n <= 21
|
|
47
|
+
return sign + ds + ("0" * (n - k))
|
|
48
|
+
elsif n.positive? && n <= 21
|
|
49
|
+
return "#{sign}#{ds[0, n]}.#{ds[n..]}"
|
|
50
|
+
elsif n > -6 && n <= 0
|
|
51
|
+
return "#{sign}0.#{'0' * -n}#{ds}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
e = n - 1
|
|
55
|
+
out = sign + ds[0]
|
|
56
|
+
out += ".#{ds[1..]}" if k > 1
|
|
57
|
+
e >= 0 ? "#{out}e+#{e}" : "#{out}e-#{-e}"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# java_double spells a float as Java's Double.toString does: a plain decimal with at least one
|
|
61
|
+
# fraction digit when 1e-3 <= |x| < 1e7, otherwise d.dddE<exp>. The readable encoder writes floats
|
|
62
|
+
# this way.
|
|
63
|
+
def java_double(x)
|
|
64
|
+
return "null" if x.nan? || x.infinite?
|
|
65
|
+
return neg_zero?(x) ? "-0.0" : "0.0" if x.zero?
|
|
66
|
+
|
|
67
|
+
ds, n = shortest_digits(x)
|
|
68
|
+
ds = "0" if ds.empty?
|
|
69
|
+
sign = x.negative? ? "-" : ""
|
|
70
|
+
a = x.abs
|
|
71
|
+
if a >= 1e-3 && a < 1e7
|
|
72
|
+
return "#{sign}0.#{'0' * -n}#{ds}" if n <= 0
|
|
73
|
+
return "#{sign}#{ds}#{'0' * (n - ds.length)}.0" if n >= ds.length
|
|
74
|
+
|
|
75
|
+
return "#{sign}#{ds[0, n]}.#{ds[n..]}"
|
|
76
|
+
end
|
|
77
|
+
frac = ds[1..]
|
|
78
|
+
frac = "0" if frac.empty?
|
|
79
|
+
"#{sign}#{ds[0]}.#{frac}E#{n - 1}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# whole_under_1e15?: a finite float with no fraction and magnitude below 10^15, which writers spell
|
|
83
|
+
# as an integer (spec 2.3 rule 1.1).
|
|
84
|
+
def whole_under_1e15?(x) = x.finite? && x.abs < 1e15 && x == x.truncate
|
|
85
|
+
|
|
86
|
+
# drop_leading_zero writes '0.5' as '.5' and '-0.5' as '-.5' (spec 2.1 rule 5.6).
|
|
87
|
+
def drop_leading_zero(s)
|
|
88
|
+
return s[1..] if s.start_with?("0.")
|
|
89
|
+
return "-#{s[2..]}" if s.start_with?("-0.")
|
|
90
|
+
|
|
91
|
+
s
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# compact_float is a float in a compact document: NaN and infinities as null (spec 2.1 rule 5.4),
|
|
95
|
+
# whole numbers below 10^15 as integers with -0 as 0, anything else in ECMAScript spelling without
|
|
96
|
+
# the leading zero.
|
|
97
|
+
def compact_float(x)
|
|
98
|
+
return "null" if x.nan? || x.infinite?
|
|
99
|
+
return x.to_i.to_s if whole_under_1e15?(x)
|
|
100
|
+
|
|
101
|
+
drop_leading_zero(es_number(x))
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
INTEGER_TOKEN = /\A-?[0-9]+\z/
|
|
105
|
+
|
|
106
|
+
# parse_float reads a token already checked to be a decimal number; nil when it is too large for a
|
|
107
|
+
# 64-bit float. Very small numbers read as 0.0, as in every SDK.
|
|
108
|
+
def parse_float(tok)
|
|
109
|
+
verbose = $VERBOSE
|
|
110
|
+
$VERBOSE = nil # Float() warns about numbers out of range; the caller reports them
|
|
111
|
+
f = Float(tok)
|
|
112
|
+
f.infinite? ? nil : f
|
|
113
|
+
ensure
|
|
114
|
+
$VERBOSE = verbose
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|