Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to binary comparison when contains RHS is UTF8 encoded #1783

Merged
merged 1 commit into from Jan 31, 2024

Conversation

ianks
Copy link
Contributor

@ianks ianks commented Jan 31, 2024

Base64 decoding of strings always results in a Encoding::BINARY string result. Unfortunately, this means the contains operator will throw an Encoding::CompatibilityError whenever we attempt to compare the decoded result against a UTF8 string.

{% assign cuties = "馃檲 馃檳" | base64_encode | base64_decode %}

{% if cuties contains "馃檲" %}
  馃樆
{% endif %}

Today, the result of the above templates is:

Liquid error (templates/index line 3): internal

But what about this template?

{% assign alphabet = "abcdef 馃敜" | base64_encode | base64_decode %}

{% if alphabet contains "abc" %}
  Next time won't you sing with me?
{% endif %}

If you don't know off the top of your head, you're in good company. Neither did I.

  Next time won't you sing with me?

Obviously, throwing an internal error is not ideal. Instead, let's fallback to binary comparison of the string since that seems to me like the most "unsuprising" thing to do here.

@ianks ianks changed the title Allow for binary comparison of incompatible strings Fallback to binary comparison when contains RHS is UTF8 encoded Jan 31, 2024
@ianks ianks merged commit 02ecaab into main Jan 31, 2024
9 checks passed
@ianks ianks deleted the contains-encoding branch January 31, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants