Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Unified Diff: third_party/golang/src/fmt/print.go

Issue 1415513008: Local modifications of golang source: Sort the keys of a map when printing them. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/golang/README ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/golang/src/fmt/print.go
diff --git a/third_party/golang/src/fmt/print.go b/third_party/golang/src/fmt/print.go
index 8cd238e772e545b5fe5f1ee72d5d30db4afc8f8a..3f913333125dc52245ce4ebbcf2e184e78f3c828 100644
--- a/third_party/golang/src/fmt/print.go
+++ b/third_party/golang/src/fmt/print.go
@@ -9,6 +9,7 @@ import (
"io"
"os"
"reflect"
+ "sort"
"sync"
"unicode/utf8"
)
@@ -850,6 +851,26 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) (wasString bo
var byteType = reflect.TypeOf(byte(0))
+// Note(rudominer) Local change. We sort map keys when they are of numeric or string type.
+type valueComparison func(a, b reflect.Value) bool
+
+type valueSlice struct {
+ less valueComparison
+ values []reflect.Value
+}
+
+func (v valueSlice) Len() int {
+ return len(v.values)
+}
+
+func (v valueSlice) Less(i, j int) bool {
+ return v.less(v.values[i], v.values[j])
+}
+
+func (v valueSlice) Swap(i, j int) {
+ v.values[i], v.values[j] = v.values[j], v.values[i]
+}
+
// printReflectValue is the fallback for both printArg and printValue.
// It uses reflect to print the value.
func (p *pp) printReflectValue(value reflect.Value, verb rune, depth int) (wasString bool) {
@@ -891,6 +912,21 @@ BigSwitch:
p.buf.Write(mapBytes)
}
keys := f.MapKeys()
+ // Note(rudominer) Local change. We sort map keys when they are of numeric or string type.
+ var compare valueComparison = nil
+ switch f.Type().Key().Kind() {
+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+ compare = func(a, b reflect.Value) bool { return a.Int() < b.Int() }
+ case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
+ compare = func(a, b reflect.Value) bool { return a.Uint() < b.Uint() }
+ case reflect.Float32, reflect.Float64:
+ compare = func(a, b reflect.Value) bool { return a.Float() < b.Float() }
+ case reflect.String:
+ compare = func(a, b reflect.Value) bool { return a.String() < b.String() }
+ }
+ if compare != nil {
+ sort.Sort(valueSlice{compare, keys})
+ }
for i, key := range keys {
if i > 0 {
if p.fmt.sharpV {
« no previous file with comments | « third_party/golang/README ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698