Sha256: 2618f0caf8793fa3218624dd9754f01ff1a9ba84c2d0f710a347c6496e8f570b
Contents?: true
Size: 1.99 KB
Versions: 4
Compression:
Stored size: 1.99 KB
Contents
package com.adhearsion.ruby_speech; import org.jruby.Ruby; import org.jruby.RubyClass; import org.jruby.RubyModule; import org.jruby.RubyObject; import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; import org.jruby.runtime.ObjectAllocator; import org.jruby.runtime.ThreadContext; import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.javasupport.util.RuntimeHelpers; 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 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); 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 potential_match.callMethod(context, "new"); } 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; } }
Version data entries
4 entries across 4 versions & 1 rubygems