Skip to content

Support numbers with exponent in WKT #3922

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

Merged
merged 1 commit into from
Jul 8, 2019
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
54 changes: 11 additions & 43 deletions src/Nest/QueryDsl/Geo/WKT/GeoWKTReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@ private static TokenType NextCloserOrComma(WellKnownTextTokenizer tokenizer)

private static double NextNumber(WellKnownTextTokenizer tokenizer)
{
if (tokenizer.NextToken() == TokenType.Number)
if (tokenizer.NextToken() == TokenType.Word)
{
if (string.Equals(tokenizer.TokenValue, WellKnownTextTokenizer.NaN, StringComparison.OrdinalIgnoreCase))
return double.NaN;

if (double.TryParse(
tokenizer.TokenValue,
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent,
CultureInfo.InvariantCulture, out var d))
return d;
}
Expand All @@ -278,7 +278,7 @@ private static double NextNumber(WellKnownTextTokenizer tokenizer)
private static bool IsNumberNext(WellKnownTextTokenizer tokenizer)
{
var token = tokenizer.PeekToken();
return token == TokenType.Number;
return token == TokenType.Word;
}
}

Expand All @@ -288,7 +288,6 @@ private static bool IsNumberNext(WellKnownTextTokenizer tokenizer)
internal enum CharacterType : byte
{
Whitespace,
Digit,
Alpha,
Comment
}
Expand All @@ -300,7 +299,6 @@ internal enum TokenType : byte
{
None,
Word,
Number,
LParen,
RParen,
Comma
Expand Down Expand Up @@ -339,15 +337,14 @@ static WellKnownTextTokenizer()
// build a map of ASCII chars and their types
// Any unmapped ASCII will be considered whitespace
// and anything > 0 outside of ASCII will be considered alpha.
// Treat + - and . as digit characters to make parsing numbers easier.
Chars('a', 'z', CharacterType.Alpha);
Chars('A', 'Z', CharacterType.Alpha);
Chars(128 + 32, 255, CharacterType.Alpha);
Chars('0', '9', CharacterType.Digit);
Chars('0', '9', CharacterType.Alpha);
Chars(LParen, RParen, CharacterType.Alpha);
Chars(Plus, Plus, CharacterType.Digit);
Chars(Plus, Plus, CharacterType.Alpha);
Chars(Comma, Comma, CharacterType.Alpha);
Chars(Minus, Dot, CharacterType.Digit);
Chars(Minus, Dot, CharacterType.Alpha);
Chars(Comment, Comment, CharacterType.Comment);
}

Expand Down Expand Up @@ -399,7 +396,6 @@ public string TokenString()
switch (TokenType)
{
case TokenType.Word:
case TokenType.Number:
return TokenValue;
case TokenType.None:
return "END-OF-STREAM";
Expand Down Expand Up @@ -514,33 +510,6 @@ public TokenType NextToken()
{
var i = 0;

do
{
_buffer.Insert(i++, (char)c);
c = Read();

if (c < 0)
characterType = CharacterType.Whitespace;
else if (c < CharacterTypesLength)
characterType = CharacterTypes[c];
else
characterType = CharacterType.Alpha;
} while (characterType == CharacterType.Alpha);

_peekChar = c;
TokenValue = new string(_buffer.ToArray(), 0, i);

// special case for NaN
if (string.Equals(TokenValue, NaN, StringComparison.OrdinalIgnoreCase))
return TokenType = TokenType.Number;

return TokenType = TokenType.Word;
}

if (characterType == CharacterType.Digit)
{
var i = 0;
var dots = 0;
do
{
_buffer.Insert(i++, (char)c);
Expand All @@ -550,20 +519,19 @@ public TokenType NextToken()
characterType = CharacterType.Whitespace;
else if (c < CharacterTypesLength)
{
if (c == LParen || c == RParen || c == Comma)
break;

characterType = CharacterTypes[c];
if (c == Dot)
dots++;
}
else
characterType = CharacterType.Alpha;
} while (characterType == CharacterType.Digit);
} while (characterType == CharacterType.Alpha);

_peekChar = c;
TokenValue = new string(_buffer.ToArray(), 0, i);

return dots > 1
? TokenType = TokenType.Word
: TokenType = TokenType.Number;
return TokenType = TokenType.Word;
}

if (characterType == CharacterType.Comment)
Expand Down
16 changes: 16 additions & 0 deletions src/Tests/Tests/QueryDsl/Geo/Shape/GeoWKTTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public void ReadAndWritePoint()
GeoWKTWriter.Write(point).Should().Be(wkt);
}

[U]
public void ReadAndWritePointWithExponent()
{
var wkt = "POINT (1.2E2 -2.5E-05)";
var shape = GeoWKTReader.Read(wkt);

shape.Should().BeOfType<PointGeoShape>();
var point = (PointGeoShape)shape;

point.Coordinates.Latitude.Should().Be(-0.000025);
point.Coordinates.Longitude.Should().Be(120);

// 1.2E2 will be expanded
GeoWKTWriter.Write(point).Should().Be("POINT (120 -2.5E-05)");
}

[U]
public void ReadAndWriteMultiPoint()
{
Expand Down