Files
PepLib/PepLib/IO/StringParsingHelper.cs
AJ b92e3a6af2 Refactor Reader classes: Extract common parsing logic to StringParsingHelper
Created StringParsingHelper utility class (107 lines) to consolidate repetitive parsing:
- ParseInt32, ParseDouble, ParsePercent methods with error handling
- TryParseKeyValue methods for extracting values from key=value strings
- ExtractAfterPrefix for removing fixed prefixes

Benefits:
- Eliminated duplicate parsing code across ReportReader and PlateReader
- Consistent error handling and debug logging
- Cleaner, more maintainable parsing methods
- Reduced PlateReader from 338 lines to 277 lines (18% reduction)
- Reduced ReportReader from 378 lines to 339 lines (10% reduction)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-08 23:35:53 -04:00

108 lines
3.2 KiB
C#

using System;
using System.Diagnostics;
namespace PepLib.IO
{
internal static class StringParsingHelper
{
public static int ParseInt32(string value, int defaultValue = 0)
{
int result;
if (!int.TryParse(value, out result))
{
Debug.WriteLine($"Failed to convert \"{value}\" from string to int");
return defaultValue;
}
return result;
}
public static double ParseDouble(string value, double defaultValue = 0.0)
{
double result;
if (!double.TryParse(value, out result))
{
Debug.WriteLine($"Failed to convert \"{value}\" from string to double");
return defaultValue;
}
return result;
}
public static double ParsePercent(string value, double defaultValue = 0.0)
{
var trimmed = value.TrimEnd('%', ' ');
double result;
if (!double.TryParse(trimmed, out result))
{
Debug.WriteLine($"Failed to convert \"{value}\" from percent string to double");
return defaultValue;
}
return result;
}
public static bool TrySplitKeyValue(string data, char separator, out string key, out string value)
{
var parts = data.Split(separator);
if (parts.Length == 2)
{
key = parts[0].Trim();
value = parts[1].Trim();
return true;
}
key = null;
value = null;
return false;
}
public static string ExtractAfterPrefix(string data, string prefix)
{
if (data.Length < prefix.Length)
return string.Empty;
return data.Remove(0, prefix.Length).Trim();
}
public static bool TryParseKeyValue(string data, char separator, out string value, Action<string> onError = null)
{
var parts = data.Split(separator);
if (parts.Length == 2)
{
value = parts[1].Trim();
return true;
}
if (onError != null)
onError($"Expected 2 parts when splitting '{data}' by '{separator}', got {parts.Length}");
value = null;
return false;
}
public static bool TryParseInt32FromKeyValue(string data, char separator, out int result, int defaultValue = 0)
{
string value;
if (TryParseKeyValue(data, separator, out value))
{
result = ParseInt32(value, defaultValue);
return true;
}
result = defaultValue;
return false;
}
public static bool TryParseDoubleFromKeyValue(string data, char separator, out double result, double defaultValue = 0.0)
{
string value;
if (TryParseKeyValue(data, separator, out value))
{
result = ParseDouble(value, defaultValue);
return true;
}
result = defaultValue;
return false;
}
}
}