| 1 |
|
| 2 |
|
| 3 |
RSpec.describe RE2 do |
| 4 |
describe ".replace" do |
| 5 |
it "only replaces the first occurrence of the pattern" do |
| 6 |
expect(RE2.replace("woo", "o", "a")).to eq("wao") |
| 7 |
end |
| 8 |
|
| 9 |
it "supports inputs with null bytes" do |
| 10 |
expect(RE2.replace("w\0oo", "o", "a")).to eq("w\0ao") |
| 11 |
end |
| 12 |
|
| 13 |
it "supports patterns with null bytes" do |
| 14 |
expect(RE2.replace("w\0oo", "\0", "o")).to eq("wooo") |
| 15 |
end |
| 16 |
|
| 17 |
it "supports replacements with null bytes" do |
| 18 |
expect(RE2.replace("woo", "o", "\0")).to eq("w\0o") |
| 19 |
end |
| 20 |
|
| 21 |
it "performs replacement based on regular expressions" do |
| 22 |
expect(RE2.replace("woo", "o+", "e")).to eq("we") |
| 23 |
end |
| 24 |
|
| 25 |
it "supports flags in patterns" do |
| 26 |
expect(RE2.replace("Good morning", "(?i)gOOD MORNING", "hi")).to eq("hi") |
| 27 |
end |
| 28 |
|
| 29 |
it "does not perform replacements in-place", :aggregate_failures do |
| 30 |
name = "Robert" |
| 31 |
replacement = RE2.replace(name, "R", "Cr") |
| 32 |
|
| 33 |
expect(name).to eq("Robert") |
| 34 |
expect(replacement).to eq("Crobert") |
| 35 |
end |
| 36 |
|
| 37 |
it "supports passing an RE2::Regexp as the pattern" do |
| 38 |
re = RE2::Regexp.new('wo{2}') |
| 39 |
|
| 40 |
expect(RE2.replace("woo", re, "miaow")).to eq("miaow") |
| 41 |
end |
| 42 |
|
| 43 |
it "respects any passed RE2::Regexp's flags" do |
| 44 |
re = RE2::Regexp.new('gOOD MORNING', case_sensitive: false) |
| 45 |
|
| 46 |
expect(RE2.replace("Good morning", re, "hi")).to eq("hi") |
| 47 |
end |
| 48 |
|
| 49 |
it "supports passing something that can be coerced to a String as input" do |
| 50 |
expect(RE2.replace(StringLike.new("woo"), "oo", "ah")).to eq("wah") |
| 51 |
end |
| 52 |
|
| 53 |
it "supports passing something that can be coerced to a String as a pattern" do |
| 54 |
expect(RE2.replace("woo", StringLike.new("oo"), "ah")).to eq("wah") |
| 55 |
end |
| 56 |
|
| 57 |
it "supports passing something that can be coerced to a String as a replacement" do |
| 58 |
expect(RE2.replace("woo", "oo", StringLike.new("ah"))).to eq("wah") |
| 59 |
end |
| 60 |
|
| 61 |
it "returns UTF-8 strings if the pattern is UTF-8" do |
| 62 |
original = "Foo".encode("ISO-8859-1") |
| 63 |
replacement = RE2.replace(original, "oo", "ah") |
| 64 |
|
| 65 |
expect(replacement.encoding).to eq(Encoding::UTF_8) |
| 66 |
end |
| 67 |
|
| 68 |
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do |
| 69 |
original = "Foo" |
| 70 |
replacement = RE2.replace(original, RE2("oo", utf8: false), "ah") |
| 71 |
|
| 72 |
expect(replacement.encoding).to eq(Encoding::ISO_8859_1) |
| 73 |
end |
| 74 |
|
| 75 |
it "returns UTF-8 strings when given a String pattern" do |
| 76 |
replacement = RE2.replace("Foo", "oo".encode("ISO-8859-1"), "ah") |
| 77 |
|
| 78 |
expect(replacement.encoding).to eq(Encoding::UTF_8) |
| 79 |
end |
| 80 |
|
| 81 |
it "raises a Type Error for input that can't be converted to String" do |
| 82 |
expect { RE2.replace(0, "oo", "ah") }.to raise_error(TypeError) |
| 83 |
end |
| 84 |
|
| 85 |
it "raises a Type Error for a non-RE2::Regexp pattern that can't be converted to String" do |
| 86 |
expect { RE2.replace("woo", 0, "ah") }.to raise_error(TypeError) |
| 87 |
end |
| 88 |
|
| 89 |
it "does not leak memory when given a non-String, non-RE2::Regexp pattern" do |
| 90 |
expect { RE2.replace("a" * 128, 0, "ah") }.to raise_error(TypeError) |
| 91 |
end |
| 92 |
|
| 93 |
it "raises a Type Error for a replacement that can't be converted to String" do |
| 94 |
expect { RE2.replace("woo", "oo", 0) }.to raise_error(TypeError) |
| 95 |
end |
| 96 |
|
| 97 |
it "can be run concurrently with the same RE2::Regexp pattern" do |
| 98 |
re = RE2::Regexp.new('(\w+)\s(\w+)') |
| 99 |
|
| 100 |
threads = 10.times.map do |
| 101 |
Thread.new { RE2.replace("one two", re, '\2 \1') } |
| 102 |
end |
| 103 |
|
| 104 |
expect(threads.map(&:value)).to all(eq("two one")) |
| 105 |
end |
| 106 |
|
| 107 |
it "can be run concurrently with the same string pattern" do |
| 108 |
re = '(\w+)\s(\w+)' |
| 109 |
|
| 110 |
threads = 10.times.map do |
| 111 |
Thread.new { RE2.replace("one two", re, '\2 \1') } |
| 112 |
end |
| 113 |
|
| 114 |
expect(threads.map(&:value)).to all(eq("two one")) |
| 115 |
end |
| 116 |
end |
| 117 |
|
| 118 |
describe ".Replace" do |
| 119 |
it "is an alias for .replace" do |
| 120 |
expect(RE2.Replace("woo", "o", "a")).to eq("wao") |
| 121 |
end |
| 122 |
end |
| 123 |
|
| 124 |
describe ".global_replace" do |
| 125 |
it "replaces every occurrence of a pattern" do |
| 126 |
expect(RE2.global_replace("woo", "o", "a")).to eq("waa") |
| 127 |
end |
| 128 |
|
| 129 |
it "supports inputs with null bytes" do |
| 130 |
expect(RE2.global_replace("w\0oo", "o", "a")).to eq("w\0aa") |
| 131 |
end |
| 132 |
|
| 133 |
it "supports patterns with null bytes" do |
| 134 |
expect(RE2.global_replace("w\0\0oo", "\0", "a")).to eq("waaoo") |
| 135 |
end |
| 136 |
|
| 137 |
it "supports replacements with null bytes" do |
| 138 |
expect(RE2.global_replace("woo", "o", "\0")).to eq("w\0\0") |
| 139 |
end |
| 140 |
|
| 141 |
it "performs replacement based on regular expressions" do |
| 142 |
expect(RE2.global_replace("woohoo", "o+", "e")).to eq("wehe") |
| 143 |
end |
| 144 |
|
| 145 |
it "supports flags in patterns" do |
| 146 |
expect(RE2.global_replace("Robert", "(?i)r", "w")).to eq("wobewt") |
| 147 |
end |
| 148 |
|
| 149 |
it "does not perform replacement in-place", :aggregate_failures do |
| 150 |
name = "Robert" |
| 151 |
replacement = RE2.global_replace(name, "(?i)R", "w") |
| 152 |
|
| 153 |
expect(name).to eq("Robert") |
| 154 |
expect(replacement).to eq("wobewt") |
| 155 |
end |
| 156 |
|
| 157 |
it "supports passing an RE2::Regexp as the pattern" do |
| 158 |
re = RE2::Regexp.new('wo{2,}') |
| 159 |
|
| 160 |
expect(RE2.global_replace("woowooo", re, "miaow")).to eq("miaowmiaow") |
| 161 |
end |
| 162 |
|
| 163 |
it "respects any passed RE2::Regexp's flags" do |
| 164 |
re = RE2::Regexp.new('gOOD MORNING', case_sensitive: false) |
| 165 |
|
| 166 |
expect(RE2.global_replace("Good morning Good morning", re, "hi")).to eq("hi hi") |
| 167 |
end |
| 168 |
|
| 169 |
it "supports passing something that can be coerced to a String as input" do |
| 170 |
expect(RE2.global_replace(StringLike.new("woo"), "o", "a")).to eq("waa") |
| 171 |
end |
| 172 |
|
| 173 |
it "supports passing something that can be coerced to a String as a pattern" do |
| 174 |
expect(RE2.global_replace("woo", StringLike.new("o"), "a")).to eq("waa") |
| 175 |
end |
| 176 |
|
| 177 |
it "supports passing something that can be coerced to a String as a replacement" do |
| 178 |
expect(RE2.global_replace("woo", "o", StringLike.new("a"))).to eq("waa") |
| 179 |
end |
| 180 |
|
| 181 |
it "returns UTF-8 strings if the pattern is UTF-8" do |
| 182 |
original = "Foo".encode("ISO-8859-1") |
| 183 |
replacement = RE2.global_replace(original, "oo", "ah") |
| 184 |
|
| 185 |
expect(replacement.encoding).to eq(Encoding::UTF_8) |
| 186 |
end |
| 187 |
|
| 188 |
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do |
| 189 |
original = "Foo" |
| 190 |
replacement = RE2.global_replace(original, RE2("oo", utf8: false), "ah") |
| 191 |
|
| 192 |
expect(replacement.encoding).to eq(Encoding::ISO_8859_1) |
| 193 |
end |
| 194 |
|
| 195 |
it "returns UTF-8 strings when given a String pattern" do |
| 196 |
replacement = RE2.global_replace("Foo", "oo".encode("ISO-8859-1"), "ah") |
| 197 |
|
| 198 |
expect(replacement.encoding).to eq(Encoding::UTF_8) |
| 199 |
end |
| 200 |
|
| 201 |
it "raises a Type Error for input that can't be converted to String" do |
| 202 |
expect { RE2.global_replace(0, "o", "a") }.to raise_error(TypeError) |
| 203 |
end |
| 204 |
|
| 205 |
it "raises a Type Error for a non-RE2::Regexp pattern that can't be converted to String" do |
| 206 |
expect { RE2.global_replace("woo", 0, "a") }.to raise_error(TypeError) |
| 207 |
end |
| 208 |
|
| 209 |
it "does not leak memory when given a non-String, non-RE2::Regexp pattern" do |
| 210 |
expect { RE2.global_replace("a" * 128, 0, "a") }.to raise_error(TypeError) |
| 211 |
end |
| 212 |
|
| 213 |
it "raises a Type Error for a replacement that can't be converted to String" do |
| 214 |
expect { RE2.global_replace("woo", "o", 0) }.to raise_error(TypeError) |
| 215 |
end |
| 216 |
|
| 217 |
it "can be run concurrently with the same RE2::Regexp pattern" do |
| 218 |
re = RE2::Regexp.new('(\w+)\s(\w+)') |
| 219 |
|
| 220 |
threads = 10.times.map do |
| 221 |
Thread.new { RE2.global_replace("one two three four", re, '\2 \1') } |
| 222 |
end |
| 223 |
|
| 224 |
expect(threads.map(&:value)).to all(eq("two one four three")) |
| 225 |
end |
| 226 |
|
| 227 |
it "can be run concurrently with the same string pattern" do |
| 228 |
re = '(\w+)\s(\w+)' |
| 229 |
|
| 230 |
threads = 10.times.map do |
| 231 |
Thread.new { RE2.global_replace("one two three four", re, '\2 \1') } |
| 232 |
end |
| 233 |
|
| 234 |
expect(threads.map(&:value)).to all(eq("two one four three")) |
| 235 |
end |
| 236 |
end |
| 237 |
|
| 238 |
describe ".GlobalReplace" do |
| 239 |
it "is an alias for .global_replace" do |
| 240 |
expect(RE2.GlobalReplace("woo", "o", "a")).to eq("waa") |
| 241 |
end |
| 242 |
end |
| 243 |
|
| 244 |
describe ".extract" do |
| 245 |
it "extracts a rewrite of the first match" do |
| 246 |
expect(RE2.extract("[email protected]", '(\w+)@(\w+)', '\2-\1')).to eq("example-alice") |
| 247 |
end |
| 248 |
|
| 249 |
it "returns nil if there is no match" do |
| 250 |
expect(RE2.extract("no match", '(\d+)', '\1')).to be_nil |
| 251 |
end |
| 252 |
|
| 253 |
it "supports passing an RE2::Regexp as the pattern" do |
| 254 |
re = RE2::Regexp.new('(\w+)@(\w+)') |
| 255 |
|
| 256 |
expect(RE2.extract("[email protected]", re, '\2-\1')).to eq("example-alice") |
| 257 |
end |
| 258 |
|
| 259 |
it "respects any passed RE2::Regexp's flags" do |
| 260 |
re = RE2::Regexp.new('(hello)', case_sensitive: false) |
| 261 |
|
| 262 |
expect(RE2.extract("Hello", re, '\1')).to eq("Hello") |
| 263 |
end |
| 264 |
|
| 265 |
it "returns UTF-8 strings if the pattern is UTF-8" do |
| 266 |
re = RE2::Regexp.new('(foo)') |
| 267 |
result = RE2.extract("foo", re, '\1') |
| 268 |
|
| 269 |
expect(result.encoding.name).to eq("UTF-8") |
| 270 |
end |
| 271 |
|
| 272 |
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do |
| 273 |
re = RE2::Regexp.new('(foo)', utf8: false) |
| 274 |
result = RE2.extract("foo", re, '\1') |
| 275 |
|
| 276 |
expect(result.encoding.name).to eq("ISO-8859-1") |
| 277 |
end |
| 278 |
|
| 279 |
it "returns UTF-8 strings when given a String pattern" do |
| 280 |
result = RE2.extract("foo", '(foo)', '\1') |
| 281 |
|
| 282 |
expect(result.encoding.name).to eq("UTF-8") |
| 283 |
end |
| 284 |
|
| 285 |
it "supports inputs with null bytes" do |
| 286 |
expect(RE2.extract("ab\0cd", '(a.*d)', '\1')).to eq("ab\0cd") |
| 287 |
end |
| 288 |
|
| 289 |
it "supports patterns with null bytes" do |
| 290 |
expect(RE2.extract("ab\0cd", "(b\0c)", '\1')).to eq("b\0c") |
| 291 |
end |
| 292 |
|
| 293 |
it "supports rewrites with null bytes" do |
| 294 |
expect(RE2.extract("abcd", '(bc)', "x\0\\1")).to eq("x\0bc") |
| 295 |
end |
| 296 |
|
| 297 |
it "supports passing something that can be coerced to a String as input" do |
| 298 |
expect(RE2.extract(StringLike.new("bob123"), '(\d+)', '\1')).to eq("123") |
| 299 |
end |
| 300 |
|
| 301 |
it "supports passing something that can be coerced to a String as a pattern" do |
| 302 |
expect(RE2.extract("bob123", StringLike.new('(\d+)'), '\1')).to eq("123") |
| 303 |
end |
| 304 |
|
| 305 |
it "supports passing something that can be coerced to a String as a rewrite" do |
| 306 |
expect(RE2.extract("bob123", '(\d+)', StringLike.new('\1'))).to eq("123") |
| 307 |
end |
| 308 |
|
| 309 |
it "raises a Type Error for input that can't be converted to String" do |
| 310 |
expect { RE2.extract(0, '(\d+)', '\1') }.to raise_error(TypeError) |
| 311 |
end |
| 312 |
|
| 313 |
it "raises a Type Error for a non-RE2::Regexp pattern that can't be converted to String" do |
| 314 |
expect { RE2.extract("woo", 0, '\1') }.to raise_error(TypeError) |
| 315 |
end |
| 316 |
|
| 317 |
it "does not leak memory when given a non-String, non-RE2::Regexp pattern" do |
| 318 |
expect { RE2.extract("a" * 128, 0, '\1') }.to raise_error(TypeError) |
| 319 |
end |
| 320 |
|
| 321 |
it "raises a Type Error for a rewrite that can't be converted to String" do |
| 322 |
expect { RE2.extract("woo", '(\w+)', 0) }.to raise_error(TypeError) |
| 323 |
end |
| 324 |
|
| 325 |
it "can be run concurrently with the same RE2::Regexp pattern" do |
| 326 |
re = RE2::Regexp.new('(\w+)@(\w+)') |
| 327 |
|
| 328 |
threads = 10.times.map do |
| 329 |
Thread.new { RE2.extract("alice@example", re, '\2-\1') } |
| 330 |
end |
| 331 |
|
| 332 |
expect(threads.map(&:value)).to all(eq("example-alice")) |
| 333 |
end |
| 334 |
|
| 335 |
it "can be run concurrently with the same string pattern" do |
| 336 |
re = '(\w+)@(\w+)' |
| 337 |
|
| 338 |
threads = 10.times.map do |
| 339 |
Thread.new { RE2.extract("alice@example", re, '\2-\1') } |
| 340 |
end |
| 341 |
|
| 342 |
expect(threads.map(&:value)).to all(eq("example-alice")) |
| 343 |
end |
| 344 |
end |
| 345 |
|
| 346 |
describe "#escape" do |
| 347 |
it "escapes a string so it can be used as a regular expression" do |
| 348 |
expect(RE2.escape("1.5-2.0?")).to eq('1\.5\-2\.0\?') |
| 349 |
end |
| 350 |
|
| 351 |
it "raises a Type Error for input that can't be converted to String" do |
| 352 |
expect { RE2.escape(0) }.to raise_error(TypeError) |
| 353 |
end |
| 354 |
|
| 355 |
it "supports passing something that can be coerced to a String as input" do |
| 356 |
expect(RE2.escape(StringLike.new("1.5"))).to eq('1\.5') |
| 357 |
end |
| 358 |
|
| 359 |
it "supports strings containing null bytes" do |
| 360 |
expect(RE2.escape("abc\0def")).to eq('abc\x00def') |
| 361 |
end |
| 362 |
end |
| 363 |
|
| 364 |
describe ".quote" do |
| 365 |
it "is an alias for .escape" do |
| 366 |
expect(RE2.quote("1.5-2.0?")).to eq('1\.5\-2\.0\?') |
| 367 |
end |
| 368 |
end |
| 369 |
|
| 370 |
describe ".QuoteMeta" do |
| 371 |
it "is an alias for .escape" do |
| 372 |
expect(RE2.QuoteMeta("1.5-2.0?")).to eq('1\.5\-2\.0\?') |
| 373 |
end |
| 374 |
end |
| 375 |
end |