mirror of
https://github.com/tonytins/cstdotnet.git
synced 2025-07-05 06:00:26 -04:00
Add project files.
This commit is contained in:
parent
52916f53b9
commit
2d6a9bb1d2
11 changed files with 863 additions and 0 deletions
83
CSTNet/CaretSeparatedText.cs
Normal file
83
CSTNet/CaretSeparatedText.cs
Normal file
|
@ -0,0 +1,83 @@
|
|||
// This project is licensed under the MIT license.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CSTNet
|
||||
{
|
||||
public static class CaretSeparatedText
|
||||
{
|
||||
/// <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}");
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
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<string> 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue