Updated handling of '\b' when inside character class, made invalid
escapes an error. The '\b' value refers to a word boundary normally, but refers to the backspace ASCII value inside a character class. I updated newEscapedNode() to deal with this. I also changed the behavior, so that trying to escape any other value results in an error, instead of just returning the character as-is.
This commit is contained in:
		| @@ -1,5 +1,7 @@ | ||||
| package main | ||||
|  | ||||
| import "fmt" | ||||
|  | ||||
| type NodeType int | ||||
|  | ||||
| // This is a slice containing all escapable characters that have special meaning. | ||||
| @@ -62,7 +64,7 @@ func newCharClassNode(nodes []postfixNode, negated bool) postfixNode { | ||||
| } | ||||
|  | ||||
| // Creates a new escaped node - the given character is assumed to have been preceded by a backslash | ||||
| func newEscapedNode(c rune) postfixNode { | ||||
| func newEscapedNode(c rune, inCharClass bool) (postfixNode, error) { | ||||
| 	toReturn := postfixNode{} | ||||
| 	toReturn.startReps = 1 | ||||
| 	toReturn.endReps = 1 | ||||
| @@ -86,8 +88,13 @@ func newEscapedNode(c rune) postfixNode { | ||||
| 		toReturn = newPostfixDotNode() | ||||
| 		toReturn.except = append([]postfixNode{}, newPostfixNode(wordChars...)) | ||||
| 	case 'b', 'B': | ||||
| 		toReturn.nodetype = ASSERTION | ||||
| 		toReturn.contents = append(toReturn.contents, c) | ||||
| 		if c == 'b' && inCharClass { | ||||
| 			toReturn.nodetype = CHARACTER | ||||
| 			toReturn.contents = append(toReturn.contents, rune(8)) | ||||
| 		} else { | ||||
| 			toReturn.nodetype = ASSERTION | ||||
| 			toReturn.contents = append(toReturn.contents, c) | ||||
| 		} | ||||
| 	case 'n': // Newline character | ||||
| 		toReturn.nodetype = CHARACTER | ||||
| 		toReturn.contents = append(toReturn.contents, '\n') | ||||
| @@ -110,10 +117,13 @@ func newEscapedNode(c rune) postfixNode { | ||||
| 		toReturn.nodetype = CHARACTER | ||||
| 		toReturn.contents = append(toReturn.contents, rune(11)) | ||||
| 	default: // None of the above - append it as a regular character | ||||
| 		if isNormalChar(c) { // Normal characters cannot be escaped | ||||
| 			return postfixNode{}, fmt.Errorf("Invalid escape character.") | ||||
| 		} | ||||
| 		toReturn.nodetype = CHARACTER | ||||
| 		toReturn.contents = append(toReturn.contents, c) | ||||
| 	} | ||||
| 	return toReturn | ||||
| 	return toReturn, nil | ||||
| } | ||||
|  | ||||
| // Creates and returns a postfixNode based on the given contents | ||||
|   | ||||
		Reference in New Issue
	
	Block a user