github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/analysis/tokenattributes/charTerm.go (about)

     1  package tokenattributes
     2  
     3  import (
     4  	"github.com/balzaczyy/golucene/core/util"
     5  )
     6  
     7  /* The term text of a Token. */
     8  type CharTermAttribute interface {
     9  	// Copies the contents of buffer into the termBuffer array
    10  	CopyBuffer(buffer []rune)
    11  	// Returns the internal termBuffer rune slice which you can then
    12  	// directly alter. If the slice is too small for your token, use
    13  	// ResizeBuffer(int) to increase it. After altering the buffer, be
    14  	// sure to call SetLength() to record the number of valid runes
    15  	// that were placed into the termBuffer.
    16  	//
    17  	// NOTE: the returned buffer may be larger than the valid Length().
    18  	Buffer() []rune
    19  	Length() int
    20  	// Appends teh specified string to this character sequence.
    21  	//
    22  	// The character of the string argument are appended, in order,
    23  	// increasing the length of this sequence by the length of the
    24  	// argument. If argument is "", then the three characters "nil" are
    25  	// appended.
    26  	AppendString(string) CharTermAttribute
    27  }
    28  
    29  const MIN_BUFFER_SIZE = 10
    30  
    31  /* Default implementation of CharTermAttribute. */
    32  type CharTermAttributeImpl struct {
    33  	termBuffer []rune
    34  	termLength int
    35  	bytes      *util.BytesRefBuilder
    36  }
    37  
    38  func newCharTermAttributeImpl() *CharTermAttributeImpl {
    39  	return &CharTermAttributeImpl{
    40  		termBuffer: make([]rune, util.Oversize(MIN_BUFFER_SIZE, util.NUM_BYTES_CHAR)),
    41  		bytes:      util.NewBytesRefBuilder(),
    42  	}
    43  }
    44  
    45  func (a *CharTermAttributeImpl) Interfaces() []string {
    46  	return []string{"CharTermAttribute", "TermToBytesRefAttribute"}
    47  }
    48  
    49  func (a *CharTermAttributeImpl) CopyBuffer(buffer []rune) {
    50  	a.growTermBuffer(len(buffer))
    51  	copy(a.termBuffer, buffer)
    52  	a.termLength = len(buffer)
    53  }
    54  
    55  func (a *CharTermAttributeImpl) Buffer() []rune {
    56  	return a.termBuffer
    57  }
    58  
    59  func (a *CharTermAttributeImpl) growTermBuffer(newSize int) {
    60  	if len(a.termBuffer) < newSize {
    61  		// not big enough: create a new slice with slight over allocation:
    62  		a.termBuffer = make([]rune, util.Oversize(newSize, util.NUM_BYTES_CHAR))
    63  	}
    64  }
    65  
    66  func (a *CharTermAttributeImpl) FillBytesRef() {
    67  	s := string(a.termBuffer[:a.termLength])
    68  	a.bytes.Copy([]byte(s))
    69  }
    70  
    71  func (a *CharTermAttributeImpl) BytesRef() *util.BytesRef {
    72  	return a.bytes.Get()
    73  }
    74  
    75  func (a *CharTermAttributeImpl) Length() int {
    76  	return a.termLength
    77  }
    78  
    79  func (a *CharTermAttributeImpl) AppendString(s string) CharTermAttribute {
    80  	if s == "" { // needed for Appendable compliance
    81  		return a.appendNil()
    82  	}
    83  	for _, ch := range s {
    84  		if a.termLength < len(a.termBuffer) {
    85  			a.termBuffer[a.termLength] = ch
    86  		} else {
    87  			a.termBuffer = append(a.termBuffer, ch)
    88  		}
    89  		a.termLength++
    90  	}
    91  	return a
    92  }
    93  
    94  func (a *CharTermAttributeImpl) appendNil() CharTermAttribute {
    95  	a.termBuffer = append(a.termBuffer, 'n')
    96  	a.termBuffer = append(a.termBuffer, 'i')
    97  	a.termBuffer = append(a.termBuffer, 'l')
    98  	a.termLength += 3
    99  	return a
   100  }
   101  
   102  func (a *CharTermAttributeImpl) Clear() {
   103  	a.termLength = 0
   104  }
   105  
   106  func (a *CharTermAttributeImpl) Clone() util.AttributeImpl {
   107  	clone := new(CharTermAttributeImpl)
   108  	// do a deep clone
   109  	clone.termBuffer = make([]rune, a.termLength)
   110  	copy(clone.termBuffer, a.termBuffer[:a.termLength])
   111  	clone.termLength = a.termLength
   112  	clone.bytes = util.NewBytesRefBuilder()
   113  	clone.bytes.Copy(a.bytes.Bytes())
   114  	return clone
   115  }
   116  
   117  func (a *CharTermAttributeImpl) String() string {
   118  	return string(a.termBuffer[:a.termLength])
   119  }
   120  
   121  func (a *CharTermAttributeImpl) CopyTo(target util.AttributeImpl) {
   122  	target.(CharTermAttribute).CopyBuffer(a.termBuffer[:a.termLength])
   123  }