lab.nexedi.com/kirr/go123@v0.0.0-20240207185015-8299741fa871/tracing/cmd/gotrace/util.go (about)

     1  // Copyright (C) 2017-2019  Nexedi SA and Contributors.
     2  //                          Kirill Smelkov <kirr@nexedi.com>
     3  //
     4  // This program is free software: you can Use, Study, Modify and Redistribute
     5  // it under the terms of the GNU General Public License version 3, or (at your
     6  // option) any later version, as published by the Free Software Foundation.
     7  //
     8  // You can also Link and Combine this program with other software covered by
     9  // the terms of any of the Free Software licenses or any of the Open Source
    10  // Initiative approved licenses and Convey the resulting work. Corresponding
    11  // source of such a combination shall include the source code for all other
    12  // software used.
    13  //
    14  // This program is distributed WITHOUT ANY WARRANTY; without even the implied
    15  // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    16  //
    17  // See COPYING file for full licensing terms.
    18  // See https://www.nexedi.com/licensing for rationale and options.
    19  
    20  package main
    21  // misc utilities
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"sort"
    27  )
    28  
    29  // Buffer is bytes.Buffer + syntatic sugar.
    30  type Buffer struct {
    31  	bytes.Buffer
    32  }
    33  
    34  func (b *Buffer) emit(format string, argv ...interface{}) {
    35  	fmt.Fprintf(b, format+"\n", argv...)
    36  }
    37  
    38  
    39  // StrSet is set<string>.
    40  type StrSet map[string]struct{}
    41  
    42  func (s StrSet) Add(itemv ...string) {
    43  	for _, item := range itemv {
    44  		s[item] = struct{}{}
    45  	}
    46  }
    47  
    48  func (s StrSet) Delete(item string) {
    49  	delete(s, item)
    50  }
    51  
    52  func (s StrSet) Has(item string) bool {
    53  	_, has := s[item]
    54  	return has
    55  }
    56  
    57  // Itemv returns ordered slice of set items.
    58  func (s StrSet) Itemv() []string {
    59  	itemv := make([]string, 0, len(s))
    60  	for item := range s {
    61  		itemv = append(itemv, item)
    62  	}
    63  	sort.Strings(itemv)
    64  	return itemv
    65  }