Przeglądaj źródła

goprotobuf: Support hexadecimal and octal integers in text format.

R=r
CC=golang-dev
http://codereview.appspot.com/6301088
David Symonds 13 lat temu
rodzic
commit
32612dd8e5
2 zmienionych plików z 20 dodań i 4 usunięć
  1. 4 4
      proto/text_parser.go
  2. 16 0
      proto/text_parser_test.go

+ 4 - 4
proto/text_parser.go

@@ -462,7 +462,7 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 			return nil
 		}
 	case reflect.Int32:
-		if x, err := strconv.ParseInt(tok.value, 10, 32); err == nil {
+		if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil {
 			fv.SetInt(x)
 			return nil
 		}
@@ -480,7 +480,7 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 		fv.SetInt(int64(x))
 		return nil
 	case reflect.Int64:
-		if x, err := strconv.ParseInt(tok.value, 10, 64); err == nil {
+		if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil {
 			fv.SetInt(x)
 			return nil
 		}
@@ -506,12 +506,12 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 		}
 		return p.readStruct(fv, terminator)
 	case reflect.Uint32:
-		if x, err := strconv.ParseUint(tok.value, 10, 32); err == nil {
+		if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil {
 			fv.SetUint(uint64(x))
 			return nil
 		}
 	case reflect.Uint64:
-		if x, err := strconv.ParseUint(tok.value, 10, 64); err == nil {
+		if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil {
 			fv.SetUint(x)
 			return nil
 		}

+ 16 - 0
proto/text_parser_test.go

@@ -129,6 +129,22 @@ var unMarshalTextTests = []UnmarshalTextTest{
 		err: "line 1.7: invalid int32: 1234567890123",
 	},
 
+	// Number in hexadecimal
+	{
+		in: "count: 0x2beef",
+		out: &MyMessage{
+			Count: Int32(0x2beef),
+		},
+	},
+
+	// Number in octal
+	{
+		in: "count: 024601",
+		out: &MyMessage{
+			Count: Int32(024601),
+		},
+	},
+
 	// Number too large for float32
 	{
 		in:  "others:< weight: 12345678901234567890123456789012345678901234567890 >",