blob: ef27e015aead8860f62a741c69ceb18810e43891 [file] [log] [blame]
Fumitoshi Ukai145598a2015-06-19 10:08:17 +09001// Copyright 2015 Google Inc. All rights reserved
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import "io"
18
19// ssvWriter is a writer to write space separated values.
20type ssvWriter struct {
21 w io.Writer
22 needsSpace bool
23}
24
25func writeByte(w io.Writer, b byte) error {
26 if bw, ok := w.(io.ByteWriter); ok {
27 return bw.WriteByte(b)
28 }
29 _, err := w.Write([]byte{b})
30 return err
31}
32
33// use io.WriteString to stringWrite.
34
35func (sw *ssvWriter) Write(b []byte) {
36 if sw.needsSpace {
37 writeByte(sw.w, ' ')
38 }
39 sw.needsSpace = true
40 sw.w.Write(b)
41}
42
43func (sw *ssvWriter) WriteString(s string) {
44 if sw.needsSpace {
45 writeByte(sw.w, ' ')
46 }
47 sw.needsSpace = true
48 io.WriteString(sw.w, s)
49}