package jx

import (
	
)

// Num decodes number.
//
// Do not retain returned value, it references underlying buffer.
func ( *Decoder) () (Num, error) {
	return .num(nil, false)
}

// NumAppend appends number.
func ( *Decoder) ( Num) (Num, error) {
	return .num(, true)
}

// num decodes number.
func ( *Decoder) ( Num,  bool) (Num, error) {
	switch .Next() {
	case String:
		 := .offset()
		 := .head

		,  := .str(value{raw: true})
		if  != nil {
			return Num{}, errors.Wrap(, "str")
		}

		// Validate number.
		{
			 := Decoder{}
			.ResetBytes(.buf)

			,  := .next()
			if  != nil {
				return Num{}, 
			}
			switch  {
			case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
				.unread()

				if  := .skipNumber();  != nil {
					return Num{}, errors.Wrap(, "skip number")
				}
			default:
				return nil, badToken(, )
			}
		}

		// If string is escaped or decoder is streaming, copy it.
		if !.raw ||  {
			 = append(, '"')
			 = append(, .buf...)
			 = append(, '"')
			return , nil
		}
		return .buf[:.head], nil
	case Number: // float or integer
		if  {
			,  := .RawAppend(Raw())
			if  != nil {
				return nil, 
			}
			return Num(), nil
		}

		,  := .Raw()
		if  != nil {
			return nil, 
		}
		return Num(), nil
	default:
		return , errors.Errorf("unexpected %s", .Next())
	}
}