-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathio.go
More file actions
917 lines (817 loc) · 20.5 KB
/
io.go
File metadata and controls
917 lines (817 loc) · 20.5 KB
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
package main
import (
"bufio"
"compress/gzip"
"encoding/csv"
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"time"
)
// progressTracker helps display loading progress
type progressTracker struct {
total int64
current int64
lastUpdate time.Time
updateEvery int
lineCount int
showProgress bool
startTime time.Time
}
func newProgressTracker(total int64, showProgress bool) *progressTracker {
return &progressTracker{
total: total,
current: 0,
lastUpdate: time.Now(),
updateEvery: 5000, // update every 5000 lines
lineCount: 0,
showProgress: showProgress,
startTime: time.Now(),
}
}
func (p *progressTracker) increment(bytes int64) {
if !p.showProgress {
return
}
p.current += bytes
p.lineCount++
// Update display every N lines OR every 0.5 seconds (whichever comes first)
if p.lineCount%p.updateEvery == 0 || time.Since(p.lastUpdate) > 500*time.Millisecond {
p.display()
p.lastUpdate = time.Now()
}
}
func (p *progressTracker) display() {
if !p.showProgress {
return
}
elapsed := time.Since(p.startTime).Seconds()
if elapsed == 0 {
elapsed = 0.001 // avoid division by zero
}
linesPerSec := float64(p.lineCount) / elapsed
if p.total > 0 {
percent := float64(p.current) * 100.0 / float64(p.total)
if percent > 100 {
percent = 100
}
progressBar := makeProgressBar(percent, 20)
fmt.Printf("\r\033[K📊 Loading: %s | %d lines | %.0f lines/sec", progressBar, p.lineCount, linesPerSec)
} else {
// For pipes or when size is unknown
fmt.Printf("\r\033[K📊 Loading: %d lines | %.0f lines/sec", p.lineCount, linesPerSec)
}
}
func (p *progressTracker) finish() {
if !p.showProgress {
return
}
elapsed := time.Since(p.startTime).Seconds()
if elapsed == 0 {
elapsed = 0.001
}
linesPerSec := float64(p.lineCount) / elapsed
// Clear the progress line and show final summary
fmt.Printf("\r\033[K✓ Loaded %d lines in %.2fs (%.0f lines/sec)\n", p.lineCount, elapsed, linesPerSec)
}
// ParsedLine represents a parsed CSV line with its order
type ParsedLine struct {
Index int
Fields []string
Bytes int64
Err error
}
// load file content to buffer (async version with concurrent parsing)
func loadFileToBufferAsync(fn string, b *Buffer, updateChan chan<- bool, doneChan chan<- error) {
totalAddedLN := 0 //the number of lines has been added into buffer
// Get file size for progress tracking
fileInfo, err := os.Stat(fn)
if err != nil {
doneChan <- err
return
}
var fileSize int64
if !fileInfo.IsDir() {
fileSize = fileInfo.Size()
}
// Initialize load progress
loadProgress.TotalBytes = fileSize
loadProgress.LoadedBytes = 0
loadProgress.IsComplete = false
// Create progress tracker (disabled for async loading since UI will show it)
progress := newProgressTracker(fileSize, false)
scanner, err := getFileScanner(fn)
if err != nil {
doneChan <- err
return
}
scanner.Split(bufio.ScanLines)
//set separator, if user does not provide it.
var detectLines []string //lines as detect separator data
if b.sep == 0 {
//read 10 lines to detect separator
lineNumber := 10
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
detectLines = append(detectLines, line)
if len(detectLines) >= lineNumber {
break
}
}
//if the suffix of file name is ".csv", set separator to ",".
//if the suffix of file name is "tsv", set separator to "\t".
if strings.HasSuffix(fn, ".csv") {
b.sep = ','
} else if strings.HasSuffix(fn, ".tsv") {
b.sep = '\t'
} else {
sd := sepDetecor{}
b.sep = sd.sepDetect(detectLines)
}
}
//check final separator
if b.sep == 0 {
doneChan <- errors.New("tv can't identify separator, you need to set it manual")
return
}
//add detectLines to buffer
for _, line := range detectLines {
//parse and add line to buffer
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
progress.finish()
doneChan <- err
return
}
totalAddedLN++
bytesRead := int64(len(line) + 1) // +1 for newline
loadProgress.LoadedBytes += bytesRead
progress.increment(bytesRead)
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
}
// Signal that initial data is ready for rendering
updateChan <- true
// === CONCURRENT PARSING PIPELINE ===
// Use worker pool for parallel CSV parsing
numWorkers := runtime.NumCPU() // Use all available CPU cores
if numWorkers > 8 {
numWorkers = 8 // Cap at 8 workers for optimal performance
}
lineChan := make(chan string, numWorkers*10) // Input: raw lines
resultChan := make(chan *ParsedLine, numWorkers*10) // Output: parsed lines
// Start worker goroutines for parsing
var wg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for line := range lineChan {
fields, err := lineCSVParseFast(line, b.sep)
result := &ParsedLine{
Fields: fields,
Bytes: int64(len(line) + 1),
Err: err,
}
resultChan <- result
}
}()
}
// Goroutine to close resultChan when all workers are done
go func() {
wg.Wait()
close(resultChan)
}()
// Goroutine to read lines and send to workers
go func() {
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 && args.NLine > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
lineChan <- line
}
close(lineChan)
}()
// Main thread: collect parsed results and add to buffer
batchSize := 0
const updateInterval = 500 // Update UI every 500 lines
for result := range resultChan {
if result.Err != nil {
progress.finish()
doneChan <- result.Err
return
}
// Apply column filtering if needed
var fields []string
if len(args.ShowNum) != 0 || len(args.HideNum) != 0 {
visCol, err := getVisCol(args.ShowNum, args.HideNum, len(result.Fields))
if err != nil {
progress.finish()
doneChan <- err
return
}
fields = make([]string, 0, len(visCol))
for _, i := range visCol {
fields = append(fields, result.Fields[i])
}
} else {
fields = result.Fields
}
// Add to buffer
err = b.contAppendSli(fields, args.Strict)
if err != nil {
progress.finish()
doneChan <- err
return
}
totalAddedLN++
batchSize++
loadProgress.LoadedBytes += result.Bytes
progress.increment(result.Bytes)
// Update UI periodically
if batchSize >= updateInterval {
select {
case updateChan <- true:
batchSize = 0
default:
// Non-blocking - skip update if channel is full
}
}
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
}
loadProgress.IsComplete = true
// Auto-detect column types after loading (async)
go b.detectAllColumnTypes()
// Enable string interning for categorical columns (async)
go b.enableStringInterning()
progress.finish()
doneChan <- nil
}
// load file content to buffer (synchronous version for small files or when preferred)
func loadFileToBuffer(fn string, b *Buffer) error {
totalAddedLN := 0 //the number of lines has been added into buffer
// Get file size for progress tracking
fileInfo, err := os.Stat(fn)
if err != nil {
return err
}
var fileSize int64
if !fileInfo.IsDir() {
fileSize = fileInfo.Size()
}
// Create progress tracker
progress := newProgressTracker(fileSize, true)
scanner, err := getFileScanner(fn)
if err != nil {
return err
}
scanner.Split(bufio.ScanLines)
//set separator, if user does not provide it.
var detectLines []string //lines as detect separator data
if b.sep == 0 {
//read 10 lines to detect separator
lineNumber := 10
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
detectLines = append(detectLines, line)
if len(detectLines) >= lineNumber {
break
}
}
//if the suffix of file name is ".csv", set separator to ",".
//if the suffix of file name is "tsv", set separator to "\t".
if strings.HasSuffix(fn, ".csv") {
b.sep = ','
} else if strings.HasSuffix(fn, ".tsv") {
b.sep = '\t'
} else {
sd := sepDetecor{}
b.sep = sd.sepDetect(detectLines)
}
}
//check final separator
if b.sep == 0 {
fatalError(errors.New("tv can't identify separator, you need to set it manual"))
}
//add detectLines to buffer
for _, line := range detectLines {
//parse and add line to buffer
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
progress.finish()
return err
}
totalAddedLN++
progress.increment(int64(len(line) + 1)) // +1 for newline
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
}
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 && args.NLine > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
//parse and add line to buffer
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
progress.finish()
return err
}
totalAddedLN++
progress.increment(int64(len(line) + 1)) // +1 for newline
}
// Auto-detect column types after loading
b.detectAllColumnTypes()
// Enable string interning for categorical columns
b.enableStringInterning()
progress.finish()
return nil
}
// load console pipe content to buffer (async version for progressive rendering)
func loadPipeToBufferAsync(stdin io.Reader, b *Buffer, updateChan chan<- bool, doneChan chan<- error) {
totalAddedLN := 0 //the number of lines has been added into buffer
var err error
// For pipes, we don't know the total size
loadProgress.TotalBytes = 0
loadProgress.LoadedBytes = 0
loadProgress.IsComplete = false
// Create progress tracker (disabled for async loading)
progress := newProgressTracker(0, false)
scanner := bufio.NewScanner(stdin)
//increase buffer size for large files and long lines
const maxScanTokenSize = 1024 * 1024
buf := make([]byte, maxScanTokenSize)
scanner.Buffer(buf, maxScanTokenSize)
//read 10 lines to detect separator
lineNumber := 10
var detectLines []string //lines as detect separator data
if b.sep == 0 {
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
detectLines = append(detectLines, line)
if len(detectLines) >= lineNumber {
break
}
}
sd := sepDetecor{}
b.sep = sd.sepDetect(detectLines)
}
//check final separator
if b.sep == 0 {
doneChan <- errors.New("tv can't identify separator, you need to set it manual")
return
}
//add detectLines to buffer
for _, line := range detectLines {
//parse and add line to buffer
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
progress.finish()
doneChan <- err
return
}
totalAddedLN++
bytesRead := int64(len(line) + 1)
loadProgress.LoadedBytes += bytesRead
progress.increment(bytesRead)
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
}
// Signal that initial data is ready for rendering
updateChan <- true
// === CONCURRENT PARSING PIPELINE FOR PIPES ===
numWorkers := runtime.NumCPU()
if numWorkers > 8 {
numWorkers = 8
}
lineChan := make(chan string, numWorkers*10)
resultChan := make(chan *ParsedLine, numWorkers*10)
// Start worker goroutines
var wg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for line := range lineChan {
fields, err := lineCSVParseFast(line, b.sep)
result := &ParsedLine{
Fields: fields,
Bytes: int64(len(line) + 1),
Err: err,
}
resultChan <- result
}
}()
}
// Close resultChan when workers done
go func() {
wg.Wait()
close(resultChan)
}()
// Read lines and send to workers
go func() {
for scanner.Scan() {
line := scanner.Text()
if line == "\n" {
continue
}
if args.SkipNum > 0 {
args.SkipNum--
continue
}
if skipLine(line, args.SkipSymbol) {
continue
}
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
lineChan <- line
}
close(lineChan)
}()
// Collect results
batchSize := 0
const updateInterval = 500
for result := range resultChan {
if result.Err != nil {
progress.finish()
doneChan <- result.Err
return
}
var fields []string
if len(args.ShowNum) != 0 || len(args.HideNum) != 0 {
visCol, err := getVisCol(args.ShowNum, args.HideNum, len(result.Fields))
if err != nil {
progress.finish()
doneChan <- err
return
}
fields = make([]string, 0, len(visCol))
for _, i := range visCol {
fields = append(fields, result.Fields[i])
}
} else {
fields = result.Fields
}
err = b.contAppendSli(fields, args.Strict)
if err != nil {
progress.finish()
doneChan <- err
return
}
totalAddedLN++
batchSize++
loadProgress.LoadedBytes += result.Bytes
progress.increment(result.Bytes)
if batchSize >= updateInterval {
select {
case updateChan <- true:
batchSize = 0
default:
}
}
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
}
loadProgress.IsComplete = true
// Auto-detect column types after loading (async)
go b.detectAllColumnTypes()
// Enable string interning for categorical columns (async)
go b.enableStringInterning()
progress.finish()
doneChan <- nil
}
// load console pipe content to buffer (synchronous version)
func loadPipeToBuffer(stdin io.Reader, b *Buffer) error {
totalAddedLN := 0 //the number of lines has been added into buffer
var err error
// Create progress tracker (no file size for pipes)
progress := newProgressTracker(0, true)
scanner := bufio.NewScanner(stdin)
//increase buffer size for large files and long lines
const maxScanTokenSize = 1024 * 1024
buf := make([]byte, maxScanTokenSize)
scanner.Buffer(buf, maxScanTokenSize)
//read 10 lines to detect separator
lineNumber := 10
var detectLines []string //lines as detect separator data
if b.sep == 0 {
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
detectLines = append(detectLines, line)
if len(detectLines) >= lineNumber {
break
}
}
sd := sepDetecor{}
b.sep = sd.sepDetect(detectLines)
}
//check final separator
if b.sep == 0 {
fatalError(errors.New("tv can't identify separator, you need to set it manual"))
}
//add detectLines to buffer
for _, line := range detectLines {
//parse and add line to buffer
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
progress.finish()
return err
}
totalAddedLN++
progress.increment(int64(len(line) + 1))
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
}
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
//parse and add line to buffer
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
progress.finish()
return err
}
totalAddedLN++
progress.increment(int64(len(line) + 1))
}
// Auto-detect column types after loading
b.detectAllColumnTypes()
// Enable string interning for categorical columns
b.enableStringInterning()
progress.finish()
return nil
}
// check a line whether should bu skip, according to prefix
func skipLine(line string, sy []string) bool {
for _, sy := range sy {
if strings.HasPrefix(line, sy) {
return true
}
}
return false
}
// get suitable scanner(compressed or not)
func getFileScanner(fn string) (*bufio.Scanner, error) {
info, err := os.Stat(fn)
if err != nil {
return nil, err
}
//check if fn is a directory
if info.IsDir() {
return nil, errors.New(fn + " is a directory")
}
file, err := os.Open(fn)
if err != nil {
return nil, err
}
var scanner *bufio.Scanner
//if input is a gzip file
if strings.HasSuffix(fn, ".gz") {
gzCont, err := gzip.NewReader(file)
if err != nil {
return nil, err
}
scanner = bufio.NewScanner(gzCont)
} else {
scanner = bufio.NewScanner(file)
}
//increase buffer size for large files and long lines
//default is 64KB, we set to 1MB for better performance
const maxScanTokenSize = 1024 * 1024
buf := make([]byte, maxScanTokenSize)
scanner.Buffer(buf, maxScanTokenSize)
return scanner, nil
}
// check columns that should be displayed
func getVisCol(showNumL, hideNumL []int, colLen int) ([]int, error) {
for _, i := range showNumL {
if i > colLen || i <= 0 {
return nil, errors.New("Column number " + I2S(i) + " does not exist")
}
}
for _, i := range hideNumL {
if i > colLen || i <= 0 {
return nil, errors.New("Column number " + I2S(i) + " does not exist")
}
}
var visCol []int
for i := 0; i < colLen; i++ {
flag, err := checkVisible(showNumL, hideNumL, i)
if err != nil {
return nil, err
}
if flag {
visCol = append(visCol, i)
}
}
return visCol, nil
}
// check ith column should be displayed or not
func checkVisible(showNumL, hideNumL []int, col int) (bool, error) {
if len(showNumL) != 0 && len(hideNumL) != 0 {
return false, errors.New("you can only set visible column or hidden column")
}
if len(showNumL) != 0 {
for _, colTestS := range showNumL {
if col+1 == colTestS {
return true, nil
}
}
return false, nil
}
if len(hideNumL) != 0 {
for _, colTestH := range hideNumL {
if col+1 == colTestH {
return false, nil
}
}
}
return true, nil
}
// use go csv library to parse a string line into csv format
// Optimized version with reusable reader
func lineCSVParse(s string, sep rune) ([]string, error) {
r := csv.NewReader(strings.NewReader(s))
r.Comma = sep
r.LazyQuotes = true
r.ReuseRecord = true //reuse backing array for performance
//r.TrimLeadingSpace = true //disable, because it will remove NULL item and cause issue.
record, err := r.Read()
if err != nil {
return nil, err
}
//make a copy since ReuseRecord=true reuses the backing array
result := make([]string, len(record))
copy(result, record)
return result, err
}
// Fast CSV parser for simple cases (no quotes, no escaping)
// Falls back to standard parser if needed
func lineCSVParseFast(s string, sep rune) ([]string, error) {
// Quick check if line contains quotes (needs full parser)
hasQuotes := false
for i := 0; i < len(s); i++ {
if s[i] == '"' {
hasQuotes = true
break
}
}
// Use fast path for simple CSV lines
if !hasQuotes {
// Count separators to pre-allocate slice
sepCount := 0
for i := 0; i < len(s); i++ {
if rune(s[i]) == sep {
sepCount++
}
}
result := make([]string, 0, sepCount+1)
start := 0
for i := 0; i < len(s); i++ {
if rune(s[i]) == sep {
result = append(result, s[start:i])
start = i + 1
}
}
// Add last field
result = append(result, s[start:])
return result, nil
}
// Fall back to standard parser for complex cases
return lineCSVParse(s, sep)
}
// add displayable(according to user's input argument) RowArray(covert line to array) To Buffer
func addDRToBuffer(b *Buffer, line string, showNum, hideNum []int) error {
var err error
lineCSVParts, err := lineCSVParseFast(line, b.sep)
if err != nil {
return err
}
if len(showNum) != 0 || len(hideNum) != 0 {
// Pre-allocate slice with known capacity
visCol, err := getVisCol(showNum, hideNum, len(lineCSVParts))
if err != nil {
return err
}
lineSli := make([]string, 0, len(visCol))
for _, i := range visCol {
lineSli = append(lineSli, lineCSVParts[i])
}
err = b.contAppendSli(lineSli, args.Strict)
if err != nil {
return err
}
} else {
err := b.contAppendSli(lineCSVParts, args.Strict)
if err != nil {
return err
}
}
return err
}