Selaa lähdekoodia

goprotobuf: Accept (and ignore) "f" suffix for text format floating point literals.

R=r
CC=golang-dev
http://codereview.appspot.com/6347047
David Symonds 13 vuotta sitten
vanhempi
commit
6bd081ecb6
2 muutettua tiedostoa jossa 19 lisäystä ja 1 poistoa
  1. 6 1
      proto/text_parser.go
  2. 13 0
      proto/text_parser_test.go

+ 6 - 1
proto/text_parser.go

@@ -480,7 +480,12 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 			return nil
 		}
 	case reflect.Float32, reflect.Float64:
-		if f, err := strconv.ParseFloat(tok.value, fv.Type().Bits()); err == nil {
+		v := tok.value
+		if strings.HasSuffix(v, "f") {
+			// Ignore 'f' for compatibility with output generated by C++.
+			v = v[:len(v)-1]
+		}
+		if f, err := strconv.ParseFloat(v, fv.Type().Bits()); err == nil {
 			fv.SetFloat(f)
 			return nil
 		}

+ 13 - 0
proto/text_parser_test.go

@@ -154,6 +154,19 @@ var unMarshalTextTests = []UnmarshalTextTest{
 		},
 	},
 
+	// Floating point number with "f" suffix
+	{
+		in: "count: 4 others:< weight: 17.0f >",
+		out: &MyMessage{
+			Count: Int32(4),
+			Others: []*OtherMessage{
+				{
+					Weight: Float32(17),
+				},
+			},
+		},
+	},
+
 	// Number too large for float32
 	{
 		in:  "others:< weight: 12345678901234567890123456789012345678901234567890 >",