Kouhei Sutou
null+****@clear*****
Fri Sep 4 15:51:31 JST 2015
Kouhei Sutou 2015-09-04 15:51:31 +0900 (Fri, 04 Sep 2015) New Revision: b9426e8edefc1258933240b5f23a278847787cf2 https://github.com/ranguba/groonga-client/commit/b9426e8edefc1258933240b5f23a278847787cf2 Message: Add Groonga::Client::ScriptSyntax.format_string Added files: lib/groonga/client/script-syntax.rb test/test-script-syntax.rb Modified files: lib/groonga/client.rb Modified: lib/groonga/client.rb (+1 -0) =================================================================== --- lib/groonga/client.rb 2015-08-08 16:56:49 +0900 (6e58d02) +++ lib/groonga/client.rb 2015-09-04 15:51:31 +0900 (5bc9659) @@ -24,6 +24,7 @@ require "groonga/client/command" require "groonga/client/empty-request" require "groonga/client/protocol/gqtp" require "groonga/client/protocol/http" +require "groonga/client/script-syntax" module Groonga class Client Added: lib/groonga/client/script-syntax.rb (+30 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga/client/script-syntax.rb 2015-09-04 15:51:31 +0900 (9face7d) @@ -0,0 +1,30 @@ +# Copyright (C) 2015 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +module Groonga + class Client + module ScriptSyntax + class << self + def format_string(string) + escaped_string = string.gsub(/["\\]/) do |matched| + "\\#{matched}" + end + "\"#{escaped_string}\"" + end + end + end + end +end Added: test/test-script-syntax.rb (+38 -0) 100644 =================================================================== --- /dev/null +++ test/test-script-syntax.rb 2015-09-04 15:51:31 +0900 (fb9fcc1) @@ -0,0 +1,38 @@ +# Copyright (C) 2015 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +class TestScriptSyntax < Test::Unit::TestCase + sub_test_case ".format_string" do + def format(string) + Groonga::Client::ScriptSyntax.format_string(string) + end + + test "no special characters" do + assert_equal("\"Hello\"", + format("Hello")) + end + + test "double quote" do + assert_equal("\"Say \\\"Hello\\\"!\"", + format("Say \"Hello\"!")) + end + + test "back slash" do + assert_equal("\"Go c:\\\\Windows!\"", + format("Go c:\\Windows!")) + end + end +end -------------- next part -------------- HTML����������������������������...Download