2022-04-29 06:47:53 -04:00
|
|
|
// This project is licensed under the BSD 3-Clause license.
|
2020-12-13 07:48:15 -05:00
|
|
|
// See the LICENSE file in the project root for more information.
|
2020-12-08 17:56:38 -05:00
|
|
|
|
2022-04-29 06:47:53 -04:00
|
|
|
namespace CSTNet;
|
2021-11-19 13:52:26 -05:00
|
|
|
|
2022-07-21 10:23:31 -04:00
|
|
|
[Obsolete("Use CST class instead.")]
|
|
|
|
public class CaretSeparatedText : CST { }
|
|
|
|
|
|
|
|
public class CST
|
2020-12-08 17:56:38 -05:00
|
|
|
{
|
2021-11-19 13:52:26 -05:00
|
|
|
const char CARET = '^';
|
|
|
|
const string LF = "\u000A";
|
|
|
|
const string CR = "\u000D";
|
|
|
|
const string CRLF = "\u000D\u000A";
|
|
|
|
const string LS = "\u2028";
|
2020-12-11 01:05:46 -05:00
|
|
|
|
2021-11-19 13:52:26 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the value from the digit-based key.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Returns the entry</returns>
|
|
|
|
public static string Parse(string content, int key) => Parse(content, key.ToString());
|
2020-12-08 17:56:38 -05:00
|
|
|
|
2021-11-19 13:52:26 -05:00
|
|
|
/// <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);
|
|
|
|
}
|
2020-12-08 17:56:38 -05:00
|
|
|
|
2021-11-19 13:52:26 -05:00
|
|
|
/// <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>
|
2023-01-07 10:45:54 -05:00
|
|
|
/// <param name="content">The content of the document.</param>
|
|
|
|
/// <returns>The document's content with native system line endings.</returns>
|
2021-11-19 13:52:26 -05:00
|
|
|
static IEnumerable<string> NormalizeEntries(string content)
|
|
|
|
{
|
2023-01-07 10:45:54 -05:00
|
|
|
// Check if the document already uses native system line endings.
|
2021-11-19 13:52:26 -05:00
|
|
|
if (!content.Contains(Environment.NewLine))
|
2020-12-08 17:56:38 -05:00
|
|
|
{
|
2023-01-07 10:45:54 -05:00
|
|
|
// If not, check for and replace other line ending types.
|
2021-11-19 13:52:26 -05:00
|
|
|
if (content.Contains(LF))
|
|
|
|
content = content.Replace(LF, Environment.NewLine);
|
2020-12-08 17:56:38 -05:00
|
|
|
|
2021-11-19 13:52:26 -05:00
|
|
|
if (content.Contains(CR))
|
|
|
|
content = content.Replace(CR, Environment.NewLine);
|
2020-12-11 01:05:46 -05:00
|
|
|
|
2021-11-19 13:52:26 -05:00
|
|
|
if (content.Contains(CRLF))
|
|
|
|
content = content.Replace(CRLF, Environment.NewLine);
|
2020-12-08 17:56:38 -05:00
|
|
|
|
2021-11-19 13:52:26 -05:00
|
|
|
if (content.Contains(LS))
|
|
|
|
content = content.Replace(LS, Environment.NewLine);
|
|
|
|
}
|
2020-12-08 17:56:38 -05:00
|
|
|
|
2023-01-07 10:45:54 -05:00
|
|
|
// Split the content by the caret and newline characters.
|
2021-11-19 13:52:26 -05:00
|
|
|
var lines = content.Split(new[] { $"{CARET}{Environment.NewLine}" },
|
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
2020-12-11 01:05:46 -05:00
|
|
|
|
2023-01-07 10:45:54 -05:00
|
|
|
// Filter out any lines that start with "//", "#", "/*", or end with "*/".
|
2021-11-19 13:52:26 -05:00
|
|
|
return lines.Where(line =>
|
|
|
|
!line.StartsWith("//") &&
|
|
|
|
!line.StartsWith("#") &&
|
|
|
|
!line.StartsWith("/*") &&
|
|
|
|
!line.EndsWith("*/"))
|
|
|
|
.AsEnumerable();
|
|
|
|
}
|
2020-12-08 17:56:38 -05:00
|
|
|
|
2023-01-07 10:45:54 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Retrieves the value for the specified key from the given entries.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="entries">The entries to search through.</param>
|
|
|
|
/// <param name="key">The key to search for.</param>
|
|
|
|
/// <returns>The value for the specified key, or a default string if not found.</returns>
|
2021-11-19 13:52:26 -05:00
|
|
|
static string GetEntry(IEnumerable<string> entries, string key)
|
|
|
|
{
|
2023-01-07 10:45:54 -05:00
|
|
|
// Iterate through the entries.
|
2021-11-19 13:52:26 -05:00
|
|
|
foreach (var entry in entries)
|
2020-12-08 17:56:38 -05:00
|
|
|
{
|
2021-11-19 13:52:26 -05:00
|
|
|
// If the line doesn't start with the key, keep searching.
|
|
|
|
if (!entry.StartsWith(key))
|
|
|
|
continue;
|
2020-12-08 17:56:38 -05:00
|
|
|
|
2023-01-07 10:45:54 -05:00
|
|
|
// Locate the index of the caret character.
|
2021-11-19 13:52:26 -05:00
|
|
|
var startIndex = entry.IndexOf(CARET);
|
2023-01-07 10:45:54 -05:00
|
|
|
// Get the line from the caret character to the end of the string.
|
|
|
|
var line = entry[startIndex..];
|
2020-12-08 17:56:38 -05:00
|
|
|
|
2023-01-07 10:45:54 -05:00
|
|
|
// Return the line with the caret characters trimmed.
|
2021-11-19 13:52:26 -05:00
|
|
|
return line.TrimStart(CARET).TrimEnd(CARET);
|
2020-12-08 17:56:38 -05:00
|
|
|
}
|
2020-12-13 07:48:15 -05:00
|
|
|
|
2023-01-07 10:45:54 -05:00
|
|
|
// If no entry is found, return a default string.
|
2021-11-19 13:52:26 -05:00
|
|
|
return "***MISSING***";
|
|
|
|
}
|
2023-01-07 10:45:54 -05:00
|
|
|
|
2020-12-13 07:48:15 -05:00
|
|
|
}
|
2022-07-21 10:23:31 -04:00
|
|
|
|
|
|
|
|