OILS / demo / survey-str-api.sh View on Github | oilshell.org

188 lines, 80 significant
1#!/usr/bin/env bash
2#
3# Survey string APIs
4#
5# Usage:
6# demo/survey-str-api.sh <function name>
7
8set -o nounset
9set -o pipefail
10set -o errexit
11
12source build/dev-shell.sh # python3 in $PATH
13
14# Python and JS string and regex replacement APIs
15
16string-replace() {
17 echo 'STRING PYTHON'
18 echo
19
20 # This is a float
21 python3 -c 'print("oils-for-unix".replace("i", "++"))'
22
23 # replace none
24 echo 'count=0'
25 python3 -c 'print("oils-for-unix".replace("i", "++", 0))'
26 echo
27
28 # replace all
29 echo 'count=-1'
30 python3 -c 'print("oils-for-unix".replace("i", "++", -1))'
31 echo
32
33 # Very weird empty string behavior -- it finds one between every char
34 python3 -c 'print("oils-for-unix".replace("", "++"))'
35 python3 -c 'print("oils-for-unix".replace("", "++", 1))'
36 echo
37
38 echo 'STRING JS'
39 echo
40 # Only replaces first occurrence!
41 # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
42 nodejs -e 'console.log("oils-for-unix".replace("i", "++"))'
43
44 nodejs -e 'console.log("oils-for-unix".replace("", "++"))'
45}
46
47regex-replace() {
48 echo 'REGEX PYTHON'
49 echo
50
51 python3 -c 'import re; p = re.compile("[i]"); print(p.sub("++", "oils-for-unix"))'
52
53 echo 'count=0 INCONSISTENT, replaces all'
54 python3 -c 'import re; p = re.compile("[i]"); print(p.sub("++", "oils-for-unix", count=0))'
55 echo
56
57 echo 'count=-1'
58 python3 -c 'import re; p = re.compile("[i]"); print(p.sub("++", "oils-for-unix", count=-1))'
59 echo
60
61 # empty string?
62 # It's consistent, it finds empty string between every char
63 python3 -c 'import re; p = re.compile(""); print(p.sub("++", "oils-for-unix"))'
64 echo
65
66 # supports equivalent of $0 and $1 ?
67 python3 -c 'import re; p = re.compile("[i](.)"); print(p.sub("[\g<0>]", "oils-for-unix"))'
68 python3 -c 'import re; p = re.compile("[i](.)"); print(p.sub("[\g<1>]", "oils-for-unix"))'
69 echo
70
71 # ^ means that only one replacement occurs
72 python3 -c 'import re; p = re.compile(r"(\d+)"); print(p.sub("[\g<1>]", "9-16-25\n100-200"))'
73 echo
74 python3 -c 'import re; p = re.compile(r"^(\d+)"); print(p.sub("[\g<1>]", "9-16-25\n100-200"))'
75 echo
76 # one replacement per line with re.MULTILINE!
77 python3 -c 'import re; p = re.compile(r"^(\d+)", re.MULTILINE); print(p.sub("[\g<1>]", "9-16-25\n100-200"))'
78 echo
79
80 echo 'REGEX JS'
81 echo
82
83 # Replaces first one
84 nodejs -e 'console.log("oils-for-unix".replace(/[i]/, "++"))'
85
86 # Replaces all
87 # no count param?
88 nodejs -e 'console.log("oils-for-unix".replace(/[i]/g, "++"))'
89
90 # Empty regex
91 nodejs -e 'console.log("oils-for-unix".replace(new RegExp(""), "++"))'
92
93 # Hm this is inconsistent -- empty string gets replaced everywhere
94 nodejs -e 'console.log("oils-for-unix".replace(new RegExp("", "g"), "++"))'
95
96 # Hm JavaScript does not support $0 for the whole match -- it has $& instead
97 # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
98 nodejs -e 'console.log("oils-for-unix".replace(new RegExp("[i](.)", "g"), "[$0]"))'
99 nodejs -e 'console.log("oils-for-unix".replace(new RegExp("[i](.)", "g"), "[$&]"))'
100 nodejs -e 'console.log("oils-for-unix".replace(new RegExp("[i](.)", "g"), "[$1]"))'
101 echo
102
103 # ^ means that only one replacement occurs
104 nodejs -e 'console.log("9-16-25\n100-200".replace(new RegExp("(\\d+)", "g"), "[$&]"))'
105 echo
106 nodejs -e 'console.log("9-16-25\n100-200".replace(new RegExp("^(\\d+)", "g"), "[$1]"))'
107 echo
108 # m flag is like re.MULTILINE
109 nodejs -e 'console.log("9-16-25\n100-200".replace(new RegExp("^(\\d+)", "gm"), "[$1]"))'
110 echo
111}
112
113survey-trim() {
114 echo 'PYTHON'
115 echo
116
117 # TODO: Test other unicode chars
118 local str=' hi '
119
120 python3 -c 'import sys; s = sys.argv[1]; print("[%s] [%s]" % (s, s.strip()))' "$str"
121
122 nodejs -e 'var s = process.argv[1]; var t = s.trim(); console.log(`[${s}] [${t}]`);' "$str"
123}
124
125survey-split() {
126 echo '============== PYTHON'
127 echo
128
129 python3 << EOF
130print('a,b,c'.split(','))
131print('aa'.split('a'))
132print('a<>b<>c<d'.split('<>'))
133print('a;b;;c'.split(';'))
134print(''.split('foo'))
135
136import re
137
138print(re.split(',|;', 'a,b;c'))
139print(re.split('.*', 'aa'))
140print(re.split('.', 'aa'))
141print(re.split('<>|@@', 'a<>b@@c<d'))
142print(re.split('\\s*', 'a b cd'))
143print(re.split('\\s+', 'a b cd'))
144print(re.split('.', ''))
145EOF
146
147 echo
148 echo '============== NODE'
149 echo
150
151 node << EOF
152console.log('a,b,c'.split(','))
153console.log('aa'.split('a'))
154console.log('a<>b<>c<d'.split('<>'))
155console.log('a;b;;c'.split(';'))
156console.log(''.split('foo'))
157
158console.log('a,b;c'.split(/,|;/))
159console.log('aa'.split(/.*/))
160console.log('aa'.split(/./))
161console.log('a<>b@@c<d'.split(/<>|@@/))
162console.log('a b cd'.split(/\s*/))
163console.log('a b cd'.split(/\s+/))
164console.log(''.split(/./))
165EOF
166
167 echo
168 echo '============== YSH'
169 echo
170
171 bin/ysh << EOF
172pp test_ ('a,b,c'.split(','))
173pp test_ ('aa'.split('a'))
174pp test_ ('a<>b<>c<d'.split('<>'))
175pp test_ ('a;b;;c'.split(';'))
176pp test_ (''.split('foo'))
177
178pp test_ ('a,b;c'.split(/ ',' | ';' /))
179pp test_ ('aa'.split(/ dot* /))
180pp test_ ('aa'.split(/ dot /))
181pp test_ ('a<>b@@c<d'.split(/ '<>' | '@@' /))
182pp test_ ('a b cd'.split(/ space* /))
183pp test_ ('a b cd'.split(/ space+ /))
184pp test_ (''.split(/ dot /))
185EOF
186}
187
188"$@"