strings: make builder implement io.Writer (#8914)
parent
5674d46965
commit
c113abe1a4
|
@ -29,33 +29,33 @@ pub fn (mut w Writer) write(record []string) ?bool {
|
||||||
for n, field_ in record {
|
for n, field_ in record {
|
||||||
mut field := field_
|
mut field := field_
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
w.sb.write(w.delimiter.ascii_str())
|
w.sb.write_string(w.delimiter.ascii_str())
|
||||||
}
|
}
|
||||||
if !w.field_needs_quotes(field) {
|
if !w.field_needs_quotes(field) {
|
||||||
w.sb.write(field)
|
w.sb.write_string(field)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
w.sb.write('"')
|
w.sb.write_string('"')
|
||||||
for field.len > 0 {
|
for field.len > 0 {
|
||||||
mut i := field.index_any('"\r\n')
|
mut i := field.index_any('"\r\n')
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
i = field.len
|
i = field.len
|
||||||
}
|
}
|
||||||
w.sb.write(field[..i])
|
w.sb.write_string(field[..i])
|
||||||
field = field[i..]
|
field = field[i..]
|
||||||
if field.len > 0 {
|
if field.len > 0 {
|
||||||
z := field[0]
|
z := field[0]
|
||||||
match z {
|
match z {
|
||||||
`"` { w.sb.write('""') }
|
`"` { w.sb.write_string('""') }
|
||||||
`\r`, `\n` { w.sb.write(le) }
|
`\r`, `\n` { w.sb.write_string(le) }
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
field = field[1..]
|
field = field[1..]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.sb.write('"')
|
w.sb.write_string('"')
|
||||||
}
|
}
|
||||||
w.sb.write(le)
|
w.sb.write_string(le)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,9 +37,11 @@ pub fn (mut b Builder) write_b(data byte) {
|
||||||
b.len++
|
b.len++
|
||||||
}
|
}
|
||||||
|
|
||||||
[deprecated: 'write(string) will be changed to write([]byte)']
|
// write implements the Writer interface
|
||||||
pub fn (mut b Builder) write(s string) {
|
pub fn (mut b Builder) write(data []byte) ?int {
|
||||||
b.write_string(s)
|
b.buf << data
|
||||||
|
b.len += data.len
|
||||||
|
return data.len
|
||||||
}
|
}
|
||||||
|
|
||||||
// write appends the string `s` to the buffer
|
// write appends the string `s` to the buffer
|
||||||
|
|
Loading…
Reference in New Issue