52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace StitcherApi.Services.Utilities;
|
|
|
|
public static class CoordinateParser
|
|
{
|
|
private static readonly Regex CoordRegex = new(
|
|
@"([A-Z]+)(\d+)",
|
|
RegexOptions.Compiled | RegexOptions.IgnoreCase
|
|
);
|
|
|
|
public static (int MinRow, int MinCol, int MaxRow, int MaxCol) ParseCanvasRect(string rect)
|
|
{
|
|
if (string.IsNullOrEmpty(rect))
|
|
{
|
|
throw new ArgumentException("canvas_rect cannot be null or empty.");
|
|
}
|
|
|
|
var parts = rect.Split(':');
|
|
if (parts.Length != 2)
|
|
{
|
|
throw new ArgumentException("Invalid canvas_rect format.");
|
|
}
|
|
|
|
var (row1, col1) = ParseSingleCoordinate(parts[0]);
|
|
var (row2, col2) = ParseSingleCoordinate(parts[1]);
|
|
|
|
return (
|
|
Math.Min(row1, row2),
|
|
Math.Min(col1, col2),
|
|
Math.Max(row1, row2),
|
|
Math.Max(col1, col2)
|
|
);
|
|
}
|
|
|
|
private static (int Row, int Col) ParseSingleCoordinate(string coord)
|
|
{
|
|
var match = CoordRegex.Match(coord);
|
|
if (!match.Success)
|
|
{
|
|
throw new ArgumentException($"Invalid coordinate format: {coord}");
|
|
}
|
|
|
|
var rowStr = match.Groups[1].Value.ToUpper();
|
|
var colStr = match.Groups[2].Value;
|
|
|
|
int row = rowStr.Length == 1 ? rowStr[0] - 'A' : 26 + (rowStr[1] - 'A');
|
|
int col = int.Parse(colStr) - 1;
|
|
|
|
return (row, col);
|
|
}
|
|
}
|