_id
stringlengths 2
7
| title
stringlengths 1
118
| partition
stringclasses 3
values | text
stringlengths 52
85.5k
| language
stringclasses 1
value | meta_information
dict |
---|---|---|---|---|---|
q181800
|
MarshalJSON
|
test
|
func (this *HawkSignatureAuthenticationResponse) MarshalJSON() ([]byte, error) {
x := json.RawMessage(*this)
return (&x).MarshalJSON()
}
|
go
|
{
"resource": ""
}
|
q181801
|
bmw256
|
test
|
func bmw256(input []byte) []byte {
b := new()
buf := make([]byte, 64)
copy(buf, input)
buf[len(input)] = 0x80
bitLen := uint64(len(input)) << 3
binary.LittleEndian.PutUint64(buf[56:], bitLen)
for i := 0; i < 16; i++ {
b.m[i] = binary.LittleEndian.Uint32(buf[i*4:])
}
b.compress(b.m)
b.h, b.h2 = b.h2, b.h
copy(b.h, final)
b.compress(b.h2)
output := make([]byte, 32)
outlen := len(output) >> 2
for i := 0; i < outlen; i++ {
j := 16 - outlen + i
binary.LittleEndian.PutUint32(output[4*i:], b.h[j])
}
return output
}
|
go
|
{
"resource": ""
}
|
q181802
|
NewCubeHash
|
test
|
func NewCubeHash() *CubeHash {
c := &CubeHash{}
c.x0 = iv[0]
c.x1 = iv[1]
c.x2 = iv[2]
c.x3 = iv[3]
c.x4 = iv[4]
c.x5 = iv[5]
c.x6 = iv[6]
c.x7 = iv[7]
c.x8 = iv[8]
c.x9 = iv[9]
c.xa = iv[10]
c.xb = iv[11]
c.xc = iv[12]
c.xd = iv[13]
c.xe = iv[14]
c.xf = iv[15]
c.xg = iv[16]
c.xh = iv[17]
c.xi = iv[18]
c.xj = iv[19]
c.xk = iv[20]
c.xl = iv[21]
c.xm = iv[22]
c.xn = iv[23]
c.xo = iv[24]
c.xp = iv[25]
c.xq = iv[26]
c.xr = iv[27]
c.xs = iv[28]
c.xt = iv[29]
c.xu = iv[30]
c.xv = iv[31]
return c
}
|
go
|
{
"resource": ""
}
|
q181803
|
cubehash256
|
test
|
func cubehash256(data []byte) []byte {
c := NewCubeHash()
buf := make([]byte, 32)
buf[0] = 0x80
c.inputBlock(data)
c.sixteenRounds()
c.inputBlock(buf)
c.sixteenRounds()
c.xv ^= 1
for j := 0; j < 10; j++ {
c.sixteenRounds()
}
out := make([]byte, 32)
binary.LittleEndian.PutUint32(out[0:], c.x0)
binary.LittleEndian.PutUint32(out[4:], c.x1)
binary.LittleEndian.PutUint32(out[8:], c.x2)
binary.LittleEndian.PutUint32(out[12:], c.x3)
binary.LittleEndian.PutUint32(out[16:], c.x4)
binary.LittleEndian.PutUint32(out[20:], c.x5)
binary.LittleEndian.PutUint32(out[24:], c.x6)
binary.LittleEndian.PutUint32(out[28:], c.x7)
return out
}
|
go
|
{
"resource": ""
}
|
q181804
|
Sum
|
test
|
func Sum(data []byte) ([]byte, error) {
blake := blake256.New()
if _, err := blake.Write(data); err != nil {
return nil, err
}
resultBlake := blake.Sum(nil)
keccak := sha3.NewKeccak256()
if _, err := keccak.Write(resultBlake); err != nil {
return nil, err
}
resultkeccak := keccak.Sum(nil)
resultcube := cubehash256(resultkeccak)
lyra2result := make([]byte, 32)
lyra2(lyra2result, resultcube, resultcube, 1, 4, 4)
var skeinresult [32]byte
skein.Sum256(&skeinresult, lyra2result, nil)
resultcube2 := cubehash256(skeinresult[:])
resultbmw := bmw256(resultcube2)
return resultbmw, nil
}
|
go
|
{
"resource": ""
}
|
q181805
|
squeeze
|
test
|
func squeeze(state []uint64, out []byte) {
tmp := make([]byte, blockLenBytes)
for j := 0; j < len(out)/blockLenBytes+1; j++ {
for i := 0; i < blockLenInt64; i++ {
binary.LittleEndian.PutUint64(tmp[i*8:], state[i])
}
copy(out[j*blockLenBytes:], tmp) //be care in case of len(out[i:])<len(tmp)
blake2bLyra(state)
}
}
|
go
|
{
"resource": ""
}
|
q181806
|
reducedSqueezeRow0
|
test
|
func reducedSqueezeRow0(state []uint64, rowOut []uint64, nCols int) {
ptr := (nCols - 1) * blockLenInt64
//M[row][C-1-col] = H.reduced_squeeze()
for i := 0; i < nCols; i++ {
ptrWord := rowOut[ptr:] //In Lyra2: pointer to M[0][C-1]
ptrWord[0] = state[0]
ptrWord[1] = state[1]
ptrWord[2] = state[2]
ptrWord[3] = state[3]
ptrWord[4] = state[4]
ptrWord[5] = state[5]
ptrWord[6] = state[6]
ptrWord[7] = state[7]
ptrWord[8] = state[8]
ptrWord[9] = state[9]
ptrWord[10] = state[10]
ptrWord[11] = state[11]
//Goes to next block (column) that will receive the squeezed data
ptr -= blockLenInt64
//Applies the reduced-round transformation f to the sponge's state
reducedBlake2bLyra(state)
}
}
|
go
|
{
"resource": ""
}
|
q181807
|
reducedDuplexRow1
|
test
|
func reducedDuplexRow1(state []uint64, rowIn []uint64, rowOut []uint64, nCols int) {
ptrIn := 0
ptrOut := (nCols - 1) * blockLenInt64
for i := 0; i < nCols; i++ {
ptrWordIn := rowIn[ptrIn:] //In Lyra2: pointer to prev
ptrWordOut := rowOut[ptrOut:] //In Lyra2: pointer to row
//Absorbing "M[prev][col]"
state[0] ^= (ptrWordIn[0])
state[1] ^= (ptrWordIn[1])
state[2] ^= (ptrWordIn[2])
state[3] ^= (ptrWordIn[3])
state[4] ^= (ptrWordIn[4])
state[5] ^= (ptrWordIn[5])
state[6] ^= (ptrWordIn[6])
state[7] ^= (ptrWordIn[7])
state[8] ^= (ptrWordIn[8])
state[9] ^= (ptrWordIn[9])
state[10] ^= (ptrWordIn[10])
state[11] ^= (ptrWordIn[11])
//Applies the reduced-round transformation f to the sponge's state
reducedBlake2bLyra(state)
//M[row][C-1-col] = M[prev][col] XOR rand
ptrWordOut[0] = ptrWordIn[0] ^ state[0]
ptrWordOut[1] = ptrWordIn[1] ^ state[1]
ptrWordOut[2] = ptrWordIn[2] ^ state[2]
ptrWordOut[3] = ptrWordIn[3] ^ state[3]
ptrWordOut[4] = ptrWordIn[4] ^ state[4]
ptrWordOut[5] = ptrWordIn[5] ^ state[5]
ptrWordOut[6] = ptrWordIn[6] ^ state[6]
ptrWordOut[7] = ptrWordIn[7] ^ state[7]
ptrWordOut[8] = ptrWordIn[8] ^ state[8]
ptrWordOut[9] = ptrWordIn[9] ^ state[9]
ptrWordOut[10] = ptrWordIn[10] ^ state[10]
ptrWordOut[11] = ptrWordIn[11] ^ state[11]
//Input: next column (i.e., next block in sequence)
ptrIn += blockLenInt64
//Output: goes to previous column
ptrOut -= blockLenInt64
}
}
|
go
|
{
"resource": ""
}
|
q181808
|
NewReaderByteCodeLoader
|
test
|
func NewReaderByteCodeLoader(p parser.Parser, c compiler.Compiler) *ReaderByteCodeLoader {
return &ReaderByteCodeLoader{NewFlags(), p, c}
}
|
go
|
{
"resource": ""
}
|
q181809
|
LoadReader
|
test
|
func (l *ReaderByteCodeLoader) LoadReader(name string, rdr io.Reader) (*vm.ByteCode, error) {
ast, err := l.Parser.ParseReader(name, rdr)
if err != nil {
return nil, err
}
if l.ShouldDumpAST() {
fmt.Fprintf(os.Stderr, "AST:\n%s\n", ast)
}
bc, err := l.Compiler.Compile(ast)
if err != nil {
return nil, err
}
return bc, nil
}
|
go
|
{
"resource": ""
}
|
q181810
|
NewV3
|
test
|
func NewV3(namespace *UUID, name []byte) *UUID {
uuid := newByHash(md5.New(), namespace, name)
uuid[6] = (uuid[6] & 0x0f) | 0x30
return uuid
}
|
go
|
{
"resource": ""
}
|
q181811
|
txLiteral
|
test
|
func txLiteral(st *State) {
st.sa = st.CurrentOp().Arg()
st.Advance()
}
|
go
|
{
"resource": ""
}
|
q181812
|
txFetchSymbol
|
test
|
func txFetchSymbol(st *State) {
// Need to handle local vars?
key := st.CurrentOp().Arg()
vars := st.Vars()
if v, ok := vars.Get(key); ok {
st.sa = v
} else {
st.sa = nil
}
st.Advance()
}
|
go
|
{
"resource": ""
}
|
q181813
|
txMarkRaw
|
test
|
func txMarkRaw(st *State) {
if reflect.ValueOf(st.sa).Type() != rawStringType {
st.sa = rawString(interfaceToString(st.sa))
}
st.Advance()
}
|
go
|
{
"resource": ""
}
|
q181814
|
txUnmarkRaw
|
test
|
func txUnmarkRaw(st *State) {
if reflect.ValueOf(st.sa).Type() == rawStringType {
st.sa = string(interfaceToString(st.sa))
}
st.Advance()
}
|
go
|
{
"resource": ""
}
|
q181815
|
txPrint
|
test
|
func txPrint(st *State) {
arg := st.sa
if arg == nil {
st.Warnf("Use of nil to print\n")
} else if reflect.ValueOf(st.sa).Type() != rawStringType {
st.AppendOutputString(html.EscapeString(interfaceToString(arg)))
} else {
st.AppendOutputString(interfaceToString(arg))
}
st.Advance()
}
|
go
|
{
"resource": ""
}
|
q181816
|
txPrintRaw
|
test
|
func txPrintRaw(st *State) {
// XXX TODO: mark_raw handling
arg := st.sa
if arg == nil {
st.Warnf("Use of nil to print\n")
} else {
st.AppendOutputString(interfaceToString(arg))
}
st.Advance()
}
|
go
|
{
"resource": ""
}
|
q181817
|
NewLoopVar
|
test
|
func NewLoopVar(idx int, array reflect.Value) *LoopVar {
lv := &LoopVar{
Index: idx,
Count: idx + 1,
Body: array,
Size: array.Len(),
MaxIndex: array.Len() - 1,
PeekNext: nil,
PeekPrev: nil,
IsFirst: false,
IsLast: false,
}
return lv
}
|
go
|
{
"resource": ""
}
|
q181818
|
txMakeArray
|
test
|
func txMakeArray(st *State) {
start := st.CurrentMark() // start
end := st.StackTip() // end
if end <= start {
panic(fmt.Sprintf("MakeArray: list start (%d) >= end (%d)", start, end))
}
list := make([]interface{}, end-start+1)
for i := end; i >= start; i-- {
list[i-start] = st.StackPop()
}
st.sa = list
st.Advance()
}
|
go
|
{
"resource": ""
}
|
q181819
|
txFunCallOmni
|
test
|
func txFunCallOmni(st *State) {
t := reflect.ValueOf(st.sa)
switch t.Kind() {
case reflect.Int:
// If it's an int, assume that it's a MACRO, which points to
// the location in the bytecode that contains the macro code
txMacroCall(st)
case reflect.Func:
txFunCall(st)
default:
st.Warnf("Unknown variable as function call: %s\n", st.sa)
st.sa = nil
st.Advance()
}
}
|
go
|
{
"resource": ""
}
|
q181820
|
DumpAST
|
test
|
func (f *Flags) DumpAST(b bool) {
if b {
f.flags |= MaskDumpAST
} else {
f.flags &= ^MaskDumpAST
}
}
|
go
|
{
"resource": ""
}
|
q181821
|
DumpByteCode
|
test
|
func (f *Flags) DumpByteCode(b bool) {
if b {
f.flags |= MaskDumpByteCode
} else {
f.flags &= ^MaskDumpByteCode
}
}
|
go
|
{
"resource": ""
}
|
q181822
|
Optimize
|
test
|
func (o *NaiveOptimizer) Optimize(bc *vm.ByteCode) error {
for i := 0; i < bc.Len(); i++ {
op := bc.Get(i)
if op == nil {
return errors.New("failed to fetch op '" + op.String() + "'")
}
switch op.Type() {
case vm.TXOPLiteral:
if i+1 < bc.Len() && bc.Get(i+1).Type() == vm.TXOPPrintRaw {
bc.OpList[i] = vm.NewOp(vm.TXOPPrintRawConst, op.ArgString())
bc.OpList[i+1] = vm.NewOp(vm.TXOPNoop)
i++
}
}
}
return nil
}
|
go
|
{
"resource": ""
}
|
q181823
|
NewFuncDepot
|
test
|
func NewFuncDepot(namespace string) *FuncDepot {
return &FuncDepot{namespace, make(map[string]reflect.Value)}
}
|
go
|
{
"resource": ""
}
|
q181824
|
Get
|
test
|
func (fc *FuncDepot) Get(key string) (reflect.Value, bool) {
f, ok := fc.depot[key]
return f, ok
}
|
go
|
{
"resource": ""
}
|
q181825
|
Set
|
test
|
func (fc *FuncDepot) Set(key string, v interface{}) {
fc.depot[key] = reflect.ValueOf(v)
}
|
go
|
{
"resource": ""
}
|
q181826
|
NewFileTemplateFetcher
|
test
|
func NewFileTemplateFetcher(paths []string) (*FileTemplateFetcher, error) {
l := &FileTemplateFetcher{
Paths: make([]string, len(paths)),
}
for k, v := range paths {
abs, err := filepath.Abs(v)
if err != nil {
return nil, err
}
l.Paths[k] = abs
}
return l, nil
}
|
go
|
{
"resource": ""
}
|
q181827
|
LastModified
|
test
|
func (s *FileSource) LastModified() (time.Time, error) {
// Calling os.Stat() for *every* Render of the same source is a waste
// Only call os.Stat() if we haven't done so in the last 1 second
if time.Since(s.LastStat) < time.Second {
// A-ha! it's not that long ago we calculated this value, just return
// the same thing as our last call
return s.LastStatResult.ModTime(), nil
}
// If we got here, our previous check was too old or this is the first
// time we're checking for os.Stat()
fi, err := os.Stat(s.Path)
if err != nil {
return time.Time{}, err
}
// Save these for later...
s.LastStat = time.Now()
s.LastStatResult = fi
return s.LastStatResult.ModTime(), nil
}
|
go
|
{
"resource": ""
}
|
q181828
|
Reader
|
test
|
func (s *FileSource) Reader() (io.Reader, error) {
fh, err := os.Open(s.Path)
if err != nil {
return nil, err
}
return fh, nil
}
|
go
|
{
"resource": ""
}
|
q181829
|
Bytes
|
test
|
func (s *FileSource) Bytes() ([]byte, error) {
rdr, err := s.Reader()
if err != nil {
return nil, err
}
return ioutil.ReadAll(rdr)
}
|
go
|
{
"resource": ""
}
|
q181830
|
NewState
|
test
|
func NewState() *State {
st := &State{
opidx: 0,
pc: NewByteCode(),
stack: stack.New(5),
markstack: stack.New(5),
framestack: stack.New(5),
frames: stack.New(5),
vars: make(Vars),
warn: os.Stderr,
MaxLoopCount: 1000,
}
st.Pushmark()
st.PushFrame()
return st
}
|
go
|
{
"resource": ""
}
|
q181831
|
PushFrame
|
test
|
func (st *State) PushFrame() *frame.Frame {
f := frame.New(st.framestack)
st.frames.Push(f)
f.SetMark(st.frames.Size())
return f
}
|
go
|
{
"resource": ""
}
|
q181832
|
PopFrame
|
test
|
func (st *State) PopFrame() *frame.Frame {
x := st.frames.Pop()
if x == nil {
return nil
}
f := x.(*frame.Frame)
for i := st.framestack.Size(); i > f.Mark(); i-- {
st.framestack.Pop()
}
return f
}
|
go
|
{
"resource": ""
}
|
q181833
|
CurrentFrame
|
test
|
func (st *State) CurrentFrame() *frame.Frame {
x, err := st.frames.Top()
if err != nil {
return nil
}
return x.(*frame.Frame)
}
|
go
|
{
"resource": ""
}
|
q181834
|
Warnf
|
test
|
func (st *State) Warnf(format string, args ...interface{}) {
st.warn.Write([]byte(fmt.Sprintf(format, args...)))
}
|
go
|
{
"resource": ""
}
|
q181835
|
AppendOutputString
|
test
|
func (st *State) AppendOutputString(o string) {
st.output.Write([]byte(o))
}
|
go
|
{
"resource": ""
}
|
q181836
|
Popmark
|
test
|
func (st *State) Popmark() int {
x := st.markstack.Pop()
return x.(int)
}
|
go
|
{
"resource": ""
}
|
q181837
|
CurrentMark
|
test
|
func (st *State) CurrentMark() int {
x, err := st.markstack.Top()
if err != nil {
x = 0
}
return x.(int)
}
|
go
|
{
"resource": ""
}
|
q181838
|
LoadByteCode
|
test
|
func (st *State) LoadByteCode(key string) (*ByteCode, error) {
return st.Loader.Load(key)
}
|
go
|
{
"resource": ""
}
|
q181839
|
Reset
|
test
|
func (st *State) Reset() {
st.opidx = 0
st.sa = nil
st.sb = nil
st.stack.Reset()
st.markstack.Reset()
st.frames.Reset()
st.framestack.Reset()
st.Pushmark()
st.PushFrame()
}
|
go
|
{
"resource": ""
}
|
q181840
|
DeclareVar
|
test
|
func (f *Frame) DeclareVar(v interface{}) int {
f.stack.Push(v)
return f.stack.Size() - 1
}
|
go
|
{
"resource": ""
}
|
q181841
|
GetLvar
|
test
|
func (f *Frame) GetLvar(i int) (interface{}, error) {
v, err := f.stack.Get(i)
if err != nil {
return nil, errors.Wrap(err, "failed to get local variable at "+strconv.Itoa(i+f.mark))
}
return v, nil
}
|
go
|
{
"resource": ""
}
|
q181842
|
SetLvar
|
test
|
func (f *Frame) SetLvar(i int, v interface{}) {
f.stack.Set(i, v)
}
|
go
|
{
"resource": ""
}
|
q181843
|
NewByteCode
|
test
|
func NewByteCode() *ByteCode {
return &ByteCode{
GeneratedOn: time.Now(),
Name: "",
OpList: nil,
Version: 1.0,
}
}
|
go
|
{
"resource": ""
}
|
q181844
|
Append
|
test
|
func (b *ByteCode) Append(op Op) {
b.OpList = append(b.OpList, op)
}
|
go
|
{
"resource": ""
}
|
q181845
|
AppendOp
|
test
|
func (b *ByteCode) AppendOp(o OpType, args ...interface{}) Op {
x := NewOp(o, args...)
b.Append(x)
return x
}
|
go
|
{
"resource": ""
}
|
q181846
|
String
|
test
|
func (b *ByteCode) String() string {
buf := rbpool.Get()
defer rbpool.Release(buf)
fmt.Fprintf(buf,
"// Bytecode for '%s'\n// Generated On: %s\n",
b.Name,
b.GeneratedOn,
)
for k, v := range b.OpList {
fmt.Fprintf(buf, "%03d. %s\n", k+1, v)
}
return buf.String()
}
|
go
|
{
"resource": ""
}
|
q181847
|
NewCachedByteCodeLoader
|
test
|
func NewCachedByteCodeLoader(
cache Cache,
cacheLevel CacheStrategy,
fetcher TemplateFetcher,
parser parser.Parser,
compiler compiler.Compiler,
) *CachedByteCodeLoader {
return &CachedByteCodeLoader{
NewStringByteCodeLoader(parser, compiler),
NewReaderByteCodeLoader(parser, compiler),
fetcher,
[]Cache{MemoryCache{}, cache},
cacheLevel,
}
}
|
go
|
{
"resource": ""
}
|
q181848
|
Load
|
test
|
func (l *CachedByteCodeLoader) Load(key string) (bc *vm.ByteCode, err error) {
defer func() {
if bc != nil && err == nil && l.ShouldDumpByteCode() {
fmt.Fprintf(os.Stderr, "%s\n", bc.String())
}
}()
var source TemplateSource
if l.CacheLevel > CacheNone {
var entity *CacheEntity
for _, cache := range l.Caches {
entity, err = cache.Get(key)
if err == nil {
break
}
}
if err == nil {
if l.CacheLevel == CacheNoVerify {
return entity.ByteCode, nil
}
t, err := entity.Source.LastModified()
if err != nil {
return nil, errors.Wrap(err, "failed to get last-modified from source")
}
if t.Before(entity.ByteCode.GeneratedOn) {
return entity.ByteCode, nil
}
// ByteCode validation failed, but we can still re-use source
source = entity.Source
}
}
if source == nil {
source, err = l.Fetcher.FetchTemplate(key)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch template")
}
}
rdr, err := source.Reader()
if err != nil {
return nil, errors.Wrap(err, "failed to get the reader")
}
bc, err = l.LoadReader(key, rdr)
if err != nil {
return nil, errors.Wrap(err, "failed to read byte code")
}
entity := &CacheEntity{bc, source}
for _, cache := range l.Caches {
cache.Set(key, entity)
}
return bc, nil
}
|
go
|
{
"resource": ""
}
|
q181849
|
NewFileCache
|
test
|
func NewFileCache(dir string) (*FileCache, error) {
f := &FileCache{dir}
return f, nil
}
|
go
|
{
"resource": ""
}
|
q181850
|
GetCachePath
|
test
|
func (c *FileCache) GetCachePath(key string) string {
// What's the best, portable way to remove make an absolute path into
// a relative path?
key = filepath.Clean(key)
key = strings.TrimPrefix(key, "/")
return filepath.Join(c.Dir, key)
}
|
go
|
{
"resource": ""
}
|
q181851
|
Get
|
test
|
func (c *FileCache) Get(key string) (*CacheEntity, error) {
path := c.GetCachePath(key)
// Need to avoid race condition
file, err := os.Open(path)
if err != nil {
return nil, errors.Wrap(err, "failed to open cache file '"+path+"'")
}
defer file.Close()
var entity CacheEntity
dec := gob.NewDecoder(file)
if err = dec.Decode(&entity); err != nil {
return nil, errors.Wrap(err, "failed to gob decode from cache file '"+path+"'")
}
return &entity, nil
}
|
go
|
{
"resource": ""
}
|
q181852
|
Set
|
test
|
func (c *FileCache) Set(key string, entity *CacheEntity) error {
path := c.GetCachePath(key)
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
return errors.Wrap(err, "failed to create directory for cache file")
}
// Need to avoid race condition
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return errors.Wrap(err, "failed to open/create a cache file")
}
defer file.Close()
f := bufio.NewWriter(file)
defer f.Flush()
enc := gob.NewEncoder(f)
if err = enc.Encode(entity); err != nil {
return errors.Wrap(err, "failed to encode Entity via gob")
}
return nil
}
|
go
|
{
"resource": ""
}
|
q181853
|
Delete
|
test
|
func (c *FileCache) Delete(key string) error {
return errors.Wrap(os.Remove(c.GetCachePath(key)), "failed to remove file cache file")
}
|
go
|
{
"resource": ""
}
|
q181854
|
Get
|
test
|
func (c MemoryCache) Get(key string) (*CacheEntity, error) {
bc, ok := c[key]
if !ok {
return nil, errors.New("cache miss")
}
return bc, nil
}
|
go
|
{
"resource": ""
}
|
q181855
|
Set
|
test
|
func (c MemoryCache) Set(key string, bc *CacheEntity) error {
c[key] = bc
return nil
}
|
go
|
{
"resource": ""
}
|
q181856
|
Delete
|
test
|
func (c MemoryCache) Delete(key string) error {
delete(c, key)
return nil
}
|
go
|
{
"resource": ""
}
|
q181857
|
NewStringLexer
|
test
|
func NewStringLexer(template string) *parser.Lexer {
l := parser.NewStringLexer(template, SymbolSet)
l.SetTagStart("[%")
l.SetTagEnd("%]")
return l
}
|
go
|
{
"resource": ""
}
|
q181858
|
NewReaderLexer
|
test
|
func NewReaderLexer(rdr io.Reader) *parser.Lexer {
l := parser.NewReaderLexer(rdr, SymbolSet)
l.SetTagStart("[%")
l.SetTagEnd("%]")
return l
}
|
go
|
{
"resource": ""
}
|
q181859
|
NewV4
|
test
|
func NewV4() *UUID {
buf := make([]byte, 16)
rand.Read(buf)
buf[6] = (buf[6] & 0x0f) | 0x40
var uuid UUID
copy(uuid[:], buf[:])
uuid.variantRFC4122()
return &uuid
}
|
go
|
{
"resource": ""
}
|
q181860
|
Parse
|
test
|
func (p *Kolonish) Parse(name string, template []byte) (*parser.AST, error) {
return p.ParseString(name, string(template))
}
|
go
|
{
"resource": ""
}
|
q181861
|
ParseReader
|
test
|
func (p *Kolonish) ParseReader(name string, rdr io.Reader) (*parser.AST, error) {
b := parser.NewBuilder()
lex := NewReaderLexer(rdr)
return b.Parse(name, lex)
}
|
go
|
{
"resource": ""
}
|
q181862
|
Visit
|
test
|
func (ast *AST) Visit() <-chan node.Node {
c := make(chan node.Node)
go func() {
defer close(c)
ast.Root.Visit(c)
}()
return c
}
|
go
|
{
"resource": ""
}
|
q181863
|
String
|
test
|
func (ast *AST) String() string {
buf := rbpool.Get()
defer rbpool.Release(buf)
c := ast.Visit()
k := 0
for v := range c {
k++
fmt.Fprintf(buf, "%03d. %s\n", k, v)
}
return buf.String()
}
|
go
|
{
"resource": ""
}
|
q181864
|
Run
|
test
|
func (vm *VM) Run(bc *ByteCode, vars Vars, output io.Writer) {
if !vm.IsSupportedByteCodeVersion(bc) {
panic(fmt.Sprintf(
"error: ByteCode version %f no supported",
bc.Version,
))
}
st := vm.st
if _, ok := output.(*bufio.Writer); !ok {
output = bufio.NewWriter(output)
defer output.(*bufio.Writer).Flush()
}
st.Reset()
st.pc = bc
st.output = output
newvars := Vars(rvpool.Get())
defer rvpool.Release(newvars)
defer newvars.Reset()
st.vars = newvars
if fc := vm.functions; fc != nil {
for k, v := range vm.functions {
st.vars[k] = v
}
}
if vars != nil {
for k, v := range vars {
st.vars[k] = v
}
}
st.Loader = vm.Loader
// This is the main loop
for op := st.CurrentOp(); op.Type() != TXOPEnd; op = st.CurrentOp() {
op.Call(st)
}
}
|
go
|
{
"resource": ""
}
|
q181865
|
DefaultParser
|
test
|
func DefaultParser(tx *Xslate, args Args) error {
syntax, ok := args.Get("Syntax")
if !ok {
syntax = "TTerse"
}
switch syntax {
case "TTerse":
tx.Parser = tterse.New()
case "Kolon", "Kolonish":
tx.Parser = kolonish.New()
default:
return errors.New("sytanx '" + syntax.(string) + "' is not available")
}
return nil
}
|
go
|
{
"resource": ""
}
|
q181866
|
DefaultLoader
|
test
|
func DefaultLoader(tx *Xslate, args Args) error {
var tmp interface{}
tmp, ok := args.Get("CacheDir")
if !ok {
tmp, _ = ioutil.TempDir("", "go-xslate-cache-")
}
cacheDir := tmp.(string)
tmp, ok = args.Get("LoadPaths")
if !ok {
cwd, _ := os.Getwd()
tmp = []string{cwd}
}
paths := tmp.([]string)
cache, err := loader.NewFileCache(cacheDir)
if err != nil {
return err
}
fetcher, err := loader.NewFileTemplateFetcher(paths)
if err != nil {
return err
}
tmp, ok = args.Get("CacheLevel")
if !ok {
tmp = 1
}
cacheLevel := tmp.(int)
tx.Loader = loader.NewCachedByteCodeLoader(cache, loader.CacheStrategy(cacheLevel), fetcher, tx.Parser, tx.Compiler)
return nil
}
|
go
|
{
"resource": ""
}
|
q181867
|
DefaultVM
|
test
|
func DefaultVM(tx *Xslate, args Args) error {
dvm := vm.NewVM()
dvm.Loader = tx.Loader
tx.VM = dvm
return nil
}
|
go
|
{
"resource": ""
}
|
q181868
|
Get
|
test
|
func (args Args) Get(key string) (interface{}, bool) {
ret, ok := args[key]
return ret, ok
}
|
go
|
{
"resource": ""
}
|
q181869
|
NewHTTPSource
|
test
|
func NewHTTPSource(r *http.Response) (*HTTPSource, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
s := &HTTPSource{
bytes.NewBuffer(body),
time.Time{},
}
if lastmodStr := r.Header.Get("Last-Modified"); lastmodStr != "" {
t, err := time.Parse(http.TimeFormat, lastmodStr)
if err != nil {
fmt.Printf("failed to parse: %s\n", err)
t = time.Now()
}
s.LastModifiedTime = t
} else {
s.LastModifiedTime = time.Now()
}
return s, nil
}
|
go
|
{
"resource": ""
}
|
q181870
|
NewStringByteCodeLoader
|
test
|
func NewStringByteCodeLoader(p parser.Parser, c compiler.Compiler) *StringByteCodeLoader {
return &StringByteCodeLoader{NewFlags(), p, c}
}
|
go
|
{
"resource": ""
}
|
q181871
|
LoadString
|
test
|
func (l *StringByteCodeLoader) LoadString(name string, template string) (*vm.ByteCode, error) {
ast, err := l.Parser.ParseString(name, template)
if err != nil {
return nil, err
}
if l.ShouldDumpAST() {
fmt.Fprintf(os.Stderr, "AST:\n%s\n", ast)
}
bc, err := l.Compiler.Compile(ast)
if err != nil {
return nil, err
}
if l.ShouldDumpByteCode() {
fmt.Fprintf(os.Stderr, "ByteCode:\n%s\n", bc)
}
return bc, nil
}
|
go
|
{
"resource": ""
}
|
q181872
|
Get
|
test
|
func (v Vars) Get(k interface{}) (interface{}, bool) {
key, ok := k.(string)
if !ok {
key = fmt.Sprintf("%s", k)
}
x, ok := v[key]
return x, ok
}
|
go
|
{
"resource": ""
}
|
q181873
|
NewOp
|
test
|
func NewOp(o OpType, args ...interface{}) Op {
h := optypeToHandler(o)
var arg interface{}
if len(args) > 0 {
arg = args[0]
}
return &op{
OpType: o,
OpHandler: h,
uArg: arg,
}
}
|
go
|
{
"resource": ""
}
|
q181874
|
MarshalBinary
|
test
|
func (o op) MarshalBinary() ([]byte, error) {
buf := rbpool.Get()
defer rbpool.Release(buf)
// Write the code/opcode
if err := binary.Write(buf, binary.LittleEndian, int64(o.OpType)); err != nil {
return nil, errors.Wrap(err, "failed to marshal op to binary")
}
// If this has args, we need to encode the args
tArg := reflect.TypeOf(o.uArg)
hasArg := tArg != nil
if hasArg {
binary.Write(buf, binary.LittleEndian, int8(1))
} else {
binary.Write(buf, binary.LittleEndian, int8(0))
}
if hasArg {
switch tArg.Kind() {
case reflect.Int:
binary.Write(buf, binary.LittleEndian, int64(2))
binary.Write(buf, binary.LittleEndian, int64(o.uArg.(int)))
case reflect.Int64:
binary.Write(buf, binary.LittleEndian, int64(2))
binary.Write(buf, binary.LittleEndian, int64(o.uArg.(int64)))
case reflect.Slice:
if tArg.Elem().Kind() != reflect.Uint8 {
panic("Slice of what?")
}
binary.Write(buf, binary.LittleEndian, int64(5))
binary.Write(buf, binary.LittleEndian, int64(len(o.uArg.([]byte))))
for _, v := range o.uArg.([]byte) {
binary.Write(buf, binary.LittleEndian, v)
}
case reflect.String:
binary.Write(buf, binary.LittleEndian, int64(6))
binary.Write(buf, binary.LittleEndian, int64(len(o.uArg.(string))))
for _, v := range []byte(o.uArg.(string)) {
binary.Write(buf, binary.LittleEndian, v)
}
default:
panic("Unknown type " + tArg.String())
}
}
v := o.comment
hasComment := v != ""
if hasComment {
binary.Write(buf, binary.LittleEndian, int8(1))
binary.Write(buf, binary.LittleEndian, v)
} else {
binary.Write(buf, binary.LittleEndian, int8(0))
}
return buf.Bytes(), nil
}
|
go
|
{
"resource": ""
}
|
q181875
|
UnmarshalBinary
|
test
|
func (o *op) UnmarshalBinary(data []byte) error {
buf := bytes.NewReader(data)
var t int64
if err := binary.Read(buf, binary.LittleEndian, &t); err != nil {
return errors.Wrap(err, "optype check failed during UnmarshalBinary")
}
o.OpType = OpType(t)
o.OpHandler = optypeToHandler(o.OpType)
var hasArg int8
if err := binary.Read(buf, binary.LittleEndian, &hasArg); err != nil {
return errors.Wrap(err, "hasArg check failed during UnmarshalBinary")
}
if hasArg == 1 {
var tArg int64
if err := binary.Read(buf, binary.LittleEndian, &tArg); err != nil {
return errors.Wrap(err, "failed to read argument from buffer during UnmarshalBinary")
}
switch tArg {
case 2:
var i int64
if err := binary.Read(buf, binary.LittleEndian, &i); err != nil {
return errors.Wrap(err, "failed to read integer argument during UnmarshalBinary")
}
o.uArg = i
case 5:
var l int64
if err := binary.Read(buf, binary.LittleEndian, &l); err != nil {
return errors.Wrap(err, "failed to read length argument during UnmarshalBinary")
}
b := make([]byte, l)
for i := int64(0); i < l; i++ {
if err := binary.Read(buf, binary.LittleEndian, &b[i]); err != nil {
return errors.Wrap(err, "failed to read bytes from buffer during UnmarshalBinary")
}
}
o.uArg = b
default:
panic(fmt.Sprintf("Unknown tArg: %d", tArg))
}
}
var hasComment int8
if err := binary.Read(buf, binary.LittleEndian, &hasComment); err != nil {
return errors.Wrap(err, "hasComment check failed during UnmarshalBinary")
}
if hasComment == 1 {
if err := binary.Read(buf, binary.LittleEndian, &o.comment); err != nil {
return errors.Wrap(err, "failed to read comment bytes during UnmarshalBinary")
}
}
return nil
}
|
go
|
{
"resource": ""
}
|
q181876
|
ArgInt
|
test
|
func (o op) ArgInt() int {
v := interfaceToNumeric(o.uArg)
return int(v.Int())
}
|
go
|
{
"resource": ""
}
|
q181877
|
ArgString
|
test
|
func (o op) ArgString() string {
// In most cases we do this because it's a sring
if v, ok := o.uArg.(string); ok {
return v
}
return interfaceToString(o.uArg)
}
|
go
|
{
"resource": ""
}
|
q181878
|
AppendOp
|
test
|
func (ctx *context) AppendOp(o vm.OpType, args ...interface{}) vm.Op {
return ctx.ByteCode.AppendOp(o, args...)
}
|
go
|
{
"resource": ""
}
|
q181879
|
Compile
|
test
|
func (c *BasicCompiler) Compile(ast *parser.AST) (*vm.ByteCode, error) {
ctx := &context{
ByteCode: vm.NewByteCode(),
}
for _, n := range ast.Root.Nodes {
compile(ctx, n)
}
// When we're done compiling, always append an END op
ctx.ByteCode.AppendOp(vm.TXOPEnd)
opt := &NaiveOptimizer{}
opt.Optimize(ctx.ByteCode)
ctx.ByteCode.Name = ast.Name
return ctx.ByteCode, nil
}
|
go
|
{
"resource": ""
}
|
q181880
|
NewV5
|
test
|
func NewV5(namespaceUUID *UUID, name []byte) *UUID {
uuid := newByHash(sha1.New(), namespaceUUID, name)
uuid[6] = (uuid[6] & 0x0f) | 0x50
return uuid
}
|
go
|
{
"resource": ""
}
|
q181881
|
Sort
|
test
|
func (list LexSymbolList) Sort() LexSymbolList {
sorter := LexSymbolSorter{
list: list,
}
sort.Sort(sorter)
return sorter.list
}
|
go
|
{
"resource": ""
}
|
q181882
|
Less
|
test
|
func (s LexSymbolSorter) Less(i, j int) bool {
return s.list[i].Priority > s.list[j].Priority
}
|
go
|
{
"resource": ""
}
|
q181883
|
Swap
|
test
|
func (s LexSymbolSorter) Swap(i, j int) {
s.list[i], s.list[j] = s.list[j], s.list[i]
}
|
go
|
{
"resource": ""
}
|
q181884
|
Copy
|
test
|
func (l *LexSymbolSet) Copy() *LexSymbolSet {
c := NewLexSymbolSet()
for k, v := range l.Map {
c.Map[k] = LexSymbol{v.Name, v.Type, v.Priority}
}
return c
}
|
go
|
{
"resource": ""
}
|
q181885
|
Set
|
test
|
func (l *LexSymbolSet) Set(name string, typ lex.ItemType, prio ...float32) {
var x float32
if len(prio) < 1 {
x = 1.0
} else {
x = prio[0]
}
l.Map[name] = LexSymbol{name, typ, x}
l.SortedList = nil // reset
}
|
go
|
{
"resource": ""
}
|
q181886
|
GetSortedList
|
test
|
func (l *LexSymbolSet) GetSortedList() LexSymbolList {
// Because symbols are parsed automatically in a loop, we need to make
// sure that we search starting with the longest term (e.g., "INCLUDE"
// must come before "IN")
// However, simply sorting the symbols using alphabetical sort then
// max-length forces us to make more comparisons than necessary.
// To get the best of both world, we allow passing a floating point
// "priority" parameter to sort the symbols
if l.SortedList != nil {
return l.SortedList
}
num := len(l.Map)
list := make(LexSymbolList, num)
i := 0
for _, v := range l.Map {
list[i] = v
i++
}
l.SortedList = list.Sort()
return l.SortedList
}
|
go
|
{
"resource": ""
}
|
q181887
|
Top
|
test
|
func (s *Stack) Top() (interface{}, error) {
if len(*s) == 0 {
return nil, errors.New("nothing on stack")
}
return (*s)[len(*s)-1], nil
}
|
go
|
{
"resource": ""
}
|
q181888
|
Resize
|
test
|
func (s *Stack) Resize(size int) {
newl := make([]interface{}, len(*s), size)
copy(newl, *s)
*s = newl
}
|
go
|
{
"resource": ""
}
|
q181889
|
Extend
|
test
|
func (s *Stack) Extend(extendBy int) {
s.Resize(s.Size() + extendBy)
}
|
go
|
{
"resource": ""
}
|
q181890
|
Grow
|
test
|
func (s *Stack) Grow(min int) {
// Automatically grow the stack to some long-enough length
if min <= s.BufferSize() {
// we have enough
return
}
s.Resize(calcNewSize(min))
}
|
go
|
{
"resource": ""
}
|
q181891
|
Get
|
test
|
func (s *Stack) Get(i int) (interface{}, error) {
if i < 0 || i >= len(*s) {
return nil, errors.New(strconv.Itoa(i) + " is out of range")
}
return (*s)[i], nil
}
|
go
|
{
"resource": ""
}
|
q181892
|
Set
|
test
|
func (s *Stack) Set(i int, v interface{}) error {
if i < 0 {
return errors.New("invalid index into stack")
}
if i >= s.BufferSize() {
s.Resize(calcNewSize(i))
}
for len(*s) < i + 1 {
*s = append(*s, nil)
}
(*s)[i] = v
return nil
}
|
go
|
{
"resource": ""
}
|
q181893
|
Push
|
test
|
func (s *Stack) Push(v interface{}) {
if len(*s) >= s.BufferSize() {
s.Resize(calcNewSize(cap(*s)))
}
*s = append(*s, v)
}
|
go
|
{
"resource": ""
}
|
q181894
|
Pop
|
test
|
func (s *Stack) Pop() interface{} {
l := len(*s)
if l == 0 {
return nil
}
v := (*s)[l-1]
*s = (*s)[:l-1]
return v
}
|
go
|
{
"resource": ""
}
|
q181895
|
String
|
test
|
func (s *Stack) String() string {
buf := bytes.Buffer{}
for k, v := range *s {
fmt.Fprintf(&buf, "%03d: %q\n", k, v)
}
return buf.String()
}
|
go
|
{
"resource": ""
}
|
q181896
|
GetHostIPs
|
test
|
func GetHostIPs() ([]net.IP, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var ips []net.IP
for _, iface := range ifaces {
if strings.HasPrefix(iface.Name, "docker") {
continue
}
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok {
ips = append(ips, ipnet.IP)
}
}
}
return ips, nil
}
|
go
|
{
"resource": ""
}
|
q181897
|
GetPrivateHostIPs
|
test
|
func GetPrivateHostIPs() ([]net.IP, error) {
ips, err := GetHostIPs()
if err != nil {
return nil, err
}
var privateIPs []net.IP
for _, ip := range ips {
// skip loopback, non-IPv4 and non-private addresses
if ip.IsLoopback() || ip.To4() == nil || !IsPrivate(ip) {
continue
}
privateIPs = append(privateIPs, ip)
}
return privateIPs, nil
}
|
go
|
{
"resource": ""
}
|
q181898
|
IsPrivate
|
test
|
func IsPrivate(ip net.IP) bool {
for _, ipnet := range privateNets {
if ipnet.Contains(ip) {
return true
}
}
return false
}
|
go
|
{
"resource": ""
}
|
q181899
|
Environ
|
test
|
func Environ() []string {
s := make([]string, 0)
FlagSet.VisitAll(func(f *flag.Flag) {
if value, ok := getenv(f.Name); ok {
s = append(s, flagAsEnv(f.Name)+"="+value)
}
})
return s
}
|
go
|
{
"resource": ""
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.