فهرست منبع

goprotobuf: Various changes for working with weekly.2011-12-06.
- strconv API changes
- rune tweak

R=r
CC=golang-dev
http://codereview.appspot.com/5463043

David Symonds 14 سال پیش
والد
کامیت
93be46f91d
3فایلهای تغییر یافته به همراه16 افزوده شده و 22 حذف شده
  1. 3 3
      compiler/generator/generator.go
  2. 8 8
      proto/lib.go
  3. 5 11
      proto/text_parser.go

+ 3 - 3
compiler/generator/generator.go

@@ -1402,12 +1402,12 @@ func isRepeated(field *descriptor.FieldDescriptorProto) bool {
 // which can be dotted in the input .proto file.  It maps dots to underscores.
 // Because we also get here from package names generated from file names, it also maps
 // minus signs to underscores.
-func DotToUnderscore(rune int) int {
-	switch rune {
+func DotToUnderscore(r rune) rune {
+	switch r {
 	case '.', '-':
 		return '_'
 	}
-	return rune
+	return r
 }
 
 // BaseName returns the last path element of the name, with the last dotted suffix removed.

+ 8 - 8
proto/lib.go

@@ -709,35 +709,35 @@ func buildDefaultMessage(t reflect.Type) (dm defaultMessage) {
 		// a scalar field: either *T or []byte
 		switch ft.Elem().Kind() {
 		case reflect.Bool:
-			x, err := strconv.Atob(prop.Default)
+			x, err := strconv.ParseBool(prop.Default)
 			if err != nil {
 				log.Printf("proto: bad default bool %q: %v", prop.Default, err)
 				continue
 			}
 			sf.value = x
 		case reflect.Float32:
-			x, err := strconv.Atof32(prop.Default)
+			x, err := strconv.ParseFloat(prop.Default, 32)
 			if err != nil {
 				log.Printf("proto: bad default float32 %q: %v", prop.Default, err)
 				continue
 			}
-			sf.value = x
+			sf.value = float32(x)
 		case reflect.Float64:
-			x, err := strconv.Atof64(prop.Default)
+			x, err := strconv.ParseFloat(prop.Default, 64)
 			if err != nil {
 				log.Printf("proto: bad default float64 %q: %v", prop.Default, err)
 				continue
 			}
 			sf.value = x
 		case reflect.Int32:
-			x, err := strconv.Atoi64(prop.Default)
+			x, err := strconv.ParseInt(prop.Default, 10, 32)
 			if err != nil {
 				log.Printf("proto: bad default int32 %q: %v", prop.Default, err)
 				continue
 			}
 			sf.value = int32(x)
 		case reflect.Int64:
-			x, err := strconv.Atoi64(prop.Default)
+			x, err := strconv.ParseInt(prop.Default, 10, 64)
 			if err != nil {
 				log.Printf("proto: bad default int64 %q: %v", prop.Default, err)
 				continue
@@ -749,14 +749,14 @@ func buildDefaultMessage(t reflect.Type) (dm defaultMessage) {
 			// []byte (not *uint8)
 			sf.value = []byte(prop.Default)
 		case reflect.Uint32:
-			x, err := strconv.Atoui64(prop.Default)
+			x, err := strconv.ParseUint(prop.Default, 10, 32)
 			if err != nil {
 				log.Printf("proto: bad default uint32 %q: %v", prop.Default, err)
 				continue
 			}
 			sf.value = uint32(x)
 		case reflect.Uint64:
-			x, err := strconv.Atoui64(prop.Default)
+			x, err := strconv.ParseUint(prop.Default, 10, 64)
 			if err != nil {
 				log.Printf("proto: bad default uint64 %q: %v", prop.Default, err)
 				continue

+ 5 - 11
proto/text_parser.go

@@ -383,12 +383,6 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) *ParseError
 	return nil
 }
 
-const (
-	minInt32  = -1 << 31
-	maxInt32  = 1<<31 - 1
-	maxUint32 = 1<<32 - 1
-)
-
 func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 	tok := p.next()
 	if tok.err != nil {
@@ -436,12 +430,12 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 			return nil
 		}
 	case reflect.Float32, reflect.Float64:
-		if f, err := strconv.AtofN(tok.value, fv.Type().Bits()); err == nil {
+		if f, err := strconv.ParseFloat(tok.value, fv.Type().Bits()); err == nil {
 			fv.SetFloat(f)
 			return nil
 		}
 	case reflect.Int32:
-		if x, err := strconv.Atoi64(tok.value); err == nil && minInt32 <= x && x <= maxInt32 {
+		if x, err := strconv.ParseInt(tok.value, 10, 32); err == nil {
 			fv.SetInt(x)
 			return nil
 		}
@@ -459,7 +453,7 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 		fv.SetInt(int64(x))
 		return nil
 	case reflect.Int64:
-		if x, err := strconv.Atoi64(tok.value); err == nil {
+		if x, err := strconv.ParseInt(tok.value, 10, 64); err == nil {
 			fv.SetInt(x)
 			return nil
 		}
@@ -485,12 +479,12 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 		}
 		return p.readStruct(fv, terminator)
 	case reflect.Uint32:
-		if x, err := strconv.Atoui64(tok.value); err == nil && x <= maxUint32 {
+		if x, err := strconv.ParseUint(tok.value, 10, 32); err == nil {
 			fv.SetUint(uint64(x))
 			return nil
 		}
 	case reflect.Uint64:
-		if x, err := strconv.Atoui64(tok.value); err == nil {
+		if x, err := strconv.ParseUint(tok.value, 10, 64); err == nil {
 			fv.SetUint(x)
 			return nil
 		}