tlang-interperter-cs/tlanglib/TArray.cs

70 lines
2.0 KiB
C#
Raw Permalink Normal View History

2023-06-08 18:14:35 +00:00
using System;
2023-03-10 10:12:36 +00:00
using System.Collections.Generic;
2023-06-08 18:14:35 +00:00
using System.Text;
using Newtonsoft.Json.Linq;
2023-03-10 10:12:36 +00:00
2023-03-09 19:40:14 +00:00
namespace tlang
{
public class TArray : TObject
{
2023-06-08 18:14:35 +00:00
public override JToken AsToken()
{
var ls = new JArray();
foreach(var item in Items)
{
ls.Add(item.AsToken());
}
return ls;
}
public byte[] ToBytes()
{
byte[] data = new byte[Items.Count];
SetBytes(data,0,data.Length);
return data;
}
public void SetBytes(byte[] data,int offset,int count)
{
count = Math.Min(data.Length - offset,count);
for(int i = 0;i<count;i++)
{
var item = Items[i+offset];
var c = item as TChar;
var n = item as TNumber;
if(c != null) data[i] = (byte)c.Value;
if(n != null) data[i] = (byte)n.Value;
}
}
public static TArray FromBytes(byte[] data,int offset,int count)
{
var array = TObject.Array(count);
for(int i = 0;i<count;i++)
{
array.Items[i+offset] = TObject.Number(data[i]);
}
return array;
}
2023-03-09 23:57:16 +00:00
public override bool AsBoolean => Items.Count > 0;
2023-06-08 18:14:35 +00:00
public TArray(int sz=0)
2023-03-09 19:40:14 +00:00
{
2023-06-08 18:14:35 +00:00
Items =new List<TObject>();
for(int i = 0;i<sz;i++)
{
Items.Add(TObject.Uninit);
}
}
public override string ToString()
{
StringBuilder b = new StringBuilder();
b.AppendLine("Array Items:");
foreach(var item in Items)
{
b.AppendLine(item.ToString());
}
return b.ToString();
2023-03-09 19:40:14 +00:00
}
2023-06-08 18:14:35 +00:00
public List<TObject> Items {get;set;}
2023-03-09 19:40:14 +00:00
}
}