Better spanify nth entry tsv fetch

Closes #3846
This commit is contained in:
Kurt 2023-03-20 15:52:27 -07:00
parent f611c4a728
commit 224283b7f8
2 changed files with 18 additions and 13 deletions

View file

@ -9,7 +9,7 @@ public static partial class Util
{
string[] inputCSV = GetStringList(textFile);
int index = GeoLocation.GetLanguageIndex(lang);
var list = GetCBListFromCSV(inputCSV, index + 1);
var list = GetCBListFromCSV(inputCSV, index);
if (list.Count > 1)
list.Sort(1, list.Count - 1, Comparer); // keep null value as first
return list;
@ -20,9 +20,11 @@ public static partial class Util
var arr = new List<ComboItem>(inputCSV.Count);
foreach (var line in inputCSV)
{
var text = StringUtil.GetNthEntry(line, index, 4);
var value = line.AsSpan(0, 3);
var item = new ComboItem(text, ToInt32(value));
var span = line.AsSpan();
// 3 digits index; 1 tab space, then the string entries.
var value = int.Parse(span[..3]);
var text = StringUtil.GetNthEntry(span[4..], index);
var item = new ComboItem(new string(text), value);
arr.Add(item);
}
return arr;

View file

@ -36,22 +36,25 @@ public static class StringUtil
}
/// <summary>
/// Gets the <see cref="nth"/> string entry within the input <see cref="line"/>, based on the <see cref="separator"/> and <see cref="start"/> position.
/// Gets the <see cref="nth"/> string entry within the input <see cref="line"/>, based on the <see cref="separator"/>.
/// </summary>
public static string GetNthEntry(ReadOnlySpan<char> line, int nth, int start, char separator = '\t')
public static ReadOnlySpan<char> GetNthEntry(ReadOnlySpan<char> line, int nth, char separator = '\t')
{
if (nth != 1)
start = line.IndexOfNth(separator, nth - 1, start + 1);
var end = line.IndexOfNth(separator, 1, start + 1);
int start = 0;
if (nth != 0)
start = line.IndexOfNth(separator, nth) + 1; // 1 char after separator
var tail = line[start..];
var end = tail.IndexOf(separator);
if (end == -1)
return new string(line[(start + 1)..]);
return new string(line[(start + 1)..end]);
return tail;
return tail[..end];
}
private static int IndexOfNth(this ReadOnlySpan<char> s, char t, int n, int start)
private static int IndexOfNth(this ReadOnlySpan<char> s, char t, int n)
{
int count = 0;
for (int i = start; i < s.Length; i++)
for (int i = 0; i < s.Length; i++)
{
if (s[i] != t)
continue;