Example #1
0
const (
	B = 1 << (10 * iota)
	KB
	MB
	GB
	TB
	PB
)

var (
	BytesizeRegexp = regexp.MustCompile(`(?i)^\s*(\-?[\d\.]+)\s*([KMGTP]?B|[BKMGTP]|)\s*$`)
	digitsRegexp   = regexp.MustCompile(`^\-?\d+$`)
)

var (
	ErrBadBytesize     = errors.Static("invalid byte size")
	ErrBadBytesizeUnit = errors.Static("invalid byte size unit")
)

func Parse(s string) (int64, error) {
	if !BytesizeRegexp.MatchString(s) {
		return 0, errors.Trace(ErrBadBytesize)
	}

	subs := BytesizeRegexp.FindStringSubmatch(s)
	if len(subs) != 3 {
		return 0, errors.Trace(ErrBadBytesize)
	}

	size := int64(0)
	switch strings.ToUpper(string(subs[2])) {
Example #2
0
// Copyright 2015 Reborndb Org. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.

package resp

import (
	"fmt"

	"github.com/reborndb/go/errors"
)

var (
	ErrBadRespType     = errors.Static("bad resp type")
	ErrBadRespEnd      = errors.Static("bad resp end")
	ErrBadRespInt      = errors.Static("bad resp int")
	ErrBadRespBytesLen = errors.Static("bad resp bytes len")
	ErrBadRespArrayLen = errors.Static("bad resp array len")
)

type RespType byte

const (
	TypeString    RespType = '+'
	TypeError     RespType = '-'
	TypeInt       RespType = ':'
	TypeBulkBytes RespType = '$'
	TypeArray     RespType = '*'
	TypePing      RespType = '\n'
)

func (t RespType) String() string {