using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AuroraRecordGenerator
{
public static class Utility
{
///
/// Splits a string into an array of strings.
///
/// The string to split.
/// How large each string probably will be. Specify to theoretically improve performance.
/// Array of chars to split on.
///
public static IEnumerable LazySplit(this string source, int predictedSplitLen, params char[] splitValues)
{
var builder = predictedSplitLen <= 0
? new StringBuilder()
: new StringBuilder(predictedSplitLen);
foreach (var c in source)
{
if (splitValues.Contains(c))
{
// split off string
var result = builder.ToString();
builder.Clear();
yield return result;
}
builder.Append(c);
}
}
///
/// Splits a string into an array of strings.
///
/// The string to split.
/// Array of chars to split on.
///
public static IEnumerable LazySplit(this string source, params char[] splitValues) =>
source.LazySplit(-1, splitValues);
///
/// Splits a string into an array of strings.
///
/// The string to split.
/// How large each string probably will be. Specify to theoretically improve performance.
/// Char to split on.
///
public static IEnumerable LazySplit(this string source, int predictedSplitLen, char splitValue)
{
var builder = predictedSplitLen <= 0
? new StringBuilder()
: new StringBuilder(predictedSplitLen);
foreach (var c in source)
{
if (c == splitValue)
{
// split off string
var result = builder.ToString();
builder.Clear();
yield return result;
}
builder.Append(c);
}
}
public static IList LineSplit(this string source) =>
source.Split('\n').Where(item => item.Trim().Length != 0).ToList();
public static string CmToFeet(double cm)
{
return "0'0\"";
}
public static string KgToLb(double kg) => $"{Math.Round(kg * 2.2046, 2)} lb";
///
/// Returns and a trailing space if val is not whitespace, otherwise.
///
///
///
public static string SpaceIfValue(this string val) => string.IsNullOrWhiteSpace(val) ? string.Empty : " " + val + " ";
public static string IfEmpty(this string target, string fallback) =>
target.IsEmpty() ? fallback : target;
public static bool IsEmpty(this string val) => string.IsNullOrWhiteSpace(val);
public static string FormatAsList(this IEnumerable target) =>
target.Aggregate(new StringBuilder(), (b, s) => b.AppendLine($" - {s.Trim()}")).ToString();
public static string Repeat(this string target, int repeatNum)
{
var builder = new StringBuilder(target.Length*repeatNum);
for (var i = 0; i < repeatNum; i++)
builder.Append(target);
return builder.ToString();
}
///
/// Returns true if the specified species has gender.
///
///
///
public static bool HasGender(this SpeciesType species) =>
!(species == SpeciesType.Diona || species == SpeciesType.IPC || species == SpeciesType.Vaurca);
public static string SubspeciesNiceName(SpeciesSubType species)
{
switch (species)
{
case SpeciesSubType.MsaiTajara:
return "M'sai";
case SpeciesSubType.ZhanTajara:
return "Zhan-Khazan";
case SpeciesSubType.VaurcaWorker:
return "Worker (Type A)";
case SpeciesSubType.VaurcaBreeder:
return "Warrior (Type B)";
case SpeciesSubType.IpcShell:
return "Shell Frame";
case SpeciesSubType.IpcG1Industrial:
return "Industrial Frame";
default:
return Enum.GetName(typeof(SpeciesSubType), species);
}
}
}
}