Explorar el Código

Support 0o octals per 1.2 spec.

Old octals spelled as 0777 will still be decoded by default in v3.
These may be dropped in v4 depending on how usage evolves.
Gustavo Niemeyer hace 7 años
padre
commit
0d383ff995
Se han modificado 2 ficheros con 36 adiciones y 0 borrados
  1. 9 0
      decode_test.go
  2. 27 0
      resolve.go

+ 9 - 0
decode_test.go

@@ -126,6 +126,15 @@ var unmarshalTests = []struct {
 	}, {
 	}, {
 		"octal: 02472256",
 		"octal: 02472256",
 		map[string]interface{}{"octal": 685230},
 		map[string]interface{}{"octal": 685230},
+	}, {
+		"octal: -02472256",
+		map[string]interface{}{"octal": -685230},
+	}, {
+		"octal: 0o2472256",
+		map[string]interface{}{"octal": 685230},
+	}, {
+		"octal: -0o2472256",
+		map[string]interface{}{"octal": -685230},
 	}, {
 	}, {
 		"hexa: 0x_0A_74_AE",
 		"hexa: 0x_0A_74_AE",
 		map[string]interface{}{"hexa": 685230},
 		map[string]interface{}{"hexa": 685230},

+ 27 - 0
resolve.go

@@ -215,6 +215,33 @@ func resolve(tag string, in string) (rtag string, out interface{}) {
 					}
 					}
 				}
 				}
 			}
 			}
+			// Octals as introduced in version 1.2 of the spec.
+			// Octals from the 1.1 spec, spelled as 0777, are still
+			// decoded by default in v3 as well for compatibility.
+			// May be dropped in v4 depending on how usage evolves.
+			if strings.HasPrefix(plain, "0o") {
+				intv, err := strconv.ParseInt(plain[2:], 8, 64)
+				if err == nil {
+					if intv == int64(int(intv)) {
+						return intTag, int(intv)
+					} else {
+						return intTag, intv
+					}
+				}
+				uintv, err := strconv.ParseUint(plain[2:], 8, 64)
+				if err == nil {
+					return intTag, uintv
+				}
+			} else if strings.HasPrefix(plain, "-0o") {
+				intv, err := strconv.ParseInt("-" + plain[3:], 8, 64)
+				if err == nil {
+					if true || intv == int64(int(intv)) {
+						return intTag, int(intv)
+					} else {
+						return intTag, intv
+					}
+				}
+			}
 		default:
 		default:
 			panic("internal error: missing handler for resolver table: " + string(rune(hint)) + " (with " + in + ")")
 			panic("internal error: missing handler for resolver table: " + string(rune(hint)) + " (with " + in + ")")
 		}
 		}