Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions main/SS/Formula/FormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,9 +1173,6 @@ private static AreaReference CreateAreaRef(SimpleRangePart part1, SimpleRangePar
}
return new AreaReference(part1.CellReference, part2.CellReference);
}
private string CELL_REF_PATTERN = "(\\$?[A-Za-z]+)?(\\$?[0-9]+)?";




/**
Expand Down Expand Up @@ -1213,11 +1210,9 @@ private SimpleRangePart ParseSimpleRangePart()
{
return null;
}
String rep = _formulaString.Substring(_pointer - 1, ptr - _pointer + 1);
ReadOnlySpan<char> rep = _formulaString.AsSpan(_pointer - 1, ptr - _pointer + 1);

Regex pattern = new Regex(CELL_REF_PATTERN);

if (!pattern.IsMatch(rep))
if (!CellReferenceParser.TryParseCellReference(rep, out _, out var column, out _, out var row))
{
return null;
}
Expand All @@ -1231,19 +1226,14 @@ private SimpleRangePart ParseSimpleRangePart()
}
else if (hasLetters)
{
if (!CellReference.IsColumnWithinRange(rep.Replace("$", ""), _ssVersion))
if (!CellReference.IsColumnWithinRange(column, _ssVersion))
{
return null;
}
}
else if (hasDigits)
{
int i;
try
{
i = Int32.Parse(rep.Replace("$", ""), CultureInfo.InvariantCulture);
}
catch
if (!CellReferenceParser.TryParsePositiveInt32Fast(row, out int i))
{
return null;
}
Expand All @@ -1260,7 +1250,7 @@ private SimpleRangePart ParseSimpleRangePart()


ResetPointer(ptr + 1); // stepping forward
return new SimpleRangePart(rep, hasLetters, hasDigits);
return new SimpleRangePart(rep.ToString(), hasLetters, hasDigits);
}


Expand Down Expand Up @@ -1544,7 +1534,9 @@ private void ResetPointer(int ptr)
/**
* @return <c>true</c> if the specified name is a valid cell reference
*/
private bool IsValidCellReference(String str)
private bool IsValidCellReference(String str) => IsValidCellReference(str.AsSpan());

private bool IsValidCellReference(ReadOnlySpan<char> str)
{
//check range bounds against grid max
bool result = CellReference.ClassifyCellReference(str, _ssVersion) == NameType.Cell;
Expand All @@ -1557,7 +1549,7 @@ private bool IsValidCellReference(String str)
* (b) LOG10 + 1
* In (a) LOG10 is a name of a built-in function. In (b) LOG10 is a cell reference
*/
bool isFunc = FunctionMetadataRegistry.GetFunctionByName(str.ToUpper()) != null;
bool isFunc = FunctionMetadataRegistry.GetFunctionByName(str.ToString().ToUpper()) != null;
if (isFunc)
{
int savePointer = _pointer;
Expand Down
6 changes: 2 additions & 4 deletions main/SS/Formula/Function/FunctionMetadataRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ public static short LookupIndexByName(String name)

private FunctionMetadata GetFunctionByNameInternal(String name)
{
if (_functionDataByName.ContainsKey(name))
return _functionDataByName[name];
else
return null;
_functionDataByName.TryGetValue(name, out var metadata);
return metadata;
}


Expand Down
7 changes: 4 additions & 3 deletions main/SS/Util/CellAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ public CellAddress(String address)
}
}

String sCol = address.Substring(0, loc).ToUpper();
String sRow = address.Substring(loc);
ReadOnlySpan<char> sCol = address.AsSpan(0, loc);
ReadOnlySpan<char> sRow = address.AsSpan(loc);

// FIXME: breaks if Address Contains a sheet name or dollar signs from an absolute CellReference
this._row = int.Parse(sRow) - 1;
CellReferenceParser.TryParsePositiveInt32Fast(sRow, out var rowNumber);
this._row = rowNumber - 1;
this._col = CellReference.ConvertColStringToIndex(sCol);
}

Expand Down
4 changes: 2 additions & 2 deletions main/SS/Util/CellRangeAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public static CellRangeAddress ValueOf(String reference)
}
else
{
a = new CellReference(reference.Substring(0, sep));
b = new CellReference(reference.Substring(sep + 1));
a = new CellReference(reference.AsSpan(0, sep));
b = new CellReference(reference.AsSpan(sep + 1));
}
return new CellRangeAddress(a.Row, b.Row, a.Col, b.Col);
}
Expand Down
Loading