Add support for whitespace control character.#30
Conversation
|
ping @Shopify/liquid |
|
This adds support for {{- and {%- syntax which will lstrip! and -}} and -%} which will rstrip! |
|
Now matches up to Shopify/liquid#773 |
|
I'd love to avoid the cycle through the Ruby VM to call rstrip!/lstrip!. Ideally we should parse white-space as a special token, then modify the token to be marked for render-skipping if {%- or -%} are encountered. |
|
I see now that this would make it harder to match with Shopify/liquid#746 - alright, let's not overthink it. Thanks! |
|
Yeah I would have liked to avoid it but the trade off isn't worth it at this stage. To keep the functions inline on both sides makes maintenance easier. Also as I'm pulling back the last token if needed it requires conversion from a ruby object then back again where as this approach keeps it clean and only a few lines |
ext/liquid_c/block.c
Outdated
| VALUE str = rb_enc_str_new(token.str, token.length, utf8_encoding); | ||
|
|
||
| if(token.trim_whitespace) | ||
| rb_funcall(str, rb_intern("lstrip!"), 0); |
There was a problem hiding this comment.
Why not find the offset after any whitespace (see lstrip_offset) and create the string with that offset? Avoids a memmove.
There was a problem hiding this comment.
I'm now thinking that it could be a good idea to make read_while respect the codepoint length (see other usages of read_while for what it does).
|
Makes sense and looks pretty good overall. |
ext/liquid_c/block.c
Outdated
| const char *start = token.str + 2; | ||
| long length = token.length - 4; | ||
|
|
||
| if (token.str[2] == '-') { |
There was a problem hiding this comment.
You can use start[0] or *start instead
|
Alright we are passing again. Detection of the whitespace control characters has now been pushed back in to the tokenizer so there is no need to push and pop any existing values. |
|
really nice work Mike.
On Thu, Jul 7, 2016 at 8:18 AM, Mike Angell notifications@github.com
|
ext/liquid_c/block.c
Outdated
|
|
||
| VALUE str = rb_enc_str_new(token_start, end - token_start, utf8_encoding); | ||
| if(token.rstrip) | ||
| rb_funcall(str, intern_rstrip, 0); |
There was a problem hiding this comment.
What do you think about adding a reverse_read_while if the lstrip part is using read_while anyway?
There was a problem hiding this comment.
Yeah I actually was thinking to do exactly that. I really only stuck to this while refactoring so I could confirm i didn't introduce anything new. Should have some time tonight to pull this out and bring it up to where the lstrip sits.
|
Definitely the better solution, nice. |
…o replace current intern_rstrip
|
Ok new change from using ruby rstrip is done which makes this pretty final. As such I've just run the final performance benchmark Current This feature The results show the performance impact of this feature is very minimal. |
|
@pushrax @tobi @dylanahsmith Where to from here? We would love to be able to start using this |
ext/liquid_c/tokenizer.c
Outdated
| continue; | ||
|
|
||
| char c = *cursor++; | ||
| char w = *cursor++; |
There was a problem hiding this comment.
This can read past the end of the string. E.g. tokenizing "{{" results in a token with "{{\u0000". The while (cursor < last) check only allows us to read two characters before having to do another bounds check to see if we can read another byte.
|
There were some corner cases that you missed as noted above. But overall this is looking very good now. Nice work |
|
@dylanahsmith is this what you were thinking? |
| char c = *cursor++; | ||
| if (cursor <= last && *cursor == '-') { | ||
| cursor++; | ||
| token->rstrip = 1; |
There was a problem hiding this comment.
If the following if (c != '%' && c != '{') branch is taken, then this change won't be reversed.
… work incorrectly.
|
I think just shifting this removes this issue. So flowing through this previously this would have allowed
|
|
Is there anything else needed for this to progress? |
|
It looks like https://github.com/Shopify/liquid-c/pull/30/files#r70563502 still hasn't been addressed. That was my last concern. |
…bles set these based on their behaviour to raw tags
|
Ok i've added a reset after this line so it will only be set if it is found. |
|
Ok this and the ruby version look like they are ready to go. What do we need to do to get this moving. |
This matches up with Shopify/liquid#746 to resolve issues Shopify/liquid#216, Shopify/liquid#215, Shopify/liquid#214, Shopify/liquid#194, Shopify/liquid#171, Shopify/liquid#162
This pull request is designed to be standalone and will pass all tests without its counterpart Shopify/liquid#746 however Shopify/liquid#746 requires this pull request and will be updated with this dependancy once merged.