Skip to content

Commit

Permalink
Full support for numbers parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Nov 2, 2013
1 parent 699bf87 commit 2f9b8da
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
38 changes: 35 additions & 3 deletions src/SVGPathDataParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,25 @@ function SVGPathDataParser() {
continue;
}
}
// Reading the exponent sign
if(this.state&SVGPathDataParser.STATE_NUMBER_EXPSIGN) {
this.state ^= SVGPathDataParser.STATE_NUMBER_EXPSIGN;
this.state |= SVGPathDataParser.STATE_NUMBER_DIGITS;
if(-1 !== SIGNS.indexOf(str[i])) {
this.curNumber += str[i];
continue;
}
}
// Reading digits
if(this.state&SVGPathDataParser.STATE_NUMBER_DIGITS) {
if(-1 !== DIGITS.indexOf(str[i])) {
this.curNumber += str[i];
continue;
}
this.state ^= SVGPathDataParser.STATE_NUMBER_DIGITS;
}
// Ended reading left side digits
if(this.state&SVGPathDataParser.STATE_NUMBER_DIGITS || i>=j-1) {
if(this.state&SVGPathDataParser.STATE_NUMBER_INT || i>=j-1) {
this.state ^= SVGPathDataParser.STATE_NUMBER_INT;
// if got a point, reading right side digits
if(-1 !== DECPOINT.indexOf(str[i])) {
Expand All @@ -56,6 +66,27 @@ function SVGPathDataParser() {
// else we're done with that number
this.state ^= SVGPathDataParser.STATE_NUMBER;
}
// Ended reading decimal digits
if(this.state&SVGPathDataParser.STATE_NUMBER_FLOAT) {
this.state ^= SVGPathDataParser.STATE_NUMBER_FLOAT;
// if got e/E, reading the exponent
if(-1 !== EXPONENTS.indexOf(str[i])) {
this.curNumber += str[i];
this.state |= SVGPathDataParser.STATE_NUMBER_EXP |
SVGPathDataParser.STATE_NUMBER_EXPSIGN;
continue;
}
// else we're done with that number
this.state ^= SVGPathDataParser.STATE_NUMBER;
}
// Ended reading exponent digits
if(this.state&SVGPathDataParser.STATE_NUMBER_EXP) {
this.state ^= SVGPathDataParser.STATE_NUMBER_EXP;
// we're done with that number
this.state ^= SVGPathDataParser.STATE_NUMBER |
SVGPathDataParser.STATE_NUMBER_EXP;
}

}
// Coordinates parsing

Expand All @@ -69,8 +100,9 @@ SVGPathDataParser.STATE_NONE = 0;
SVGPathDataParser.STATE_NUMBER = 1;
SVGPathDataParser.STATE_NUMBER_DIGITS = 2;
SVGPathDataParser.STATE_NUMBER_INT = 4;
SVGPathDataParser.STATE_NUMBER_EXP = 8;
SVGPathDataParser.STATE_NUMBER_FLOAT = 16;
SVGPathDataParser.STATE_NUMBER_FLOAT = 8;
SVGPathDataParser.STATE_NUMBER_EXP = 16;
SVGPathDataParser.STATE_NUMBER_EXPSIGN = 32;
SVGPathDataParser.STATE_NUMBER_MASK = SVGPathDataParser.STATE_NUMBER |
SVGPathDataParser.STATE_NUMBER_DIGITS | SVGPathDataParser.STATE_NUMBER_INT |
SVGPathDataParser.STATE_NUMBER_EXP | SVGPathDataParser.STATE_NUMBER_FLOAT;
Expand Down
48 changes: 45 additions & 3 deletions test/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,51 @@ describe("Parsing numbers", function() {
SVGPathDataParser.STATE_NUMBER);
});

it("should work with a explicitly positive integer", function() {
parser.parse('+1254664');
assert.equal(parser.curNumber, '+1254664');
it("should work with a negative integer", function() {
parser.parse('-1254664');
assert.equal(parser.curNumber, '-1254664');
assert.equal(parser.state&SVGPathDataParser.STATE_NUMBER,
SVGPathDataParser.STATE_NUMBER);
});

it("should work with a float with left side digits", function() {
parser.parse('123.456');
assert.equal(parser.curNumber, '123.456');
assert.equal(parser.state&SVGPathDataParser.STATE_NUMBER,
SVGPathDataParser.STATE_NUMBER);
});

it("should work with a float without left side digits", function() {
parser.parse('.456');
assert.equal(parser.curNumber, '.456');
assert.equal(parser.state&SVGPathDataParser.STATE_NUMBER,
SVGPathDataParser.STATE_NUMBER);
});

it("should work with a float without right side digits", function() {
parser.parse('123.');
assert.equal(parser.curNumber, '123.');
assert.equal(parser.state&SVGPathDataParser.STATE_NUMBER,
SVGPathDataParser.STATE_NUMBER);
});

it("should work with a number with a positive exponent", function() {
parser.parse('123.456e125');
assert.equal(parser.curNumber, '123.456e125');
assert.equal(parser.state&SVGPathDataParser.STATE_NUMBER,
SVGPathDataParser.STATE_NUMBER);
});

it("should work with a number with an explicitly positive exponent", function() {
parser.parse('123.456e+125');
assert.equal(parser.curNumber, '123.456e+125');
assert.equal(parser.state&SVGPathDataParser.STATE_NUMBER,
SVGPathDataParser.STATE_NUMBER);
});

it("should work with a number with a negative exponent", function() {
parser.parse('123.456e-125');
assert.equal(parser.curNumber, '123.456e-125');
assert.equal(parser.state&SVGPathDataParser.STATE_NUMBER,
SVGPathDataParser.STATE_NUMBER);
});
Expand Down

0 comments on commit 2f9b8da

Please sign in to comment.