-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
TextLoaderParser.cs
1378 lines (1194 loc) · 55.9 KB
/
TextLoaderParser.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using Microsoft.ML.Data.Conversion;
using Microsoft.ML.Internal.Utilities;
using Microsoft.ML.Runtime;
namespace Microsoft.ML.Data
{
using Conditional = System.Diagnostics.ConditionalAttribute;
public sealed partial class TextLoader
{
/// <summary>
/// This type exists to provide efficient delegates for creating a ColumnValue specific to a DataKind.
/// </summary>
private sealed class ValueCreatorCache
{
private static readonly FuncInstanceMethodInfo1<ValueCreatorCache, PrimitiveDataViewType, Func<RowSet, ColumnPipe>> _getCreatorOneCoreMethodInfo
= FuncInstanceMethodInfo1<ValueCreatorCache, PrimitiveDataViewType, Func<RowSet, ColumnPipe>>.Create(target => target.GetCreatorOneCore<int>);
private static readonly FuncInstanceMethodInfo1<ValueCreatorCache, PrimitiveDataViewType, Func<RowSet, ColumnPipe>> _getCreatorVecCoreMethodInfo
= FuncInstanceMethodInfo1<ValueCreatorCache, PrimitiveDataViewType, Func<RowSet, ColumnPipe>>.Create(target => target.GetCreatorVecCore<int>);
private static volatile ValueCreatorCache _instance;
public static ValueCreatorCache Instance
{
get
{
return _instance ??
Interlocked.CompareExchange(ref _instance, new ValueCreatorCache(), null) ??
_instance;
}
}
private readonly Conversions _conv;
// Indexed by DataKind.ToIndex()
private readonly Func<RowSet, ColumnPipe>[] _creatorsOne;
private readonly Func<RowSet, ColumnPipe>[] _creatorsVec;
private ValueCreatorCache()
{
_conv = Conversions.Instance;
_creatorsOne = new Func<RowSet, ColumnPipe>[InternalDataKindExtensions.KindCount];
_creatorsVec = new Func<RowSet, ColumnPipe>[InternalDataKindExtensions.KindCount];
for (var kind = InternalDataKindExtensions.KindMin; kind < InternalDataKindExtensions.KindLim; kind++)
{
var type = ColumnTypeExtensions.PrimitiveTypeFromKind(kind);
_creatorsOne[kind.ToIndex()] = GetCreatorOneCore(type);
_creatorsVec[kind.ToIndex()] = GetCreatorVecCore(type);
}
}
private Func<RowSet, ColumnPipe> GetCreatorOneCore(PrimitiveDataViewType type)
{
return Utils.MarshalInvoke(_getCreatorOneCoreMethodInfo, this, type.RawType, type);
}
private Func<RowSet, ColumnPipe> GetCreatorOneCore<T>(PrimitiveDataViewType type)
{
Contracts.Assert(type.IsStandardScalar() || type is KeyDataViewType);
Contracts.Assert(typeof(T) == type.RawType);
var fn = _conv.GetTryParseConversion<T>(type);
return rows => new PrimitivePipe<T>(rows, type, fn);
}
private Func<RowSet, ColumnPipe> GetCreatorVecCore(PrimitiveDataViewType type)
{
return Utils.MarshalInvoke(_getCreatorVecCoreMethodInfo, this, type.RawType, type);
}
private Func<RowSet, ColumnPipe> GetCreatorVecCore<T>(PrimitiveDataViewType type)
{
Contracts.Assert(type.IsStandardScalar() || type is KeyDataViewType);
Contracts.Assert(typeof(T) == type.RawType);
var fn = _conv.GetTryParseConversion<T>(type);
return rows => new VectorPipe<T>(rows, type, fn);
}
public Func<RowSet, ColumnPipe> GetCreatorOne(KeyDataViewType key)
{
// Have to produce a specific one - can't use a cached one.
return Utils.MarshalInvoke(_getCreatorOneCoreMethodInfo, this, key.RawType, key);
}
public Func<RowSet, ColumnPipe> GetCreatorVec(KeyDataViewType key)
{
// Have to produce a specific one - can't use a cached one.
return Utils.MarshalInvoke(_getCreatorVecCoreMethodInfo, this, key.RawType, key);
}
public Func<RowSet, ColumnPipe> GetCreatorOne(InternalDataKind kind)
{
int index = kind.ToIndex();
Contracts.Assert(0 <= index & index < _creatorsOne.Length);
return _creatorsOne[index];
}
public Func<RowSet, ColumnPipe> GetCreatorVec(InternalDataKind kind)
{
int index = kind.ToIndex();
Contracts.Assert(0 <= index & index < _creatorsOne.Length);
return _creatorsVec[index];
}
}
/// <summary>
/// Basic statistics and reporting of unparsable stuff.
/// </summary>
private sealed class ParseStats
{
// Maximum number of messages to show.
private const long MaxShow = 10;
private readonly long _maxShow;
// The channel to report messages on.
private readonly IChannel _ch;
// Reference count.
private volatile int _cref;
// Total number of rows, number of unparsable values, number of format errors.
private long _rowCount;
private long _badCount;
private long _fmtCount;
public ParseStats(IChannelProvider provider, int cref, long maxShow = MaxShow)
{
Contracts.CheckValue(provider, nameof(provider));
_ch = provider.Start("ParseStats");
_ch.Assert(cref > 0);
_cref = cref;
_maxShow = maxShow;
}
public void Release()
{
int n = Interlocked.Decrement(ref _cref);
_ch.Assert(n >= 0);
if (n == 0)
{
if (_badCount > 0 || _fmtCount > 0)
{
_ch.Info("Processed {0} rows with {1} bad values and {2} format errors",
_rowCount, _badCount, _fmtCount);
}
_ch.Dispose();
}
}
public void LogRow()
{
Interlocked.Increment(ref _rowCount);
}
public void LogBadValue(long line, string colName, int slot)
{
long n = Interlocked.Increment(ref _badCount);
if (n <= _maxShow)
{
_ch.Info(MessageSensitivity.Schema, " Bad value at line {0} in column {1} at slot {2}", line, colName, slot);
if (n == _maxShow)
_ch.Info(" Suppressing further bad value messages");
}
}
public void LogBadValue(long line, string colName)
{
long n = Interlocked.Increment(ref _badCount);
if (n <= _maxShow)
{
_ch.Info(MessageSensitivity.Schema, " Bad value at line {0} in column {1}", line, colName);
if (n == _maxShow)
_ch.Info(" Suppressing further bad value messages");
}
}
public void LogBadFmt(ref ScanInfo scan, string msg)
{
long n = Interlocked.Increment(ref _fmtCount);
if (n <= _maxShow)
{
if (scan.Line > 0)
{
// The -1 is so the indices are 1-based instead of 0-based.
int ichBase = scan.IchMinBuf - 1;
_ch.Warning("Format error at {0}({1},{2})-({1},{3}): {4}",
scan.Path, scan.Line, scan.IchMin - ichBase, scan.IchLim - ichBase, msg);
}
else
_ch.Warning("Format error: {0}", msg);
if (n == _maxShow)
_ch.Warning("Suppressing further format error messages");
}
}
}
private abstract class ColumnPipe
{
public readonly RowSet Rows;
public abstract bool HasNA { get; }
protected ColumnPipe(RowSet rows)
{
Contracts.AssertValue(rows);
Rows = rows;
}
public abstract void Reset(int irow, int size);
// Passed by-ref for efficiency, not so it can be modified.
public abstract bool Consume(int irow, int index, ref ReadOnlyMemory<char> text);
public abstract Delegate GetGetter();
}
private sealed class PrimitivePipe<TResult> : ColumnPipe
{
private readonly TryParseMapper<TResult> _conv;
// Has length Rows.Count, so indexed by irow.
private TResult[] _values;
public override bool HasNA { get; }
public PrimitivePipe(RowSet rows, PrimitiveDataViewType type, TryParseMapper<TResult> conv)
: base(rows)
{
Contracts.AssertValue(conv);
Contracts.Assert(typeof(TResult) == type.RawType);
_conv = conv;
_values = new TResult[Rows.Count];
HasNA = Conversions.Instance.TryGetIsNAPredicate(type, out var del);
}
public override void Reset(int irow, int size)
{
Contracts.Assert(0 <= irow && irow < _values.Length);
Contracts.Assert(size == 0);
_values[irow] = default(TResult);
}
public override bool Consume(int irow, int index, ref ReadOnlyMemory<char> text)
{
Contracts.Assert(0 <= irow && irow < _values.Length);
Contracts.Assert(index == 0);
return _conv(in text, out _values[irow]);
}
public void Get(ref TResult value)
{
int index = Rows.Index;
Contracts.Assert(-1 <= index && index < Rows.Count);
Contracts.Check(index >= 0);
value = _values[index];
}
public override Delegate GetGetter()
{
return (ValueGetter<TResult>)Get;
}
}
private sealed class VectorPipe<TItem> : ColumnPipe
{
private readonly TryParseMapper<TItem> _conv;
public override bool HasNA { get; }
private class VectorValue
{
private readonly VectorPipe<TItem> _pipe;
private readonly TryParseMapper<TItem> _conv;
// We don't need the full power of the BufferBuilder stuff. We always record things
// in index order, and never have to combine values.
private int _size;
private int _count;
private int _indexPrev;
private TItem[] _values;
private int[] _indices;
public VectorValue(VectorPipe<TItem> pipe)
{
_pipe = pipe;
_conv = pipe._conv;
_values = new TItem[4];
_indices = new int[4];
}
[Conditional("DEBUG")]
public void AssertValid()
{
if (_size == 0)
return;
Contracts.Assert(_size > 0);
Contracts.Assert(-1 <= _indexPrev);
Contracts.Assert(_indexPrev < _size);
Contracts.Assert(0 <= _count);
Contracts.Assert(_count <= _size);
Contracts.Assert(_count <= _values.Length);
if (_count < _size)
{
// We're sparse, so there should not be more than _size/2 items and indices should
// be big enough.
Contracts.Assert(_count <= _size / 2);
Contracts.Assert(_count <= _indices.Length);
}
}
public void Reset(int size)
{
Contracts.Assert(size >= 0);
_size = size;
_count = 0;
_indexPrev = -1;
AssertValid();
}
public bool Consume(int index, ref ReadOnlyMemory<char> text)
{
AssertValid();
Contracts.Assert(_indexPrev < index & index < _size);
TItem tmp = default(TItem);
bool f = _conv(in text, out tmp);
if (_count < _size)
{
if (_count < _size / 2)
{
// Stay sparse.
if (_values.Length <= _count)
Array.Resize(ref _values, 2 * _count);
if (_indices.Length <= _count)
Array.Resize(ref _indices, 2 * _count);
_values[_count] = tmp;
_indices[_count] = index;
_count++;
AssertValid();
return f;
}
// Convert to dense.
if (_values.Length >= _size)
Array.Clear(_values, _count, _size - _count);
else
{
if (_values.Length > _count)
Array.Clear(_values, _count, _values.Length - _count);
Array.Resize(ref _values, _size);
}
for (int ii = _count; --ii >= 0; )
{
int i = _indices[ii];
Contracts.Assert(ii <= i);
// If ii == i then we have every slot covered below this.
if (ii >= i)
break;
// Must fill vacated slots with default(TItem).
_values[i] = _values[ii];
_values[ii] = default(TItem);
}
_count = _size;
AssertValid();
}
Contracts.Assert(_count == _size);
_values[index] = tmp;
AssertValid();
return f;
}
public void Get(ref VBuffer<TItem> dst)
{
AssertValid();
if (_count == 0)
{
VBufferUtils.Resize(ref dst, _size, 0);
return;
}
var editor = VBufferEditor.Create(ref dst, _size, _count);
_values.AsSpan(0, _count).CopyTo(editor.Values);
if (_count == _size)
{
dst = editor.Commit();
return;
}
_indices.AsSpan(0, _count).CopyTo(editor.Indices);
dst = editor.Commit();
}
}
// Has length Rows.Count, so indexed by irow.
private VectorValue[] _values;
public VectorPipe(RowSet rows, PrimitiveDataViewType type, TryParseMapper<TItem> conv)
: base(rows)
{
Contracts.AssertValue(conv);
Contracts.Assert(typeof(TItem) == type.RawType);
_conv = conv;
_values = new VectorValue[Rows.Count];
for (int i = 0; i < _values.Length; i++)
_values[i] = new VectorValue(this);
HasNA = Conversions.Instance.TryGetIsNAPredicate(type, out var del);
}
public override void Reset(int irow, int size)
{
Contracts.Assert(0 <= irow && irow < _values.Length);
Contracts.Assert(size >= 0);
_values[irow].Reset(size);
}
public override bool Consume(int irow, int index, ref ReadOnlyMemory<char> text)
{
Contracts.Assert(0 <= irow && irow < _values.Length);
return _values[irow].Consume(index, ref text);
}
public void Get(ref VBuffer<TItem> dst)
{
int index = Rows.Index;
Contracts.Assert(-1 <= index && index < Rows.Count);
Contracts.Check(index >= 0);
_values[index].Get(ref dst);
}
public override Delegate GetGetter()
{
return (ValueGetter<VBuffer<TItem>>)Get;
}
}
private sealed class RowSet
{
// The associated parse statistics object. Note that multiple RowSets can share the
// same stats object.
public readonly ParseStats Stats;
// The total number of rows in this row set.
public readonly int Count;
// The pipes - one per column. Inactive columns have a null entry.
public readonly ColumnPipe[] Pipes;
// Current row index being yielded. Only assigned or read on the main
// cursor thread (assuming clients don't call the getters from other threads).
public int Index;
/// <summary>
/// Takes the number of blocks, number of rows per block, and number of columns.
/// </summary>
public RowSet(ParseStats stats, int count, int ccol)
{
Contracts.AssertValue(stats);
Contracts.Assert(count > 0);
Stats = stats;
Count = count;
Pipes = new ColumnPipe[ccol];
Index = -1;
}
}
/// <summary>
/// This is info tracked while scanning a line to find "fields". For each line, the first
/// several values, Path, Line, LineText, IchMinText, and IchLimText, are unchanging, but the
/// remaining values are updated for each field processed.
/// </summary>
private struct ScanInfo
{
/// <summary>
/// Path for the input file containing the given line (may be null).
/// </summary>
public readonly string Path;
/// <summary>
/// Line number.
/// </summary>
public readonly long Line;
/// <summary>
/// The current text for the entire line (all fields), and possibly more.
/// </summary>
public ReadOnlyMemory<char> TextBuf;
/// <summary>
/// The min position in <see cref="TextBuf"/> to consider (all fields).
/// </summary>
public readonly int IchMinBuf;
/// <summary>
/// The lim position in <see cref="TextBuf"/> to consider (all fields).
/// </summary>
public readonly int IchLimBuf;
/// <summary>
/// Where to start for the next field. This is both an input and
/// output to the code that fetches the next field.
/// </summary>
public int IchMinNext;
/// <summary>
/// The (unquoted) text of the field.
/// </summary>
public ReadOnlyMemory<char> Span;
/// <summary>
/// Whether there was a quoting error in the field.
/// </summary>
public bool QuotingError;
/// <summary>
/// For sparse encoding, this is the index of the field. Otherwise, -1.
/// </summary>
public int Index;
/// <summary>
/// The start character location in <see cref="TextBuf"/>, including the sparse index
/// and quoting, if present. Used for logging.
/// </summary>
public int IchMin;
/// <summary>
/// The end character location in <see cref="TextBuf"/>, including the sparse index
/// and quoting, if present. Used for logging.
/// </summary>
public int IchLim;
/// <summary>
/// Initializes the ScanInfo.
/// </summary>
public ScanInfo(ref ReadOnlyMemory<char> text, string path, long line)
: this()
{
Contracts.AssertValueOrNull(path);
Contracts.Assert(line >= 0);
Path = path;
Line = line;
TextBuf = text;
IchMinBuf = 0;
IchLimBuf = text.Length;
IchMinNext = IchMinBuf;
}
}
private sealed class Parser
{
/// <summary>
/// This holds a set of raw text fields. This is the input into the parsing
/// of the individual typed values.
/// </summary>
private sealed class FieldSet
{
public int Count;
// Source indices and associated text (parallel arrays).
public int[] Indices;
public ReadOnlyMemory<char>[] Spans;
public FieldSet()
{
// Always allocate/size Columns after Spans so even if exceptions are thrown we
// are guaranteed that Spans.Length >= Columns.Length.
Spans = new ReadOnlyMemory<char>[8];
Indices = new int[8];
}
[Conditional("DEBUG")]
public void AssertValid()
{
Contracts.AssertValue(Spans);
Contracts.AssertValue(Indices);
Contracts.Assert(0 <= Count & Count <= Indices.Length & Indices.Length <= Spans.Length);
}
[Conditional("DEBUG")]
public void AssertEmpty()
{
Contracts.AssertValue(Spans);
Contracts.AssertValue(Indices);
Contracts.Assert(Count == 0);
}
/// <summary>
/// Make sure there is enough space to add one more item.
/// </summary>
public void EnsureSpace()
{
AssertValid();
if (Count >= Indices.Length)
{
int size = 2 * Count;
if (Spans.Length < size)
Array.Resize(ref Spans, size);
Array.Resize(ref Indices, size);
}
AssertValid();
}
public void Clear()
{
AssertValid();
Array.Clear(Spans, 0, Count);
Count = 0;
AssertEmpty();
}
}
private readonly char[] _separators;
private readonly OptionFlags _flags;
private readonly int _inputSize;
private readonly ColInfo[] _infos;
// These delegates are used to construct new row objects.
private readonly Func<RowSet, ColumnPipe>[] _creator;
private volatile int _csrc;
private volatile int _mismatchCount;
public Parser(TextLoader parent)
{
Contracts.AssertValue(parent);
_infos = parent._bindings.Infos;
_creator = new Func<RowSet, ColumnPipe>[_infos.Length];
var cache = ValueCreatorCache.Instance;
var mapOne = new Dictionary<InternalDataKind, Func<RowSet, ColumnPipe>>();
var mapVec = new Dictionary<InternalDataKind, Func<RowSet, ColumnPipe>>();
for (int i = 0; i < _creator.Length; i++)
{
var info = _infos[i];
if (info.ColType is KeyDataViewType keyType)
{
_creator[i] = cache.GetCreatorOne(keyType);
continue;
}
VectorDataViewType vectorType = info.ColType as VectorDataViewType;
if (vectorType?.ItemType is KeyDataViewType vectorKeyType)
{
_creator[i] = cache.GetCreatorVec(vectorKeyType);
continue;
}
DataViewType itemType = vectorType?.ItemType ?? info.ColType;
Contracts.Assert(itemType is KeyDataViewType || itemType.IsStandardScalar());
var map = vectorType != null ? mapVec : mapOne;
if (!map.TryGetValue(info.Kind, out _creator[i]))
{
var fn = vectorType != null ?
cache.GetCreatorVec(info.Kind) :
cache.GetCreatorOne(info.Kind);
map.Add(info.Kind, fn);
_creator[i] = fn;
}
}
_separators = parent._separators;
_flags = parent._flags;
_inputSize = parent._inputSize;
Contracts.Assert(_inputSize >= 0);
}
public static void GetInputSize(TextLoader parent, List<ReadOnlyMemory<char>> lines, out int minSize, out int maxSize)
{
Contracts.AssertNonEmpty(lines);
Contracts.Assert(parent._inputSize == 0, "Why is this being called when inputSize is known?");
minSize = int.MaxValue;
maxSize = 0;
var stats = new ParseStats(parent._host, cref: 1, maxShow: 0);
var impl = new HelperImpl(stats, parent._flags, parent._separators, 0, int.MaxValue);
try
{
foreach (var line in lines)
{
var text = (parent._flags & OptionFlags.TrimWhitespace) != 0 ? ReadOnlyMemoryUtils.TrimEndWhiteSpace(line) : line;
if (text.IsEmpty)
continue;
// REVIEW: This is doing more work than we need, but makes sure we're consistent....
int srcLim = impl.GatherFields(text, text.Span);
// Don't need the fields, just srcLim.
impl.Fields.Clear();
if (srcLim == 0)
continue;
if (minSize > srcLim)
minSize = srcLim;
if (maxSize < srcLim)
maxSize = srcLim;
}
}
finally
{
stats.Release();
}
}
public static void ParseSlotNames(TextLoader parent, ReadOnlyMemory<char> textHeader, ColInfo[] infos, VBuffer<ReadOnlyMemory<char>>[] slotNames)
{
Contracts.Assert(!textHeader.IsEmpty);
Contracts.Assert(infos.Length == slotNames.Length);
var sb = new StringBuilder();
var stats = new ParseStats(parent._host, cref: 1, maxShow: 0);
var impl = new HelperImpl(stats, parent._flags, parent._separators, parent._inputSize, int.MaxValue);
try
{
impl.GatherFields(textHeader, textHeader.Span);
}
finally
{
stats.Release();
}
var header = impl.Fields;
var bldr = BufferBuilder<ReadOnlyMemory<char>>.CreateDefault();
for (int iinfo = 0; iinfo < infos.Length; iinfo++)
{
var info = infos[iinfo];
if (!info.ColType.IsKnownSizeVector())
continue;
bldr.Reset(info.SizeBase, false);
int ivDst = 0;
// The following code is similar to the code in ProcessVec.
for (int i = 0; i < info.Segments.Length; i++)
{
var seg = info.Segments[i];
Contracts.Assert(!seg.IsVariable);
int min = seg.Min;
int lim = seg.Lim;
int sizeSeg = lim - min;
Contracts.Assert(ivDst <= info.SizeBase - sizeSeg);
int isrc = header.Indices.FindIndexSorted(0, header.Count, min);
if (isrc < header.Count && header.Indices[isrc] < lim)
{
int indexBase = ivDst - min;
int isrcLim = header.Indices.FindIndexSorted(isrc, header.Count, lim);
Contracts.Assert(isrc < isrcLim);
for (; isrc < isrcLim; isrc++)
{
var srcCur = header.Indices[isrc];
Contracts.Assert(min <= srcCur & srcCur < lim);
bldr.AddFeature(indexBase + srcCur, ReadOnlyMemoryUtils.TrimWhiteSpace(header.Spans[isrc]));
}
}
ivDst += sizeSeg;
}
Contracts.Assert(ivDst == info.SizeBase);
if (!bldr.IsEmpty)
bldr.GetResult(ref slotNames[iinfo]);
}
}
public RowSet CreateRowSet(ParseStats stats, int count, bool[] active)
{
Contracts.Assert(active == null || active.Length == _creator.Length);
RowSet rows = new RowSet(stats, count, _creator.Length);
for (int i = 0; i < rows.Pipes.Length; i++)
{
if (active == null || active[i])
rows.Pipes[i] = _creator[i](rows);
}
return rows;
}
/// <summary>
/// Returns a <see cref="ReadOnlyMemory{T}"/> of <see cref="char"/> with trailing whitespace trimmed.
/// </summary>
private ReadOnlyMemory<char> TrimEndWhiteSpace(ReadOnlyMemory<char> memory, ReadOnlySpan<char> span)
{
if (memory.IsEmpty)
return memory;
int ichLim = memory.Length;
if (!char.IsWhiteSpace(span[ichLim - 1]))
return memory;
while (0 < ichLim && char.IsWhiteSpace(span[ichLim - 1]))
ichLim--;
return memory.Slice(0, ichLim);
}
public void ParseRow(RowSet rows, int irow, Helper helper, bool[] active, string path, long line, string text)
{
Contracts.AssertValue(rows);
Contracts.Assert(irow >= 0);
Contracts.Assert(helper is HelperImpl);
Contracts.Assert(active == null | Utils.Size(active) == _infos.Length);
var impl = (HelperImpl)helper;
var lineSpan = text.AsMemory();
var span = lineSpan.Span;
if ((_flags & OptionFlags.TrimWhitespace) != 0)
lineSpan = TrimEndWhiteSpace(lineSpan, span);
try
{
// Parse the spans into items, ensuring that sparse don't precede non-sparse.
int srcLim = impl.GatherFields(lineSpan, span, path, line);
impl.Fields.AssertValid();
// REVIEW: When should we report inconsistency?
// VerifyColumnCount(srcLim);
ProcessItems(rows, irow, active, impl.Fields, srcLim, line);
rows.Stats.LogRow();
}
finally
{
impl.Fields.Clear();
}
}
public Helper CreateHelper(ParseStats stats, int srcNeeded)
{
Contracts.AssertValue(stats);
Contracts.Assert(srcNeeded >= 0);
return new HelperImpl(stats, _flags, _separators, _inputSize, srcNeeded);
}
/// <summary>
/// This is an abstraction containing all the useful stuff for splitting a raw line of text
/// into a FieldSet. A cursor has one of these that it passes in whenever it wants a line
/// parsed.
/// </summary>
public abstract class Helper
{
}
private sealed class HelperImpl : Helper
{
private readonly ParseStats _stats;
private readonly char[] _seps;
private readonly char _sep0;
private readonly char _sep1;
private readonly bool _sepContainsSpace;
private readonly int _inputSize;
private readonly int _srcNeeded;
private readonly bool _quoting;
private readonly bool _sparse;
// This is a working buffer.
private readonly StringBuilder _sb;
// Result of a blank field - either Missing or Empty, depending on _quoting.
private readonly ReadOnlyMemory<char> _blank;
public readonly FieldSet Fields;
public HelperImpl(ParseStats stats, OptionFlags flags, char[] seps, int inputSize, int srcNeeded)
{
Contracts.AssertValue(stats);
// inputSize == 0 means unknown.
Contracts.Assert(inputSize >= 0);
Contracts.Assert(srcNeeded >= 0);
Contracts.Assert(inputSize == 0 || srcNeeded < inputSize);
Contracts.AssertNonEmpty(seps);
_stats = stats;
_seps = seps;
_sep0 = _seps[0];
_sep1 = _seps.Length > 1 ? _seps[1] : '\0';
_sepContainsSpace = IsSep(' ');
_inputSize = inputSize;
_srcNeeded = srcNeeded;
_quoting = (flags & OptionFlags.AllowQuoting) != 0;
_sparse = (flags & OptionFlags.AllowSparse) != 0;
_sb = new StringBuilder();
_blank = ReadOnlyMemory<char>.Empty;
Fields = new FieldSet();
}
/// <summary>
/// Check if the given char is a separator.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsSep(char ch)
{
if (ch == _sep0)
return true;
for (int i = 1; i < _seps.Length; i++)
{
if (ch == _seps[i])
return true;
}
return false;
}
/// <summary>
/// Process the line of text into fields, stored in the Fields field. Ensures that sparse
/// don't precede non-sparse. Returns the lim of the src columns.
/// </summary>
public int GatherFields(ReadOnlyMemory<char> lineSpan, ReadOnlySpan<char> span, string path = null, long line = 0)
{
Fields.AssertEmpty();
ScanInfo scan = new ScanInfo(ref lineSpan, path, line);
Contracts.Assert(scan.IchMinBuf <= scan.IchMinNext && scan.IchMinNext <= scan.IchLimBuf);
int src = 0;
if (!_sparse)
{
for (; ; )
{
Contracts.Assert(scan.IchMinBuf <= scan.IchMinNext && scan.IchMinNext <= scan.IchLimBuf);
bool more = FetchNextField(ref scan, span);
Contracts.Assert(scan.IchMinBuf <= scan.IchMinNext && scan.IchMinNext <= scan.IchLimBuf);
Contracts.Assert(scan.Index == -1);
if (scan.QuotingError)
_stats.LogBadFmt(ref scan, "Illegal quoting");
if (!scan.Span.IsEmpty)
{
Fields.EnsureSpace();
Fields.Spans[Fields.Count] = scan.Span;
Fields.Indices[Fields.Count++] = src;
}
if (++src > _srcNeeded || !more)
break;
}
return src;
}
// Allow sparse. inputSize == 0 means the text specifies the maxDim value.
// Note that we always go one past srcNeeded in case what we think is srcNeeded is
// actually the sparse length value.
int csrcSparse = -1;
int indexPrev = -1;
int srcLimFixed = -1;
int inputSize = _inputSize;
int srcNeeded = _srcNeeded;
for (; ; )
{
Contracts.Assert(scan.IchMinBuf <= scan.IchMinNext && scan.IchMinNext <= scan.IchLimBuf);
bool more = FetchNextField(ref scan, span);
Contracts.Assert(scan.IchMinBuf <= scan.IchMinNext && scan.IchMinNext <= scan.IchLimBuf);
Contracts.Assert(scan.Index >= -1);
if (scan.QuotingError)
_stats.LogBadFmt(ref scan, "Illegal quoting");
if (scan.Index < 0)
{
// Not sparse.
if (srcLimFixed >= 0)
{
_stats.LogBadFmt(ref scan, "Non-sparse formatted value follows sparse formatted value");
break;
}
if (!scan.Span.IsEmpty)
{
Fields.EnsureSpace();
Fields.Spans[Fields.Count] = scan.Span;
Fields.Indices[Fields.Count++] = src;
}
if (src++ > srcNeeded || !more)
break;
continue;
}
if (srcLimFixed < 0)
{
// First sparse item.
Contracts.Assert(src - 1 <= srcNeeded);
if (inputSize == 0)