-
Notifications
You must be signed in to change notification settings - Fork 1
/
mulle-string.sh
executable file
·1507 lines (1287 loc) · 30 KB
/
mulle-string.sh
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
# shellcheck shell=bash
# shellcheck disable=SC2236
# shellcheck disable=SC2166
# shellcheck disable=SC2006
#
# Copyright (c) 2017 Nat! - Mulle kybernetiK
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of Mulle kybernetiK nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
if ! [ ${MULLE_STRING_SH+x} ]
then
MULLE_STRING_SH='included'
[ -z "${MULLE_BASHGLOBAL_SH}" ] && _fatal "mulle-bashglobal.sh must be included before mulle-file.sh"
[ -z "${MULLE_COMPATIBILITY_SH}" ] && _fatal "mulle-compatibility.sh must be included before mulle-string.sh"
# RESET
# NOCOLOR
#
# Assortment of various string functions.
#
# Functions prefixed "r_" return the result in the global variable RVAL.
# The return value 0 indicates success.
#
# TITLE INTRO
# COLOR
# ####################################################################
# Conversion
# ####################################################################
#
# RESET
# NOCOLOR
#
# conversion function do simple conversions such as turning a string
# uppercase or removing whitespace. The value in these functions is
# portability across zsh and older bash versions.
#
# SUBTITLE Conversion
# COLOR
#
# r_trim_whitespace <string>
#
# Remove surrounding whitespace from a <string>. Does not touch whitespace in
# the string.
# " VfL Bochum 1848 " -> "VfL Bochum 1848"
#
function r_trim_whitespace()
{
# taken from:
# https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
RVAL="$*"
RVAL="${RVAL#"${RVAL%%[![:space:]]*}"}"
RVAL="${RVAL%"${RVAL##*[![:space:]]}"}"
}
#
# r_upper_firstchar <s>
#
# Turn the first character of <s> into uppercase.
# Example: "vfl Bochum" -> "Vfl Bochum"
#
function r_upper_firstchar()
{
case "${BASH_VERSION:-}" in
[0123]*)
RVAL="`printf "%s" "${1:0:1}" | tr '[:lower:]' '[:upper:]'`"
RVAL="${RVAL}${1:1}"
;;
*)
if [ ${ZSH_VERSION+x} ]
then
RVAL="${1:0:1}"
RVAL="${RVAL:u}${1:1}"
else
RVAL="${1^}"
fi
;;
esac
}
#
# r_capitalize <s> ...
#
# Turn the first character of <s> into uppercase. The remaining characters
# become lowercase.
# Example: "VFL Bochum" -> "Vfl bochum"
#
function r_capitalize()
{
r_lowercase "$@"
r_upper_firstchar "${RVAL}"
}
#
# r_uppercase <s>
#
# Turn all character of <s> into uppercase.
# Example: "VFL Bochum" -> "VFL BOCHUM"
#
function r_uppercase()
{
case "${BASH_VERSION:-}" in
[0123]*)
RVAL="`printf "%s" "$1" | tr '[:lower:]' '[:upper:]'`"
;;
*)
if [ ${ZSH_VERSION+x} ]
then
RVAL="${1:u}"
else
RVAL="${1^^}"
fi
;;
esac
}
#
# r_lowercase <s>
#
# Turn all character of <s> into uppercase.
# Example: "VfL Bochum" -> "vfl bochum"
#
function r_lowercase()
{
case "${BASH_VERSION:-}" in
[0123]*)
RVAL="`printf "%s" "$1" | tr '[:upper:]' '[:lower:]'`"
;;
*)
if [ ${ZSH_VERSION+x} ]
then
RVAL="${1:l}"
else
RVAL="${1,,}"
fi
;;
esac
}
#
# r_identifier <s>
#
# replace any non-identifier characters in <s> with '_'
# Example: foo|bar becomes foo_bar
#
function r_identifier()
{
# works in bash 3.2
# may want to disambiguate mulle-scion and MulleScion with __
# but it looks surprising for mulle--testallocator
#
RVAL="${1//-/_}" # __
RVAL="${RVAL//[^a-zA-Z0-9]/_}"
case "${RVAL}" in
[0-9]*)
RVAL="_${RVAL}"
;;
esac
}
#
# r_extended_identifier <s>
#
# An extended identifier can start and contain any letter digit
# and +-=:._. It' assumed that these characters need to quoting
#
# Example: f.oo|b-ar becomes f.oo_b-ar
#
function r_extended_identifier()
{
RVAL="${1//[^a-zA-Z0-9+:.=_-]/_}"
}
# ####################################################################
# Concatenation
# ####################################################################
#
# RESET
# NOCOLOR
#
# concat functions append two strings, possibly separated by a separator
# string. Care is taken, that an empty string does not produce a leading
# or dangling separator. Some functions go a step further and remove
# duplicate and leading/dangling separators, which are considered ugly.
#
# append functions do not use a separator.
#
# SUBTITLE Concatenation
# COLOR
#
# r_append <s1> <s2>
#
# Concatenates two strings. Same as "${s1}${s2}"
# "a" "b" -> "ab"
#
function r_append()
{
RVAL="${1}${2}"
}
#
# r_concat <s1> <s2> [separator]
#
# Concatenates two strings with a separator in between.
# If one or both strings are empty, the separator is omitted.
# This function does not remove duplicate separators.
#
# "a" "b" -> "a b"
# "" "a" -> "a"
# "a " " b" "-" -> "a - b"
# "a-" "-b" "-" -> "a---b"
#
function r_concat()
{
local separator="${3:- }"
if [ -z "${1}" ]
then
RVAL="${2}"
else
if [ -z "${2}" ]
then
RVAL="${1}"
else
RVAL="${1}${separator}${2}"
fi
fi
}
#
# r_remove_duplicate_separators <s1> [separator]
#
# Removes all duplicate separators. The default separator is " ".
# "//x///y//" -> "/x/y/"
#
r_remove_duplicate_separators()
{
local s="$1"
local separator="${2:- }"
local escaped
local dualescaped
local replacement
printf -v escaped '%q' "${separator}"
dualescaped="${escaped//\//\/\/}"
replacement="${separator}"
case "${separator}" in
*/*)
replacement="${escaped}"
;;
esac
local old
RVAL="${s}"
old=''
while [ "${RVAL}" != "${old}" ]
do
old="${RVAL}"
RVAL="${RVAL//${dualescaped}${dualescaped}/${replacement}}"
done
}
#
# r_remove_ugly <s1> [separator]
#
# Removes separators from front and back. Removes duplicate separators from
# the middle. Works for a "common" set of separators.
# Mainly used to clean up filepaths.
#
# "//x//y//" -> "x/y"
#
r_remove_ugly()
{
local s="$1"
local separator="${2:- }"
local escaped
local dualescaped
local replacement
printf -v escaped '%q' "${separator}"
dualescaped="${escaped//\//\/\/}"
replacement="${separator}"
case "${separator}" in
*/*)
replacement="${escaped}"
;;
esac
local old
RVAL="${s}"
old=''
while [ "${RVAL}" != "${old}" ]
do
old="${RVAL}"
RVAL="${RVAL##[${separator}]}"
RVAL="${RVAL%%[${separator}]}"
RVAL="${RVAL//${dualescaped}${dualescaped}/${replacement}}"
done
}
#
# r_colon_concat <s1> <s2>
#
# concatenate strings, separating them with a ':'
# use for PATHs. This function removes duplicate ':' as well as leading
# and trailing ':'
#
function r_colon_concat()
{
r_concat "$1" "$2" ":"
r_remove_ugly "${RVAL}" ":"
}
#
# r_comma_concat <s1> <s2>
#
# concatenate strings, separating them with a ','
# use for lists w/o empty elements. This function removes duplicate ','
# as well as leading and trailing ','.
#
function r_comma_concat()
{
r_concat "$1" "$2" ","
r_remove_ugly "${RVAL}" ","
}
#
# r_semicolon_concat <s1> <s2>
#
# concatenate strings, separating them with a ';'
# use for CSV. This function does not remove duplicate ';' or leading or
# trailing ones.
#
function r_semicolon_concat()
{
r_concat "$1" "$2" ";"
}
#
# r_slash_concat <s1> <s2>
#
# concatenate strings, separating them with a '/'.
# Use for filepaths, as this is "cross-platform", because the paths on
# MINGW are converted already from '\' to '/'.
# This function removes duplicate '/' but leaves trailing and leading '/'
# intact.
#
function r_slash_concat()
{
r_concat "$1" "$2" "/"
r_remove_duplicate_separators "${RVAL}" "/"
}
# ####################################################################
# Lists
# ####################################################################
#
# RESET
# NOCOLOR
#
# A "list" is a string that consists of substrings (items), separated by a
# separator string. A special sort of "list" uses the linefeed ($'\n') as
# the item separator. Here the item is called a "line" and the list is
# called "lines". There is extensive support for handling such line lists.
#
# SUBTITLE Lists
# COLOR
#
# r_list_remove <list> <value> [separator]
#
# remove a value from a list.
# Example:
# r_list_remove "a b c" "b" will return "a c"
#
function r_list_remove()
{
local sep="${3:- }"
RVAL="${sep}$1${sep}//${sep}$2${sep}/}"
RVAL="${RVAL##"${sep}"}"
RVAL="${RVAL%%"${sep}"}"
}
#
# r_list_remove <list> <value>
#
# remove value from a colon separated list
#
function r_colon_remove()
{
r_list_remove "$1" "$2" ":"
}
#
# r_comma_remove <list> <value>
#
# Remove value from a comma separated list.
#
function r_comma_remove()
{
r_list_remove "$1" "$2" ","
}
#
# r_add_line <lines> <line>
#
# Add a <line> to a <lines> which consists of zero, one or multiple
# substrings separated by linefeeds.
#
# this function suppresses empty lines. To not suppress empty lines
# use r_add_line_lf (in 'array')
#
function r_add_line()
{
if [ ! -z "${1:0:1}" -a ! -z "${2:0:1}" ]
then
RVAL="$1"$'\n'"$2"
else
RVAL="$1$2"
fi
}
#
# r_remove_line <lines> <search>
#
# Remove a line from a string that contains zero, one or multiple
# substrings separated by linefeeds.
#
# Multiple occurences will be deleted
#
function r_remove_line()
{
local lines="$1"
local search="$2"
local line
local delim
delim=""
RVAL=
.foreachline line in ${lines}
.do
if [ "${line}" != "${search}" ]
then
RVAL="${RVAL}${delim}${line}"
delim=$'\n'
fi
.done
}
#
# r_remove_line_once <lines> <search>
#
# Remove a line from a string that contains zero, one or multiple
# substrings separated by linefeeds.
#
# Only one occurence will be deleted
#
function r_remove_line_once()
{
local lines="$1"
local search="$2"
local line
local delim
delim=""
RVAL=
.foreachline line in ${lines}
.do
if [ -z "${search}" -o "${line}" != "${search}" ]
then
RVAL="${RVAL}${delim}${line}"
delim=$'\n'
else
search=""
fi
.done
}
#
# r_get_last_line <lines>
#
# Retrieve the last line from a string that contains zero, one or multiple
# substrings separated by linefeeds.
#
function r_get_last_line()
{
RVAL="$(sed -n '$p' <<< "$1")" # get last line
}
#
# r_line_at_index <lines> <index>
#
# Retrieve a line by its index (which starts at 0) from a string that
# contains zero, one or multiple substrings separated by linefeeds..
#
function r_line_at_index()
{
RVAL="$(sed -n -e "$(( $2 + 1 ))p" <<< "$1")"
}
#
# r_remove_last_line <lines>
#
# Remove the last line from a string that contains zero, one or multiple
# substrings separated by linefeeds.
#
function r_remove_last_line()
{
RVAL="$(sed '$d' <<< "$1")" # remove last line
}
#
# find_item <s> <search> [separator]
#
# Check if a search string is contained as a substring of a string s. The
# string consists of separated by separator, which by default is the ','.
#
# Returns 0 if found, 1 if not found
#
# can't have linefeeds as delimiter
# e.g. find_item "a,b,c" b -> 0
# find_item "a,b,c" d -> 1
# find_item "a,b,c" "," -> 1
#
function find_item()
{
local line="$1"
local search="$2"
local delim="${3:-,}"
shell_is_extglob_enabled || _internal_fail "need extglob enabled"
if [ ${ZSH_VERSION+x} ]
then
case "${delim}${line}${delim}" in
*"${delim}${~search}${delim}"*)
return 0
;;
esac
else
case "${delim}${line}${delim}" in
*"${delim}${search}${delim}"*)
return 0
;;
esac
fi
return 1
}
#
# find_line is fairly critical for mulle-sourcetree walk, which
# is the slowest operation and most used operation. Don't dick
# around with this without profiling!
#
_find_empty_line_zsh()
{
local lines="$1"
case "${lines}" in
*$'\n'$'\n'*)
return 0
;;
esac
return 1
}
# zsh:
# this is faster than calling grep -F externally (for small arrays)
# this is faster than while read line <<< lines
# this is faster than case ${lines} in
#
_find_line_zsh()
{
local lines="$1"
local search="$2"
if [ -z "${search:0:1}" ]
then
if [ -z "${lines:0:1}" ]
then
return 0
fi
_find_empty_line_zsh "${lines}"
return $?
fi
local line
.foreachline line in ${lines}
.do
if [ "${line}" = "${search}" ]
then
return 0
fi
.done
return 1
}
#
# find_line <lines> <line>
#
# Check if a substring <line> is contained in the <lines> string, which
# consists of substrings separated by linefeed.
#
# Returns 0 if found, 1 if not found
#
function find_line()
{
# bash:
# this is faster than calling grep -F externally
# this is faster than while read line <<< lines
# this is faster than for line in lines
#
# ZSH is apparently super slow in pattern matching
if [ ${ZSH_VERSION+x} ]
then
_find_line_zsh "$@"
return $?
fi
local lines="$1"
local search="$2"
local escaped_lines
local pattern
# ensure leading and trailing linefeed for matching and $'' escaping
printf -v escaped_lines "%q" "
${lines}
"
# add a linefeed here to get also $'' escaping
printf -v pattern "%q" "${search}
"
# remove $'
pattern="${pattern:2}"
# remove \n'
pattern="${pattern%???}"
local rval
rval=1
shell_is_extglob_enabled || _internal_fail "extglob must be enabled"
if [ ${ZSH_VERSION+x} ]
then
case "${escaped_lines}" in
*"\\n${~pattern}\\n"*)
rval=0
;;
esac
else
case "${escaped_lines}" in
*"\\n${pattern}\\n"*)
rval=0
;;
esac
fi
return $rval
}
#
# r_count_lines <lines>
#
# Count the number of lines contained in <lines>.
#
function r_count_lines()
{
local array="$1"
RVAL=0
local line
.foreachline line in ${array}
.do
RVAL=$((RVAL + 1))
.done
}
#
# r_add_unique_line <lines> <line>
#
# Add <line> to <lines>, but ensures that this does not introduce a
# new duplicate.
#
function r_add_unique_line()
{
local lines="$1"
local line="$2"
if [ -z "${line:0:1}" -o -z "${lines:0:1}" ]
then
RVAL="${lines}${line}"
return
fi
if find_line "${lines}" "${line}"
then
RVAL="${lines}"
return
fi
RVAL="${lines}
${line}"
}
#
# r_remove_duplicate_separators_lines <lines>
#
# Remove any duplicate strings in <lines>
#
function r_remove_duplicate_separators_lines()
{
RVAL="`awk '!x[$0]++' <<< "$@"`"
}
#
# remove_duplicate_lines <lines> ...
#
# Remove any duplicate strings in <lines>. Print result to stdout.
#
function remove_duplicate_lines()
{
awk '!x[$0]++' <<< "$@"
}
#
# remove_duplicate_lines_stdin
#
# Remove any duplicate strings in stdin. Print result to stdout.
#
function remove_duplicate_lines_stdin()
{
awk '!x[$0]++'
}
#
# r_reverse_lines <lines>
#
# Reverse the order of <lines>.
# For very many lines use
# `sed -n '1!G;h;$p' <<< "${lines}"`"
#
function r_reverse_lines()
{
local lines="$1"
local line
local delim
delim=""
RVAL=
IFS=$'\n'
while read -r line
do
RVAL="${line}${delim}${RVAL}"
delim=$'\n'
done <<< "${lines}"
IFS="${DEFAULT_IFS}"
}
#
# r_split <string> [sep]
#
# Parse substrings of <string> separated by <sep> into an array returned
# as RVAL. The default separator is the contents of the IFS variable.
#
# e.g. r_split "a,b,c" ","
# printf "%s" "${RVAL[*]}"
#
function r_split()
{
local s="$1"
local sep="${2:-${IFS}}"
if [ ${ZSH_VERSION+x} ]
then
unset RVAL
RVAL=("${(@ps:$sep:)s}")
else
shell_disable_glob
IFS="${sep}" read -r -a RVAL <<< "${s}"
shell_enable_glob
fi
}
#
# r_betwixt <sep> ...
#
# Interpose a string between array elements to create a large string.
#
# e.g. r_betwixt ',' a b c -> "a,b,c"
#
function r_betwixt()
{
local sep="$1" ; shift
local tmp
printf -v tmp "%s${sep}" "$@"
RVAL="${tmp%"${sep}"}"
}
# ####################################################################
# Strings
# ####################################################################
#
#
# is_yes <s>
#
# this non-localized variant detects 0/1 yes/no on/off y/n and
# upper lowercase variante and returns 0 for YES, 1 for NO or empty
# and 4 for all other values
#
is_yes()
{
local s
case "$1" in
[yY][eE][sS]|[yY]|1|[oO][nN])
return 0
;;
[nN][oO]|[nN]|0|[oO][fF][fF]|"")
return 1
;;
*)
return 4
;;
esac
}
# ####################################################################
# Escape
# ####################################################################
#
# RESET
# NOCOLOR
#
# "esape" functions prefer arbitarty strings for processing with the
# external tools like `sed` or internal use in `eval`.
#
# SUBTITLE Conversion
# COLOR
#
#
# unused code
#
# escape_linefeeds()
# {
# local text
#
# text="${text//\|/\\\|}"
# printf "%s" "${text}" | tr '\012' '|'
# }
#
#
# _unescape_linefeeds()
# {
# tr '|' '\012' | sed -e 's/\\$/|/g' -e '/^$/d'
# }
#
#
# unescape_linefeeds()
# {
# printf "%s\n" "$@" | tr '|' '\012' | sed -e 's/\\$/|/g' -e '/^$/d'
# }
#
# r_escaped_grep_pattern <s>
#
# escape a string for use as a grep search pattern
#
# this is heaps faster than the sed code
#
function r_escaped_grep_pattern()
{
local s="$1"
s="${s//\\/\\\\}"
s="${s//\[/\\[}"
s="${s//\]/\\]}"
# s="${s//\//\\/}"
s="${s//\$/\\$}"
s="${s//\*/\\*}"
s="${s//\./\\.}"
s="${s//\^/\\^}"
s="${s//\|/\\|}"
RVAL="$s"
}
#
# r_escaped_sed_pattern <s>
#
# escape a string for use as a sed search pattern
#
# assumed that / is used like in sed -e 's/x/y/'
#
function r_escaped_sed_pattern()
{
local s="$1"
s="${s//\\/\\\\}"