Commits

Ross Light committed ad1c538

Add hex escapes in double-quoted scalars

  • Participants
  • Parent commits 8a1b500

Comments (0)

Files changed (1)

 	"fmt"
 	"io"
 	"os"
+	"strconv"
 	"strings"
 )
 
 func (s *scanner) escapeChar() int {
 	s.match("\\")
 
-	r := s.next()
-	switch r {
+	switch r := s.next(); r {
 	case '0':
 		return 0
 	case 'a':
 		// Unicode paragraph separator
 		return 0x2029
 	case 'x':
-		// TODO
+		return s.hexEscape(2)
 	case 'u':
-		// TODO
+		return s.hexEscape(4)
 	case 'U':
-		// TODO
+		return s.hexEscape(8)
 	}
 
 	s.backup()
-	s.errorf("Expected escape, got '\\%c'", r)
+	s.expected("escape")
 	return -1
 }
 
+func (s *scanner) hexEscape(n int) int {
+	initIndex := s.index
+	for i := 0; i < n; i++ {
+		// TODO: give a better error message
+		s.match(hexDigits)
+	}
+	hex := string(s.buf[initIndex:s.index])
+
+	ui, err := strconv.Btoui64(hex, 16)
+	if err != nil {
+		s.error(err)
+	}
+	return int(ui)
+}
+
 func (s *scanner) plain(indent, context int) {
 	// ns-plain-first
 	firstRune := s.peek()