| 1 |
// =========================================================================== |
| 2 |
// |
| 3 |
// PUBLIC DOMAIN NOTICE |
| 4 |
// National Center for Biotechnology Information (NCBI) |
| 5 |
// |
| 6 |
// This software/database is a "United States Government Work" under the |
| 7 |
// terms of the United States Copyright Act. It was written as part of |
| 8 |
// the author's official duties as a United States Government employee and |
| 9 |
// thus cannot be copyrighted. This software/database is freely available |
| 10 |
// to the public for use. The National Library of Medicine and the U.S. |
| 11 |
// Government do not place any restriction on its use or reproduction. |
| 12 |
// We would, however, appreciate having the NCBI and the author cited in |
| 13 |
// any work or product based on this material. |
| 14 |
// |
| 15 |
// Although all reasonable efforts have been taken to ensure the accuracy |
| 16 |
// and reliability of the software and data, the NLM and the U.S. |
| 17 |
// Government do not and cannot warrant the performance or results that |
| 18 |
// may be obtained by using this software or data. The NLM and the U.S. |
| 19 |
// Government disclaim all warranties, express or implied, including |
| 20 |
// warranties of performance, merchantability or fitness for any particular |
| 21 |
// purpose. |
| 22 |
// |
| 23 |
// =========================================================================== |
| 24 |
// |
| 25 |
// File Name: chan.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"io" |
| 35 |
"os" |
| 36 |
"strings" |
| 37 |
) |
| 38 |
|
| 39 |
// stringChanReader connects a string output channel to an io.Reader interface |
| 40 |
type stringChanReader struct { |
| 41 |
c <-chan string |
| 42 |
s string |
| 43 |
} |
| 44 |
|
| 45 |
func (r *stringChanReader) Read(b []byte) (n int, err error) { |
| 46 |
|
| 47 |
if r.s != "" { |
| 48 |
n = copy(b, []byte(r.s)) |
| 49 |
r.s = r.s[n:] |
| 50 |
return |
| 51 |
} |
| 52 |
|
| 53 |
for str := range r.c { |
| 54 |
r.s = str |
| 55 |
n = copy(b, []byte(r.s)) |
| 56 |
r.s = r.s[n:] |
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
return 0, io.EOF |
| 61 |
} |
| 62 |
|
| 63 |
// ChanToReader converts a string channel to an ioReader |
| 64 |
func ChanToReader(inp <-chan string) io.Reader { |
| 65 |
|
| 66 |
if inp == nil { |
| 67 |
return nil |
| 68 |
} |
| 69 |
|
| 70 |
return &stringChanReader{c: inp} |
| 71 |
} |
| 72 |
|
| 73 |
// ChanToStdout sends a string channel to stdout |
| 74 |
func ChanToStdout(inp <-chan string) { |
| 75 |
|
| 76 |
if inp == nil { |
| 77 |
return |
| 78 |
} |
| 79 |
|
| 80 |
last := "" |
| 81 |
|
| 82 |
for str := range inp { |
| 83 |
last = str |
| 84 |
os.Stdout.WriteString(str) |
| 85 |
} |
| 86 |
|
| 87 |
if !strings.HasSuffix(last, "\n") { |
| 88 |
os.Stdout.WriteString("\n") |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// ChanToString converts a string channel to a string |
| 93 |
func ChanToString(inp <-chan string) string { |
| 94 |
|
| 95 |
if inp == nil { |
| 96 |
return "" |
| 97 |
} |
| 98 |
|
| 99 |
var buffer strings.Builder |
| 100 |
|
| 101 |
last := "" |
| 102 |
|
| 103 |
for str := range inp { |
| 104 |
last = str |
| 105 |
buffer.WriteString(str) |
| 106 |
} |
| 107 |
|
| 108 |
if !strings.HasSuffix(last, "\n") { |
| 109 |
buffer.WriteString("\n") |
| 110 |
} |
| 111 |
|
| 112 |
return buffer.String() |
| 113 |
} |
| 114 |
|
| 115 |
// ChanToChan buffers a string channel |
| 116 |
func ChanToChan(inp <-chan string) <-chan string { |
| 117 |
|
| 118 |
if inp == nil { |
| 119 |
return nil |
| 120 |
} |
| 121 |
|
| 122 |
out := make(chan string, chanDepth) |
| 123 |
if out == nil { |
| 124 |
return nil |
| 125 |
} |
| 126 |
|
| 127 |
bufferChannel := func(inp <-chan string, out chan<- string) { |
| 128 |
|
| 129 |
// close channel when all records have been processed |
| 130 |
defer close(out) |
| 131 |
|
| 132 |
var arry []string |
| 133 |
|
| 134 |
// drain input channel |
| 135 |
for str := range inp { |
| 136 |
arry = append(arry, str) |
| 137 |
} |
| 138 |
|
| 139 |
// retransmit buffered strings |
| 140 |
for _, str := range arry { |
| 141 |
out <- str |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// launch single buffering goroutine |
| 146 |
go bufferChannel(inp, out) |
| 147 |
|
| 148 |
return out |
| 149 |
} |
| 150 |
|
| 151 |
// StringToChan sends a single string down a string channel |
| 152 |
func StringToChan(str string) <-chan string { |
| 153 |
|
| 154 |
if str == "" { |
| 155 |
return nil |
| 156 |
} |
| 157 |
|
| 158 |
out := make(chan string, chanDepth) |
| 159 |
if out == nil { |
| 160 |
return nil |
| 161 |
} |
| 162 |
|
| 163 |
sendString := func(str string, out chan<- string) { |
| 164 |
|
| 165 |
// close channel when all records have been processed |
| 166 |
defer close(out) |
| 167 |
|
| 168 |
out <- str |
| 169 |
} |
| 170 |
|
| 171 |
// launch single buffering goroutine |
| 172 |
go sendString(str, out) |
| 173 |
|
| 174 |
return out |
| 175 |
} |
| 176 |
|
| 177 |
// SliceToChan sends slice elements down a string channel |
| 178 |
func SliceToChan(values []string) <-chan string { |
| 179 |
|
| 180 |
if values == nil { |
| 181 |
return nil |
| 182 |
} |
| 183 |
|
| 184 |
out := make(chan string, chanDepth) |
| 185 |
if out == nil { |
| 186 |
return nil |
| 187 |
} |
| 188 |
|
| 189 |
sendStrings := func(values []string, out chan<- string) { |
| 190 |
|
| 191 |
// close channel when all records have been processed |
| 192 |
defer close(out) |
| 193 |
|
| 194 |
for _, str := range values { |
| 195 |
out <- str |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// launch single buffering goroutine |
| 200 |
go sendStrings(values, out) |
| 201 |
|
| 202 |
return out |
| 203 |
} |