本文整理匯總了Golang中code/google/com/p/go/text/transform.NewReader函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewReader函數的具體用法?Golang NewReader怎麽用?Golang NewReader使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewReader函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: parseSgf
//在命令行執行
func parseSgf(file string) {
b, err := ioutil.ReadFile(file)
rInNop := transform.NewReader(bytes.NewReader(b), encoding.Nop.NewDecoder())
b, _ = ioutil.ReadAll(rInNop)
NopB := string(b)
b1 := tryFixSgf(NopB)
c, err := sgf.ParseSgf(b1)
if err != nil {
log.Println("ERR")
}
node := c.GameTrees[0].Nodes[0]
f := func(pName string) string {
for _, prop := range node.Properties {
if pName == prop.Ident {
return strings.Join(prop.Values, ",")
}
}
return ""
}
log.Println("dt ", f("DT"))
log.Println("HA ", f("HA"))
log.Println("BR ", f("BR"))
log.Println("WR ", f("WR"))
log.Println("PB ", f("PB"))
log.Println("PW ", f("PW"))
log.Println("RE ", f("RE"))
log.Println("KM ", f("KM"))
log.Println("====================")
}
示例2: eucjp_to_utf8
func eucjp_to_utf8(str string) (string, error) {
ret, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(str), japanese.EUCJP.NewDecoder()))
if err != nil {
return "", err
}
return string(ret), err
}
示例3: TestNonRepertoire
// TestNonRepertoire tests that codes outside of an Encoding's repertoire are
// converted:
// - to the Unicode replacement character '\ufffd' when decoding to UTF-8,
// - to the ASCII substitute character '\x1a' when encoding from UTF-8.
func TestNonRepertoire(t *testing.T) {
testCases := []struct {
e encoding.Encoding
dSrc, eSrc string
}{
{charmap.Windows1252, "\x81", "갂"},
{japanese.EUCJP, "\xfe\xfc", "갂"},
{japanese.ISO2022JP, "\x1b$B\x7e\x7e", "갂"},
{japanese.ShiftJIS, "\xef\xfc", "갂"},
{korean.EUCKR, "\xfe\xfe", "א"},
{simplifiedchinese.GBK, "\xfe\xfe", "갂"},
{simplifiedchinese.HZGB2312, "~{z~", "갂"},
{traditionalchinese.Big5, "\x81\x40", "갂"},
}
for _, tc := range testCases {
for _, direction := range []string{"Decode", "Encode"} {
enc, want, src := (transform.Transformer)(nil), "", ""
if direction == "Decode" {
enc, want, src = tc.e.NewDecoder(), "\ufffd", tc.dSrc
} else {
enc, want, src = tc.e.NewEncoder(), "\x1a", tc.eSrc
}
dst, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(src), enc))
if err != nil {
t.Errorf("%s %v: %v", direction, tc.e, err)
continue
}
if got := string(dst); got != want {
t.Errorf("%s %v:\ngot %q\nwant %q", direction, tc.e, got, want)
continue
}
}
}
}
示例4: TransUTF8FromCode
func TransUTF8FromCode(src []byte, code int) ([]byte, error) {
var rInUTF8 io.Reader
switch code {
case GBK:
rInUTF8 = transform.NewReader(bytes.NewReader(src), simplifiedchinese.GBK.NewDecoder())
break
case GB2312:
rInUTF8 = transform.NewReader(bytes.NewReader(src), simplifiedchinese.HZGB2312.NewDecoder())
break
default:
return nil, NOT_SUPPORTED_CODE
}
out, _ := ioutil.ReadAll(rInUTF8)
return out, nil
}
示例5: Decode
func Decode(s []byte) ([]byte, error) {
f := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
d, e := ioutil.ReadAll(f)
if e != nil {
return nil, e
}
return d, nil
}
示例6: TestBasics
func TestBasics(t *testing.T) {
for _, tc := range basicTestCases {
for _, direction := range []string{"Decode", "Encode"} {
newTransformer, want, src := (func() transform.Transformer)(nil), "", ""
wPrefix, sPrefix := "", ""
if direction == "Decode" {
newTransformer, want, src = tc.e.NewDecoder, tc.utf8, tc.encoded
wPrefix, sPrefix = "", tc.encPrefix
} else {
newTransformer, want, src = tc.e.NewEncoder, tc.encoded, tc.utf8
wPrefix, sPrefix = tc.encPrefix, ""
}
dst := make([]byte, len(wPrefix)+len(want))
nDst, nSrc, err := newTransformer().Transform(dst, []byte(sPrefix+src), true)
if err != nil {
t.Errorf("%v: %s: %v", tc.e, direction, err)
continue
}
if nDst != len(wPrefix)+len(want) {
t.Errorf("%v: %s: nDst got %d, want %d",
tc.e, direction, nDst, len(wPrefix)+len(want))
continue
}
if nSrc != len(sPrefix)+len(src) {
t.Errorf("%v: %s: nSrc got %d, want %d",
tc.e, direction, nSrc, len(sPrefix)+len(src))
continue
}
if got := string(dst); got != wPrefix+want {
t.Errorf("%v: %s:\ngot %q\nwant %q",
tc.e, direction, got, wPrefix+want)
continue
}
for _, n := range []int{0, 1, 2, 10, 123, 4567} {
input := sPrefix + strings.Repeat(src, n)
sr := strings.NewReader(input)
g, err := ioutil.ReadAll(transform.NewReader(sr, newTransformer()))
if err != nil {
t.Errorf("%v: %s: ReadAll: n=%d: %v", tc.e, direction, n, err)
continue
}
if len(g) == 0 && len(input) == 0 {
// If the input is empty then the output can be empty,
// regardless of whatever wPrefix is.
continue
}
got1, want1 := string(g), wPrefix+strings.Repeat(want, n)
if got1 != want1 {
t.Errorf("%v: %s: ReadAll: n=%d\ngot %q\nwant %q",
tc.e, direction, n, trim(got1), trim(want1))
continue
}
}
}
}
}
示例7: BigDecodeUTF8
func BigDecodeUTF8(s []byte) ([]byte, error) {
I := bytes.NewReader(s)
O := transform.NewReader(I, traditionalchinese.Big5.NewDecoder())
d, e := ioutil.ReadAll(O)
if e != nil {
return nil, e
}
return d, nil
}
示例8: transfer
func transfer(b []byte) string {
rInUTF8 := transform.NewReader(bytes.NewReader(b), simplifiedchinese.GBK.NewDecoder())
gbk, err := ioutil.ReadAll(rInUTF8)
if err != nil {
log.Println("[wanring]", err)
return ""
}
return string(gbk)
}
示例9: utf8_to_sjis
func utf8_to_sjis(str string) string {
iostr := strings.NewReader(str)
rio := transform.NewReader(iostr, japanese.ShiftJIS.NewEncoder())
ret, err := ioutil.ReadAll(rio)
if err != nil {
return ""
}
return string(ret)
}
示例10: conUtf
func conUtf(s string) string {
sr := strings.NewReader(s)
tr := transform.NewReader(sr, charmap.Windows1251.NewDecoder())
buf, err := ioutil.ReadAll(tr)
if err != err {
// обработка ошибки
}
return string(buf)
}
示例11: benchmark
func benchmark(b *testing.B, direction string, enc encoding.Encoding) {
_, src, newTransformer, err := load(direction, enc)
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(src)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := transform.NewReader(bytes.NewReader(src), newTransformer())
io.Copy(ioutil.Discard, r)
}
}
示例12: utf8DocByEucjpUrl
func utf8DocByEucjpUrl(eucjpUrl string) *goquery.Document {
resp, err := http.Get(eucjpUrl)
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
}
doc, err := goquery.NewDocumentFromReader(transform.NewReader(resp.Body, japanese.EUCJP.NewDecoder()))
if err != nil {
log.Fatal("Fail to ", err.Error(), ", for: ", eucjpUrl)
}
return doc
}
示例13: TestBig5CircumflexAndMacron
// TestBig5CircumflexAndMacron tests the special cases listed in
// http://encoding.spec.whatwg.org/#big5
// Note that these special cases aren't preserved by round-tripping through
// decoding and encoding (since
// http://encoding.spec.whatwg.org/index-big5.txt does not have an entry for
// U+0304 or U+030C), so we can't test this in TestBasics.
func TestBig5CircumflexAndMacron(t *testing.T) {
src := "\x88\x5f\x88\x60\x88\x61\x88\x62\x88\x63\x88\x64\x88\x65\x88\x66 " +
"\x88\xa2\x88\xa3\x88\xa4\x88\xa5\x88\xa6"
want := "ÓǑÒ\u00ca\u0304Ế\u00ca\u030cỀÊ " +
"ü\u00ea\u0304ế\u00ea\u030cề"
dst, err := ioutil.ReadAll(transform.NewReader(
strings.NewReader(src), traditionalchinese.Big5.NewDecoder()))
if err != nil {
t.Fatal(err)
}
if got := string(dst); got != want {
t.Fatalf("\ngot %q\nwant %q", got, want)
}
}
示例14: main
//在命令行執行
func main() {
// userFile := "/Users/lihexing/Downloads/002/i.sgf"
// userFile := "/Users/lihexing/Downloads/002/m.sgf"
// userFile := "/Users/lihexing/Documents/kifu/ys.sgf"
// userFile := "/Users/lihexing/Documents/kifu/pa.sgf"
// userFile := "/Users/lihexing/Documents/kifu/cl.sgf"
// userFile := "/Users/lihexing/Documents/kifu/wt.sgf"
// userFile := "/Users/lihexing/Documents/12月1日處理/棋譜處理/已上傳/LG杯/11/第11屆LG杯32強賽_2006-05-15_樸永訓_古力_62839.sgf"
// userFile := "/Users/lihexing/Documents/12月1日處理/棋譜處理/未上傳/分好類待上傳/中國晚報杯/2003成都晚報隊選拔賽_2003-12-13_沈逢春_龍霖_61565.sgf"
// userFile := "/Users/lihexing/Documents/棋譜待解決問題/2/日本十段戰/第51期日本十段戰挑戰賽五番棋第五局_2013-04-26_井山裕太_結城聰_107576.sgf"
// userFile := "/Users/lihexing/Documents/棋譜待解決問題/2/日本十段戰/第8期十段戰_1969-12-18_工藤紀夫_武宮正樹_101715.sgf"
// userFile := "/Users/lihexing/Documents/棋譜待解決問題/2/應氏杯/第二屆應氏杯第一輪_1992-07-13_王銘琬_石井邦生_51138.sgf"
// userFile := "/Users/lihexing/Documents/kifu/第9屆理光杯第一輪_2008-12-22_彭荃_廖行文_48857.sgf"
// userFile := "/Users/lihexing/Downloads/第9屆理光杯第一輪_2008-12-22_彭荃_廖行文_48857.sgf"
// userFile := "/Users/lihexing/Desktop/第24屆富士通杯決賽(周睿羊講解)_2011-08-14_樸廷桓_邱峻_73705.sgf"
// userFile := "/Users/lihexing/Downloads/第24屆富士通杯決賽(周睿羊講解)-73705.sgf"
// userFile := "/Users/lihexing/Desktop/第3期名人戰三次預選_1978-11-30_山部俊郎_武宮正樹_61320.sgf"
// userFile := "/Users/lihexing/Downloads/第3期名人戰三次預選-61320.sgf"
// userFile := "/Users/lihexing/Desktop/DT_PB_PW_BR_WR_RE_第2屆北林弘通杯圍棋爭霸賽8強賽_2013-12-08_別中華_楊宇軒_117622.sgf"
// userFile := "/Users/lihexing/Desktop/第42期本因坊戰循環賽(日本每日新聞主辦)_1987-03-05_山城宏_林海峰_50781.sgf"//給曉光處理手數
// userFile := "/Users/lihexing/Downloads/2012首屆北林弘通杯圍棋爭霸賽3、4名決定戰-92553.sgf" //弘通官網下載
userFile := "/Users/lihexing/Desktop/2012首屆北林弘通杯圍棋爭霸賽3、4名決定戰_2012-10-14_陳其洲_李應碩_92553.sgf" //爬蟲下載
file, err := os.Open(userFile)
defer file.Close()
if err != nil {
fmt.Println(userFile, err)
return
}
b, err := ioutil.ReadAll(file)
rInNop := transform.NewReader(bytes.NewReader(b), encoding.Nop.NewDecoder())
b, _ = ioutil.ReadAll(rInNop)
NopB := string(b)
// log.Println("string(b) ", NopB)
parseKifu(NopB)
// rInGBK := transform.NewReader(bytes.NewReader(b), simplifiedchinese.GBK.NewDecoder())
// b, _ = ioutil.ReadAll(rInGBK)
// GbkB := string(b)
//// log.Println("rInGBK ", GbkB)
// parseKifu(GbkB)
}
示例15: read
func read(userFile string) string {
file, err := os.Open(userFile)
defer file.Close()
if err != nil {
fmt.Println(userFile, err)
return "fileOpenERR"
}
b, err := ioutil.ReadAll(file)
rInNop := transform.NewReader(bytes.NewReader(b), encoding.Nop.NewDecoder())
b, _ = ioutil.ReadAll(rInNop)
NoOK := string(b)
return parseKifu(NoOK)
}