2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using IndexInput = Mono.Lucene.Net.Store.IndexInput;
21 using UnicodeUtil = Mono.Lucene.Net.Util.UnicodeUtil;
23 namespace Mono.Lucene.Net.Index
26 sealed class TermBuffer : System.ICloneable
29 private System.String field;
30 private Term term; // cached
31 private bool preUTF8Strings; // true if strings are stored in modified UTF8 encoding (LUCENE-510)
32 private bool dirty; // true if text was set externally (ie not read via UTF8 bytes)
34 private UnicodeUtil.UTF16Result text = new UnicodeUtil.UTF16Result();
35 private UnicodeUtil.UTF8Result bytes = new UnicodeUtil.UTF8Result();
37 public int CompareTo(TermBuffer other)
39 if ((System.Object) field == (System.Object) other.field)
40 // fields are interned
41 return CompareChars(text.result, text.length, other.text.result, other.text.length);
43 return String.CompareOrdinal(field, other.field);
46 private static int CompareChars(char[] chars1, int len1, char[] chars2, int len2)
48 int end = len1 < len2?len1:len2;
49 for (int k = 0; k < end; k++)
61 /// <summary>Call this if the IndexInput passed to {@link #read}
62 /// stores terms in the "modified UTF8" (pre LUCENE-510)
65 internal void SetPreUTF8Strings()
67 preUTF8Strings = true;
70 public void Read(IndexInput input, FieldInfos fieldInfos)
72 this.term = null; // invalidate cache
73 int start = input.ReadVInt();
74 int length = input.ReadVInt();
75 int totalLength = start + length;
78 text.SetLength(totalLength);
79 input.ReadChars(text.result, start, length);
86 // Fully convert all bytes since bytes is dirty
87 UnicodeUtil.UTF16toUTF8(text.result, 0, text.length, bytes);
88 bytes.SetLength(totalLength);
89 input.ReadBytes(bytes.result, start, length);
90 UnicodeUtil.UTF8toUTF16(bytes.result, 0, totalLength, text);
95 // Incrementally convert only the UTF8 bytes that are new:
96 bytes.SetLength(totalLength);
97 input.ReadBytes(bytes.result, start, length);
98 UnicodeUtil.UTF8toUTF16(bytes.result, start, length, text);
101 this.field = fieldInfos.FieldName(input.ReadVInt());
104 public void Set(Term term)
111 System.String termText = term.Text();
112 int termLen = termText.Length;
113 text.SetLength(termLen);
114 SupportClass.TextSupport.GetCharsFromString(termText, 0, termLen, text.result, 0);
116 field = term.Field();
120 public void Set(TermBuffer other)
122 text.CopyText(other.text);
143 term = new Term(field, new System.String(text.result, 0, text.length), false);
148 public System.Object Clone()
150 TermBuffer clone = null;
153 clone = (TermBuffer) base.MemberwiseClone();
155 catch (System.Exception e)
160 clone.bytes = new UnicodeUtil.UTF8Result();
161 clone.text = new UnicodeUtil.UTF16Result();
162 clone.text.CopyText(text);