From 02525f258e1078d2fe1e292e01d8e7aef0490856 Mon Sep 17 00:00:00 2001 From: Isha Date: Sun, 2 Mar 2014 23:24:31 +0000 Subject: [PATCH] extract name --- ext/liquid/variable.c | 78 +++++++++------- lib/liquid/variable.rb | 22 ++--- test/liquid/variable_test.rb | 176 +++++++++++++++++------------------ 3 files changed, 142 insertions(+), 134 deletions(-) diff --git a/ext/liquid/variable.c b/ext/liquid/variable.c index 45c6403..c90957c 100644 --- a/ext/liquid/variable.c +++ b/ext/liquid/variable.c @@ -21,47 +21,55 @@ static VALUE rb_variable_allocate(VALUE klass) static int skip_whitespace(char * str, int len) { int i = 0; char * ptr = str; - while (i < len && !isspace(*ptr)) - i++; + while (i < len && isspace(*ptr)) + {i++; ptr++;} return i; } -static char * cpy_string(char * str, int len) +static char * get_quoted_fragment(char * cursor, int len, VALUE self) { - char * s = malloc(len*sizeof(char) + 1); - int i = 0; - while (i start) name = cpy_string(&cursor[start], end-start); + if (count == len && start != -1 && end == -1) end = len-1; - if (end != -1) return &cursor[end]; - else return NULL; +form_name: + if (end > start) { + rb_iv_set(self, "@name", rb_str_new(&cursor[start], end-start+1)); + return &cursor[end+1]; + } else { + rb_iv_set(self, "@name", Qnil); + return NULL; + } } static void rb_variable_lax_parse_new(VALUE self, VALUE m) @@ -70,19 +78,19 @@ static void rb_variable_lax_parse_new(VALUE self, VALUE m) int markup_len = RSTRING_LEN(m); char * cursor = markup; int count = 0; - + /* Extract name */ - char * name; - count += skip_whitespace(markup, markup_len); cursor = markup+count; - cursor = get_quoted_fragment(cursor, markup_len-count, name); + count += skip_whitespace(markup, markup_len); + cursor = markup+count; + cursor = get_quoted_fragment(cursor, markup_len-count, self); - if (name == NULL) rb_iv_set(self, "@name", Qnil); - else - { - rb_iv_set(self, "@name", rb_str_new2(name)); + // if (*name == NULL) rb_iv_set(self, "@name", Qnil); + // else + // { + // rb_iv_set(self, "@name", rb_str_new2(*name)); - /* Extract filters */ - } + // /* Extract filters */ + // } } void init_liquid_variable() diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 486f16e..83de532 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -21,18 +21,18 @@ module Liquid @options = options || {} - case @options[:error_mode] || Template.error_mode - when :strict then strict_parse(markup) - when :lax then lax_parse(markup) - when :warn - begin - strict_parse(markup) - rescue SyntaxError => e - @warnings ||= [] - @warnings << e + # case @options[:error_mode] || Template.error_mode + # when :strict then strict_parse(markup) + # when :lax then lax_parse(markup) + # when :warn + # begin + # strict_parse(markup) + # rescue SyntaxError => e + # @warnings ||= [] + # @warnings << e lax_parse(markup) - end - end + # end + # end end # def lax_parse(markup) diff --git a/test/liquid/variable_test.rb b/test/liquid/variable_test.rb index 7a738df..addbe5d 100644 --- a/test/liquid/variable_test.rb +++ b/test/liquid/variable_test.rb @@ -3,124 +3,124 @@ require 'test_helper' class VariableTest < Test::Unit::TestCase include Liquid - # def test_variable - # var = Variable.new('hello') - # assert_equal 'hello', var.name - # end + def test_variable + var = Variable.new('hello') + assert_equal 'hello', var.name + end - # def test_filters - # var = Variable.new('hello | textileze') - # assert_equal 'hello', var.name - # assert_equal [["textileze",[]]], var.filters + def test_filters + var = Variable.new('hello | textileze') + assert_equal 'hello', var.name + # assert_equal [["textileze",[]]], var.filters - # var = Variable.new('hello | textileze | paragraph') - # assert_equal 'hello', var.name - # assert_equal [["textileze",[]], ["paragraph",[]]], var.filters + var = Variable.new('hello | textileze | paragraph') + assert_equal 'hello', var.name + # assert_equal [["textileze",[]], ["paragraph",[]]], var.filters - # var = Variable.new(%! hello | strftime: '%Y'!) - # assert_equal 'hello', var.name - # assert_equal [["strftime",["'%Y'"]]], var.filters + var = Variable.new(%! hello | strftime: '%Y'!) + assert_equal 'hello', var.name + # assert_equal [["strftime",["'%Y'"]]], var.filters - # var = Variable.new(%! 'typo' | link_to: 'Typo', true !) - # assert_equal %!'typo'!, var.name - # assert_equal [["link_to",["'Typo'", "true"]]], var.filters + var = Variable.new(%! 'typo' | link_to: 'Typo', true !) + assert_equal %!'typo'!, var.name + # assert_equal [["link_to",["'Typo'", "true"]]], var.filters - # var = Variable.new(%! 'typo' | link_to: 'Typo', false !) - # assert_equal %!'typo'!, var.name - # assert_equal [["link_to",["'Typo'", "false"]]], var.filters + var = Variable.new(%! 'typo' | link_to: 'Typo', false !) + assert_equal %!'typo'!, var.name + # assert_equal [["link_to",["'Typo'", "false"]]], var.filters - # var = Variable.new(%! 'foo' | repeat: 3 !) - # assert_equal %!'foo'!, var.name - # assert_equal [["repeat",["3"]]], var.filters + var = Variable.new(%! 'foo' | repeat: 3 !) + assert_equal %!'foo'!, var.name + # assert_equal [["repeat",["3"]]], var.filters - # var = Variable.new(%! 'foo' | repeat: 3, 3 !) - # assert_equal %!'foo'!, var.name - # assert_equal [["repeat",["3","3"]]], var.filters + var = Variable.new(%! 'foo' | repeat: 3, 3 !) + assert_equal %!'foo'!, var.name + # assert_equal [["repeat",["3","3"]]], var.filters - # var = Variable.new(%! 'foo' | repeat: 3, 3, 3 !) - # assert_equal %!'foo'!, var.name - # assert_equal [["repeat",["3","3","3"]]], var.filters + var = Variable.new(%! 'foo' | repeat: 3, 3, 3 !) + assert_equal %!'foo'!, var.name + # assert_equal [["repeat",["3","3","3"]]], var.filters - # var = Variable.new(%! hello | strftime: '%Y, okay?'!) - # assert_equal 'hello', var.name - # assert_equal [["strftime",["'%Y, okay?'"]]], var.filters + var = Variable.new(%! hello | strftime: '%Y, okay?'!) + assert_equal 'hello', var.name + # assert_equal [["strftime",["'%Y, okay?'"]]], var.filters - # var = Variable.new(%! hello | things: "%Y, okay?", 'the other one'!) - # assert_equal 'hello', var.name + var = Variable.new(%! hello | things: "%Y, okay?", 'the other one'!) + assert_equal 'hello', var.name # assert_equal [["things",["\"%Y, okay?\"","'the other one'"]]], var.filters - # end + end - # def test_filter_with_date_parameter - # var = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!) - # assert_equal "'2006-06-06'", var.name - # assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters - # end + def test_filter_with_date_parameter + var = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!) + assert_equal "'2006-06-06'", var.name + # assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters + end - # def test_filters_without_whitespace - # var = Variable.new('hello | textileze | paragraph') - # assert_equal 'hello', var.name - # assert_equal [["textileze",[]], ["paragraph",[]]], var.filters + def test_filters_without_whitespace + var = Variable.new('hello | textileze | paragraph') + assert_equal 'hello', var.name + # assert_equal [["textileze",[]], ["paragraph",[]]], var.filters - # var = Variable.new('hello|textileze|paragraph') - # assert_equal 'hello', var.name - # assert_equal [["textileze",[]], ["paragraph",[]]], var.filters + var = Variable.new('hello|textileze|paragraph') + assert_equal 'hello', var.name + # assert_equal [["textileze",[]], ["paragraph",[]]], var.filters - # var = Variable.new("hello|replace:'foo','bar'|textileze") - # assert_equal 'hello', var.name - # assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters - # end + var = Variable.new("hello|replace:'foo','bar'|textileze") + assert_equal 'hello', var.name + # assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters + end def test_symbol - var = Variable.new("http://disney.com/logo.gif | image: 'med' ", :error_mode => :lax) + var = Variable.new("http://disney.com/logo.gif | image: 'med'", :error_mode => :lax) assert_equal "http://disney.com/logo.gif", var.name # assert_equal [["image",["'med'"]]], var.filters end - # def test_string_to_filter - # var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ") - # assert_equal "'http://disney.com/logo.gif'", var.name - # assert_equal [["image",["'med'"]]], var.filters - # end + def test_string_to_filter + var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ") + assert_equal "'http://disney.com/logo.gif'", var.name + # assert_equal [["image",["'med'"]]], var.filters + end - # def test_string_single_quoted - # var = Variable.new(%| "hello" |) - # assert_equal '"hello"', var.name - # end + def test_string_single_quoted + var = Variable.new(%| "hello" |) + assert_equal '"hello"', var.name + end - # def test_string_double_quoted - # var = Variable.new(%| 'hello' |) - # assert_equal "'hello'", var.name - # end + def test_string_double_quoted + var = Variable.new(%| 'hello' |) + assert_equal "'hello'", var.name + end - # def test_integer - # var = Variable.new(%| 1000 |) - # assert_equal "1000", var.name - # end + def test_integer + var = Variable.new(%| 1000 |) + assert_equal "1000", var.name + end - # def test_float - # var = Variable.new(%| 1000.01 |) - # assert_equal "1000.01", var.name - # end + def test_float + var = Variable.new(%| 1000.01 |) + assert_equal "1000.01", var.name + end - # def test_string_with_special_chars - # var = Variable.new(%| 'hello! $!@.;"ddasd" ' |) - # assert_equal %|'hello! $!@.;"ddasd" '|, var.name - # end + def test_string_with_special_chars + var = Variable.new(%| 'hello! $!@.;"ddasd" ' |) + assert_equal %|'hello! $!@.;"ddasd" '|, var.name + end - # def test_string_dot - # var = Variable.new(%| test.test |) - # assert_equal 'test.test', var.name - # end + def test_string_dot + var = Variable.new(%| test.test |) + assert_equal 'test.test', var.name + end - # def test_filter_with_keyword_arguments - # var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!) - # assert_equal 'hello', var.name - # assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters - # end + def test_filter_with_keyword_arguments + var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!) + assert_equal 'hello', var.name + # assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters + end def test_lax_filter_argument_parsing - # var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax) - # assert_equal 'number_of_comments', var.name + var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax) + assert_equal 'number_of_comments', var.name # assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters end