title: Case-insensitive Lua pattern-matching
body: <p>I'm writing a grep utility in Lua for our mobile devices running Windows CE 6/7, but I've run into some issues implementing case-insensitive match patterns. The obvious solution of converting everything to uppercase (or lower) does not work so simply due to the character classes.</p>
  
  <p>The only other thing I can think of is converting the literals in the pattern itself to uppercase.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>function toUpperPattern(instr)
      -- Check first character
      if string.find(instr, "^%l") then
          instr = string.upper(string.sub(instr, 1, 1)) .. string.sub(instr, 2)
      end
      -- Check the rest of the pattern
      while 1 do
          local a, b, str = string.find(instr, "[^%%](%l+)")
          if not a then break end
          if str then
              instr = string.sub(instr, 1, a) .. string.upper(string.sub(instr, a+1, b)) .. string.sub(instr, b + 1)
          end
      end
      return instr
  end
  </code></pre>
  
  <p>I hate to admit how long it took to get even that far, and I can still see right away there are going to be problems with things like escaped percent signs '%%'</p>
  
  <p>I figured this must be a fairly common issue, but I can't seem to find much on the topic.
  Are there any easier (or at least complete) ways to do this? I'm starting to go crazy here...
  Hoping you Lua gurus out there can enlighten me!</p>