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 | "$@"
|