LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 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 csv | 5 package csv |
6 | 6 |
7 import ( | 7 import ( |
8 "bufio" | 8 "bufio" |
9 "io" | 9 "io" |
10 "strings" | 10 "strings" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 } | 85 } |
86 if w.UseCRLF { | 86 if w.UseCRLF { |
87 _, err = w.w.WriteString("\r\n") | 87 _, err = w.w.WriteString("\r\n") |
88 } else { | 88 } else { |
89 err = w.w.WriteByte('\n') | 89 err = w.w.WriteByte('\n') |
90 } | 90 } |
91 return | 91 return |
92 } | 92 } |
93 | 93 |
94 // Flush writes any buffered data to the underlying io.Writer. | 94 // Flush writes any buffered data to the underlying io.Writer. |
| 95 // To check if an error occured during the Flush, call Error. |
95 func (w *Writer) Flush() { | 96 func (w *Writer) Flush() { |
96 w.w.Flush() | 97 w.w.Flush() |
| 98 } |
| 99 |
| 100 // Error reports any error that has occurred during a previous Write or Flush. |
| 101 func (w *Writer) Error() error { |
| 102 _, err := w.w.Write(nil) |
| 103 return err |
97 } | 104 } |
98 | 105 |
99 // WriteAll writes multiple CSV records to w using Write and then calls Flush. | 106 // WriteAll writes multiple CSV records to w using Write and then calls Flush. |
100 func (w *Writer) WriteAll(records [][]string) (err error) { | 107 func (w *Writer) WriteAll(records [][]string) (err error) { |
101 for _, record := range records { | 108 for _, record := range records { |
102 err = w.Write(record) | 109 err = w.Write(record) |
103 if err != nil { | 110 if err != nil { |
104 return err | 111 return err |
105 } | 112 } |
106 } | 113 } |
107 return w.w.Flush() | 114 return w.w.Flush() |
108 } | 115 } |
109 | 116 |
110 // fieldNeedsQuotes returns true if our field must be enclosed in quotes. | 117 // fieldNeedsQuotes returns true if our field must be enclosed in quotes. |
111 // Empty fields, files with a Comma, fields with a quote or newline, and | 118 // Empty fields, files with a Comma, fields with a quote or newline, and |
112 // fields which start with a space must be enclosed in quotes. | 119 // fields which start with a space must be enclosed in quotes. |
113 func (w *Writer) fieldNeedsQuotes(field string) bool { | 120 func (w *Writer) fieldNeedsQuotes(field string) bool { |
114 if len(field) == 0 || strings.IndexRune(field, w.Comma) >= 0 || strings.
IndexAny(field, "\"\r\n") >= 0 { | 121 if len(field) == 0 || strings.IndexRune(field, w.Comma) >= 0 || strings.
IndexAny(field, "\"\r\n") >= 0 { |
115 return true | 122 return true |
116 } | 123 } |
117 | 124 |
118 r1, _ := utf8.DecodeRuneInString(field) | 125 r1, _ := utf8.DecodeRuneInString(field) |
119 return unicode.IsSpace(r1) | 126 return unicode.IsSpace(r1) |
120 } | 127 } |
LEFT | RIGHT |