psych 3.0.3.pre2-java → 3.2.0-java

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.
@@ -3,7 +3,7 @@
3
3
  #endif
4
4
 
5
5
  #if HAVE_CONFIG_H
6
- #include <config.h>
6
+ #include "config.h"
7
7
  #endif
8
8
 
9
9
  #include <yaml.h>
@@ -12,16 +12,6 @@
12
12
  #include <limits.h>
13
13
  #include <stddef.h>
14
14
 
15
- #ifndef _MSC_VER
16
- #include <stdint.h>
17
- #else
18
- #ifdef _WIN64
19
- #define PTRDIFF_MAX _I64_MAX
20
- #else
21
- #define PTRDIFF_MAX INT_MAX
22
- #endif
23
- #endif
24
-
25
15
  /*
26
16
  * Memory management.
27
17
  */
@@ -80,6 +70,17 @@ yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
80
70
 
81
71
  #define OUTPUT_RAW_BUFFER_SIZE (OUTPUT_BUFFER_SIZE*2+2)
82
72
 
73
+ /*
74
+ * The maximum size of a YAML input file.
75
+ * This used to be PTRDIFF_MAX, but that's not entirely portable
76
+ * because stdint.h isn't available on all platforms.
77
+ * It is not entirely clear why this isn't the maximum value
78
+ * that can fit into the parser->offset field.
79
+ */
80
+
81
+ #define MAX_FILE_SIZE (~(size_t)0 / 2)
82
+
83
+
83
84
  /*
84
85
  * The size of other stacks and queues.
85
86
  */
@@ -93,7 +94,7 @@ yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
93
94
  */
94
95
 
95
96
  #define BUFFER_INIT(context,buffer,size) \
96
- (((buffer).start = yaml_malloc(size)) ? \
97
+ (((buffer).start = (yaml_char_t *)yaml_malloc(size)) ? \
97
98
  ((buffer).last = (buffer).pointer = (buffer).start, \
98
99
  (buffer).end = (buffer).start+(size), \
99
100
  1) : \
@@ -133,7 +134,7 @@ yaml_string_join(
133
134
  (value).pointer = (string))
134
135
 
135
136
  #define STRING_INIT(context,string,size) \
136
- (((string).start = yaml_malloc(size)) ? \
137
+ (((string).start = YAML_MALLOC(size)) ? \
137
138
  ((string).pointer = (string).start, \
138
139
  (string).end = (string).start+(size), \
139
140
  memset((string).start, 0, (size)), \
@@ -174,14 +175,14 @@ yaml_string_join(
174
175
  * Check the octet at the specified position.
175
176
  */
176
177
 
177
- #define CHECK_AT(string,octet,offset) \
178
+ #define CHECK_AT(string,octet,offset) \
178
179
  ((string).pointer[offset] == (yaml_char_t)(octet))
179
180
 
180
181
  /*
181
182
  * Check the current octet in the buffer.
182
183
  */
183
184
 
184
- #define CHECK(string,octet) CHECK_AT((string),(octet),0)
185
+ #define CHECK(string,octet) (CHECK_AT((string),(octet),0))
185
186
 
186
187
  /*
187
188
  * Check if the character at the specified position is an alphabetical
@@ -423,10 +424,10 @@ yaml_stack_extend(void **start, void **top, void **end);
423
424
  YAML_DECLARE(int)
424
425
  yaml_queue_extend(void **start, void **head, void **tail, void **end);
425
426
 
426
- #define STACK_INIT(context,stack,size) \
427
- (((stack).start = yaml_malloc((size)*sizeof(*(stack).start))) ? \
427
+ #define STACK_INIT(context,stack,type) \
428
+ (((stack).start = (type)yaml_malloc(INITIAL_STACK_SIZE*sizeof(*(stack).start))) ? \
428
429
  ((stack).top = (stack).start, \
429
- (stack).end = (stack).start+(size), \
430
+ (stack).end = (stack).start+INITIAL_STACK_SIZE, \
430
431
  1) : \
431
432
  ((context)->error = YAML_MEMORY_ERROR, \
432
433
  0))
@@ -456,8 +457,8 @@ yaml_queue_extend(void **start, void **head, void **tail, void **end);
456
457
  #define POP(context,stack) \
457
458
  (*(--(stack).top))
458
459
 
459
- #define QUEUE_INIT(context,queue,size) \
460
- (((queue).start = yaml_malloc((size)*sizeof(*(queue).start))) ? \
460
+ #define QUEUE_INIT(context,queue,size,type) \
461
+ (((queue).start = (type)yaml_malloc((size)*sizeof(*(queue).start))) ? \
461
462
  ((queue).head = (queue).tail = (queue).start, \
462
463
  (queue).end = (queue).start+(size), \
463
464
  1) : \
@@ -660,3 +661,28 @@ yaml_queue_extend(void **start, void **head, void **tail, void **end);
660
661
  (node).data.mapping.pairs.end = (node_pairs_end), \
661
662
  (node).data.mapping.pairs.top = (node_pairs_start), \
662
663
  (node).data.mapping.style = (node_style))
664
+
665
+ /* Strict C compiler warning helpers */
666
+
667
+ #if defined(__clang__) || defined(__GNUC__)
668
+ # define HASATTRIBUTE_UNUSED
669
+ #endif
670
+ #ifdef HASATTRIBUTE_UNUSED
671
+ # define __attribute__unused__ __attribute__((__unused__))
672
+ #else
673
+ # define __attribute__unused__
674
+ #endif
675
+
676
+ /* Shim arguments are arguments that must be included in your function,
677
+ * but serve no purpose inside. Silence compiler warnings. */
678
+ #define SHIM(a) /*@unused@*/ a __attribute__unused__
679
+
680
+ /* UNUSED_PARAM() marks a shim argument in the body to silence compiler warnings */
681
+ #ifdef __clang__
682
+ # define UNUSED_PARAM(a) (void)(a);
683
+ #else
684
+ # define UNUSED_PARAM(a) /*@-noeffect*/if (0) (void)(a)/*@=noeffect*/;
685
+ #endif
686
+
687
+ #define YAML_MALLOC_STATIC(type) (type*)yaml_malloc(sizeof(type))
688
+ #define YAML_MALLOC(size) (yaml_char_t *)yaml_malloc(size)
@@ -10,11 +10,7 @@ when 'jruby'
10
10
  org.jruby.ext.psych.PsychLibrary.new.load(JRuby.runtime, false)
11
11
  end
12
12
  else
13
- begin
14
- require "#{RUBY_VERSION[/\d+\.\d+/]}/psych.so"
15
- rescue LoadError
16
- require 'psych.so'
17
- end
13
+ require 'psych.so'
18
14
  end
19
15
  require 'psych/nodes'
20
16
  require 'psych/streaming'
@@ -36,7 +32,7 @@ require 'psych/class_loader'
36
32
  # = Overview
37
33
  #
38
34
  # Psych is a YAML parser and emitter.
39
- # Psych leverages libyaml [Home page: http://pyyaml.org/wiki/LibYAML]
35
+ # Psych leverages libyaml [Home page: https://pyyaml.org/wiki/LibYAML]
40
36
  # or [HG repo: https://bitbucket.org/xi/libyaml] for its YAML parsing
41
37
  # and emitting capabilities. In addition to wrapping libyaml, Psych also
42
38
  # knows how to serialize and de-serialize most Ruby objects to and from
@@ -235,14 +231,16 @@ require 'psych/class_loader'
235
231
  module Psych
236
232
  # The version of libyaml Psych is using
237
233
  LIBYAML_VERSION = Psych.libyaml_version.join '.'
238
-
239
- FALLBACK = Struct.new :to_ruby # :nodoc:
234
+ # Deprecation guard
235
+ NOT_GIVEN = Object.new
236
+ private_constant :NOT_GIVEN
240
237
 
241
238
  ###
242
239
  # Load +yaml+ in to a Ruby data structure. If multiple documents are
243
240
  # provided, the object contained in the first document will be returned.
244
- # +filename+ will be used in the exception message if any exception is raised
245
- # while parsing.
241
+ # +filename+ will be used in the exception message if any exception
242
+ # is raised while parsing. If +yaml+ is empty, it returns
243
+ # the specified +fallback+ return value, which defaults to +false+.
246
244
  #
247
245
  # Raises a Psych::SyntaxError when a YAML syntax error is detected.
248
246
  #
@@ -252,7 +250,7 @@ module Psych
252
250
  # Psych.load("---\n - a\n - b") # => ['a', 'b']
253
251
  #
254
252
  # begin
255
- # Psych.load("--- `", "file.txt")
253
+ # Psych.load("--- `", filename: "file.txt")
256
254
  # rescue Psych::SyntaxError => ex
257
255
  # ex.file # => 'file.txt'
258
256
  # ex.message # => "(file.txt): found character that cannot start any token"
@@ -264,10 +262,21 @@ module Psych
264
262
  # Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
265
263
  # Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
266
264
  #
267
- def self.load yaml, filename = nil, fallback: false, symbolize_names: false
268
- result = parse(yaml, filename, fallback: fallback)
269
- result = result.to_ruby if result
270
- symbolize_names!(result) if symbolize_names
265
+ # Raises a TypeError when `yaml` parameter is NilClass
266
+ #
267
+ # NOTE: This method *should not* be used to parse untrusted documents, such as
268
+ # YAML documents that are supplied via user input. Instead, please use the
269
+ # safe_load method.
270
+ #
271
+ def self.load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false, symbolize_names: false, freeze: false
272
+ if legacy_filename != NOT_GIVEN
273
+ warn_with_uplevel 'Passing filename with the 2nd argument of Psych.load is deprecated. Use keyword argument like Psych.load(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
274
+ filename = legacy_filename
275
+ end
276
+
277
+ result = parse(yaml, filename: filename)
278
+ return fallback unless result
279
+ result = result.to_ruby(symbolize_names: symbolize_names, freeze: freeze) if result
271
280
  result
272
281
  end
273
282
 
@@ -284,27 +293,27 @@ module Psych
284
293
  # * Hash
285
294
  #
286
295
  # Recursive data structures are not allowed by default. Arbitrary classes
287
- # can be allowed by adding those classes to the +whitelist+. They are
296
+ # can be allowed by adding those classes to the +permitted_classes+ keyword argument. They are
288
297
  # additive. For example, to allow Date deserialization:
289
298
  #
290
- # Psych.safe_load(yaml, [Date])
299
+ # Psych.safe_load(yaml, permitted_classes: [Date])
291
300
  #
292
301
  # Now the Date class can be loaded in addition to the classes listed above.
293
302
  #
294
- # Aliases can be explicitly allowed by changing the +aliases+ parameter.
303
+ # Aliases can be explicitly allowed by changing the +aliases+ keyword argument.
295
304
  # For example:
296
305
  #
297
306
  # x = []
298
307
  # x << x
299
308
  # yaml = Psych.dump x
300
309
  # Psych.safe_load yaml # => raises an exception
301
- # Psych.safe_load yaml, [], [], true # => loads the aliases
310
+ # Psych.safe_load yaml, aliases: true # => loads the aliases
302
311
  #
303
312
  # A Psych::DisallowedClass exception will be raised if the yaml contains a
304
- # class that isn't in the whitelist.
313
+ # class that isn't in the +permitted_classes+ list.
305
314
  #
306
315
  # A Psych::BadAlias exception will be raised if the yaml contains aliases
307
- # but the +aliases+ parameter is set to false.
316
+ # but the +aliases+ keyword argument is set to false.
308
317
  #
309
318
  # +filename+ will be used in the exception message if any exception is raised
310
319
  # while parsing.
@@ -315,20 +324,39 @@ module Psych
315
324
  # Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
316
325
  # Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
317
326
  #
318
- def self.safe_load yaml, whitelist_classes = [], whitelist_symbols = [], aliases = false, filename = nil, symbolize_names: false
319
- result = parse(yaml, filename)
320
- return unless result
327
+ def self.safe_load yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false
328
+ if legacy_permitted_classes != NOT_GIVEN
329
+ warn_with_uplevel 'Passing permitted_classes with the 2nd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_classes: ...) instead.', uplevel: 1 if $VERBOSE
330
+ permitted_classes = legacy_permitted_classes
331
+ end
321
332
 
322
- class_loader = ClassLoader::Restricted.new(whitelist_classes.map(&:to_s),
323
- whitelist_symbols.map(&:to_s))
324
- scanner = ScalarScanner.new class_loader
325
- if aliases
326
- visitor = Visitors::ToRuby.new scanner, class_loader
327
- else
328
- visitor = Visitors::NoAliasRuby.new scanner, class_loader
333
+ if legacy_permitted_symbols != NOT_GIVEN
334
+ warn_with_uplevel 'Passing permitted_symbols with the 3rd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_symbols: ...) instead.', uplevel: 1 if $VERBOSE
335
+ permitted_symbols = legacy_permitted_symbols
336
+ end
337
+
338
+ if legacy_aliases != NOT_GIVEN
339
+ warn_with_uplevel 'Passing aliases with the 4th argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, aliases: ...) instead.', uplevel: 1 if $VERBOSE
340
+ aliases = legacy_aliases
329
341
  end
342
+
343
+ if legacy_filename != NOT_GIVEN
344
+ warn_with_uplevel 'Passing filename with the 5th argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
345
+ filename = legacy_filename
346
+ end
347
+
348
+ result = parse(yaml, filename: filename)
349
+ return fallback unless result
350
+
351
+ class_loader = ClassLoader::Restricted.new(permitted_classes.map(&:to_s),
352
+ permitted_symbols.map(&:to_s))
353
+ scanner = ScalarScanner.new class_loader
354
+ visitor = if aliases
355
+ Visitors::ToRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze
356
+ else
357
+ Visitors::NoAliasRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze
358
+ end
330
359
  result = visitor.accept result
331
- symbolize_names!(result) if symbolize_names
332
360
  result
333
361
  end
334
362
 
@@ -344,28 +372,40 @@ module Psych
344
372
  # Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Document:0x00>
345
373
  #
346
374
  # begin
347
- # Psych.parse("--- `", "file.txt")
375
+ # Psych.parse("--- `", filename: "file.txt")
348
376
  # rescue Psych::SyntaxError => ex
349
377
  # ex.file # => 'file.txt'
350
378
  # ex.message # => "(file.txt): found character that cannot start any token"
351
379
  # end
352
380
  #
353
381
  # See Psych::Nodes for more information about YAML AST.
354
- def self.parse yaml, filename = nil, fallback: false
355
- parse_stream(yaml, filename) do |node|
382
+ def self.parse yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: NOT_GIVEN
383
+ if legacy_filename != NOT_GIVEN
384
+ warn_with_uplevel 'Passing filename with the 2nd argument of Psych.parse is deprecated. Use keyword argument like Psych.parse(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
385
+ filename = legacy_filename
386
+ end
387
+
388
+ parse_stream(yaml, filename: filename) do |node|
356
389
  return node
357
390
  end
358
- fallback
391
+
392
+ if fallback != NOT_GIVEN
393
+ warn_with_uplevel 'Passing the `fallback` keyword argument of Psych.parse is deprecated.', uplevel: 1 if $VERBOSE
394
+ fallback
395
+ else
396
+ false
397
+ end
359
398
  end
360
399
 
361
400
  ###
362
401
  # Parse a file at +filename+. Returns the Psych::Nodes::Document.
363
402
  #
364
403
  # Raises a Psych::SyntaxError when a YAML syntax error is detected.
365
- def self.parse_file filename
366
- File.open filename, 'r:bom|utf-8' do |f|
367
- parse f, filename
404
+ def self.parse_file filename, fallback: false
405
+ result = File.open filename, 'r:bom|utf-8' do |f|
406
+ parse f, filename: filename
368
407
  end
408
+ result || fallback
369
409
  end
370
410
 
371
411
  ###
@@ -394,14 +434,21 @@ module Psych
394
434
  # end
395
435
  #
396
436
  # begin
397
- # Psych.parse_stream("--- `", "file.txt")
437
+ # Psych.parse_stream("--- `", filename: "file.txt")
398
438
  # rescue Psych::SyntaxError => ex
399
439
  # ex.file # => 'file.txt'
400
440
  # ex.message # => "(file.txt): found character that cannot start any token"
401
441
  # end
402
442
  #
443
+ # Raises a TypeError when NilClass is passed.
444
+ #
403
445
  # See Psych::Nodes for more information about YAML AST.
404
- def self.parse_stream yaml, filename = nil, &block
446
+ def self.parse_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, &block
447
+ if legacy_filename != NOT_GIVEN
448
+ warn_with_uplevel 'Passing filename with the 2nd argument of Psych.parse_stream is deprecated. Use keyword argument like Psych.parse_stream(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
449
+ filename = legacy_filename
450
+ end
451
+
405
452
  if block_given?
406
453
  parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
407
454
  parser.parse yaml, filename
@@ -502,14 +549,22 @@ module Psych
502
549
  # end
503
550
  # list # => ['foo', 'bar']
504
551
  #
505
- def self.load_stream yaml, filename = nil
506
- if block_given?
507
- parse_stream(yaml, filename) do |node|
508
- yield node.to_ruby
509
- end
510
- else
511
- parse_stream(yaml, filename).children.map { |child| child.to_ruby }
552
+ def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: []
553
+ if legacy_filename != NOT_GIVEN
554
+ warn_with_uplevel 'Passing filename with the 2nd argument of Psych.load_stream is deprecated. Use keyword argument like Psych.load_stream(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
555
+ filename = legacy_filename
512
556
  end
557
+
558
+ result = if block_given?
559
+ parse_stream(yaml, filename: filename) do |node|
560
+ yield node.to_ruby
561
+ end
562
+ else
563
+ parse_stream(yaml, filename: filename).children.map(&:to_ruby)
564
+ end
565
+
566
+ return fallback if result.is_a?(Array) && result.empty?
567
+ result
513
568
  end
514
569
 
515
570
  ###
@@ -518,7 +573,7 @@ module Psych
518
573
  # the specified +fallback+ return value, which defaults to +false+.
519
574
  def self.load_file filename, fallback: false
520
575
  File.open(filename, 'r:bom|utf-8') { |f|
521
- self.load f, filename, fallback: FALLBACK.new(fallback)
576
+ self.load f, filename: filename, fallback: fallback
522
577
  }
523
578
  end
524
579
 
@@ -547,18 +602,20 @@ module Psych
547
602
  @dump_tags[klass] = tag
548
603
  end
549
604
 
550
- def self.symbolize_names!(result)
551
- case result
552
- when Hash
553
- result.keys.each do |key|
554
- result[key.to_sym] = symbolize_names!(result.delete(key))
555
- end
556
- when Array
557
- result.map! { |r| symbolize_names!(r) }
605
+ # Workaround for emulating `warn '...', uplevel: 1` in Ruby 2.4 or lower.
606
+ def self.warn_with_uplevel(message, uplevel: 1)
607
+ at = parse_caller(caller[uplevel]).join(':')
608
+ warn "#{at}: #{message}"
609
+ end
610
+
611
+ def self.parse_caller(at)
612
+ if /^(.+?):(\d+)(?::in `.*')?/ =~ at
613
+ file = $1
614
+ line = $2.to_i
615
+ [file, line]
558
616
  end
559
- result
560
617
  end
561
- private_class_method :symbolize_names!
618
+ private_class_method :warn_with_uplevel, :parse_caller
562
619
 
563
620
  class << self
564
621
  attr_accessor :load_tags
@@ -105,7 +105,7 @@ module Psych
105
105
  # - first element
106
106
  # - *ponies
107
107
  #
108
- # &ponies is the achor, *ponies is the alias. In this case, alias is
108
+ # &ponies is the anchor, *ponies is the alias. In this case, alias is
109
109
  # called with "ponies".
110
110
  def alias anchor
111
111
  end
@@ -46,8 +46,8 @@ module Psych
46
46
  # Convert this node to Ruby.
47
47
  #
48
48
  # See also Psych::Visitors::ToRuby
49
- def to_ruby
50
- Visitors::ToRuby.create.accept(self)
49
+ def to_ruby(symbolize_names: false, freeze: false)
50
+ Visitors::ToRuby.create(symbolize_names: symbolize_names, freeze: freeze).accept(self)
51
51
  end
52
52
  alias :transform :to_ruby
53
53
 
@@ -14,16 +14,15 @@ module Psych
14
14
  |\.(nan|NaN|NAN)(?# not a number))$/x
15
15
 
16
16
  # Taken from http://yaml.org/type/int.html
17
- INTEGER = /^(?:[-+]?0b[0-1_]+ (?# base 2)
18
- |[-+]?0[0-7_]+ (?# base 8)
19
- |[-+]?(?:0|[1-9][0-9_]*) (?# base 10)
20
- |[-+]?0x[0-9a-fA-F_]+ (?# base 16))$/x
17
+ INTEGER = /^(?:[-+]?0b[0-1_,]+ (?# base 2)
18
+ |[-+]?0[0-7_,]+ (?# base 8)
19
+ |[-+]?(?:0|[1-9][0-9_,]*) (?# base 10)
20
+ |[-+]?0x[0-9a-fA-F_,]+ (?# base 16))$/x
21
21
 
22
22
  attr_reader :class_loader
23
23
 
24
24
  # Create a new scanner
25
25
  def initialize class_loader
26
- @string_cache = {}
27
26
  @symbol_cache = {}
28
27
  @class_loader = class_loader
29
28
  end
@@ -31,81 +30,70 @@ module Psych
31
30
  # Tokenize +string+ returning the Ruby object
32
31
  def tokenize string
33
32
  return nil if string.empty?
34
- return string if @string_cache.key?(string)
35
33
  return @symbol_cache[string] if @symbol_cache.key?(string)
36
34
 
37
- case string
38
35
  # Check for a String type, being careful not to get caught by hash keys, hex values, and
39
36
  # special floats (e.g., -.inf).
40
- when /^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/, /\n/
41
- if string.length > 5
42
- @string_cache[string] = true
43
- return string
44
- end
37
+ if string.match?(/^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/) || string.match?(/\n/)
38
+ return string if string.length > 5
45
39
 
46
- case string
47
- when /^[^ytonf~]/i
48
- @string_cache[string] = true
40
+ if string.match?(/^[^ytonf~]/i)
49
41
  string
50
- when '~', /^null$/i
42
+ elsif string == '~' || string.match?(/^null$/i)
51
43
  nil
52
- when /^(yes|true|on)$/i
44
+ elsif string.match?(/^(yes|true|on)$/i)
53
45
  true
54
- when /^(no|false|off)$/i
46
+ elsif string.match?(/^(no|false|off)$/i)
55
47
  false
56
48
  else
57
- @string_cache[string] = true
58
49
  string
59
50
  end
60
- when TIME
51
+ elsif string.match?(TIME)
61
52
  begin
62
53
  parse_time string
63
54
  rescue ArgumentError
64
55
  string
65
56
  end
66
- when /^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/
57
+ elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/)
67
58
  require 'date'
68
59
  begin
69
60
  class_loader.date.strptime(string, '%Y-%m-%d')
70
61
  rescue ArgumentError
71
62
  string
72
63
  end
73
- when /^\.inf$/i
64
+ elsif string.match?(/^\.inf$/i)
74
65
  Float::INFINITY
75
- when /^-\.inf$/i
66
+ elsif string.match?(/^-\.inf$/i)
76
67
  -Float::INFINITY
77
- when /^\.nan$/i
68
+ elsif string.match?(/^\.nan$/i)
78
69
  Float::NAN
79
- when /^:./
70
+ elsif string.match?(/^:./)
80
71
  if string =~ /^:(["'])(.*)\1/
81
72
  @symbol_cache[string] = class_loader.symbolize($2.sub(/^:/, ''))
82
73
  else
83
74
  @symbol_cache[string] = class_loader.symbolize(string.sub(/^:/, ''))
84
75
  end
85
- when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/
76
+ elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/)
86
77
  i = 0
87
78
  string.split(':').each_with_index do |n,e|
88
79
  i += (n.to_i * 60 ** (e - 2).abs)
89
80
  end
90
81
  i
91
- when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/
82
+ elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/)
92
83
  i = 0
93
84
  string.split(':').each_with_index do |n,e|
94
85
  i += (n.to_f * 60 ** (e - 2).abs)
95
86
  end
96
87
  i
97
- when FLOAT
98
- if string =~ /\A[-+]?\.\Z/
99
- @string_cache[string] = true
88
+ elsif string.match?(FLOAT)
89
+ if string.match?(/\A[-+]?\.\Z/)
100
90
  string
101
91
  else
102
92
  Float(string.gsub(/[,_]|\.([Ee]|$)/, '\1'))
103
93
  end
94
+ elsif string.match?(INTEGER)
95
+ parse_int string
104
96
  else
105
- int = parse_int string.gsub(/[,_]/, '')
106
- return int if int
107
-
108
- @string_cache[string] = true
109
97
  string
110
98
  end
111
99
  end
@@ -113,8 +101,7 @@ module Psych
113
101
  ###
114
102
  # Parse and return an int from +string+
115
103
  def parse_int string
116
- return unless INTEGER === string
117
- Integer(string)
104
+ Integer(string.gsub(/[,_]/, ''))
118
105
  end
119
106
 
120
107
  ###