File tree Expand file tree Collapse file tree 2 files changed +27
-20
lines changed
Expand file tree Collapse file tree 2 files changed +27
-20
lines changed Original file line number Diff line number Diff line change @@ -12,8 +12,7 @@ import "C"
1212import (
1313 "bytes"
1414 "errors"
15- "io"
16- "os"
15+ "strconv"
1716 "runtime"
1817)
1918
@@ -205,24 +204,24 @@ func (rb *Bitmap) ToArray() []uint32 {
205204
206205// String creates a string representation of the Bitmap
207206func (rb * Bitmap ) String () string {
208- old := os . Stdout // keep backup of the real stdout
209- r , w , _ := os . Pipe ()
210- os . Stdout = w
211- C . roaring_bitmap_printf ( rb . cpointer )
212- C . fflush ( C . stdout )
213- outC := make ( chan string )
214- // copy the output in a separate goroutine so printing can't block indefinitely
215- go func () {
216- var buf bytes. Buffer
217- io . Copy ( & buf , r )
218- outC <- buf . String ()
219- }( )
220-
221- // back to normal state
222- w . Close ()
223- os . Stdout = old // restoring the real stdout
224- out := <- outC
225- return out
207+ arr := rb . ToArray () // todo: replace with an iterator
208+ var buffer bytes. Buffer
209+ start := [] byte ( "{" )
210+ buffer . Write ( start )
211+ l := len ( arr )
212+ for counter , i := range arr {
213+ // to avoid exhausting the memory
214+ if counter > 0x40000 {
215+ buffer . WriteString ( "..." )
216+ break
217+ }
218+ buffer . WriteString ( strconv . FormatInt ( int64 ( i ), 10 ) )
219+ if counter + 1 < l { // there is more
220+ buffer . WriteString ( "," )
221+ }
222+ }
223+ buffer . WriteString ( "}" )
224+ return buffer . String ()
226225}
227226
228227// Read reads a serialized version of the bitmap (you need to call Free on it once you are done)
Original file line number Diff line number Diff line change @@ -111,6 +111,14 @@ func TestFancier(t *testing.T) {
111111 }
112112}
113113
114+ func TestString (t * testing.T ) {
115+ ans := New (1 ,2 ,3 ).String ()
116+ fmt .Println (ans )
117+ if ans != "{1,2,3}" {
118+ t .Errorf ("bad string " )
119+ }
120+ }
121+
114122func TestStats (t * testing.T ) {
115123
116124 rb := New ()
You can’t perform that action at this time.
0 commit comments