private static int SRep(Thread l) { var str = (LString)l[1]; int n = (int)l[2]; if (n == 0) { return(l.SetReturnValues(LString.Empty)); } if (n < 0) { throw new ArgumentOutOfRangeException("negative repeat count"); } var sep = l.StackTop >= 3 ? (LString)l[3] : LString.Empty; if (n == 1) { return(l.SetReturnValues(str)); } int strOfs, strLen, sepOfs, sepLen; byte[] strBuf, sepBuf; str.UnsafeGetDataBuffer(out strBuf, out strOfs, out strLen); sep.UnsafeGetDataBuffer(out sepBuf, out sepOfs, out sepLen); var retLen = strLen * n + sepLen * (n - 1); var retBuf = LString.InternalAllocBuffer(retLen); int retOfs = LString.BufferDataOffset; Buffer.BlockCopy(strBuf, strOfs, retBuf, retOfs, strLen); if (sepLen == 0) { for (int i = 1; i < n; i++) { Buffer.BlockCopy(strBuf, strOfs, retBuf, retOfs += strLen, strLen); } } else { for (int i = 1; i < n; i++) { Buffer.BlockCopy(sepBuf, sepOfs, retBuf, retOfs += strLen, sepLen); Buffer.BlockCopy(strBuf, strOfs, retBuf, retOfs += sepLen, strLen); } } Debug.Assert(retOfs + strLen == retBuf.Length); return(l.SetReturnValues(LString.InternalFinishBuffer(retBuf))); }
private static int SChar(Thread l) { var buf = LString.InternalAllocBuffer(l.StackTop); for (int i = LString.BufferDataOffset; i < buf.Length; i++) { var cch = (int)l[i - LString.BufferDataOffset + 1]; if (cch < 0 || cch > 0xFF) { throw new ArgumentException("value out of range"); } buf[i] = (byte)cch; } return(l.SetReturnValues(LString.InternalFinishBuffer(buf))); }
private static int SLower(Thread l) { var str = (LString)l[1]; var strDat = str.InternalData; var ret = LString.InternalAllocBuffer(str.Length); for (int i = LString.BufferDataOffset; i < ret.Length; i++) { var ch = strDat[i]; if (ch >= (byte)'A' && ch <= (byte)'Z') { ch = (byte)((byte)'a' + (ch - (byte)'A')); } ret[i] = ch; } return(l.SetReturnValues(LString.InternalFinishBuffer(ret))); }