ext/ruby_speech/RubySpeechGRXMLMatcher.java in ruby_speech-2.0.1 vs ext/ruby_speech/RubySpeechGRXMLMatcher.java in ruby_speech-2.0.2
- old
+ new
@@ -15,28 +15,50 @@
import java.util.regex.*;
@JRubyClass(name="RubySpeech::GRXML::Matcher")
public class RubySpeechGRXMLMatcher extends RubyObject {
+ Pattern p;
+
public RubySpeechGRXMLMatcher(final Ruby runtime, RubyClass rubyClass) {
super(runtime, rubyClass);
}
@JRubyMethod(visibility=Visibility.PRIVATE)
- public IRubyObject check_potential_match(ThreadContext context, IRubyObject buffer)
+ public IRubyObject compile_regex(ThreadContext context, IRubyObject regex) {
+ Ruby runtime = context.getRuntime();
+ p = Pattern.compile(regex.toString());
+ return runtime.getNil();
+ }
+
+ @JRubyMethod(visibility=Visibility.PUBLIC)
+ public IRubyObject find_match(ThreadContext context, IRubyObject buffer)
{
Ruby runtime = context.getRuntime();
+ String string_buffer = buffer.toString();
+ Matcher m = p.matcher(string_buffer);
- IRubyObject regex = getInstanceVariable("@regex");
-
- Pattern p = Pattern.compile(regex.toString());
- Matcher m = p.matcher(buffer.toString());
-
if (m.matches()) {
+ if (is_max_match(string_buffer)) {
+ return RuntimeHelpers.invoke(context, this, "match_for_buffer", buffer, runtime.getTrue());
+ }
+ return callMethod(context, "match_for_buffer", buffer);
} else if (m.hitEnd()) {
RubyModule potential_match = runtime.getClassFromPath("RubySpeech::GRXML::PotentialMatch");
- return RuntimeHelpers.invoke(context, potential_match, "new");
+ return potential_match.callMethod(context, "new");
}
- return runtime.getNil();
+ RubyModule nomatch = runtime.getClassFromPath("RubySpeech::GRXML::NoMatch");
+ return nomatch.callMethod(context, "new");
+ }
+
+ public Boolean is_max_match(String buffer)
+ {
+ String search_set = "0123456789#*ABCD";
+ for (int i = 0; i < 16; i++) {
+ String new_buffer = buffer + search_set.charAt(i);
+ Matcher m = p.matcher(new_buffer);
+ if (m.matches()) return false;
+ }
+ return true;
}
}