====== match(Xtal) ======
// param1 type : String, ...
// return type : xpeg::Executor or Null
String::match: method(pattern);
===== Description =====
XtalのString::matchは、//pattern//にmatchする文字列があった場合、その文字列をcaptureした瞬間のExecutorを、matchする文字列がなかった場合はnullを返します。
返ってきたExecutorを使うことで、matchした文字列のprefixやsuffixを得ることが出来たり、matchした後の処理を続けることが出来ます。
===== Example1 =====
"foo"にマッチする文字列が存在する限り処理を続ける
==== C++ ====
-
==== Xtal ====
str : "foobarfoobarhogehoge";
e : str.match("foo");
if (e){
e.captures{|k, value|
value.p;
}
e.prefix.p;
e.suffix.p;
while (e.match("foo")){
e.captures{|k, value|
value.p;
}
e.prefix.p;
e.suffix.p;
}
}
==== Output ====
foo
barfoobarhogehoge
foo
bar
barhogehoge
===== Example2 =====
String_gsubの実装
==== C++ ====
-
==== Xtal ====
fun String_gsub(str, pattern, fn){
mm : MemoryStream();
exec : str.match(pattern);
if (exec){
prefix : exec.prefix;
mm.put_s(prefix);
mm.put_s(fn(exec));
while (exec.match(pattern)){
prefix : exec.prefix;
mm.put_s(prefix);
mm.put_s(fn(exec));
}
mm.put_s(exec.suffix);
return mm.to_s;
}
else {
return str;
}
}
String_gsub("foobar piyo hoge foo", "foo", "bar").p;
==== Output ====
barbar piyo hoge bar