Improve nil handling
This commit is contained in:
		
							parent
							
								
									67db021c2b
								
							
						
					
					
						commit
						59145cacfe
					
				
					 2 changed files with 25 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -60,13 +60,18 @@ class CBOR::Decoder
 | 
			
		|||
 | 
			
		||||
  def read_nil : Nil
 | 
			
		||||
    read_type(Token::SimpleValueT) do |token|
 | 
			
		||||
      case token.value
 | 
			
		||||
      when SimpleValue::Null,
 | 
			
		||||
           SimpleValue::Undefined
 | 
			
		||||
        nil
 | 
			
		||||
      else
 | 
			
		||||
        unexpected_token(token, "SimpleValue::Null or SimpleValue::Undefined")
 | 
			
		||||
      end
 | 
			
		||||
      return nil if token.value.is_nil?
 | 
			
		||||
 | 
			
		||||
      unexpected_token(token, "SimpleValue::Null or SimpleValue::Undefined")
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def read_nil_or
 | 
			
		||||
    if @current_token.is_a?(Token::SimpleValueT) && @current_token.value.is_nil?
 | 
			
		||||
      finish_token!
 | 
			
		||||
      nil
 | 
			
		||||
    else
 | 
			
		||||
      yield
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -76,6 +81,10 @@ class CBOR::Decoder
 | 
			
		|||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def read_begin_hash
 | 
			
		||||
    read_type(Token::MapT, finish_token: false) { |token| }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def consume_hash(&block)
 | 
			
		||||
    read_type(Token::MapT) do |token|
 | 
			
		||||
      consume_sequence(token.size) { yield }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,4 +18,13 @@ enum CBOR::SimpleValue : UInt8
 | 
			
		|||
      "simple(#{self.value.to_s})"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def is_nil? : Bool
 | 
			
		||||
    case self
 | 
			
		||||
    when Null, Undefined
 | 
			
		||||
      true
 | 
			
		||||
    else
 | 
			
		||||
      false
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue