public override int Execute( Thread l ) { var tbl = (Table)l[1]; if( tbl == null ) throw new ArgumentNullException(); Value key, val; if( tbl.GetNext( ref loc, out key, out val ) ) return l.SetReturnValues( key, val ); else return l.SetNilReturnValue(); }
private static int BToNumber( Thread l ) { var nval = l[1]; if( nval.ValueType == LValueType.Number ) return l.SetReturnValues( nval ); var nstr = nval.ToLString(); if( nstr.IsNil ) return l.SetNilReturnValue(); byte[] nbuf; int nIndex, nCount; nstr.UnsafeGetDataBuffer( out nbuf, out nIndex, out nCount ); double num; if( l.StackTop < 2 ) { if( !Helpers.StrToNum( nbuf, nIndex, nCount, out num ) ) return l.SetNilReturnValue(); } else { int radix = (int)l[2]; if( radix < 2 || radix > 36 ) throw new ArgumentOutOfRangeException( "base out of range" ); if( !Helpers.StrToInt( nbuf, nIndex, nCount, out num, radix ) ) return l.SetNilReturnValue(); } return l.SetReturnValues( num ); }
private static int BINext( Thread l ) { l.StackTop = 2; var tbl = (Table)l[1]; if( tbl == null ) throw new InvalidCastException(); var key = (int)(double)l[2] + 1; Value val; if( tbl.TryGetValue( key, out val ) ) return l.SetReturnValues( key, val ); else return l.SetNilReturnValue(); }
private static int BNext( Thread l ) { var tbl = (Table)l[1]; if( tbl == null ) throw new ArgumentNullException(); Value key = l[2]; Value val; if( tbl.GetNext( ref key, out val ) ) return l.SetReturnValues( key, val ); else return l.SetNilReturnValue(); }
private static int FindCore( Thread l, bool isFind ) { var str = (LString)l[1]; var pat = (LString)l[2]; int init = l.StackTop >= 3 ? StrIdxArg( str, (int)l[3] ) : 0; if( init == -1 ) init = 0; if( init == str.Length && init != 0 ) return l.SetNilReturnValue(); if( isFind && (l[4].ToBool() || !HasPatternSpecials( pat )) ) { //do a plain search int idx = str.IndexOf( pat, init ); if( idx != -1 ) return l.SetReturnValues( idx + 1, idx + pat.Length ); else return l.SetNilReturnValue(); } else { MatchState ms; InitMatchState( out ms, l ); ms.Str = str.InternalData; ms.StrInit = LString.BufferDataOffset; ms.Pat = pat.InternalData; int patInit = LString.BufferDataOffset; bool anchor = patInit < ms.Pat.Length && ms.Pat[patInit] == (byte)'^'; if( anchor ) patInit++; int sPos = LString.BufferDataOffset + init; do { Debug.Assert( ms.MatchDepth == MaxCCalls ); ms.Level = 0; var res = SMatch( ref ms, sPos, patInit ); if( res != -1 ) { if( isFind ) { l.StackTop = 0; l.Push( sPos - LString.BufferDataOffset + 1 ); l.Push( res - LString.BufferDataOffset ); return PushCaptures( ref ms, -1, -1 ) + 2; } else { return PushCaptures( ref ms, sPos, res ); } } } while( sPos++ < ms.Str.Length && !anchor ); RetireMatchState( ref ms ); return l.SetNilReturnValue(); } }