1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Survey string APIs
|
4 | #
|
5 | # Usage:
|
6 | # demo/survey-str-api.sh <function name>
|
7 |
|
8 | set -o nounset
|
9 | set -o pipefail
|
10 | set -o errexit
|
11 |
|
12 | source build/dev-shell.sh # python3 in $PATH
|
13 |
|
14 | # Python and JS string and regex replacement APIs
|
15 |
|
16 | string-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 |
|
47 | regex-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 |
|
113 | survey-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 |
|
125 | survey-split() {
|
126 | echo '============== PYTHON'
|
127 | echo
|
128 |
|
129 | python3 << EOF
|
130 | print('a,b,c'.split(','))
|
131 | print('aa'.split('a'))
|
132 | print('a<>b<>c<d'.split('<>'))
|
133 | print('a;b;;c'.split(';'))
|
134 | print(''.split('foo'))
|
135 |
|
136 | import re
|
137 |
|
138 | print(re.split(',|;', 'a,b;c'))
|
139 | print(re.split('.*', 'aa'))
|
140 | print(re.split('.', 'aa'))
|
141 | print(re.split('<>|@@', 'a<>b@@c<d'))
|
142 | print(re.split('\\s*', 'a b cd'))
|
143 | print(re.split('\\s+', 'a b cd'))
|
144 | print(re.split('.', ''))
|
145 | EOF
|
146 |
|
147 | echo
|
148 | echo '============== NODE'
|
149 | echo
|
150 |
|
151 | node << EOF
|
152 | console.log('a,b,c'.split(','))
|
153 | console.log('aa'.split('a'))
|
154 | console.log('a<>b<>c<d'.split('<>'))
|
155 | console.log('a;b;;c'.split(';'))
|
156 | console.log(''.split('foo'))
|
157 |
|
158 | console.log('a,b;c'.split(/,|;/))
|
159 | console.log('aa'.split(/.*/))
|
160 | console.log('aa'.split(/./))
|
161 | console.log('a<>b@@c<d'.split(/<>|@@/))
|
162 | console.log('a b cd'.split(/\s*/))
|
163 | console.log('a b cd'.split(/\s+/))
|
164 | console.log(''.split(/./))
|
165 | EOF
|
166 |
|
167 | echo
|
168 | echo '============== YSH'
|
169 | echo
|
170 |
|
171 | bin/ysh << EOF
|
172 | pp test_ ('a,b,c'.split(','))
|
173 | pp test_ ('aa'.split('a'))
|
174 | pp test_ ('a<>b<>c<d'.split('<>'))
|
175 | pp test_ ('a;b;;c'.split(';'))
|
176 | pp test_ (''.split('foo'))
|
177 |
|
178 | pp test_ ('a,b;c'.split(/ ',' | ';' /))
|
179 | pp test_ ('aa'.split(/ dot* /))
|
180 | pp test_ ('aa'.split(/ dot /))
|
181 | pp test_ ('a<>b@@c<d'.split(/ '<>' | '@@' /))
|
182 | pp test_ ('a b cd'.split(/ space* /))
|
183 | pp test_ ('a b cd'.split(/ space+ /))
|
184 | pp test_ (''.split(/ dot /))
|
185 | EOF
|
186 | }
|
187 |
|
188 | "$@"
|