<commit_before>%%{
  machine Message;

  # data, p, pe, eof, cs, top, stack, ts, te and act

  action mark { m = p }
  action write_command { message.command = data[m..p] }

  action store_key { key = data[m..p] }
  action write_header { message.headers[key] = data[m..p] }

  NULL = "\0";
  LF = "\n";
  CR = "\r";
  EOL = CR? LF;
  OCTET = any;
  HEADER_OCTET = OCTET - CR - LF - ":";

  command_name = "CONNECT";
  command = command_name > mark @ write_command;

  header_key = HEADER_OCTET+ > mark @ store_key;
  header_value = HEADER_OCTET* > mark @ write_header;
  header = header_key ":" header_value EOL;

  message := command EOL header* EOL NULL;
}%%

module Stompede
  module Stomp
    module Parser
      # this manipulates the singleton class of our context,
      # so we do not want to run this code very often or we
      # bust our ruby method caching
      %% write data noprefix;

      class << self
        # @param [String] data
        # @return [Stomp::Message]
        def parse(data)
          # re-encode the input as BINARY to be able to refer
          # on the byte-level with data[i].ord
          data = data.force_encoding("BINARY")

          # this is where our parsed components end up
          message = Stomp::Message.new

          p = 0 # pointer to current character
          m = 0 # pointer to marked character (for buffering)
          pe = data.length # pointer to end of input
          cs = start # current starting state

          # write out the ragel state machine parser
          %% write exec;

          # if parsing parsed a complete message, return it
          message if cs >= first_final
        end
      end
    end
  end
end<commit_msg>Make it less attractive to create methods depending on shared state<commit_after>%%{
  machine message;

  # data, p, pe, eof, cs, top, stack, ts, te and act

  action mark { m = p }
  action write_command { message.command = data[m..p] }

  action store_key { key = data[m..p] }
  action write_header { message.headers[key] = data[m..p] }

  NULL = "\0";
  LF = "\n";
  CR = "\r";
  EOL = CR? LF;
  OCTET = any;
  HEADER_OCTET = OCTET - CR - LF - ":";

  command_name = "CONNECT";
  command = command_name > mark @ write_command;

  header_key = HEADER_OCTET+ > mark @ store_key;
  header_value = HEADER_OCTET* > mark @ write_header;
  header = header_key ":" header_value EOL;

  message := command EOL header* EOL NULL;
}%%

module Stompede
  module Stomp
    class Parser
      # this manipulates the singleton class of our context,
      # so we do not want to run this code very often or we
      # bust our ruby method caching
      %% write data noprefix;

      # Parse a chunk of Stomp-formatted data into a Message.
      #
      # @param [String] data
      # @return [Stomp::Message]
      def self.parse(data)
        # re-encode the input as BINARY to be able to refer
        # on the byte-level with data[i].ord
        data = data.force_encoding("BINARY")

        # this is where our parsed components end up
        message = Stomp::Message.new

        p = 0 # pointer to current character
        pe = data.length # pointer to end of input
        cs = Stomp::Parser.start # current starting state
        m = 0 # pointer to marked character (for buffering)

        # write out the ragel state machine parser
        %% write exec;

        # if parsing parsed a complete message, return it
        message if cs >= Stomp::Parser.first_final
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09421534936998854, 'fraction_numerical': 0.001718213058419244, 'mean_word_length': 2.323501427212179, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5081756', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1050, 'n_words': 399}