Skip to content

Instantly share code, notes, and snippets.

@justinAurand
Created November 11, 2014 16:24
Show Gist options
  • Save justinAurand/a1258d95f60a2925f28a to your computer and use it in GitHub Desktop.
Save justinAurand/a1258d95f60a2925f28a to your computer and use it in GitHub Desktop.
Parse a fixed width record.
using System.Collections.Generic;
using System.Linq;
using System.Text;
internal static class RecordParser
{
/// <summary>
/// Parses a fixed width record on the endpoints supplied.
/// </summary>
/// <param name="record">The fixed width record to parse</param>
/// <param name="endpoints">Endpoints (using zero-based numbering) of each data point within the record</param>
/// <returns></returns>
internal static List<string> Parse(string record, List<int> endpoints)
{
if (string.IsNullOrWhiteSpace(record) || !endpoints.Any())
return null;
endpoints.Sort();
var builder = new StringBuilder();
var parsedElements = new List<string>();
int recordIndex = 0;
int endpointIndex = 0;
while (recordIndex < record.Length && endpointIndex < endpoints.Count)
{
builder.Append(record[recordIndex]);
if (recordIndex == endpoints[endpointIndex])
{
var value = builder.ToString();
parsedElements.Add(string.IsNullOrWhiteSpace(value) ? null : value.Trim());
builder.Clear();
endpointIndex++;
}
recordIndex++;
}
return parsedElements;
}
}
@justinAurand
Copy link
Author

One improvement here could be to require a sorted list parameter. Then we could check if the list argument is sorted and bounce if it's not. This would take us from O(nlog(n)) for sorting to O(n) for checking if the list is sorted.

Even better would be to accept a list of records and return tabular string data, causing us to only have to sort the endpoint list once rather than for each record parsed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment