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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « third_party/golang/README ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package fmt 5 package fmt
6 6
7 import ( 7 import (
8 "errors" 8 "errors"
9 "io" 9 "io"
10 "os" 10 "os"
11 "reflect" 11 "reflect"
12 "sort"
12 "sync" 13 "sync"
13 "unicode/utf8" 14 "unicode/utf8"
14 ) 15 )
15 16
16 // Some constants in the form of bytes, to avoid string overhead. 17 // Some constants in the form of bytes, to avoid string overhead.
17 // Needlessly fastidious, I suppose. 18 // Needlessly fastidious, I suppose.
18 var ( 19 var (
19 commaSpaceBytes = []byte(", ") 20 commaSpaceBytes = []byte(", ")
20 nilAngleBytes = []byte("<nil>") 21 nilAngleBytes = []byte("<nil>")
21 nilParenBytes = []byte("(nil)") 22 nilParenBytes = []byte("(nil)")
(...skipping 821 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 } 844 }
844 if handled := p.handleMethods(verb, depth); handled { 845 if handled := p.handleMethods(verb, depth); handled {
845 return false 846 return false
846 } 847 }
847 848
848 return p.printReflectValue(value, verb, depth) 849 return p.printReflectValue(value, verb, depth)
849 } 850 }
850 851
851 var byteType = reflect.TypeOf(byte(0)) 852 var byteType = reflect.TypeOf(byte(0))
852 853
854 // Note(rudominer) Local change. We sort map keys when they are of numeric or st ring type.
855 type valueComparison func(a, b reflect.Value) bool
856
857 type valueSlice struct {
858 less valueComparison
859 values []reflect.Value
860 }
861
862 func (v valueSlice) Len() int {
863 return len(v.values)
864 }
865
866 func (v valueSlice) Less(i, j int) bool {
867 return v.less(v.values[i], v.values[j])
868 }
869
870 func (v valueSlice) Swap(i, j int) {
871 v.values[i], v.values[j] = v.values[j], v.values[i]
872 }
873
853 // printReflectValue is the fallback for both printArg and printValue. 874 // printReflectValue is the fallback for both printArg and printValue.
854 // It uses reflect to print the value. 875 // It uses reflect to print the value.
855 func (p *pp) printReflectValue(value reflect.Value, verb rune, depth int) (wasSt ring bool) { 876 func (p *pp) printReflectValue(value reflect.Value, verb rune, depth int) (wasSt ring bool) {
856 oldValue := p.value 877 oldValue := p.value
857 p.value = value 878 p.value = value
858 BigSwitch: 879 BigSwitch:
859 switch f := value; f.Kind() { 880 switch f := value; f.Kind() {
860 case reflect.Invalid: 881 case reflect.Invalid:
861 p.buf.WriteString("<invalid reflect.Value>") 882 p.buf.WriteString("<invalid reflect.Value>")
862 case reflect.Bool: 883 case reflect.Bool:
(...skipping 21 matching lines...) Expand all
884 p.buf.WriteString(f.Type().String()) 905 p.buf.WriteString(f.Type().String())
885 if f.IsNil() { 906 if f.IsNil() {
886 p.buf.WriteString("(nil)") 907 p.buf.WriteString("(nil)")
887 break 908 break
888 } 909 }
889 p.buf.WriteByte('{') 910 p.buf.WriteByte('{')
890 } else { 911 } else {
891 p.buf.Write(mapBytes) 912 p.buf.Write(mapBytes)
892 } 913 }
893 keys := f.MapKeys() 914 keys := f.MapKeys()
915 // Note(rudominer) Local change. We sort map keys when they are of numeric or string type.
916 var compare valueComparison = nil
917 switch f.Type().Key().Kind() {
918 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, re flect.Int64:
919 compare = func(a, b reflect.Value) bool { return a.Int() < b.Int() }
920 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32 , reflect.Uint64:
921 compare = func(a, b reflect.Value) bool { return a.Uint( ) < b.Uint() }
922 case reflect.Float32, reflect.Float64:
923 compare = func(a, b reflect.Value) bool { return a.Float () < b.Float() }
924 case reflect.String:
925 compare = func(a, b reflect.Value) bool { return a.Strin g() < b.String() }
926 }
927 if compare != nil {
928 sort.Sort(valueSlice{compare, keys})
929 }
894 for i, key := range keys { 930 for i, key := range keys {
895 if i > 0 { 931 if i > 0 {
896 if p.fmt.sharpV { 932 if p.fmt.sharpV {
897 p.buf.Write(commaSpaceBytes) 933 p.buf.Write(commaSpaceBytes)
898 } else { 934 } else {
899 p.buf.WriteByte(' ') 935 p.buf.WriteByte(' ')
900 } 936 }
901 } 937 }
902 p.printValue(key, verb, depth+1) 938 p.printValue(key, verb, depth+1)
903 p.buf.WriteByte(':') 939 p.buf.WriteByte(':')
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 if addspace || !isString && !prevString { 1294 if addspace || !isString && !prevString {
1259 p.buf.WriteByte(' ') 1295 p.buf.WriteByte(' ')
1260 } 1296 }
1261 } 1297 }
1262 prevString = p.printArg(arg, 'v', 0) 1298 prevString = p.printArg(arg, 'v', 0)
1263 } 1299 }
1264 if addnewline { 1300 if addnewline {
1265 p.buf.WriteByte('\n') 1301 p.buf.WriteByte('\n')
1266 } 1302 }
1267 } 1303 }
OLDNEW
« 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