1
/*------------------------------------------------------------------------------
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
4
* Distributable under the terms of either the Apache License (Version 2.0) or
5
* the GNU Lesser General Public License, as specified in the COPYING file.
6
------------------------------------------------------------------------------*/
7
#include "CLucene/StdHeader.h"
9
#include "CLucene/util/StringIntern.h"
15
//Intern fld and assign it to field
16
_field = LUCENE_BLANK_STRING;
21
//Duplicate txt and assign it to text
22
#ifdef LUCENE_TERM_TEXT_LENGTH
25
_text = LUCENE_BLANK_STRING;
31
Term::Term(const TCHAR* fld, const TCHAR* txt, bool internField){
33
// Constructs a Term with the given field and text. Field and text are not copied
34
// Field and text are deleted in destructor only if intern is false.
35
//Pre - fld != NULL and contains the name of the field
36
// txt != NULL and contains the value of the field
37
// internF is true or false and indicates if term Field is interned or not
38
// internT is true or false and indicates if term Text is interned or not
39
// canDelete defaults to true but can be false and indicates to the IGarbageCollector that the Term can be deleted when finalized
40
//Post - An instance of Term has been created.Field and txt have not been copied but assigned
42
_field = LUCENE_BLANK_STRING;
45
#ifdef LUCENE_TERM_TEXT_LENGTH
48
_text = LUCENE_BLANK_STRING;
52
set(fld,txt,internField);
56
Term::Term(const Term* fieldTerm, const TCHAR* txt){
57
_field = LUCENE_BLANK_STRING;
60
#ifdef LUCENE_TERM_TEXT_LENGTH
63
_text = LUCENE_BLANK_STRING;
70
Term::Term(const TCHAR* fld, const TCHAR* txt){
71
_field = LUCENE_BLANK_STRING;
74
#ifdef LUCENE_TERM_TEXT_LENGTH
77
_text = LUCENE_BLANK_STRING;
87
//Post - The instance has been destroyed. field and text have been deleted if pre(intrn) is false
91
CLStringIntern::unintern(_field);
94
#ifndef LUCENE_TERM_TEXT_LENGTH
95
//Deletetext if it is the owner
96
if ( _text != LUCENE_BLANK_STRING)
97
_CLDELETE_CARRAY( _text );
101
const TCHAR* Term::field() const {
102
//Func - Returns the field of this term, an interned string. The field indicates
103
// the part of a document which this term came from.
105
//Post - field has been returned
110
const TCHAR* Term::text() const {
111
//Func - Returns the text of this term. In the case of words, this is simply the
112
// text of the word. In the case of dates and other types, this is an
113
// encoding of the object as a string.
115
//Post - text has been returned
121
void Term::set(const Term* term, const TCHAR* txt){
122
set(term->field(),txt,false);
125
void Term::set(const TCHAR* fld, const TCHAR* txt,const bool internField){
126
//Func - Resets the field and text of a Term.
127
//Pre - fld != NULL and contains the name of the field
128
// txt != NULL and contains the value of the field
129
// internF is true or false
130
// internT is true or false
131
//Post - field and text of Term have been reset
133
CND_PRECONDITION(fld != NULL, "fld contains NULL");
134
CND_PRECONDITION(txt != NULL, "txt contains NULL");
136
//save field for unintern later
137
const TCHAR* oldField = _field;
138
//bool oldInternF = internF; //Not used
141
textLen = _tcslen(txt);
143
//Delete text if it is the owner
144
#ifdef LUCENE_TERM_TEXT_LENGTH
145
if ( textLen > LUCENE_TERM_TEXT_LENGTH )
146
textLen = LUCENE_TERM_TEXT_LENGTH;
147
_tcsncpy(_text,txt,textLen+1);
151
//if the term text buffer is bigger than what we have
152
if ( _text && textLen > textLenBuf){
153
if ( _text != LUCENE_BLANK_STRING ){
154
_CLDELETE_ARRAY( _text );
160
if ( _text==LUCENE_BLANK_STRING )
161
_text = LUCENE_BLANK_STRING;
162
else if ( _text==NULL ){
164
//if the string is blank and we aren't re-using the buffer...
165
_text = LUCENE_BLANK_STRING;
168
_text = stringDuplicate(txt);
169
textLenBuf = textLen;
180
_field = CLStringIntern::intern(fld CL_FILELINE);
184
//unintern old field after interning new one,
186
CLStringIntern::unintern(oldField);
187
internF = internField;
189
CND_PRECONDITION(_tcscmp(fld, _field)==0,"field not equal");
192
/** Compares two terms, returning true iff they have the same
194
bool Term::equals(const Term* other) const{
195
if ( cachedHashCode != 0 && other->cachedHashCode != 0 &&
196
other->cachedHashCode != cachedHashCode )
199
if ( _field==other->_field ){
200
//this can be quicker than using compareTo, because checks
202
if ( textLen == other->textLen ){
203
return (_tcscmp(_text,other->_text)==0);
210
size_t Term::hashCode(){
211
if ( cachedHashCode == 0 )
212
cachedHashCode = Misc::thashCode(_field) + Misc::thashCode(_text,textLen);
213
return cachedHashCode;
217
int32_t Term::compareTo(const Term* other) const {
218
//Func - Compares two terms, to see if this term belongs before,is equal to or after
219
// after the argument term.
220
//Pre - other is a reference to another term
221
//Post - A negative integer is returned if this term belongs before the argument,
222
// zero is returned if this term is equal to the argument, and a positive integer
223
// if this term belongs after the argument.
225
//Check ret to see if text needs to be compared
226
if ( _field == other->_field ){ // fields are interned
227
//Compare text with text of other and return the result
228
return _tcscmp(_text,other->_text);
230
return _tcscmp(_field,other->_field);
233
TCHAR* Term::toString() const{
234
//Func - Forms the contents of Field and term in some kind of tuple notation
237
//Post - a string formatted as <field:text> is returned if pre(field) is NULL and
238
// text is NULL the returned string will be formatted as <:>
240
return CL_NS(util)::Misc::join( _field, _T(":"), _text);