UIText class

The UIText class allows for travseing in "/<directory>/<language>.dir"  directories and searching for CST files by their Id number.  See usage.md for more info.
This commit is contained in:
Tony Bark 2020-12-13 07:48:15 -05:00
parent 86ed534870
commit 0ff7bb68c4
22 changed files with 289 additions and 170 deletions

View file

@ -1,92 +0,0 @@
// This project is licensed under the MIT license.
using System;
using System.Collections.Generic;
namespace CSTNet
{
public static class CaretSeparatedText
{
const char CARET = '^';
static readonly string _lf = "\u000A";
static readonly string _cr = "\u000D";
static readonly string _crlf = "\u000D\u000A";
static readonly string _ls = "\u2028";
/// <summary>
/// Gets the value from the integer-based key.
/// </summary>
/// <returns>Returns the entry</returns>
public static string Parse(string content, int key)
{
var entries = NormalizeEntries(content);
return GetEntry(entries, key.ToString());
}
/// <summary>
/// Gets the value from the string-based key.
/// </summary>
/// <returns>Returns the entry</returns>
public static string Parse(string content, string key)
{
var entries = NormalizeEntries(content);
return GetEntry(entries, key);
}
/// <summary>
/// Replaces the document's line endings with the native system line endings.
/// </summary>
/// <remarks>This stage ensures there are no crashes during parsing.</remarks>
static IEnumerable<string> NormalizeEntries(string content)
{
if (!content.Contains(Environment.NewLine))
{
if (content.Contains(_lf))
content = content.Replace(_lf, Environment.NewLine);
if (content.Contains(_cr))
content = content.Replace(_cr, Environment.NewLine);
if (content.Contains(_crlf))
content = content.Replace(_crlf, Environment.NewLine);
if (content.Contains(_ls))
content = content.Replace(_ls, Environment.NewLine);
}
var lines = content.Split(new[] { $"{CARET}{Environment.NewLine}" },
StringSplitOptions.RemoveEmptyEntries);
var entries = new List<string>();
foreach (var line in lines)
{
// Skip comments
if (line.StartsWith("//") || line.StartsWith("#") ||
line.StartsWith("/*") || line.EndsWith("*/"))
continue;
entries.Add(line);
}
return entries;
}
static string GetEntry(IEnumerable<string> entries, string key)
{
// Search through list
foreach (var entry in entries)
{
// If the line doesn't start with the key, keep searching.
if (!entry.StartsWith(key))
continue;
// Locate index, trim carets and return translation.
var startIndex = entry.IndexOf(CARET);
var line = entry.Substring(startIndex);
return line.TrimStart(CARET).TrimEnd(CARET);
}
return "***MISSING***";
}
}
}

View file

@ -1,14 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.0.2</Version>
<Authors>Tony Bark</Authors>
<PackageDescription>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 contents. CST.NET is a library for prasing the CST format.</PackageDescription>
<RepositoryUrl>https://github.com/sixamsoft/cst-dotnet</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Product>CST.Net</Product>
<AssemblyName>CSTNet</AssemblyName>
</PropertyGroup>
</Project>