forked from apache/lucenenet
/
FacetsConfig.cs
714 lines (650 loc) · 27.7 KB
/
FacetsConfig.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace Lucene.Net.Facet
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using ArrayUtil = Lucene.Net.Util.ArrayUtil;
using AssociationFacetField = Lucene.Net.Facet.Taxonomy.AssociationFacetField;
using BinaryDocValuesField = Lucene.Net.Documents.BinaryDocValuesField;
using BytesRef = Lucene.Net.Util.BytesRef;
using Document = Lucene.Net.Documents.Document;
using FacetLabel = Lucene.Net.Facet.Taxonomy.FacetLabel;
using Field = Lucene.Net.Documents.Field;
using SingleAssociationFacetField = Lucene.Net.Facet.Taxonomy.SingleAssociationFacetField;
using IIndexableField = Lucene.Net.Index.IIndexableField;
using IIndexableFieldType = Lucene.Net.Index.IIndexableFieldType;
using Int32AssociationFacetField = Lucene.Net.Facet.Taxonomy.Int32AssociationFacetField;
using Int32sRef = Lucene.Net.Util.Int32sRef;
using SortedSetDocValuesFacetField = Lucene.Net.Facet.SortedSet.SortedSetDocValuesFacetField;
using SortedSetDocValuesField = Lucene.Net.Documents.SortedSetDocValuesField;
using StringField = Lucene.Net.Documents.StringField;
using ITaxonomyWriter = Lucene.Net.Facet.Taxonomy.ITaxonomyWriter;
/// <summary>
/// Records per-dimension configuration. By default a
/// dimension is flat, single valued and does
/// not require count for the dimension; use
/// the setters in this class to change these settings for
/// each dim.
///
/// <para>
/// <b>NOTE</b>: this configuration is not saved into the
/// index, but it's vital, and up to the application to
/// ensure, that at search time the provided <see cref="FacetsConfig"/>
/// matches what was used during indexing.
///
/// @lucene.experimental
/// </para>
/// </summary>
public class FacetsConfig
{
/// <summary>
/// Which Lucene field holds the drill-downs and ords (as
/// doc values).
/// </summary>
public const string DEFAULT_INDEX_FIELD_NAME = "$facets";
private readonly IDictionary<string, DimConfig> fieldTypes = new ConcurrentDictionary<string, DimConfig>();
// Used only for best-effort detection of app mixing
// int/float/bytes in a single indexed field:
private readonly IDictionary<string, string> assocDimTypes = new ConcurrentDictionary<string, string>();
/// <summary>
/// Holds the configuration for one dimension
///
/// @lucene.experimental
/// </summary>
public sealed class DimConfig
{
/// <summary>
/// True if this dimension is hierarchical. </summary>
public bool IsHierarchical { get; set; }
/// <summary>
/// True if this dimension is multi-valued. </summary>
public bool IsMultiValued { get; set; }
/// <summary>
/// True if the count/aggregate for the entire dimension
/// is required, which is unusual (default is false).
/// </summary>
public bool RequireDimCount { get; set; }
/// <summary>
/// Actual field where this dimension's facet labels
/// should be indexed
/// </summary>
public string IndexFieldName { get; set; }
/// <summary>
/// Default constructor.
/// </summary>
public DimConfig()
{
IndexFieldName = DEFAULT_INDEX_FIELD_NAME;
}
}
/// <summary>
/// Default per-dimension configuration. </summary>
public static readonly DimConfig DEFAULT_DIM_CONFIG = new DimConfig();
/// <summary>
/// Default constructor.
/// </summary>
public FacetsConfig()
{
}
/// <summary>
/// Get the default configuration for new dimensions. Useful when
/// the dimension is not known beforehand and may need different
/// global default settings, like <c>multivalue = true</c>.
/// </summary>
/// <returns>
/// The default configuration to be used for dimensions that
/// are not yet set in the <see cref="FacetsConfig"/>
/// </returns>
protected virtual DimConfig DefaultDimConfig
{
get
{
return DEFAULT_DIM_CONFIG;
}
}
/// <summary>
/// Get the current configuration for a dimension.
/// </summary>
public virtual DimConfig GetDimConfig(string dimName)
{
lock (this)
{
DimConfig ft;
if (!fieldTypes.TryGetValue(dimName, out ft))
{
ft = DefaultDimConfig;
}
return ft;
}
}
/// <summary>
/// Pass <c>true</c> if this dimension is hierarchical
/// (has depth > 1 paths).
/// </summary>
public virtual void SetHierarchical(string dimName, bool v)
{
lock (this)
{
if (!fieldTypes.ContainsKey(dimName))
{
var ft = new DimConfig { IsHierarchical = v };
fieldTypes[dimName] = ft;
}
else
{
fieldTypes[dimName].IsHierarchical = v;
}
}
}
/// <summary>
/// Pass <c>true</c> if this dimension may have more than
/// one value per document.
/// </summary>
public virtual void SetMultiValued(string dimName, bool v)
{
lock (this)
{
if (!fieldTypes.ContainsKey(dimName))
{
var ft = new DimConfig { IsMultiValued = v };
fieldTypes[dimName] = ft;
}
else
{
fieldTypes[dimName].IsMultiValued = v;
}
}
}
/// <summary>
/// Pass <c>true</c> if at search time you require
/// accurate counts of the dimension, i.e. how many
/// hits have this dimension.
/// </summary>
public virtual void SetRequireDimCount(string dimName, bool v)
{
lock (this)
{
if (!fieldTypes.ContainsKey(dimName))
{
var ft = new DimConfig { RequireDimCount = v };
fieldTypes[dimName] = ft;
}
else
{
fieldTypes[dimName].RequireDimCount = v;
}
}
}
/// <summary>
/// Specify which index field name should hold the
/// ordinals for this dimension; this is only used by the
/// taxonomy based facet methods.
/// </summary>
public virtual void SetIndexFieldName(string dimName, string indexFieldName)
{
lock (this)
{
if (!fieldTypes.ContainsKey(dimName))
{
var ft = new DimConfig { IndexFieldName = indexFieldName };
fieldTypes[dimName] = ft;
}
else
{
fieldTypes[dimName].IndexFieldName = indexFieldName;
}
}
}
/// <summary>
/// Returns map of field name to <see cref="DimConfig"/>.
/// </summary>
public virtual IDictionary<string, DimConfig> DimConfigs
{
get
{
return fieldTypes;
}
}
private static void CheckSeen(HashSet<string> seenDims, string dim)
{
if (seenDims.Contains(dim))
{
throw new System.ArgumentException("dimension \"" + dim + "\" is not multiValued, but it appears more than once in this document");
}
seenDims.Add(dim);
}
/// <summary>
/// Translates any added <see cref="FacetField"/>s into normal fields for indexing;
/// only use this version if you did not add any taxonomy-based fields
/// (<see cref="FacetField"/> or <see cref="AssociationFacetField"/>).
///
/// <para>
/// <b>NOTE:</b> you should add the returned document to <see cref="Index.IndexWriter"/>, not the
/// input one!
/// </para>
/// </summary>
public virtual Document Build(Document doc)
{
return Build(null, doc);
}
/// <summary>
/// Translates any added <see cref="FacetField"/>s into normal fields for indexing.
///
/// <para>
/// <b>NOTE:</b> you should add the returned document to <see cref="Index.IndexWriter"/>, not the
/// input one!
/// </para>
/// </summary>
public virtual Document Build(ITaxonomyWriter taxoWriter, Document doc)
{
// Find all FacetFields, collated by the actual field:
IDictionary<string, IList<FacetField>> byField = new Dictionary<string, IList<FacetField>>();
// ... and also all SortedSetDocValuesFacetFields:
IDictionary<string, IList<SortedSetDocValuesFacetField>> dvByField = new Dictionary<string, IList<SortedSetDocValuesFacetField>>();
// ... and also all AssociationFacetFields
IDictionary<string, IList<AssociationFacetField>> assocByField = new Dictionary<string, IList<AssociationFacetField>>();
var seenDims = new HashSet<string>();
foreach (IIndexableField field in doc.Fields)
{
if (field.IndexableFieldType == FacetField.TYPE)
{
FacetField facetField = (FacetField)field;
FacetsConfig.DimConfig dimConfig = GetDimConfig(facetField.Dim);
if (dimConfig.IsMultiValued == false)
{
CheckSeen(seenDims, facetField.Dim);
}
string indexFieldName = dimConfig.IndexFieldName;
IList<FacetField> fields;
if (!byField.TryGetValue(indexFieldName, out fields))
{
fields = new List<FacetField>();
byField[indexFieldName] = fields;
}
fields.Add(facetField);
}
if (field.IndexableFieldType == SortedSetDocValuesFacetField.TYPE)
{
var facetField = (SortedSetDocValuesFacetField)field;
FacetsConfig.DimConfig dimConfig = GetDimConfig(facetField.Dim);
if (dimConfig.IsMultiValued == false)
{
CheckSeen(seenDims, facetField.Dim);
}
string indexFieldName = dimConfig.IndexFieldName;
IList<SortedSetDocValuesFacetField> fields;
if (!dvByField.TryGetValue(indexFieldName, out fields))
{
fields = new List<SortedSetDocValuesFacetField>();
dvByField[indexFieldName] = fields;
}
fields.Add(facetField);
}
if (field.IndexableFieldType == AssociationFacetField.TYPE)
{
AssociationFacetField facetField = (AssociationFacetField)field;
FacetsConfig.DimConfig dimConfig = GetDimConfig(facetField.Dim);
if (dimConfig.IsMultiValued == false)
{
CheckSeen(seenDims, facetField.Dim);
}
if (dimConfig.IsHierarchical)
{
throw new System.ArgumentException("AssociationFacetField cannot be hierarchical (dim=\"" + facetField.Dim + "\")");
}
if (dimConfig.RequireDimCount)
{
throw new System.ArgumentException("AssociationFacetField cannot requireDimCount (dim=\"" + facetField.Dim + "\")");
}
string indexFieldName = dimConfig.IndexFieldName;
IList<AssociationFacetField> fields;
if (!assocByField.TryGetValue(indexFieldName, out fields))
{
fields = new List<AssociationFacetField>();
assocByField[indexFieldName] = fields;
}
fields.Add(facetField);
// Best effort: detect mis-matched types in same
// indexed field:
string type;
if (facetField is Int32AssociationFacetField)
{
type = "int";
}
else if (facetField is SingleAssociationFacetField)
{
type = "float";
}
else
{
type = "bytes";
}
// NOTE: not thread safe, but this is just best effort:
string curType;
if (!assocDimTypes.TryGetValue(indexFieldName, out curType))
{
assocDimTypes[indexFieldName] = type;
}
else if (!curType.Equals(type, StringComparison.Ordinal))
{
throw new System.ArgumentException("mixing incompatible types of AssocationFacetField (" + curType + " and " + type + ") in indexed field \"" + indexFieldName + "\"; use FacetsConfig to change the indexFieldName for each dimension");
}
}
}
Document result = new Document();
ProcessFacetFields(taxoWriter, byField, result);
ProcessSSDVFacetFields(dvByField, result);
ProcessAssocFacetFields(taxoWriter, assocByField, result);
//System.out.println("add stored: " + addedStoredFields);
foreach (IIndexableField field in doc.Fields)
{
IIndexableFieldType ft = field.IndexableFieldType;
if (ft != FacetField.TYPE && ft != SortedSetDocValuesFacetField.TYPE && ft != AssociationFacetField.TYPE)
{
result.Add(field);
}
}
return result;
}
private void ProcessFacetFields(ITaxonomyWriter taxoWriter, IDictionary<string, IList<FacetField>> byField, Document doc)
{
foreach (KeyValuePair<string, IList<FacetField>> ent in byField)
{
string indexFieldName = ent.Key;
//System.out.println(" indexFieldName=" + indexFieldName + " fields=" + ent.getValue());
Int32sRef ordinals = new Int32sRef(32);
foreach (FacetField facetField in ent.Value)
{
FacetsConfig.DimConfig ft = GetDimConfig(facetField.Dim);
if (facetField.Path.Length > 1 && ft.IsHierarchical == false)
{
throw new System.ArgumentException("dimension \"" + facetField.Dim + "\" is not hierarchical yet has " + facetField.Path.Length + " components");
}
FacetLabel cp = new FacetLabel(facetField.Dim, facetField.Path);
CheckTaxoWriter(taxoWriter);
int ordinal = taxoWriter.AddCategory(cp);
if (ordinals.Length == ordinals.Int32s.Length)
{
ordinals.Grow(ordinals.Length + 1);
}
ordinals.Int32s[ordinals.Length++] = ordinal;
//System.out.println("ords[" + (ordinals.length-1) + "]=" + ordinal);
//System.out.println(" add cp=" + cp);
if (ft.IsMultiValued && (ft.IsHierarchical || ft.RequireDimCount))
{
//System.out.println(" add parents");
// Add all parents too:
int parent = taxoWriter.GetParent(ordinal);
while (parent > 0)
{
if (ordinals.Int32s.Length == ordinals.Length)
{
ordinals.Grow(ordinals.Length + 1);
}
ordinals.Int32s[ordinals.Length++] = parent;
parent = taxoWriter.GetParent(parent);
}
if (ft.RequireDimCount == false)
{
// Remove last (dimension) ord:
ordinals.Length--;
}
}
// Drill down:
for (int i = 1; i <= cp.Length; i++)
{
doc.Add(new StringField(indexFieldName, PathToString(cp.Components, i), Field.Store.NO));
}
}
// Facet counts:
// DocValues are considered stored fields:
doc.Add(new BinaryDocValuesField(indexFieldName, DedupAndEncode(ordinals)));
}
}
public void ProcessSSDVFacetFields(IDictionary<string, IList<SortedSetDocValuesFacetField>> byField, Document doc)
{
//System.out.println("process SSDV: " + byField);
foreach (KeyValuePair<string, IList<SortedSetDocValuesFacetField>> ent in byField)
{
string indexFieldName = ent.Key;
//System.out.println(" field=" + indexFieldName);
foreach (SortedSetDocValuesFacetField facetField in ent.Value)
{
FacetLabel cp = new FacetLabel(facetField.Dim, facetField.Label);
string fullPath = PathToString(cp.Components, cp.Length);
//System.out.println("add " + fullPath);
// For facet counts:
doc.Add(new SortedSetDocValuesField(indexFieldName, new BytesRef(fullPath)));
// For drill-down:
doc.Add(new StringField(indexFieldName, fullPath, Field.Store.NO));
doc.Add(new StringField(indexFieldName, facetField.Dim, Field.Store.NO));
}
}
}
private void ProcessAssocFacetFields(ITaxonomyWriter taxoWriter, IDictionary<string, IList<AssociationFacetField>> byField, Document doc)
{
foreach (KeyValuePair<string, IList<AssociationFacetField>> ent in byField)
{
byte[] bytes = new byte[16];
int upto = 0;
string indexFieldName = ent.Key;
foreach (AssociationFacetField field in ent.Value)
{
// NOTE: we don't add parents for associations
CheckTaxoWriter(taxoWriter);
FacetLabel label = new FacetLabel(field.Dim, field.Path);
int ordinal = taxoWriter.AddCategory(label);
if (upto + 4 > bytes.Length)
{
bytes = ArrayUtil.Grow(bytes, upto + 4);
}
// big-endian:
bytes[upto++] = (byte)(ordinal >> 24);
bytes[upto++] = (byte)(ordinal >> 16);
bytes[upto++] = (byte)(ordinal >> 8);
bytes[upto++] = (byte)ordinal;
if (upto + field.Assoc.Length > bytes.Length)
{
bytes = ArrayUtil.Grow(bytes, upto + field.Assoc.Length);
}
Array.Copy(field.Assoc.Bytes, field.Assoc.Offset, bytes, upto, field.Assoc.Length);
upto += field.Assoc.Length;
// Drill down:
for (int i = 1; i <= label.Length; i++)
{
doc.Add(new StringField(indexFieldName, PathToString(label.Components, i), Field.Store.NO));
}
}
doc.Add(new BinaryDocValuesField(indexFieldName, new BytesRef(bytes, 0, upto)));
}
}
/// <summary>
/// Encodes ordinals into a <see cref="BytesRef"/>; expert: subclass can
/// override this to change encoding.
/// </summary>
protected virtual BytesRef DedupAndEncode(Int32sRef ordinals)
{
Array.Sort(ordinals.Int32s, ordinals.Offset, ordinals.Length);
byte[] bytes = new byte[5 * ordinals.Length];
int lastOrd = -1;
int upto = 0;
for (int i = 0; i < ordinals.Length; i++)
{
int ord = ordinals.Int32s[ordinals.Offset + i];
// ord could be == lastOrd, so we must dedup:
if (ord > lastOrd)
{
int delta;
if (lastOrd == -1)
{
delta = ord;
}
else
{
delta = ord - lastOrd;
}
if ((delta & ~0x7F) == 0)
{
bytes[upto] = (byte)delta;
upto++;
}
else if ((delta & ~0x3FFF) == 0)
{
bytes[upto] = unchecked((byte)(0x80 | ((delta & 0x3F80) >> 7)));
bytes[upto + 1] = (byte)(delta & 0x7F);
upto += 2;
}
else if ((delta & ~0x1FFFFF) == 0)
{
bytes[upto] = unchecked((byte)(0x80 | ((delta & 0x1FC000) >> 14)));
bytes[upto + 1] = unchecked((byte)(0x80 | ((delta & 0x3F80) >> 7)));
bytes[upto + 2] = (byte)(delta & 0x7F);
upto += 3;
}
else if ((delta & ~0xFFFFFFF) == 0)
{
bytes[upto] = unchecked((byte)(0x80 | ((delta & 0xFE00000) >> 21)));
bytes[upto + 1] = unchecked((byte)(0x80 | ((delta & 0x1FC000) >> 14)));
bytes[upto + 2] = unchecked((byte)(0x80 | ((delta & 0x3F80) >> 7)));
bytes[upto + 3] = (byte)(delta & 0x7F);
upto += 4;
}
else
{
bytes[upto] = unchecked((byte)(0x80 | ((delta & 0xF0000000) >> 28)));
bytes[upto + 1] = unchecked((byte)(0x80 | ((delta & 0xFE00000) >> 21)));
bytes[upto + 2] = unchecked((byte)(0x80 | ((delta & 0x1FC000) >> 14)));
bytes[upto + 3] = unchecked((byte)(0x80 | ((delta & 0x3F80) >> 7)));
bytes[upto + 4] = (byte)(delta & 0x7F);
upto += 5;
}
lastOrd = ord;
}
}
return new BytesRef(bytes, 0, upto);
}
private void CheckTaxoWriter(ITaxonomyWriter taxoWriter)
{
if (taxoWriter == null)
{
throw new ThreadStateException("a non-null ITaxonomyWriter must be provided when indexing FacetField or AssociationFacetField");
}
}
// Joins the path components together:
private const char DELIM_CHAR = '\u001F';
// Escapes any occurrence of the path component inside the label:
private const char ESCAPE_CHAR = '\u001E';
/// <summary>
/// Turns a dim + path into an encoded string.
/// </summary>
public static string PathToString(string dim, string[] path)
{
string[] fullPath = new string[1 + path.Length];
fullPath[0] = dim;
Array.Copy(path, 0, fullPath, 1, path.Length);
return PathToString(fullPath, fullPath.Length);
}
/// <summary>
/// Turns a dim + path into an encoded string.
/// </summary>
public static string PathToString(string[] path)
{
return PathToString(path, path.Length);
}
/// <summary>
/// Turns the first <paramref name="length"/> elements of <paramref name="path"/>
/// into an encoded string.
/// </summary>
public static string PathToString(string[] path, int length)
{
if (length == 0)
{
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
string s = path[i];
if (s.Length == 0)
{
throw new System.ArgumentException("each path component must have length > 0 (got: \"\")");
}
int numChars = s.Length;
for (int j = 0; j < numChars; j++)
{
char ch = s[j];
if (ch == DELIM_CHAR || ch == ESCAPE_CHAR)
{
sb.Append(ESCAPE_CHAR);
}
sb.Append(ch);
}
sb.Append(DELIM_CHAR);
}
// Trim off last DELIM_CHAR:
sb.Length = sb.Length - 1;
return sb.ToString();
}
/// <summary>
/// Turns an encoded string (from a previous call to <see cref="PathToString(string[])"/>)
/// back into the original <see cref="T:string[]"/>.
/// </summary>
public static string[] StringToPath(string s)
{
IList<string> parts = new List<string>();
int length = s.Length;
if (length == 0)
{
return new string[0];
}
char[] buffer = new char[length];
int upto = 0;
bool lastEscape = false;
for (int i = 0; i < length; i++)
{
char ch = s[i];
if (lastEscape)
{
buffer[upto++] = ch;
lastEscape = false;
}
else if (ch == ESCAPE_CHAR)
{
lastEscape = true;
}
else if (ch == DELIM_CHAR)
{
parts.Add(new string(buffer, 0, upto));
upto = 0;
}
else
{
buffer[upto++] = ch;
}
}
parts.Add(new string(buffer, 0, upto));
Debug.Assert(!lastEscape);
return parts.ToArray();
}
}
}