diff options
author | Jon Bratseth <bratseth@verizonmedia.com> | 2019-12-17 13:00:13 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@verizonmedia.com> | 2019-12-17 13:00:13 +0100 |
commit | c589718d26f2b89875acfdd90433832292e6bca2 (patch) | |
tree | d9566cf590a866e67c1aa4ab0005304cdd1c4e0a /vespajlib | |
parent | 3a84c90423e86bb95c9a620c1c9ccc1a055b2d37 (diff) |
More robust parsing
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java | 5 | ||||
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/tensor/TensorParserTestCase.java | 7 |
2 files changed, 10 insertions, 2 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java index 8f8469cc63a..9aa764a0b36 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java @@ -143,7 +143,7 @@ class TensorParser { } protected void skipSpace() { - while (position < string.length() && string.charAt(position) == ' ') + while (position < string.length() && Character.isWhitespace(string.charAt(position))) position++; } @@ -227,6 +227,7 @@ class TensorParser { protected int nextStopCharIndex(int position, String valueString) { while (position < valueString.length()) { + if (Character.isWhitespace(valueString.charAt(position))) return position; if (valueString.charAt(position) == ',') return position; if (valueString.charAt(position) == ']') return position; if (valueString.charAt(position) == '}') return position; @@ -365,6 +366,8 @@ class TensorParser { TensorAddress address = consumeLabels(); if ( ! address.isEmpty()) consume(':'); + else + consumeOptional(':'); int valueEnd = string.indexOf(',', position); if (valueEnd < 0) { // last value diff --git a/vespajlib/src/test/java/com/yahoo/tensor/TensorParserTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/TensorParserTestCase.java index 78afa1f7449..5a68df6c7df 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/TensorParserTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/TensorParserTestCase.java @@ -9,6 +9,11 @@ import static org.junit.Assert.fail; public class TensorParserTestCase { @Test + public void testEmpty() { + assertEquals(Tensor.Builder.of(TensorType.empty).cell(1).build(), Tensor.from("tensor():{{}:1}")); + } + + @Test public void testSparseParsing() { assertEquals(Tensor.Builder.of(TensorType.fromSpec("tensor()")).build(), Tensor.from("{}")); @@ -18,7 +23,7 @@ public class TensorParserTestCase { Tensor.from("{{x:l0}:1.0}")); assertEquals("If the type is specified, a dense tensor can be created from the sparse text form", Tensor.Builder.of(TensorType.fromSpec("tensor(x[1])")).cell(1.0, 0).build(), - Tensor.from("tensor(x[1]):{{x:0}:1.0}")); + Tensor.from("tensor(x[1]):{{x:0 }:1.0}")); assertEquals(Tensor.Builder.of(TensorType.fromSpec("tensor(x{})")).cell().label("x", "..\",]}:..").value(1.0).build(), Tensor.from("{{x:'..\",]}:..'}:1.0}")); assertEquals(Tensor.Builder.of(TensorType.fromSpec("tensor(x{})")).cell().label("x", "..'..").value(1.0).build(), |