From 95d1c98155abcf847c77351976362cdab953b957 Mon Sep 17 00:00:00 2001 From: Christopher Kohnert Date: Thu, 1 Jun 2017 16:19:35 -0700 Subject: [PATCH] Better handling of whitespace at end of input stream. --- ext/import-svg.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ext/import-svg.cpp b/ext/import-svg.cpp index 6b4c012..a434861 100644 --- a/ext/import-svg.cpp +++ b/ext/import-svg.cpp @@ -17,10 +17,11 @@ namespace msdfgen { #endif -static bool readNodeType(char &output, const char *&pathDef) { +static bool readNodeType(char &output, bool &haveInput, const char *&pathDef) { int shift; char nodeType; - if (sscanf(pathDef, " %c%n", &nodeType, &shift) == 1 && nodeType != '+' && nodeType != '-' && nodeType != '.' && nodeType != ',' && (nodeType < '0' || nodeType > '9')) { + haveInput = sscanf(pathDef, " %c%n", &nodeType, &shift) == 1; + if ( haveInput && nodeType != '+' && nodeType != '-' && nodeType != '.' && nodeType != ',' && (nodeType < '0' || nodeType > '9')) { pathDef += shift; output = nodeType; return true; @@ -67,7 +68,8 @@ static void consumeOptionalComma(const char *&pathDef) { static bool buildFromPath(Shape &shape, const char *pathDef) { char nodeType; Point2 prevNode(0, 0); - while (readNodeType(nodeType, pathDef)) { + bool haveInput = pathDef && *pathDef; + while (haveInput && readNodeType(nodeType, haveInput, pathDef)) { Contour &contour = shape.addContour(); bool contourStart = true; @@ -75,7 +77,7 @@ static bool buildFromPath(Shape &shape, const char *pathDef) { Point2 controlPoint[2]; Point2 node; - while (*pathDef) { + while (haveInput) { switch (nodeType) { case 'M': case 'm': REQUIRE(contourStart); @@ -149,7 +151,7 @@ static bool buildFromPath(Shape &shape, const char *pathDef) { } contourStart &= nodeType == 'M' || nodeType == 'm'; prevNode = node; - readNodeType(nodeType, pathDef); + readNodeType(nodeType, haveInput, pathDef); } NEXT_CONTOUR:; }