-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from neilpa/unicode-encodings
UTF-16 support for JSON and option to skip BOMs
- Loading branch information
Showing
51 changed files
with
286 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// +build ignore | ||
|
||
// gen_testdata clones the utf-8 tests data to the other | ||
// unicode encodings and adds BOM variants of each. | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"golang.org/x/text/encoding" | ||
"golang.org/x/text/encoding/unicode" | ||
) | ||
|
||
func main() { | ||
var xforms = []struct { | ||
dir, bom string | ||
enc encoding.Encoding | ||
}{ | ||
{"testdata/utf-16be", "\xFE\xFF", unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)}, | ||
{"testdata/utf-16le", "\xFF\xFE", unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)}, | ||
} | ||
|
||
paths, _ := filepath.Glob("testdata/utf-8/*") | ||
for _, p := range paths { | ||
src, err := ioutil.ReadFile(p) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
write("testdata/utf-8_bom", p, "\xEF\xBB\xBF", src) | ||
for _, xform := range xforms { | ||
dst, err := xform.enc.NewEncoder().Bytes(src) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
write(xform.dir, p, "", dst) | ||
write(xform.dir+"_bom", p, xform.bom, dst) | ||
} | ||
} | ||
} | ||
|
||
func write(dir, orig, bom string, buf []byte) { | ||
f, err := os.Create(filepath.Join(dir, filepath.Base(orig))) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if _, err = f.Write([]byte(bom)); err != nil { | ||
log.Fatal(err) | ||
} | ||
if _, err = f.Write(buf); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.