mirror of
https://github.com/tonytins/cstdotnet.git
synced 2025-03-15 14:11:26 +00:00
140 lines
No EOL
4.6 KiB
Text
140 lines
No EOL
4.6 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Caret-Separated Text\n",
|
|
"\n",
|
|
"Caret-Separated Text (or CST) is a key-value pair format represented by numbers or words as keys and the value is the string enclosed between carets (^) that contains the translation. Any text which is not enclosed with carets is considered a comment and ignored.\n",
|
|
"\n",
|
|
"## CST.NET\n",
|
|
"\n",
|
|
"My parser, CST.NET, uses .NET's built-in indexing extension function to accomplish locating of each respective key. Originally, CST keys were only numbered-based but the indexing naturally elimates this restriction. There is no consequence for using something other numbers now. An additional normalizion process of line endings from the document's to the system's, if needed, happens before it reads the file. This was done in order to prevent avoid crashes."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"source": [
|
|
"using System.IO;"
|
|
],
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"source": [
|
|
"public static class CST\n",
|
|
"{\n",
|
|
" public static string GetValue(string cst, int key, params string[] args)\n",
|
|
" {\n",
|
|
" var entries = NormalizeEntries(cst);\n",
|
|
" return ReadEntries(entries, $\"{key}\", args);\n",
|
|
" }\n",
|
|
"\n",
|
|
" public static string GetValue(string cst, string key, params string[] args)\n",
|
|
" {\n",
|
|
" var entries = NormalizeEntries(cst);\n",
|
|
" return ReadEntries(entries, key, args);\n",
|
|
" }\n",
|
|
"\n",
|
|
" static IEnumerable<string> NormalizeEntries(string cst)\n",
|
|
" {\n",
|
|
" var lineBreaks = new string[] { \"^\\u000A\", \"^\\u000D\", \"^\\u000A\" };\n",
|
|
"\n",
|
|
" foreach (var line in lineBreaks)\n",
|
|
" {\n",
|
|
" var eol = Environment.NewLine; // System's line break\n",
|
|
"\n",
|
|
" // If the new line matches the system's, do nothing\n",
|
|
" if (line.Contains(eol))\n",
|
|
" continue;\n",
|
|
"\n",
|
|
" cst.Replace(line, eol);\n",
|
|
" }\n",
|
|
"\n",
|
|
" return cst.Split(lineBreaks, StringSplitOptions.RemoveEmptyEntries);\n",
|
|
"\n",
|
|
" }\n",
|
|
"\n",
|
|
" // TODO: support argument parameters\n",
|
|
" static string ReadEntries(IEnumerable<string> entries, string key, params string[] args)\n",
|
|
" {\n",
|
|
" var translation = \"[ENTRY NOT FOUND]\";\n",
|
|
"\n",
|
|
" // Search through array\n",
|
|
" foreach (var entry in entries)\n",
|
|
" {\n",
|
|
" // Locate index, trim carets and return translation\n",
|
|
" if (!entry.StartsWith(key))\n",
|
|
" continue;\n",
|
|
" \n",
|
|
" const char caret = '^';\n",
|
|
" // const char token = '%';\n",
|
|
"\n",
|
|
" var startIndex = entry.IndexOf(caret.ToString(),\n",
|
|
" StringComparison.OrdinalIgnoreCase);\n",
|
|
"\n",
|
|
" var line = entry.Substring(startIndex);\n",
|
|
"\n",
|
|
" /*foreach (var arg in args)\n",
|
|
" Regex.Replace(line, $\"%[1-100]\", arg);*/\n",
|
|
"\n",
|
|
" translation = line.Trim(caret);\n",
|
|
" }\n",
|
|
"\n",
|
|
" return translation;\n",
|
|
" }\n",
|
|
"}"
|
|
],
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"source": [
|
|
"var v1Path = Path.Combine(Environment.CurrentDirectory, \"data\", \"v1.cst\");\n",
|
|
"var v1File = File.ReadAllText(v1Path);\n",
|
|
"var multiLine = CST.GetValue(v1File, 1);\n",
|
|
"var singleLine = CST.GetValue(v1File, 2);\n",
|
|
"Console.WriteLine(singleLine);\n",
|
|
"Console.WriteLine(singleLine);"
|
|
],
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"source": [
|
|
"var v2Path = Path.Combine(Environment.CurrentDirectory, \"data\", \"v2.cst\");\n",
|
|
"var v2File = File.ReadAllText(v2Path);\n",
|
|
"var multiLineV2 = CST.GetValue(v2File, \"Multiline\");\n",
|
|
"var singleLineV2 = CST.GetValue(v2File, \"Singleline\");\n",
|
|
"Console.WriteLine(singleLineV2);\n",
|
|
"Console.WriteLine(multiLineV2);"
|
|
],
|
|
"outputs": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": ".NET (C#)",
|
|
"language": "C#",
|
|
"name": ".net-csharp"
|
|
},
|
|
"language_info": {
|
|
"file_extension": ".cs",
|
|
"mimetype": "text/x-csharp",
|
|
"name": "C#",
|
|
"pygments_lexer": "csharp",
|
|
"version": "8.0"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
} |