本文整理汇总了Golang中golang.org/x/text/transform.NewWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewWriter函数的具体用法?Golang NewWriter怎么用?Golang NewWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewWriter函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: transEnc
//「Golangで文字コード判定」qiita.com/nobuhito/items/ff782f64e32f7ed95e43
func transEnc(text string, encode string) (string, error) {
body := []byte(text)
var f []byte
encodings := []string{"sjis", "utf-8"}
if encode != "" {
encodings = append([]string{encode}, encodings...)
}
for _, enc := range encodings {
if enc != "" {
ee, _ := charset.Lookup(enc)
if ee == nil {
continue
}
var buf bytes.Buffer
ic := transform.NewWriter(&buf, ee.NewDecoder())
_, err := ic.Write(body)
if err != nil {
continue
}
err = ic.Close()
if err != nil {
continue
}
f = buf.Bytes()
break
}
}
return string(f), nil
}
开发者ID:ikawaha,项目名称:dajarep,代码行数:31,代码来源:main.go
示例2: TestReaderEncodings
func TestReaderEncodings(t *testing.T) {
for _, test := range tests {
t.Logf("test codec: %v", test.encoding)
codec, ok := findEncoding(test.encoding)
if !ok {
t.Errorf("can not find encoding '%v'", test.encoding)
continue
}
buffer := bytes.NewBuffer(nil)
// write with encoding to buffer
writer := transform.NewWriter(buffer, codec.NewEncoder())
var expectedCount []int
for _, line := range test.strings {
writer.Write([]byte(line))
writer.Write([]byte{'\n'})
expectedCount = append(expectedCount, buffer.Len())
}
// create line reader
reader, err := newLineReader(buffer, codec, 1024)
if err != nil {
t.Errorf("failed to initialize reader: %v", err)
continue
}
// read decodec lines from buffer
var readLines []string
var byteCounts []int
current := 0
for {
bytes, sz, err := reader.next()
if sz > 0 {
readLines = append(readLines, string(bytes[:len(bytes)-1]))
}
if err != nil {
break
}
current += sz
byteCounts = append(byteCounts, current)
}
// validate lines and byte offsets
if len(test.strings) != len(readLines) {
t.Errorf("number of lines mismatch (expected=%v actual=%v)",
len(test.strings), len(readLines))
continue
}
for i := range test.strings {
expected := test.strings[i]
actual := readLines[i]
assert.Equal(t, expected, actual)
assert.Equal(t, expectedCount[i], byteCounts[i])
}
}
}
开发者ID:urso,项目名称:filebeat,代码行数:60,代码来源:reader_test.go
示例3: to_utf8
// Shift-JIS -> UTF-8
func to_utf8(str string) (string, error) {
body, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(str), japanese.ShiftJIS.NewEncoder()))
if err != nil {
return "", err
}
var f []byte
encodings := []string{"sjis", "utf-8"}
for _, enc := range encodings {
if enc != "" {
ee, _ := charset.Lookup(enc)
if ee == nil {
continue
}
var buf bytes.Buffer
ic := transform.NewWriter(&buf, ee.NewDecoder())
_, err := ic.Write(body)
if err != nil {
continue
}
err = ic.Close()
if err != nil {
continue
}
f = buf.Bytes()
break
}
}
return string(f), nil
}
开发者ID:sharkattack51,项目名称:go_Shift-JIS_to_UTF-8,代码行数:31,代码来源:main.go
示例4: getWriter
func getWriter(writer io.Writer) io.Writer {
encoding := getEncoding()
if encoding == nil {
return writer
}
return transform.NewWriter(writer, encoding.NewEncoder())
}
开发者ID:kintone,项目名称:cli-kintone,代码行数:7,代码来源:export.go
示例5: TestReaderPartialWithEncodings
func TestReaderPartialWithEncodings(t *testing.T) {
for _, test := range tests {
t.Logf("test codec: %v", test.encoding)
codecFactory, ok := encoding.FindEncoding(test.encoding)
if !ok {
t.Errorf("can not find encoding '%v'", test.encoding)
continue
}
buffer := bytes.NewBuffer(nil)
codec, _ := codecFactory(buffer)
writer := transform.NewWriter(buffer, codec.NewEncoder())
reader, err := newLineReader(buffer, codec, 1024)
if err != nil {
t.Errorf("failed to initialize reader: %v", err)
continue
}
var expected []string
var partials []string
lastString := ""
for _, str := range test.strings {
writer.Write([]byte(str))
lastString += str
expected = append(expected, lastString)
line, sz, err := reader.next()
assert.NotNil(t, err)
assert.Equal(t, 0, sz)
assert.Nil(t, line)
partial, _, err := reader.partial()
partials = append(partials, string(partial))
t.Logf("partials: %v", partials)
}
// finish line:
writer.Write([]byte{'\n'})
// finally read line
line, _, err := reader.next()
assert.Nil(t, err)
t.Logf("line: '%v'", line)
// validate partial lines
if len(test.strings) != len(expected) {
t.Errorf("number of lines mismatch (expected=%v actual=%v)",
len(test.strings), len(partials))
continue
}
for i := range expected {
assert.Equal(t, expected[i], partials[i])
}
assert.Equal(t, lastString+"\n", string(line))
}
}
开发者ID:ruflin,项目名称:filebeat,代码行数:59,代码来源:reader_test.go
示例6: newWriter
func newWriter(option *Option) io.Writer {
encoder := func() io.Writer {
switch option.OutputEncode {
case "sjis":
return transform.NewWriter(os.Stdout, japanese.ShiftJIS.NewEncoder())
case "euc":
return transform.NewWriter(os.Stdout, japanese.EUCJP.NewEncoder())
case "jis":
return transform.NewWriter(os.Stdout, japanese.ISO2022JP.NewEncoder())
default:
return os.Stdout
}
}()
if option.EnableColor {
return ansicolor.NewAnsiColorWriter(encoder)
}
return encoder
}
开发者ID:LK4D4,项目名称:the_platinum_searcher,代码行数:18,代码来源:print.go
示例7: ConvTo
func ConvTo(b []byte, e encoding.Encoding) (result []byte, err error) {
w := new(bytes.Buffer)
writer := transform.NewWriter(w, e.NewEncoder())
defer writer.Close()
if _, err = writer.Write(b); err != nil {
return
}
return w.Bytes(), nil
}
开发者ID:fanyang01,项目名称:crawler,代码行数:10,代码来源:util.go
示例8: Do
func (r *ResultCSV) Do(ctx WebContext) error {
w := ctx.ResponseWriter()
w.Header().Set("Content-Type", "text/csv; char=utf-8")
if r.Attachment {
w.Header().Set("Content-Disposition", "attachment")
}
w.WriteHeader(r.Code)
csvw := csv.NewWriter(transform.NewWriter(w, japanese.ShiftJIS.NewEncoder()))
return csvw.WriteAll(r.Data)
}
开发者ID:winespace,项目名称:result,代码行数:10,代码来源:result.go
示例9: decodeBy
func decodeBy(charset string, dst io.Writer) io.Writer {
if "UTF-8" == strings.ToUpper(charset) || "UTF8" == strings.ToUpper(charset) {
return dst
}
cs := GetCharset(charset)
if nil == cs {
panic("charset '" + charset + "' is not exists.")
}
return transform.NewWriter(dst, cs.NewDecoder())
}
开发者ID:runner-mei,项目名称:web-terminal,代码行数:11,代码来源:main.go
示例10: EncodeReader
func EncodeReader(s io.Reader, enc string) ([]byte, error) {
e, _ := charset.Lookup(enc)
if e == nil {
return nil, errors.New(fmt.Sprintf("unsupported charset: %q", enc))
}
var buf bytes.Buffer
writer := transform.NewWriter(&buf, e.NewEncoder())
_, err := io.Copy(writer, s)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
开发者ID:yuin,项目名称:charsetutil,代码行数:13,代码来源:charsetutil.go
示例11: convertUnicodeToAscii
// Convert unicode text to ASCII text
// using specific codepage mapping.
func convertUnicodeToAscii(text string,
codepage encoding.Encoding) []byte {
b := []byte(text)
// fmt.Printf("Text length: %d\n", len(b))
var buf bytes.Buffer
if codepage == nil {
codepage = charmap.Windows1252
}
w := transform.NewWriter(&buf, codepage.NewEncoder())
defer w.Close()
w.Write(b)
// fmt.Printf("Buffer length: %d\n", len(buf.Bytes()))
return buf.Bytes()
}
开发者ID:d2r2,项目名称:go-max7219,代码行数:16,代码来源:matrix.go
示例12: saveDatasetAsCsv
func saveDatasetAsCsv(dataset []Data, filePath string) {
f, err := os.Create(filePath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
var w *csv.Writer
if runtime.GOOS == "windows" {
// on Windows, use Shift-JIS to open csv file via Microsoft Excel.
converter := bufio.NewWriter(transform.NewWriter(f, japanese.ShiftJIS.NewEncoder()))
w = csv.NewWriter(converter)
} else {
w = csv.NewWriter(f)
}
defer w.Flush()
// Write header first
header := []string{
// "日付",
"証券会社名",
"n225_sell",
"n225_buy",
"n225_net",
"topix_sell",
"topix_buy",
"topix_net",
"net_total",
}
w.Write(header)
// Write dataset
for _, data := range dataset {
var record []string
// record = append(record, obj.InfoDate)
record = append(record, data.Company)
record = append(record, data.N225Sell)
record = append(record, data.N225Buy)
record = append(record, data.N225Net)
record = append(record, data.TopixSell)
record = append(record, data.TopixBuy)
record = append(record, data.TopixNet)
record = append(record, data.NetTotal)
w.Write(record)
}
}
开发者ID:supermomonga,项目名称:go-sakimonoteguchi,代码行数:46,代码来源:main.go
示例13: Conversion
//Conversion
func Conversion(inStream io.Reader, outStream io.Writer) error {
//read from stream (Shift-JIS to UTF-8)
scanner := bufio.NewScanner(transform.NewReader(inStream, japanese.ShiftJIS.NewDecoder()))
list := make([]string, 0)
for scanner.Scan() {
list = append(list, scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
//write to stream (UTF-8 to EUC-JP)
writer := bufio.NewWriter(transform.NewWriter(outStream, japanese.EUCJP.NewEncoder()))
for _, line := range list {
if _, err := fmt.Fprintln(writer, line); err != nil {
return err
}
}
return writer.Flush()
}
开发者ID:spiegel-im-spiegel,项目名称:go-practice,代码行数:20,代码来源:conversion.go
示例14: writeCsv
func (w *ReportWriter) writeCsv(reports []Report) error {
wr := w.w
if w.dialect.Encoding == "sjis" {
log.Info("use ShiftJIS encoder for output.")
encoder := japanese.ShiftJIS.NewEncoder()
wr = transform.NewWriter(wr, encoder)
}
writer := csv.NewWriter(wr)
if w.dialect.Comma != 0 {
writer.Comma = w.dialect.Comma
}
for i, report := range reports {
if i > 0 {
writer.Write(nil)
}
log.Debugf("[%d] write csv file", i+1)
w.writeCsvOne(writer, report)
}
writer.Flush()
return writer.Error()
}
开发者ID:skitazaki,项目名称:cntblank,代码行数:21,代码来源:writer.go
示例15: encode
func encode(data []byte, charsetName string) ([]byte, error) {
encoding, _ := charset.Lookup(charsetName)
if encoding == nil {
return nil, fmt.Errorf("Unsupported charset: %v", charsetName)
}
reader := bytes.NewReader(data)
var b bytes.Buffer
writer := bufio.NewWriter(&b)
encodeWriter := transform.NewWriter(writer, encoding.NewEncoder())
if _, err := io.Copy(encodeWriter, reader); err != nil {
return nil, err
}
if err := writer.Flush(); err != nil {
return nil, err
}
if isUTF8Charset(charsetName) {
return addBOM(b.Bytes()), nil
}
return b.Bytes(), nil
}
开发者ID:shiwano,项目名称:master,代码行数:23,代码来源:encoding.go
示例16: NewWriter
// NewWriter returns a writer which encodes to the given encoding, utf8.
//
// If enc is nil, then only an utf8-enforcing replacement writer
// (see http://godoc.org/code.google.com/p/go.text/encoding#pkg-variables)
// is used.
func NewWriter(w io.Writer, enc encoding.Encoding) io.WriteCloser {
if enc == nil || enc == encoding.Replacement {
return transform.NewWriter(w, encoding.Replacement.NewEncoder())
}
return transform.NewWriter(w, transform.Chain(enc.NewEncoder()))
}
开发者ID:xingskycn,项目名称:go,代码行数:11,代码来源:writer.go
示例17: NewWriter
// NewWriter returns an io.Writer that converts LF line endings to CRLF.
func NewWriter(w io.Writer) io.Writer {
return transform.NewWriter(w, ToCRLF{})
}
开发者ID:andybalholm,项目名称:crlf,代码行数:4,代码来源:transform.go
示例18: Writer
// Writer wraps another Writer to encode its UTF-8 output.
//
// The Encoder may not be used for any other operation as long as the returned
// Writer is in use.
func (e *Encoder) Writer(w io.Writer) io.Writer {
return transform.NewWriter(w, e)
}
开发者ID:YaSuenag,项目名称:hsbeat,代码行数:7,代码来源:encoding.go
示例19: main
//.........这里部分代码省略.........
case name == "null-data":
zeroData = true
case name == "help":
usage(false)
default:
usage(true)
}
} else {
args = append(args, argv[n])
}
}
if len(args) == 0 {
usage(true)
}
var err error
var pattern interface{}
if encs != "" {
encodings = strings.Split(encs, ",")
} else {
enc_env := os.Getenv("JVGREP_ENCODINGS")
if enc_env != "" {
encodings = strings.Split(enc_env, ",")
}
}
out_enc := os.Getenv("JVGREP_OUTPUT_ENCODING")
if out_enc != "" {
ee, _ := charset.Lookup(out_enc)
if ee == nil {
errorline(fmt.Sprintf("unknown encoding: %s", out_enc))
os.Exit(1)
}
oc = transform.NewWriter(os.Stdout, ee.NewEncoder())
}
instr := ""
argindex := 0
if len(infile) > 0 {
b, err := ioutil.ReadFile(infile)
if err != nil {
errorline(err.Error())
os.Exit(1)
}
instr = strings.TrimSpace(string(b))
} else {
instr = args[0]
argindex = 1
}
if fixed {
pattern = instr
} else if perl {
re, err := syntax.Parse(instr, syntax.Perl)
if err != nil {
errorline(err.Error())
os.Exit(1)
}
rec, err := syntax.Compile(re)
if err != nil {
errorline(err.Error())
os.Exit(1)
}
instr = rec.String()
if ignorecase {
instr = "(?i:" + instr + ")"
}
开发者ID:koron,项目名称:jvgrep,代码行数:67,代码来源:jvgrep.go
示例20: doGrep
func doGrep(path string, f []byte, arg *GrepArg) {
encs := encodings
if ignorebinary {
if bytes.IndexFunc(f, func(r rune) bool { return 0 < r && r < 0x9 }) != -1 {
return
}
}
if len(f) > 2 {
if f[0] == 0xfe && f[1] == 0xff {
arg.bom = f[0:2]
f = f[2:]
} else if f[0] == 0xff && f[1] == 0xfe {
arg.bom = f[0:2]
f = f[2:]
}
}
if len(arg.bom) > 0 {
if arg.bom[0] == 0xfe && arg.bom[1] == 0xff {
encs = []string{"utf-16be"}
} else if arg.bom[0] == 0xff && arg.bom[1] == 0xfe {
encs = []string{"utf-16le"}
}
}
for _, enc := range encs {
if verbose {
println("trying("+enc+"):", path)
}
if len(arg.bom) > 0 && enc != "utf-16be" && enc != "utf-16le" {
continue
}
did := false
var t []byte
var n, l, size, next, prev int
if enc != "" {
if len(arg.bom) > 0 || bytes.IndexFunc(f, func(r rune) bool { return 0 < r && r < 0x9 }) == -1 {
ee, _ := charset.Lookup(enc)
if ee == nil {
continue
}
var buf bytes.Buffer
ic := transform.NewWriter(&buf, ee.NewDecoder())
_, err := ic.Write(f)
if err != nil {
next = -1
continue
}
lf := false
if len(arg.bom) > 0 && len(f)%2 != 0 {
ic.Write([]byte{0})
lf = true
}
err = ic.Close()
if err != nil {
if verbose {
println(err.Error())
}
next = -1
continue
}
f = buf.Bytes()
if lf {
f = f[:len(f)-1]
}
}
}
size = len(f)
if size == 0 {
continue
}
for next != -1 {
for {
if next >= size {
next = -1
break
}
if f[next] == '\n' {
break
}
next++
}
n++
if next == -1 {
t = f[prev:]
} else {
t = f[prev:next]
prev = next + 1
next++
}
l = len(t)
if l > 0 && t[l-1] == '\r' {
t = t[:l-1]
l--
}
//.........这里部分代码省略.........
开发者ID:koron,项目名称:jvgrep,代码行数:101,代码来源:jvgrep.go
注:本文中的golang.org/x/text/transform.NewWriter函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论