[Groonga-commit] droonga/droonga-engine at dcae56d [master] Add LineBuffer to get lines from chunk data

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Jun 3 12:23:04 JST 2014


Kouhei Sutou	2014-06-03 12:23:04 +0900 (Tue, 03 Jun 2014)

  New Revision: dcae56d7b22e868addb5486c4e146ddcad0d35f7
  https://github.com/droonga/droonga-engine/commit/dcae56d7b22e868addb5486c4e146ddcad0d35f7

  Message:
    Add LineBuffer to get lines from chunk data

  Added files:
    lib/droonga/line_buffer.rb
    test/unit/test_line_buffer.rb

  Added: lib/droonga/line_buffer.rb (+42 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/line_buffer.rb    2014-06-03 12:23:04 +0900 (5768d87)
@@ -0,0 +1,42 @@
+# Copyright (C) 2014 Droonga Project
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License version 2.1 as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+module Droonga
+  class LineBuffer
+    def initialize
+      @buffer = ""
+    end
+
+    def feed(data)
+      position = 0
+      loop do
+        new_line_position = data.index("\n", position)
+        if new_line_position.nil?
+          @buffer << data[position..-1]
+          break
+        end
+
+        line = data[position..new_line_position]
+        if position.zero?
+          yield(@buffer + line)
+          @buffer.clear
+        else
+          yield(line)
+        end
+        position = new_line_position + 1
+      end
+    end
+  end
+end

  Added: test/unit/test_line_buffer.rb (+62 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/test_line_buffer.rb    2014-06-03 12:23:04 +0900 (bd5daca)
@@ -0,0 +1,62 @@
+# Copyright (C) 2014 Droonga Project
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License version 2.1 as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+require "droonga/line_buffer"
+
+class LineBufferTest < Test::Unit::TestCase
+  def setup
+    @line_buffer = Droonga::LineBuffer.new
+  end
+
+  def feed(data)
+    lines = []
+    @line_buffer.feed(data) do |line|
+      lines << line
+    end
+    lines
+  end
+
+  class NoBufferTest < self
+    def test_no_new_line
+      assert_equal([], feed("a"))
+    end
+
+    def test_one_new_line
+      assert_equal(["a\n"], feed("a\n"))
+    end
+
+    def test_multiple_new_lines
+      assert_equal(["a\n", "b\n"], feed("a\nb\n"))
+    end
+  end
+
+  class BufferedTest < self
+    def test_one_line
+      assert_equal([], feed("a"))
+      assert_equal(["a\n"], feed("\n"))
+    end
+
+    def test_multiple_lines
+      assert_equal([], feed("a"))
+      assert_equal(["a\n", "b\n"], feed("\nb\n"))
+    end
+
+    def test_multiple_buffered
+      assert_equal([], feed("a"))
+      assert_equal(["a\n"], feed("\nb"))
+      assert_equal(["b\n"], feed("\n"))
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Télécharger 



More information about the Groonga-commit mailing list
Back to archive index