How to match a string, but case-insensitively?

前端 未结 5 890
渐次进展
渐次进展 2020-12-29 05:59

Let\'s say that I want to match \"beer\", but don\'t care about case sensitivity.

Currently I am defining a token to be (\'b\'|\'B\' \'e\'|\'E\' \'e\'|\'E\' \'r\'|

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 06:26

    A solution I used in C#: use ASCII code to shift character to smaller case.

    class CaseInsensitiveStream : Antlr4.Runtime.AntlrInputStream {
      public CaseInsensitiveStream(string sExpr)
         : base(sExpr) {
      }
      public override int La(int index) {
         if(index == 0) return 0;
         if(index < 0) index++;
         int pdx = p + index - 1;
         if(pdx < 0 || pdx >= n) return TokenConstants.Eof;
         var x1 = data[pdx];
         return (x1 >= 65 && x1 <= 90) ? (97 + x1 - 65) : x1;
      }
    }
    

提交回复
热议问题