strings: make builder implement io.Writer (#8914)

pull/8926/head
zakuro 2021-02-23 16:42:48 +09:00 committed by GitHub
parent 5674d46965
commit c113abe1a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 11 deletions

View File

@ -29,33 +29,33 @@ pub fn (mut w Writer) write(record []string) ?bool {
for n, field_ in record {
mut field := field_
if n > 0 {
w.sb.write(w.delimiter.ascii_str())
w.sb.write_string(w.delimiter.ascii_str())
}
if !w.field_needs_quotes(field) {
w.sb.write(field)
w.sb.write_string(field)
continue
}
w.sb.write('"')
w.sb.write_string('"')
for field.len > 0 {
mut i := field.index_any('"\r\n')
if i < 0 {
i = field.len
}
w.sb.write(field[..i])
w.sb.write_string(field[..i])
field = field[i..]
if field.len > 0 {
z := field[0]
match z {
`"` { w.sb.write('""') }
`\r`, `\n` { w.sb.write(le) }
`"` { w.sb.write_string('""') }
`\r`, `\n` { w.sb.write_string(le) }
else {}
}
field = field[1..]
}
}
w.sb.write('"')
w.sb.write_string('"')
}
w.sb.write(le)
w.sb.write_string(le)
return true
}

View File

@ -37,9 +37,11 @@ pub fn (mut b Builder) write_b(data byte) {
b.len++
}
[deprecated: 'write(string) will be changed to write([]byte)']
pub fn (mut b Builder) write(s string) {
b.write_string(s)
// write implements the Writer interface
pub fn (mut b Builder) write(data []byte) ?int {
b.buf << data
b.len += data.len
return data.len
}
// write appends the string `s` to the buffer