@@ -31,6 +31,7 @@ import (
31
31
"os"
32
32
"path"
33
33
"path/filepath"
34
+ "regexp"
34
35
"strings"
35
36
"sync"
36
37
"unicode"
@@ -83,25 +84,21 @@ func CLDRVersion() string {
83
84
}
84
85
85
86
var tags = []struct { version , buildTags string }{
86
- {"10.0.0" , "go1.10" },
87
- {"" , "!go1.10" },
87
+ {"9.0.0" , "!go1.10" },
88
+ {"10.0.0" , "go1.10, !go1.13" },
89
+ {"11.0.0" , "go1.13" },
88
90
}
89
91
90
92
// buildTags reports the build tags used for the current Unicode version.
91
93
func buildTags () string {
92
94
v := UnicodeVersion ()
93
- for _ , x := range tags {
94
- // We should do a numeric comparison, but including the collate package
95
- // would create an import cycle. We approximate it by assuming that
96
- // longer version strings are later.
97
- if len (x .version ) <= len (v ) {
98
- return x .buildTags
99
- }
100
- if len (x .version ) == len (v ) && x .version <= v {
101
- return x .buildTags
95
+ for _ , e := range tags {
96
+ if e .version == v {
97
+ return e .buildTags
102
98
}
103
99
}
104
- return tags [0 ].buildTags
100
+ log .Fatalf ("Unknown build tags for Unicode version %q." , v )
101
+ return ""
105
102
}
106
103
107
104
// IsLocal reports whether data files are available locally.
@@ -269,29 +266,46 @@ func WriteGoFile(filename, pkg string, b []byte) {
269
266
}
270
267
}
271
268
272
- func insertVersion (filename , version string ) string {
269
+ func fileToPattern (filename string ) string {
273
270
suffix := ".go"
274
271
if strings .HasSuffix (filename , "_test.go" ) {
275
272
suffix = "_test.go"
276
273
}
277
- return fmt .Sprint (filename [:len (filename )- len (suffix )], version , suffix )
274
+ prefix := filename [:len (filename )- len (suffix )]
275
+ return fmt .Sprint (prefix , "%s" , suffix )
276
+ }
277
+
278
+ func updateBuildTags (pattern string ) {
279
+ for _ , t := range tags {
280
+ oldFile := fmt .Sprintf (pattern , t .version )
281
+ b , err := ioutil .ReadFile (oldFile )
282
+ if err != nil {
283
+ continue
284
+ }
285
+ build := fmt .Sprintf ("// +build %s" , t .buildTags )
286
+ b = regexp .MustCompile (`// \+build .*` ).ReplaceAll (b , []byte (build ))
287
+ err = ioutil .WriteFile (oldFile , b , 0644 )
288
+ if err != nil {
289
+ log .Fatal (err )
290
+ }
291
+ }
278
292
}
279
293
280
294
// WriteVersionedGoFile prepends a standard file comment, adds build tags to
281
295
// version the file for the current Unicode version, and package statement to
282
296
// the given bytes, applies gofmt, and writes them to a file with the given
283
297
// name. It will call log.Fatal if there are any errors.
284
298
func WriteVersionedGoFile (filename , pkg string , b []byte ) {
285
- tags := buildTags ( )
286
- if tags != "" {
287
- filename = insertVersion ( filename , UnicodeVersion ())
288
- }
299
+ pattern := fileToPattern ( filename )
300
+ updateBuildTags ( pattern )
301
+ filename = fmt . Sprintf ( pattern , UnicodeVersion ())
302
+
289
303
w , err := os .Create (filename )
290
304
if err != nil {
291
305
log .Fatalf ("Could not create file %s: %v" , filename , err )
292
306
}
293
307
defer w .Close ()
294
- if _ , err = WriteGo (w , pkg , tags , b ); err != nil {
308
+ if _ , err = WriteGo (w , pkg , buildTags () , b ); err != nil {
295
309
log .Fatalf ("Error writing file %s: %v" , filename , err )
296
310
}
297
311
}
0 commit comments