Skip to content

Instantly share code, notes, and snippets.

@apriori
Created March 10, 2014 18:57
Show Gist options
  • Save apriori/9471845 to your computer and use it in GitHub Desktop.
Save apriori/9471845 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SQFArrayParser
{
class ListBlock
{
string value = null;
List<ListBlock> block = null;
}
class Program
{
static string test = "[[\"SAVE_COMMAND\",\"76561197964280320\"],[[\"ItemMap\",\"ItemCompass\",\"ItemWatch\",\"H_MilCap_mcamo\",\"G_Tactical_Black\"],\"\",[],\"Colt1911\",[\"\",\"\",\"\"],\"\",[],\"U_B_CombatUniform_mcam\",[\"7Rnd_45ACP_1911\"],\"V_BandollierB_rgr\",[],\"B_Carryall_cbr\",[],[[],[\"7Rnd_45ACP_1911\"],[],[\"7Rnd_45ACP_1911\",\"7Rnd_45ACP_1911\",\"7Rnd_45ACP_1911\",\"7Rnd_45ACP_1911\",\"7Rnd_45ACP_1911\"]],\"Colt1911\",\"Colt1911\"]]";
//static string test = "[[SAVE_COMMAND,76561197964280320],[1.2, 3.4]]";
static List<string> explodeIfNotEscaped(string str, char blockStart, char blockEnd, char delimiter) {
List<string> result = new List<string>();
if (String.IsNullOrEmpty(str))
{
return result;
}
int start = 0;
Boolean ignore = false;
for (int i = 0; i < str.Length; ++i)
{
char current = str[i];
if (current == blockStart)
{
ignore = true;
}
if (current == blockEnd)
{
ignore = false;
}
if ((current == delimiter || i == str.Length -1) && !ignore)
{
string sub = str.Substring(start, i - start+1);
result.Add(sub);
start = i + 1;
}
}
return result;
}
static string trimOnce(string str, string beginString, string endString)
{
if (String.IsNullOrEmpty(str))
{
return str;
}
if (str.StartsWith(beginString) && str.EndsWith(endString))
{
return str.Substring(1, str.Length - 2);
}
return str;
}
static ListBlock explodeNested(string str)
{
ListBlock result = new ListBlock();
if (String.IsNullOrEmpty(str))
{
return result;
}
str = trimOnce(str, "[", "]");
if (!str.StartsWith("[") && !str.EndsWith("]"))
{
foreach (string element in explodeIfNotEscaped(str, '\"', '\"', ','))
{
Console.WriteLine(element);
}
return result;
}
int braceBalanced = 0;
int start = 0;
int end = 0;
for (int i = start; i < str.Length; ++i)
{
if (str[i] == '[')
{
braceBalanced++;
}
if (str[i] == ']') {
braceBalanced--;
}
if (braceBalanced == 0 && (str[i] == ',' || i == str.Length -1))
{
end = str.Length - 1;
if (str[i] == ',')
{
end = i - 1;
}
string sub = str.Substring(start, end - start+1);
if (sub.StartsWith("[") && sub.EndsWith("]"))
{
explodeNested(sub);
}
start = i+1;
}
}
return result;
}
static void Main(string[] args)
{
List<List<string>> lists = new List<List<string>>();
Console.WriteLine("Results");
explodeNested(test);
/*
foreach (string element in explodeIfNotEscaped("\"TEST\", \"testset\"", '\"', '\"', ','))
{
Console.WriteLine(element);
}
*/
/*
foreach (string element in explodeNested(test)) {
Console.WriteLine(element);
}
*/
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment