Skip to content

Commit

Permalink
Ensured whitespace stripping doesn't affected quoted characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Knetic committed Dec 28, 2014
1 parent 500511d commit 8174bc3
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions parsing_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package govaluate

import (
"strings"
"bytes"
"unicode"
"unicode/utf8"
"time"
"testing"
)
Expand Down Expand Up @@ -461,10 +463,11 @@ func combineWhitespaceExpressions(testCases []TokenParsingTest) []TokenParsingTe
for i := 0; i < caseLength; i++ {

currentCase = testCases[i];

strippedCase = TokenParsingTest {

Name: (currentCase.Name + " (without whitespace)"),
Input: strings.Replace(currentCase.Input, " ", "", -1),
Input: stripUnquotedWhitespace(currentCase.Input),
Expected: currentCase.Expected,
}

Expand All @@ -474,6 +477,30 @@ func combineWhitespaceExpressions(testCases []TokenParsingTest) []TokenParsingTe
return testCases;
}

func stripUnquotedWhitespace(expression string) string {

var expressionBuffer bytes.Buffer;
var character rune;
var quoted bool;

for i := 0; i < len(expression); i++ {

character, _ = utf8.DecodeRuneInString(expression[i:]);

if(!quoted && unicode.IsSpace(character)) {
continue;
}

if(character == '\'') {
quoted = !quoted;
}

expressionBuffer.WriteString(string(character));
}

return expressionBuffer.String();
}

func runTokenParsingTest(tokenParsingTests []TokenParsingTest, test *testing.T) {

var expression *EvaluableExpression
Expand Down

0 comments on commit 8174bc3

Please sign in to comment.