Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor digit and letter parsing to allow Unicode characters #1077

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions ClosedXML/Excel/CalcEngine/CalcEngine.cs
Expand Up @@ -524,8 +524,8 @@ private void GetToken()
// operators
// this gets called a lot, so it's pretty optimized.
// note that operators must start with non-letter/digit characters.
var isLetter = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
var isDigit = c >= '0' && c <= '9';
var isLetter = char.IsLetter(c);
var isDigit = char.IsDigit(c);

var isEnclosed = matchingClosingSymbols.Keys.Contains(c);
char matchingClosingSymbol = '\0';
Expand All @@ -535,8 +535,8 @@ private void GetToken()
if (!isLetter && !isDigit && !isEnclosed)
{
// if this is a number starting with a decimal, don't parse as operator
var nxt = _ptr + 1 < _len ? _expr[_ptr + 1] : 0;
bool isNumber = c == _decimal && nxt >= '0' && nxt <= '9';
var nxt = _ptr + 1 < _len ? _expr[_ptr + 1] : '0';
bool isNumber = c == _decimal && char.IsDigit(nxt);
if (!isNumber)
{
// look up localized list separator
Expand Down Expand Up @@ -581,7 +581,7 @@ private void GetToken()
c = _expr[_ptr + i];

// digits always OK
if (c >= '0' && c <= '9')
if (char.IsDigit(c))
{
val = val * 10 + (c - '0');
if (div > -1)
Expand Down Expand Up @@ -694,8 +694,8 @@ private void GetToken()
for (i = 1; i + _ptr < _len; i++)
{
c = _expr[_ptr + i];
isLetter = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
isDigit = c >= '0' && c <= '9';
isLetter = char.IsLetter(c);
isDigit = char.IsDigit(c);

if (isEnclosed && c == matchingClosingSymbol)
{
Expand All @@ -704,8 +704,8 @@ private void GetToken()

i++;
c = _expr[_ptr + i];
isLetter = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
isDigit = c >= '0' && c <= '9';
isLetter = char.IsLetter(c);
isDigit = char.IsDigit(c);
}

var disallowedSymbols = new List<char>() { '\\', '/', '*', '[', ':', '?' };
Expand Down
20 changes: 20 additions & 0 deletions ClosedXML_Tests/Excel/Misc/FormulaTests.cs
Expand Up @@ -186,5 +186,25 @@ public void FormulasWithErrors()
Assert.Throws<NullValueException>(() => XLWorkbook.EvaluateExpr("YEAR(#NULL!)"));
Assert.Throws<NumberException>(() => XLWorkbook.EvaluateExpr("YEAR(#NUM!)"));
}

[Test]
public void UnicodeLetterParsing()
{
using (var wb = new XLWorkbook())
{
var ws1 = wb.AddWorksheet("Sheet C C�");
var ws2 = wb.AddWorksheet("�C");
var ws3 = wb.AddWorksheet("Sheet3");

ws1.FirstCell().SetValue(100);
ws2.FirstCell().SetValue(50);

ws3.FirstCell().FormulaA1 = "='Sheet C C�'!A1";
ws3.FirstCell().CellBelow().FormulaA1 = "�C!A1";

Assert.AreEqual(100, ws3.FirstCell().Value);
Assert.AreEqual(50, ws3.FirstCell().CellBelow().Value);
}
}
}
}