<div class="gmail_extra"><div class="gmail_quote">On Mon, Apr 30, 2012 at 10:54 AM, Tristan Sloughter <span dir="ltr"><<a href="mailto:tristan.sloughter@gmail.com" target="_blank">tristan.sloughter@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>So you want to find a tuple in a list converted from JSON so the key is binary. And not only that, you want it to be case insensitive! </div>
<div><br></div><div>Now as far as I found, I'm only now bothering to go back to this code and ask why, there is only string:equal/2 and it doesn't support case insensitive matching. And not only that but string:to_lower/1 only supports lists. So I ended up with a conversion to a list, then to_lower and then back to a binary....</div>
<div><br></div><div>It is too ugly to think it was right... Does anyone know a better way of doing this? I feel I am missing something obvious.</div><div><br></div><div>case_insensitive_binary_string_keyfind(_String, _Pos, []) -> </div>
<div> false;</div><div>case_insensitive_binary_string_keyfind(String, Pos, [H|T]) -> </div><div> case list_to_binary(</div><div> string:to_lower(</div><div> binary_to_list(</div><div> element(Pos, H)))) of</div>
<div> String -></div><div> H;</div><div> _ -></div><div> case_insensitive_binary_string_keyfind(String, Pos, T)</div><div> end.</div></blockquote><div><br></div><div>Well, just implement it yourself. Maybe something like this:</div>
<div><br></div><div>lower_match(<<C, B0/binary>>, <<C, B1/binary>>) -></div><div> lower_match(B0, B1);</div><div>lower_match(<<C0, B0/binary>>, <<C1, B1/binary>>)</div>
<div> when (C1 >= $a andalso C1 =< $z andalso C0 >= $A andalso</div><div> (C0 + ($a - $A)) =:= C1) -></div><div> lower_match(B0, B1);</div><div>lower_match(<<C1, B0/binary>>, <<C0, B1/binary>>)</div>
<div> when (C1 >= $a andalso C1 =< $z andalso C0 >= $A andalso</div><div> (C0 + ($a - $A)) =:= C1) -></div><div> lower_match(B0, B1);</div><div>lower_match(<<>>, <<>>) -></div>
<div> true;</div><div>lower_match(_B0, _B1) -></div><div> false.</div><div><br></div><div>lower_keyfind(_K, _P, []) -></div><div> false;</div><div>lower_keyfind(K, P, [H | T]) -></div><div> case lower_match(K, element(P, H)) of</div>
<div> true -></div><div> H;</div><div> false -></div><div> lower_keyfind(K, P, T)</div><div> end.</div><div> </div><div>-bob</div></div></div>