示例#1
0
func ParseUint(i interface{}) (uint64, error) {
	var s string
	switch x := Num64(i).(type) {
	case int64:
		if x < 0 {
			return 0, errors.New("integer overflow")
		}
		return uint64(x), nil
	case uint64:
		return uint64(x), nil
	case float64:
		switch {
		case math.IsNaN(x):
			return 0, errors.New("float is NaN")
		case math.IsInf(x, 0):
			return 0, errors.New("float is Inf")
		case math.Abs(x-float64(uint64(x))) > 1e-9:
			return 0, errors.New("float to uint64")
		}
		return uint64(x), nil
	case string:
		s = x
	case []byte:
		s = string(x)
	default:
		s = fmt.Sprint(x)
	}
	u, err := strconv.ParseUint(s, 10, 64)
	return u, errors.Trace(err)
}
示例#2
0
func ParseFloat(i interface{}) (float64, error) {
	var s string
	switch x := Num64(i).(type) {
	case int64:
		return float64(x), nil
	case uint64:
		return float64(x), nil
	case float64:
		switch {
		case math.IsNaN(x):
			return 0, errors.New("float is NaN")
		case math.IsInf(x, 0):
			return 0, errors.New("float is Inf")
		}
		return float64(x), nil
	case string:
		s = x
	case []byte:
		s = string(x)
	default:
		s = fmt.Sprint(x)
	}
	f, err := strconv.ParseFloat(s, 64)
	return f, errors.Trace(err)
}
示例#3
0
文件: main.go 项目: fengshao0907/rpdb
func session(arg0 interface{}, args [][]byte) (Session, error) {
	s, _ := arg0.(Session)
	if s == nil {
		return nil, errors.New("invalid session")
	}
	for i, v := range args {
		if len(v) == 0 {
			return nil, errors.Errorf("args[%d] is nil", i)
		}
	}
	return s, nil
}
示例#4
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.New("invalid byte size")
	ErrBadBytesizeUnit = errors.New("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])) {
示例#5
0
// Licensed under the MIT (MIT-LICENSE.txt) license.

package redis

import (
	"bufio"
	"bytes"
	"io"
	"strconv"

	"github.com/wandoulabs/redis-port/pkg/libs/errors"
	"github.com/wandoulabs/redis-port/pkg/libs/log"
)

var (
	ErrBadRespCRLFEnd  = errors.New("bad resp CRLF end")
	ErrBadRespBytesLen = errors.New("bad resp bytes len")
	ErrBadRespArrayLen = errors.New("bad resp array len")
)

type decoder struct {
	r *bufio.Reader
}

func Decode(r *bufio.Reader) (Resp, error) {
	d := &decoder{r}
	return d.decodeResp(0)
}

func MustDecode(r *bufio.Reader) Resp {
	resp, err := Decode(r)
示例#6
0
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.

package backlog

import (
	"os"
	"sync"

	"github.com/wandoulabs/redis-port/pkg/libs/errors"
)

var (
	ErrClosedBacklog = errors.New("closed backlog")
	ErrInvalidOffset = errors.New("invalid offset")
)

type buffer interface {
	readSomeAt(b []byte, rpos uint64) (int, error)
	writeSome(b []byte) (int, error)

	dataRange() (rpos, wpos uint64)

	close() error
}

type Backlog struct {
	wl sync.Mutex
	mu sync.Mutex

	err error