// This project is licensed under the MIT license.
using System;
using System.Collections.Generic;
namespace CSTNet
{
public static class CaretSeparatedText
{
///
/// Gets the value from the integer-based key.
///
/// Returns the entry
public static string Parse(string content, int key)
{
var entries = NormalizeEntries(content);
return GetEntry(entries, $"{key}");
}
///
/// Gets the value from the string-based key.
///
/// Returns the entry
public static string Parse(string content, string key)
{
var entries = NormalizeEntries(content);
return GetEntry(entries, key);
}
///
/// Replaces the document's line endings with the native system line endings.
///
/// This stage ensures there are no crashes during parsing.
static IEnumerable NormalizeEntries(string content)
{
var lineBreaks = new string[]
{
"^\u000A", // LF
"^\u000D", // CR
"^\u000D\u000A", // CR+LF
"^\u2028" // LS
};
foreach (var line in lineBreaks)
{
var eol = Environment.NewLine; // System's line break
// If the new line matches the system's, do nothing
if (line.Contains(eol))
continue;
content.Replace(line, eol);
}
return content.Split(lineBreaks, StringSplitOptions.RemoveEmptyEntries);
}
// TODO: support argument parameters
static string GetEntry(IEnumerable entries, string key)
{
var translation = "[ENTRY NOT FOUND]";
// Search through array
foreach (var entry in entries)
{
// Locate index, trim carets and return translation
if (!entry.StartsWith(key))
continue;
const char caret = '^';
var startIndex = entry.IndexOf(caret.ToString(),
StringComparison.OrdinalIgnoreCase);
var line = entry.Substring(startIndex);
translation = line.Trim(caret);
}
return translation;
}
}
}