Debian Codegraph @

dwtest/test_regex.praat

raw
1 # test_regex.praat
2 # djmw 20070528, 20070917, 20080122, 20110526
3
4 printline test_regex
5
6 debug = 0
7
8 procedure match_index .s$ .match$ .i
9 .il = index_regex (.s$, .match$)
10 if debug = 1
11 printline '.i' ('.il' = index_regex ("'.s$'", "'.match$'"))
12 endif
13 assert .il = .i; index_regex ("'.s$'", "'.match$'")
14 endproc
15
16 procedure match_rindex .s$ .match$ .i
17 .ir = rindex_regex (.s$, .match$)
18 if debug = 1
19 printline '.i' ('.ir' = rindex_regex ("'.s$'", "'.match$'"))
20 endif
21 assert .ir = .i; rindex_regex ("'.s$'", "'.match$'")
22 endproc
23
24 procedure replace_re .string$ .search$ .replace$ .n .result$
25 .r$ = replace_regex$ (.string$, .search$, .replace$, .n)
26 if debug = 1
27 printline '.result$' ("'.r$'" = replace_regex$ ("'.string$'", "'.search$'", "'.replace$'", "'.n'"))
28 endif
29 assert .r$ = .result$; '.result$' ("'.r$'" = replace_regex ("'.string$'", "'.search$'", "'.replace$'", "'.n'"))
30 endproc
31
32 # ordinary characters
33
34 printline ---- # match characters
35 alphabet$ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
36
37 s$ = alphabet$
38 sl = length (s$)
39 sd$ = s$ + s$
40
41 for i to 52
42 match$ = mid$ (s$, i, 1)
43 call match_index "'s$'" "'match$'" i
44 ir = i + sl
45 call match_rindex "'sd$'" "'match$'" ir
46 endfor
47
48 printline ---- # special characters # backslash \
49
50 call match_index "cscscAaa\n" "aa\\n" 7
51 call match_rindex "cscscAaa\n" "aa\\n" 7
52
53 call match_index "cscscAa+a+" "b\+" 0
54 call match_index "cscscAa+a+" "a\+" 7
55 call match_rindex "cscscAa+a+" "b\+" 0
56 call match_rindex "cscscAa+a+" "a\+" 9
57
58 printline ---- # special characters # caret ^
59
60 call match_index "cscscAa+a+" "^c" 1
61 call match_rindex "cscscAa+a+" "^c" 1
62
63 printline ---- # special characters # dollar $
64
65 call match_index "cscscAa+ac" "c$" 10
66 call match_rindex "cscscAa+ac" "c$" 10
67
68 printline ---- # special characters # quantifier brackets {}
69
70 call match_index "cscscAaa\n" "a{1}" 7
71 call match_rindex "cscscAaa\n" "a{1}" 8
72 call match_index "cscscAaa\n" "a{2}" 7
73 call match_rindex "cscscAaa\n" "a{2}" 7
74 call match_index "cscscAaa\n" "a{2,}" 7
75 call match_rindex "cscscAaa\n" "a{2,}" 7
76 call match_index "cscscAaa\n" "a{,2}" 1
77 call match_rindex "cscscAaa\n" "a{,2}" 11
78 call match_index "cscscAaa\n" "a{1,2}" 7
79 call match_rindex "cscscAaa\n" "a{1,2}" 8
80
81 printline ---- # special characters # open and close brackets []
82
83 s$ = alphabet$
84 sd$ = s$ + s$
85 for i to 26
86 # match [a-z], [b-z], ..., [z-z]
87 bl$ = mid$ (s$, i, 1)
88 match$ = "[" + bl$ + "-z]"
89 call match_index "'s$'" "'match$'" i
90 call match_rindex "'sd$'" "'match$'" 78
91 # now for uppercase
92 bu$ = mid$ (s$, i+26, 1)
93 match$ = "[" + bu$ + "-Z]"
94 il = i+26
95 call match_index "'s$'" "'match$'" il
96 ir = 104
97 call match_rindex "'sd$'" "'match$'" ir
98 # match [a-a], [b-b], [z-z]
99 match$ = "[" + bl$ + "-" + bl$ +"]"
100 call match_index "'s$'" "'match$'" i
101 ir = 52+i
102 call match_rindex "'sd$'" "'match$'" ir
103 endfor
104
105 printline ---- # special characters # grouping characters ()
106
107 call match_index "c" "(ab)" 0
108 call match_index "cc" "(ab)" 0
109 call match_index "ccc" "(ab)" 0
110 call match_index "ac" "(ab)" 0
111 call match_index "acc" "(ab)" 0
112 call match_index "accc" "(ab)" 0
113 call match_index "aca" "(ab)" 0
114 call match_index "acca" "(ab)" 0
115 call match_index "accca" "(ab)" 0
116
117 call match_index "ababcccc" "(ab)" 1
118 call match_rindex "ababcccc" "(ab)" 3
119 call match_index "cababcccc" "(ab)" 2
120 call match_rindex "cababcccc" "(ab)" 4
121 call match_index "cccccccabab" "(ab)" 8
122 call match_rindex "cccccccabab" "(ab)" 10
123 call match_index "ababcccc" "(ab)\1" 1
124 call match_rindex "ababcccc" "(ab)\1" 1
125
126 printline ---- # special characters # dot .
127
128 call match_index "ababcccc" ".d" 0
129 call match_rindex "ababcccc" ".d" 0
130 call match_index "ababcccc" ".b" 1
131 call match_rindex "ababcccc" ".b" 3
132 call match_index "ababcccc" ".a" 2
133 call match_rindex "ababcccc" ".a" 2
134 call match_index "ababcccc" ".c" 4
135 call match_rindex "ababcccc" ".c" 7
136
137 printline ---- # special characters # star *
138
139 call match_index "ababccccd" "b*" 1
140 call match_index "ababccccd" "ab*" 1
141 call match_rindex "ababccccd" "ab*" 3
142 call match_index "ababccccd" "^.*$" 1
143
144 printline ---- # special characters # plus +
145
146 call match_index "ababccccd" "b+" 2
147 call match_rindex "ababccccd" "b+" 4
148
149 printline ---- # special characters # question mark ?
150
151 call match_index "ababccccd" "ab?" 1
152 call match_rindex "ababccccd" "ab?" 3
153
154 printline ---- # quantifiers # *
155
156 call replace_re "ab" "(ab)c*" "x" 0 x
157 call replace_re "abc" "(ab)c*" "x" 0 x
158 call replace_re "abcc" "(ab)c*" "x" 0 x
159 call replace_re "abcccd" "(ab)c*" "x" 0 xd
160
161 call replace_re "ab" "(ab)c+" "x" 0 ab
162 call replace_re "abc" "(ab)c+" "x" 0 x
163 call replace_re "abcc" "(ab)c+" "x" 0 x
164 call replace_re "abcccd" "(ab)c+" "x" 0 xd
165 call replace_re "yyabcccd" "(ab)c+" "x" 0 yyxd
166
167 printline ---- # non greedy ?
168
169 call replace_re "abc" "(ab)c*?" "x" 0 xc
170 call replace_re "abcc" "(ab)c*?" "x" 0 xcc
171 call replace_re "abcccd" "(ab)c*?" "x" 0 xcccd
172
173 call replace_re "abcc" "(ab)c+?" "x" 0 xc
174 call replace_re "abcccd" "(ab)c+?" "x" 0 xccd
175
176 printline ---- # anchors
177
178 call replace_re "ababccccd" "ab" "x" 1 xabccccd
179
180 call replace_re "ababccccd" "^ab" "x" 1 xabccccd
181
182 printline --- # empty string
183
184 call replace_re "" ".*" "aaa" 0 aaa
185 call replace_re "" ".*" "aaa" 1 aaa
186 call replace_re "" ".*" "aaa" 10 aaa
187
188 # special constructs with parentheses
189
190 # special control characters
191
192 # convenience escapes
193
194 # octal and hexadecima escapes
195
196 # sustitution special characters
197
198 printline ---- # replaced string much longer than original
199
200 call replace_re "ababababababababababababababababababab" "b" "xxxxx" 1 axxxxxabababababababababababababababababab
201
202 # The returned value is "xxccccd" instead of "xabccccd".
203 call replace_re "ababccccd" "^ab" "x" 0 xabccccd
204
205 printline --------- PREVIOUS BUGS < 5.0.4
206 printline ---- # replace by empty string ""
207 # Bugs in praat versions before 5.0.4
208 # Returned empty string ""
209 call replace_re "abc " " $" "" 1 abc
210 # Returned "xxccccd" instead of "xabccccd".
211 call replace_re "ababccccd" "^ab" "x" 0 xabccccd
212 printline --------- PREVIOUS BUGS < 5.0.5
213 call replace_re "hallo" "l" "b" 0 habbo
214 call replace_re "hallo" "." "&&" 0 hhaalllloo
215 printline --------- PREVIOUS BUGS --- END
216
217 printline --------- BUG in 5.0.40
218 call replace_re "c " ".*" "aaaa" 0 aaaa
219 printline --------- PREVIOUS BUGS --- END
220
221 printline test_regex OK