-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfl
1420 lines (1256 loc) · 73.9 KB
/
fl
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
#!/bin/bash
###################################################################################################
#
# Find all lines or multi-line paragraphs which contain a set of strings in filtered files
# of current folder and its subfolders recursively, in a simple way for most common usage,
# without need of complex regular expressions. Special characters and binary files contents
# can be searched.
#
# USAGE:
# fl [-i] [-p=N] [-w] [pathstr|/pathstr]* str [+|-|++|-- str]*
#
# DESCRIPTION:
# The goal of this linux bash script is to simply and quickly display all the lines
# or multi-line paragraphs matching the str parameters in all the files being filtered
# by the pathstr parameters, see below examples.
# Special characters (like *, +, |, :, ...) do not need to be escaped, they are searched
# directly as provided in the str parameters, which makes the usage very simple.
# Binary files contents are also searched, so that as many matching lines as possible are
# displayed at the end and no occurrence is missed.
#
# OPTIONS:
# -i: ignore lower/upper case during the search
# -p=N: consider paragraphs of 2N+1 lines during the search (N >= 1) instead of line
# by line search (2N+1 = N lines before first str matching line + first str
# matching line + N lines after first str matching line)
# -w: first str to find shall be a whole word (word-constituent characters are letters,
# digits and underscore)
#
# EXAMPLES:
# fl str
# => find all lines containing str in all files of current folder and its subfolders
# fl .c str
# => find all lines containing str in the .c files of current folder and its subfolders
# fl .c .h str1 + str2 - str3
# => find all lines containing str1 and str2 and not str3 in the .c or .h files
# of current folder and its subfolders
# fl .c .h str1 ++ str2 -- str3
# => find all lines containing str1 and then str2 and then not str3 in the .c or .h files
# of current folder and its subfolders
# fl mypathstr str
# => find all lines containing str in the files of current folder and its subfolders
# whose path contains "mypathstr" ("mypathstr" in file or folder names)
# fl .c /mypathstr str
# => find all lines containing str in the .c files of current folder and its subfolders
# whose path does not contain "mypathstr" ("mypathstr" not in file and folder names)
# fl -i .c str
# => find all lines containing str in the .c files of current folder and its subfolders
# with lower/upper case ignored
# fl -p=1 .c str1 + str2
# => find all 3 lines long paragraphs containing str1 and str2 in the .c files
# of current folder and its subfolders
# fl -p=2 .c str1 ++ str2 -- str3
# => find all 5 lines long paragraphs containing str1 and then str2 and then not str3
# in the .c files of current folder and its subfolders
# fl -w .c str
# => find all lines containing the whole word "str" in the .c files of current folder
# and its subfolders
#
# Of course, all operators +/-/++/-- and all options -i/-p=N/-w can be used jointly.
#
# Examples of searches with special characters:
# - Special characters can be searched directly, for example
# fl "(a*b+[(x/y)-z] || d == e & f)" will search exactly this string without need to escape
# the special characters.
# - Linux shell's special characters \, $ and ` shall be escaped with \\, \$ and \` respectively.
# For example fl "\\xxx\$" will search all lines containing "\xxx$" in all files.
# - Special characters can also be searched directly with their ASCII code, for example:
# fl $'\x09' will search all lines containing tab characters (\t) in all files,
# fl $'\x0D' will search all lines containing carriage return characters (\r) in all files.
#
###################################################################################################
###########
# Constants
###########
export LC_ALL=en_US.UTF-8
MAX_ABBREVIATION_LEN=80
MAX_LEN_FOR_LINE_SPLIT=140
ERROR_COLOR=$'\E[1m\E[4m\E[31m' # Red
SOURCE_COLOR=$'\E[1m\E[4m\E[31m' # Red
HEADER_COLOR=$'\E[1m\E[4m\E[32m' # Green
OTHER_COLOR=$'\E[1m\E[4m\E[36m' # Cyan
FILEPATH_COLOR=$'\E[2m\E[37m' # White
BOLD=$'\E[1m'
ITALIC=$'\E[3m'
NC=$'\E[0m'
character_separator="§"
paragraph_separator="§§§§§§§§§§"
paragraph_line_separator="${paragraph_separator}§§§§§"
no_match="[?????]"
grep_ignorecase_option="-i"
grep_word_option="-w"
abbreviation_str="$BOLD [...] $NC"
###########
# Functions
###########
# print error message
printError() {
local error_str=$1
echo -e -n "$ERROR_COLOR" >&2
echo -n "$error_str" >&2
echo -e "$NC" >&2
}
# display spaces
display_spaces() {
local nb_spaces=$1
spaces_str=$(printf "%${nb_spaces}s")
echo "$spaces_str"
}
# abbreviate long strings
abbreviate() {
local str=$1
length=${#str}
if [[ $length -gt $((MAX_ABBREVIATION_LEN * 2 + 10)) ]]; then
abbreviated_str_1="${str:0:MAX_ABBREVIATION_LEN}" # substring
abbreviated_str_1="${abbreviated_str_1%"${abbreviated_str_1##*[![:space:]]}"}" # right trim
idx=$((length - MAX_ABBREVIATION_LEN))
abbreviated_str_2="${str:idx:MAX_ABBREVIATION_LEN}" # substring
abbreviated_str_2="${abbreviated_str_2#"${abbreviated_str_2%%[![:space:]]*}"}" # left trim
abbreviated_str=$(echo "$abbreviated_str_1$abbreviation_str$abbreviated_str_2")
echo "$abbreviated_str"
else
echo "$str"
fi
}
# escape special characters as needed by grep -E commmand
escape_special_chars() {
local str=$1
echo "$str" | sed 's/\\/\\\\/g' | sed 's/\[/\\[/g' | sed 's/\]/\\]/g' | sed 's/+/\\+/g' | sed 's/(/\\(/g' | sed 's/)/\\)/g' | sed 's/{/\\{/g' | sed 's/}/\\}/g' | sed 's/\./\\./g' | sed 's/\^/\\^/g' | sed 's/\$/\\$/g' | sed 's/\?/\\?/g' | sed 's/!/\\!/g' | sed 's/|/\\|/g' | sed 's/\*/\\*/g' | sed 's/-/\\-/g'
}
# check if 2 strings are "correlated"
correlated_strings() {
local str1_p=$1
local str2_p=$2
local ignorecase_p=$3
local word_for_str1_p=$4
if [[ $ignorecase_p -eq 0 ]]; then
if [[ $word_for_str1_p -eq 0 ]]; then
str1=$(echo "$str1_p")
str2=$(echo "$str2_p")
else
# this simple str1_p reformatting covers most of cases
str1=$(echo " $str1_p " | sed 's/[^a-zA-Z0-9_]/ /g')
str2=$(echo "$str2_p" | sed 's/[^a-zA-Z0-9_]/ /g')
fi
else
if [[ $word_for_str1_p -eq 0 ]]; then
str1=$(echo "$str1_p" | tr '[:upper:]' '[:lower:]')
str2=$(echo "$str2_p" | tr '[:upper:]' '[:lower:]')
else
# this simple str1_p reformatting covers most of cases
str1=$(echo " $str1_p " | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9_]/ /g')
str2=$(echo "$str2_p" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9_]/ /g')
fi
fi
str1_length=${#str1}
str2_length=${#str2}
min_str_length=$(( str1_length < str2_length ? str1_length : str2_length ))
if [[ $min_str_length -eq 0 ]]; then
echo "0"
return
fi
# check if strings are included in each other
if [[ "$str1" == *"$str2"* || "$str2" == *"$str1"* ]]; then
echo "1"
return
fi
# check if strings have common start/end substrings, taking into account word boundaries
len=1
while [[ $len -le $min_str_length ]]; do
startsubstr1="${str1:0:len}" # substring
if [[ "$str2" == *"$startsubstr1" ]]; then
echo "1"
return
fi
startsubstr2="${str2:0:len}" # substring
if [[ "$str1" == *"$startsubstr2" ]]; then
echo "1"
return
fi
len=$((len + 1))
done
echo "0"
}
# gather paragraph lines
gather_paragraph_lines() {
paragraph_lines=""
at_least_one_line=0
# in this loop, "IFS=" avoids input lines being trimmed
while IFS= read -r input_str; do
at_least_one_line=1
if [[ "$input_str" == "$paragraph_separator" ]]; then
if [[ "$paragraph_lines" == "" ]]; then
printError "gather_paragraph_lines error: empty paragraph"
exit 1
fi
echo "$paragraph_lines"
paragraph_lines=""
echo "$paragraph_separator"
else
if [[ "$paragraph_lines" == "" ]]; then
paragraph_lines="$input_str"
else
paragraph_lines="$paragraph_lines$paragraph_line_separator$input_str"
fi
fi
done
if [[ "$paragraph_lines" == "" && $at_least_one_line -ne 0 ]]; then
printError "gather_paragraph_lines error: empty ending paragraph"
exit 1
fi
echo "$paragraph_lines"
}
# format output
format_output() {
local searched_str_p=$1
local grep_ignorecase_option_p=$2
local grep_word_option_p=$3
local paragraph_size_p=$4
if ! [[ "$paragraph_size_p" =~ ^[0-9]+$ ]]; then
printError "format_output error: paragraph size \"$paragraph_size_p\" is not a number"
exit 1
fi
searched_str_length=${#searched_str_p}
if [[ $searched_str_length -eq 0 ]]; then
printError "format_output error: empty searched_str_p"
exit 1
fi
reformatted_searched_str_p=$(escape_special_chars "$searched_str_p")
last_filepath=""
last_binary_filepath=""
highlight_color="$OTHER_COLOR"
non_printable_status=0
total_nb_lines_displayed=0
# in this loop, "IFS=" avoids input lines being trimmed
while IFS= read -r input_str; do
if [[ "$input_str" == "" ]]; then
continue
fi
input_str_length=${#input_str}
# extract filepath and line number
# input_str format is ./xxx/yyy/zzz.txt:123:line_contents or ./xxx/yyy/zzz.txt-123-line_contents (possible in paragraph mode)
OLDIFS=$IFS
IFS=":"
read -r filepath line_number current_input_str <<< "$input_str"
IFS=$OLDIFS # restore IFS
if [[ "$line_number" =~ ^[0-9]+$ ]]; then
if [[ "$current_input_str" == "" && $paragraph_size_p -eq 0 ]]; then
printError "unexpected empty matching line for $filepath:$line_number"
exit 1
fi
filepath_and_line_number_str="$filepath:$line_number"
reformatted_filepath_and_line_number_str="${filepath_and_line_number_str#./}" # remove leading ./
skip_occurrence_number_display=0
else
# try slower method
filepath_and_line_number_str=$(grep --text -o -P "^\./.*?:[0-9]+:" <<< "$input_str") # Perl regexp is used for non-greedy mode (not supported by Extended Regular Expressions with grep -E)
filepath_and_line_number_length=${#filepath_and_line_number_str}
if [[ $filepath_and_line_number_length -ne 0 ]]; then
reformatted_filepath_and_line_number_str="${filepath_and_line_number_str#./}" # remove leading ./
reformatted_filepath_and_line_number_str="${reformatted_filepath_and_line_number_str%:}" # remove ending :
line_number_with_separators=$(grep -o -E ":[0-9]+:$" <<< "$filepath_and_line_number_str")
line_number_with_separators_length=${#line_number_with_separators}
idx=$((filepath_and_line_number_length - line_number_with_separators_length))
filepath="${filepath_and_line_number_str:0:idx}" # substring
length=$((input_str_length - filepath_and_line_number_length))
current_input_str="${input_str:filepath_and_line_number_length:length}" # substring
if [[ "$current_input_str" == "" && $paragraph_size_p -eq 0 ]]; then
printError "unexpected empty matching line for $filepath:$line_number"
exit 1
fi
skip_occurrence_number_display=0
elif [[ $paragraph_size_p -ne 0 ]]; then
if [[ "$input_str" == "$paragraph_separator" ]]; then
continue
else
IFS="-"
read -r filepath line_number current_input_str <<< "$input_str"
IFS=$OLDIFS # restore IFS
if [[ "$line_number" =~ ^[0-9]+$ ]]; then
filepath_and_line_number_str="$filepath-$line_number"
reformatted_filepath_and_line_number_str="${filepath_and_line_number_str#./}" # remove leading ./
if [[ "$current_input_str" == "" ]]; then
echo -e "$(display_spaces ${#total_nb_lines_displayed}) $FILEPATH_COLOR$reformatted_filepath_and_line_number_str$NC"
continue
fi
else
# try slower method
filepath_and_line_number_str=$(grep --text -o -P "^\./.*?-[0-9]+-" <<< "$input_str") # Perl regexp is used for non-greedy mode (not supported by Extended Regular Expressions with grep -E)
filepath_and_line_number_length=${#filepath_and_line_number_str}
if [[ $filepath_and_line_number_length -ne 0 ]]; then
reformatted_filepath_and_line_number_str="${filepath_and_line_number_str#./}" # remove leading ./
reformatted_filepath_and_line_number_str="${reformatted_filepath_and_line_number_str%-}" # remove ending -
line_number_with_separators=$(grep -o -E "\-[0-9]+-$" <<< "$filepath_and_line_number_str")
line_number_with_separators_length=${#line_number_with_separators}
idx=$((filepath_and_line_number_length - line_number_with_separators_length))
filepath="${filepath_and_line_number_str:0:idx}" # substring
length=$((input_str_length - filepath_and_line_number_length))
current_input_str="${input_str:filepath_and_line_number_length:length}" # substring
if [[ "$current_input_str" == "" ]]; then
echo -e "$(display_spaces ${#total_nb_lines_displayed}) $FILEPATH_COLOR$reformatted_filepath_and_line_number_str$NC"
continue
fi
else
printError "format_output error: cannot extract filepath and line number in paragraph mode from \"$input_str\""
exit 1
fi
fi
skip_occurrence_number_display=1
fi
else
printError "format_output error: cannot extract filepath and line number from \"$input_str\""
exit 1
fi
fi
if [[ "$last_filepath" != "$filepath" ]]; then
# specific formatting (conditions on filepath to be adapted as needed - with below conditions, test files will be displayed in italic)
specific_formatting=""
if [[ "$filepath" == *"test"* || "$filepath" == *"Test"* || "$filepath" == *"TEST"* || "$filepath" == *"tst"* ]]; then
specific_formatting="$ITALIC"
fi
# source files (conditions on file suffix to be adapted as needed)
if [[ "$filepath" == *".c" || "$filepath" == *".cpp" || "$filepath" == *".java" ]]; then
highlight_color="$SOURCE_COLOR$specific_formatting"
non_printable_status=0
# header files (conditions on file suffix to be adapted as needed)
elif [[ "$filepath" == *".h" || "$filepath" == *".hh" || "$filepath" == *".hpp" ]]; then
highlight_color="$HEADER_COLOR$specific_formatting"
non_printable_status=0
# common non binary files
elif [[ "$filepath" == *".txt" || "$filepath" == *".js" || "$filepath" == *".log" || "$filepath" == *".html" || "$filepath" == *".xml" || "$filepath" == *".csv" ]]; then
highlight_color="$OTHER_COLOR$specific_formatting"
non_printable_status=0
# common binary files
elif [[ "$filepath" == *".o" || "$filepath" == *".obj" || "$filepath" == *".png" || "$filepath" == *".jpg" || "$filepath" == *".docx" || "$filepath" == *".zip" || "$filepath" == *".gz" || "$filepath" == *".bin" ]]; then
highlight_color="$OTHER_COLOR$specific_formatting"
non_printable_status=1
# other files
else
highlight_color="$OTHER_COLOR$specific_formatting"
# check if current_input_str contains non printable characters (binary line detection)
# Notes:
# - non-ASCII characters (<=> command "grep -qE $'[^\x01-\x7F]';") are multi-byte and are supported by this script so are not considered as non printable
# - among others \r characters are also supported (aka Ctrl+M or "carriage return" characters = \x0D in ASCII, typically present in Windows files)
# - impossible to check \x00 presence in a string with a bash script => \x00 is considered as printable and a warning message will be displayed if needed
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< "$current_input_str")" != "" ]]; then # code duplicated in unit tests (a little slower but better than tr -d '[:print:][:blank:]\r§µ¨£°²' which operates based on single-byte ASCII characters by default)
non_printable_status=1
else
non_printable_status=0
fi
fi
last_filepath="$filepath"
fi
# Case all characters are printable
if [[ $non_printable_status -eq 0 ]]; then
current_input_str_length=${#current_input_str}
if [[ $input_str_length -ge $MAX_LEN_FOR_LINE_SPLIT && $paragraph_size_p -eq 0 ]]; then
leading_spaces="${current_input_str%%[^ ]*}"
leading_spaces_count=${#leading_spaces}
trailing_spaces="${current_input_str##*[^ ]}"
trailing_spaces_count=${#trailing_spaces}
# indentation is replaced by a \t or new line if line is very long
reformatted_filepath_and_line_number_str_length=${#reformatted_filepath_and_line_number_str}
if [[ $((reformatted_filepath_and_line_number_str_length + current_input_str_length - leading_spaces_count - trailing_spaces_count)) -le $MAX_LEN_FOR_LINE_SPLIT ]]; then
separator="\t"
else
separator="\n"
fi
else
separator="\t"
fi
if [[ $skip_occurrence_number_display -eq 0 ]]; then
total_nb_lines_displayed=$((total_nb_lines_displayed + 1))
echo -e -n "$FILEPATH_COLOR$total_nb_lines_displayed $reformatted_filepath_and_line_number_str$NC$separator"
else
echo -e -n "$(display_spaces ${#total_nb_lines_displayed}) $FILEPATH_COLOR$reformatted_filepath_and_line_number_str$NC$separator"
fi
at_least_one_occurrence_on_line=0
while true; do
# get offset of reformatted_searched_str_p in current_input_str in bytes, using the same grep command and options as in main search to keep full compatibility between search and formatting
byte_idx_str=$(grep -E --text -o -b $grep_ignorecase_option_p $grep_word_option_p -- "$reformatted_searched_str_p" <<< "$current_input_str" | head -n 1)
byte_idx=${byte_idx_str%%[^0-9]*}
if [[ "$byte_idx" == "" ]]; then
idx=-1
else
# convert number of bytes into number of characters, useful for multi-byte non-ASCII characters
char_idx=$(echo -n "$current_input_str" | head -c $byte_idx | wc -m)
idx=$char_idx
fi
if [[ $idx -eq -1 ]]; then
if [[ $at_least_one_occurrence_on_line -eq 0 && $paragraph_size_p -eq 0 ]]; then
# warning that no occurrence was successfully highlighted on current line: can occur due to \x00 specical character
echo -e -n "$highlight_color$no_match$NC "
current_input_str="${current_input_str#"${current_input_str%%[![:space:]]*}"}" # left trim
fi
if [[ "$current_input_str" != "" ]]; then
tmp_abbreviated_str="${current_input_str%"${current_input_str##*[![:space:]]}"}" # right trim
tmp_abbreviated_str=$(abbreviate "$tmp_abbreviated_str")
echo -n "$tmp_abbreviated_str"
fi
break
fi
if [[ $idx -gt 0 ]]; then
tmp_str="${current_input_str:0:idx}" # substring
# do not change indentation in paragraph mode
if [[ $at_least_one_occurrence_on_line -eq 0 && $paragraph_size_p -eq 0 ]]; then
tmp_str="${tmp_str#"${tmp_str%%[![:space:]]*}"}" # left trim
fi
tmp_abbreviated_str=$(abbreviate "$tmp_str")
echo -n "$tmp_abbreviated_str"
fi
next_idx=$((idx + $searched_str_length))
length=$((next_idx - idx))
tmp_str="${current_input_str:idx:length}" # substring
echo -e -n "$highlight_color"
echo -n "$tmp_str"
echo -e -n "$NC"
at_least_one_occurrence_on_line=1
if [[ $next_idx -lt $current_input_str_length ]]; then
length=$((current_input_str_length - next_idx))
current_input_str="${current_input_str:next_idx:length}" # substring
current_input_str_length=${#current_input_str}
else
break
fi
done
echo
# Case some characters are non printable
else
if [[ "$last_binary_filepath" != "$filepath" ]]; then
binary_line_msg="${highlight_color}binary line(s)${NC}"
if [[ $skip_occurrence_number_display -eq 0 ]]; then
total_nb_lines_displayed=$((total_nb_lines_displayed + 1))
# searched_str_p deemed always present at this stage (explicit presence check can be disturbed by special characters)
echo -e "$FILEPATH_COLOR$total_nb_lines_displayed $reformatted_filepath_and_line_number_str$NC $binary_line_msg"
else
echo -e "$(display_spaces ${#total_nb_lines_displayed}) $FILEPATH_COLOR$reformatted_filepath_and_line_number_str$NC $binary_line_msg"
fi
last_binary_filepath="$filepath"
fi
fi
done
if [[ $total_nb_lines_displayed -eq 0 ]]; then
echo "no occurrence"
fi
}
###################################################################################################
############
# Unit tests
############
check_test_condition() {
local res=$1
local exp=$2
local testid=$3
if [[ ${#res} != ${#exp} || "$res" != "$exp" ]]; then
printf 'ERROR %d: \"%q\" is different from \"%q\" (%d, %d)\n' "$testid" "$res" "$exp" "${#res}" "${#exp}" >&2
else
echo "OK $testid"
fi
}
unit_tests() {
echo "Running unit tests..."
MAX_ABBREVIATION_LEN=10000
MAX_LEN_FOR_LINE_SPLIT=10000
complex_str=" 019AbcD E FxYZ,,?&|+;;. ...:://!! != §%% * ** **** µùéèàâêîôûŷäëïöüÿãẽĩõũỹ^^¨¨\$\$££ = == === {{}} {} + ++ +++ (( )) ( ) () [[ ]] [ ] [] ° @ / // /* */ ^ ^^ _ __ \ \\ \` - -- --- | || ' '' ''' \" \"\" ## # \" ~ & && ² <>aA "
trimmed_complex_str="${complex_str#"${complex_str%%[![:space:]]*}"}" # left trim
trimmed_complex_str="${trimmed_complex_str%"${trimmed_complex_str##*[![:space:]]}"}" # right trim
test_idx=1
# (string not found)
res=$(echo "./a/b/c/file name.cpp:123: aBc " | format_output "aBcX" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}$no_match${NC} aBc")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:$complex_str" | format_output "" "" "" 0 2>&1);
if [[ "$res" == *"empty searched_str_p"* ]]; then
echo "OK $((test_idx++))"
else
echo "ERROR $((test_idx++))"
fi
res=$(echo "./a/b/c/file name.cpp:123:" | format_output "ABC" "" "" 0 2>&1);
if [[ "$res" == *"unexpected empty matching line for"* ]]; then
echo "OK $((test_idx++))"
else
echo "ERROR $((test_idx++))"
fi
# (basic [with spaces])
res=$(echo "./a/b/c/file name.cpp:123:aBc" | format_output "aBc" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}aBc${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.hpp:123: aBc " | format_output "aBc" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.hpp:123${NC}\t${HEADER_COLOR}aBc${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.txt:123: aBc " | format_output "aBc" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.txt:123${NC}\t${OTHER_COLOR}aBc${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name:123: aBc " | format_output "aBc" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name:123${NC}\t${OTHER_COLOR}aBc${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:1: aBc " | format_output " aBc " "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:1${NC}\t${SOURCE_COLOR} aBc ${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:1234567890: aBc " | format_output " aBc" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:1234567890${NC}\t${SOURCE_COLOR} aBc${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123: aBc " | format_output "aBc " "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}aBc ${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123: aBc " | format_output " aBc" "" "" 0 2>&1);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}$no_match${NC} aBc")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123: aBc " | format_output "aBc " "" "" 0 2>&1);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}$no_match${NC} aBc")
check_test_condition "$res" "$exp" $((test_idx++))
test_idx=100
# (special characters)
res=$(echo "./a/b/c/file name.cpp:123:aBc -a -i -- ----- edF" | format_output "--" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc -a -i ${SOURCE_COLOR}--${NC} ${SOURCE_COLOR}--${NC}${SOURCE_COLOR}--${NC}- edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc + ++ +++++ edF" | format_output "++" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc + ${SOURCE_COLOR}++${NC} ${SOURCE_COLOR}++${NC}${SOURCE_COLOR}++${NC}+ edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc * ** ***** edF" | format_output "**" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc * ${SOURCE_COLOR}**${NC} ${SOURCE_COLOR}**${NC}${SOURCE_COLOR}**${NC}* edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc ^ ^^ ^^^^^ edF" | format_output "^^" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc ^ ${SOURCE_COLOR}^^${NC} ${SOURCE_COLOR}^^${NC}${SOURCE_COLOR}^^${NC}^ edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc \$ \$\$ \$\$\$\$\$ edF" | format_output "\$\$" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc \$ ${SOURCE_COLOR}\$\$${NC} ${SOURCE_COLOR}\$\$${NC}${SOURCE_COLOR}\$\$${NC}\$ edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc ( () ( (()()) edF" | format_output "()" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc ( ${SOURCE_COLOR}()${NC} ( (${SOURCE_COLOR}()${NC}${SOURCE_COLOR}()${NC}) edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc ( [] [ [[][]] edF" | format_output "[]" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc ( ${SOURCE_COLOR}[]${NC} [ [${SOURCE_COLOR}[]${NC}${SOURCE_COLOR}[]${NC}] edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc ( {} { {{}{}} edF" | format_output "{}" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc ( ${SOURCE_COLOR}{}${NC} { {${SOURCE_COLOR}{}${NC}${SOURCE_COLOR}{}${NC}} edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc / / edF" | format_output "/" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc ${SOURCE_COLOR}/${NC} ${SOURCE_COLOR}/${NC} edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc \" \" edF" | format_output "\"" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc ${SOURCE_COLOR}\"${NC} ${SOURCE_COLOR}\"${NC} edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc ' ' edF" | format_output "'" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc ${SOURCE_COLOR}'${NC} ${SOURCE_COLOR}'${NC} edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:aBc \` \` edF" | format_output "\`" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc ${SOURCE_COLOR}\`${NC} ${SOURCE_COLOR}\`${NC} edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:### #" | format_output "#" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}#${NC}${SOURCE_COLOR}#${NC}${SOURCE_COLOR}#${NC} ${SOURCE_COLOR}#${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:xxxxxxx" | format_output "xxx" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}xxx${NC}${SOURCE_COLOR}xxx${NC}x")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123: " | format_output " " "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR} ${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
# (substring at the beginning of string)
res=$(echo "./a/b/c/file name.cpp:123://x//xx//xxx// edF" | format_output "//" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}//${NC}x${SOURCE_COLOR}//${NC}xx${SOURCE_COLOR}//${NC}xxx${SOURCE_COLOR}//${NC} edF")
check_test_condition "$res" "$exp" $((test_idx++))
# (substring at the end of string)
res=$(echo "./a/b/c/file name.cpp:123://x//xx//xxx//" | format_output "//" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}//${NC}x${SOURCE_COLOR}//${NC}xx${SOURCE_COLOR}//${NC}xxx${SOURCE_COLOR}//${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
# (spaces only)
res=$(echo "./a/b/c/file name.cpp:123: aBc deF " | format_output " " "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR} ${NC}aBc${SOURCE_COLOR} ${NC} deF${SOURCE_COLOR} ${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123: aBc deF " | format_output " " "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR} ${NC} aBc${SOURCE_COLOR} ${NC} deF${SOURCE_COLOR} ${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
# (ignore case)
res=$(echo "./a/b/c/file name.cpp:123: abcDEF " | format_output "ABCdef " "" "" 0 2>&1);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}$no_match${NC} abcDEF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123: abcDEF " | format_output "ABCdef " "$grep_ignorecase_option" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t${SOURCE_COLOR}abcDEF ${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
test_idx=200
# (complex_str)
# (first half of complex_str)
res=$(echo "./a/b/c/file name.xxx:123:$complex_str" | format_output "AbcD E FxYZ,,?&|+;;. ...:://!! != §%% * ** **** µùéèàâêîôûŷäëïöüÿãẽĩõũỹ^^¨¨\$\$££ = == === {{}} {} + ++ +++ (( ))" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.xxx:123${NC}\t019${OTHER_COLOR}AbcD E FxYZ,,?&|+;;. ...:://!! != §%% * ** **** µùéèàâêîôûŷäëïöüÿãẽĩõũỹ^^¨¨\$\$££ = == === {{}} {} + ++ +++ (( ))${NC} ( ) () [[ ]] [ ] [] ° @ / // /* */ ^ ^^ _ __ \ \\ \` - -- --- | || ' '' ''' \" \"\" ## # \" ~ & && ² <>aA")
check_test_condition "$res" "$exp" $((test_idx++))
# (second half of complex_str)
res=$(echo "./a/b/c/file name.xxx:123:$complex_str" | format_output " ( ) () [[ ]] [ ] [] ° @ / // /* */ ^ ^^ _ __ \ \\ \` - -- --- | || ' '' ''' \" \"\" ## # \" ~ & && ² <>aA" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.xxx:123${NC}\t019AbcD E FxYZ,,?&|+;;. ...:://!! != §%% * ** **** µùéèàâêîôûŷäëïöüÿãẽĩõũỹ^^¨¨\$\$££ = == === {{}} {} + ++ +++ (( ))${OTHER_COLOR} ( ) () [[ ]] [ ] [] ° @ / // /* */ ^ ^^ _ __ \ \\ \` - -- --- | || ' '' ''' \" \"\" ## # \" ~ & && ² <>aA${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:$complex_str" | format_output " E FxYZ,,?&|+;;. " "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t019AbcD${SOURCE_COLOR} E FxYZ,,?&|+;;. ${NC}...:://!! != §%% * ** **** µùéèàâêîôûŷäëïöüÿãẽĩõũỹ^^¨¨\$\$££ = == === {{}} {} + ++ +++ (( )) ( ) () [[ ]] [ ] [] ° @ / // /* */ ^ ^^ _ __ \ \\ \` - -- --- | || ' '' ''' \" \"\" ## # \" ~ & && ² <>aA")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.hpp:123:$complex_str" | format_output " e FXYZ,,?&|+;;. " "" "" 0 2>&1);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.hpp:123${NC}\t${HEADER_COLOR}$no_match${NC} $trimmed_complex_str")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:$complex_str" | format_output " e FXYZ,,?&|+;;. " "$grep_ignorecase_option" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t019AbcD${SOURCE_COLOR} E FxYZ,,?&|+;;. ${NC}...:://!! != §%% * ** **** µùéèàâêîôûŷäëïöüÿãẽĩõũỹ^^¨¨\$\$££ = == === {{}} {} + ++ +++ (( )) ( ) () [[ ]] [ ] [] ° @ / // /* */ ^ ^^ _ __ \ \\ \` - -- --- | || ' '' ''' \" \"\" ## # \" ~ & && ² <>aA")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:$complex_str" | format_output "² <>aA" "$grep_ignorecase_option" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t019AbcD E FxYZ,,?&|+;;. ...:://!! != §%% * ** **** µùéèàâêîôûŷäëïöüÿãẽĩõũỹ^^¨¨\$\$££ = == === {{}} {} + ++ +++ (( )) ( ) () [[ ]] [ ] [] ° @ / // /* */ ^ ^^ _ __ \ \\ \` - -- --- | || ' '' ''' \" \"\" ## # \" ~ & && ${SOURCE_COLOR}² <>aA${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:$complex_str" | format_output "* " "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t019AbcD E FxYZ,,?&|+;;. ...:://!! != §%% ${SOURCE_COLOR}* ${NC}*${SOURCE_COLOR}* ${NC} ***${SOURCE_COLOR}* ${NC} µùéèàâêîôûŷäëïöüÿãẽĩõũỹ^^¨¨\$\$££ = == === {{}} {} + ++ +++ (( )) ( ) () [[ ]] [ ] [] ° @ / // /${SOURCE_COLOR}* ${NC}*/ ^ ^^ _ __ \ \\ \` - -- --- | || ' '' ''' \" \"\" ## # \" ~ & && ² <>aA")
check_test_condition "$res" "$exp" $((test_idx++))
test_idx=300
# (correlated strings)
res=$(correlated_strings "abc" "dabe" 0 0)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "dae" "ab" 1 0)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "abc" "abc" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "aBc" "AbC" 0 0)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "aBc" "AbC" 1 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "xxxdabc" "d" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "xxxdabc" "Da" 0 0)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "xxxdabc" "Dab" 1 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "d" "xxxdabc" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "Da" "xxxdabc" 0 0)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "Dab" "xxxdabc" 1 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "xxxda" "abc_" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "xxxdabc" "abc_" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "xxxdabc" "aBc_" 0 0)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "xxxdabc" "aBc_" 1 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "abc_" "xxxda" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "abc_" "xxxdabc" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "aBc_""xxxdabc" 0 0)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "aBc_" "xxxdabc" 1 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "\\" "\\" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "\$\\" "\\^" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "\\^" "\$\\" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "%µ" "µ¨¨¨¨" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "µ¨¨¨¨" "%µ" 0 0)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "abc" "abc" 0 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "abc" "a" 0 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "a" "abc" 0 1)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "xdabc" "abc_" 0 1)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "abc_" "xabc" 0 1)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "a,b,cd" "!efg" 0 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "!efg" "a,b,cd" 0 1)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "a,b,cd" "b!cd" 0 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "b!cd" "a,b,cd" 0 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "b!cdxxx" "yyya,b,cd" 0 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "a,b,cdxxx" "!a!b!" 0 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "!a!b!" ",,a,b!xxx" 0 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "A,b;cd" "\$\\a!B-" 1 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "\$\\a!B-" "A,b;cd" 1 1)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "\$\\a!B-" "A,b;;cd" 1 1)
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "a" "" 1 1)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
res=$(correlated_strings "" "a" 1 1)
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
test_idx=400
# (non-printable characters)
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< " Hel lo ")" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< $'\x00')" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< $'a\x00b')" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< $'\x01')" != "" ]]; then # code duplicated from actual code"
res=1
else
res=0
fi
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< $'\ax02b')" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< $' abc \x08\x08 def ')" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="1"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< $'\x0D')" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< $'\x09')" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< $'a\x09b')" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< $'abc\x09\x0Ddef')" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
if [[ "$(sed 's/[[:print:][:blank:]\x0D\x09]//g' <<< 'abcµùéèàâêîôûŷäëïöüÿãẽĩõũỹdef')" != "" ]]; then # code duplicated from actual code
res=1
else
res=0
fi
exp="0"
check_test_condition "$res" "$exp" $((test_idx++))
# (binary lines)
res=$(echo $'./a/b/c/file name.cpp:123:xx\x01aBcxx' | format_output "aBc" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\txx\x01${SOURCE_COLOR}aBc${NC}xx")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo $'./a/b/c/file name.xxx:123:xx\x01aBcxx' | format_output "aBc" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.xxx:123${NC} ${OTHER_COLOR}binary line(s)${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo $'./a/b/c/file name.bin:123:xxaBcxx' | format_output "aBc" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.bin:123${NC} ${OTHER_COLOR}binary line(s)${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo $'./a/b/c/file name.cpp:123: aBcAbcd \x00DEF ' | format_output "Abcd" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc${SOURCE_COLOR}Abcd${NC}")
check_test_condition "$res" "$exp" $((test_idx++))
# (non-ASCII character: µ is encoded on 2 bytes)
res=$(echo "./a/b/c/file name.cpp:123:µµµabcDEF" | format_output "µµabcD" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\tµ${SOURCE_COLOR}µµabcD${NC}EF")
check_test_condition "$res" "$exp" $((test_idx++))
# (non-ASCII character: § is encoded on 2 bytes)
res=$(echo "./a/b/c/file name.cpp:123:x§µ§abcD§EF" | format_output "§abcD§" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\tx§µ${SOURCE_COLOR}§abcD§${NC}EF")
check_test_condition "$res" "$exp" $((test_idx++))
# (\x09=\t shall be ignored for binary line detection)
res=$(echo $'./a/b/c/file name.hpp:123:AB\x09abcDE\tF' | format_output $'\x09' "$grep_ignorecase_option" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.hpp:123${NC}\t")
exp=$(echo -e "${exp}AB${HEADER_COLOR}\t${NC}abcDE${HEADER_COLOR}\t${NC}F")
check_test_condition "$res" "$exp" $((test_idx++))
# (\x09=\t shall be ignored for binary line detection)
res=$(echo $'./a/b/c/file name.hpp:123:AB\x09abcDEF' | format_output $'b\x09a' "$grep_ignorecase_option" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.hpp:123${NC}\t")
exp=$(echo -e "${exp}A${HEADER_COLOR}B\ta${NC}bcDEF")
check_test_condition "$res" "$exp" $((test_idx++))
# (\x0D=\r shall be ignored for binary line detection)
res=$(echo $'./a/b/c/file name.hpp:123:AB\x0DabcDE\rF' | format_output $'\x0D' "$grep_ignorecase_option" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.hpp:123${NC}\t")
exp=$(echo -e "${exp}AB${HEADER_COLOR}\r${NC}abcDE${HEADER_COLOR}\r${NC}F")
check_test_condition "$res" "$exp" $((test_idx++))
# (\x0D=\r shall be ignored for binary line detection)
res=$(echo $'./a/b/c/file name.hpp:123:AB\x0DabcDEF' | format_output $'b\x0Da' "$grep_ignorecase_option" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.hpp:123${NC}\t")
exp=$(echo -e "${exp}A${HEADER_COLOR}B\ra${NC}bcDEF")
check_test_condition "$res" "$exp" $((test_idx++))
test_idx=500
# (file path and line number extraction)
res=$(echo "./a/b/c/file name.cpp:123:aBc - -- ----- edF" | format_output "--" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\taBc - ${SOURCE_COLOR}--${NC} ${SOURCE_COLOR}--${NC}${SOURCE_COLOR}--${NC}- edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.hpp:123: aBc - -- ----- edF " | format_output "--" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.hpp:123${NC}\taBc - ${HEADER_COLOR}--${NC} ${HEADER_COLOR}--${NC}${HEADER_COLOR}--${NC}- edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./x:1:aBc - -- ----- edF" | format_output "--" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 x:1${NC}\taBc - ${OTHER_COLOR}--${NC} ${OTHER_COLOR}--${NC}${OTHER_COLOR}--${NC}- edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:456:aBc - -- ----- edF" | format_output "--" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\t456:aBc - ${SOURCE_COLOR}--${NC} ${SOURCE_COLOR}--${NC}${SOURCE_COLOR}--${NC}- edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:123:xyz:aBc - -- ----- edF" | format_output "--" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:123${NC}\txyz:aBc - ${SOURCE_COLOR}--${NC} ${SOURCE_COLOR}--${NC}${SOURCE_COLOR}--${NC}- edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:xyz:123:aBc - -- ----- edF" | format_output "--" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp:xyz:123${NC}\taBc - ${OTHER_COLOR}--${NC} ${OTHER_COLOR}--${NC}${OTHER_COLOR}--${NC}- edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./x:y:1:aBc - -- ----- edF" | format_output "--" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 x:y:1${NC}\taBc - ${OTHER_COLOR}--${NC} ${OTHER_COLOR}--${NC}${OTHER_COLOR}--${NC}- edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp-123-xxx:456:aBc - -- ----- edF" | format_output "--" "" "" 0);
exp=$(echo -e "${FILEPATH_COLOR}1 a/b/c/file name.cpp-123-xxx:456${NC}\taBc - ${OTHER_COLOR}--${NC} ${OTHER_COLOR}--${NC}${OTHER_COLOR}--${NC}- edF")
check_test_condition "$res" "$exp" $((test_idx++))
res=$(echo "./a/b/c/file name.cpp:xxx:123" | format_output "B" "" "" 0 2>&1);
if [[ "$res" == *"cannot extract filepath and line number from"* ]]; then
echo "OK $((test_idx++))"
else
echo "ERROR $((test_idx++))"