github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/coverage/stringtab/stringtab.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package stringtab
     6  
     7  import (
     8  	"github.com/shogo82148/std/internal/coverage/slicereader"
     9  	"github.com/shogo82148/std/io"
    10  )
    11  
    12  // Writer implements a string table writing utility.
    13  type Writer struct {
    14  	stab   map[string]uint32
    15  	strs   []string
    16  	tmp    []byte
    17  	frozen bool
    18  }
    19  
    20  // InitWriter initializes a stringtab.Writer.
    21  func (stw *Writer) InitWriter()
    22  
    23  // Nentries returns the number of strings interned so far.
    24  func (stw *Writer) Nentries() uint32
    25  
    26  // Lookup looks up string 's' in the writer's table, adding
    27  // a new entry if need be, and returning an index into the table.
    28  func (stw *Writer) Lookup(s string) uint32
    29  
    30  // Size computes the memory in bytes needed for the serialized
    31  // version of a stringtab.Writer.
    32  func (stw *Writer) Size() uint32
    33  
    34  // Write writes the string table in serialized form to the specified
    35  // io.Writer.
    36  func (stw *Writer) Write(w io.Writer) error
    37  
    38  // Freeze sends a signal to the writer that no more additions are
    39  // allowed, only lookups of existing strings (if a lookup triggers
    40  // addition, a panic will result). Useful as a mechanism for
    41  // "finalizing" a string table prior to writing it out.
    42  func (stw *Writer) Freeze()
    43  
    44  // Reader is a helper for reading a string table previously
    45  // serialized by a Writer.Write call.
    46  type Reader struct {
    47  	r    *slicereader.Reader
    48  	strs []string
    49  }
    50  
    51  // NewReader creates a stringtab.Reader to read the contents
    52  // of a string table from 'r'.
    53  func NewReader(r *slicereader.Reader) *Reader
    54  
    55  // Read reads/decodes a string table using the reader provided.
    56  func (str *Reader) Read()
    57  
    58  // Entries returns the number of decoded entries in a string table.
    59  func (str *Reader) Entries() int
    60  
    61  // Get returns string 'idx' within the string table.
    62  func (str *Reader) Get(idx uint32) string