Hi,

In a Unicode context case-insensitivity becomes a non-trivial requirement (for example: matching strings might have a different length depending on the type of comparison).

However, if you only want to match strings with simple unicode code points, the following parser will probably do the trick:

1
2
3
4
5
6
7
8
9
10
open FParsec.Primitives
open FParsec.CharParser

/// `pstringCI s` parses any case insensitive variant of the string `s` and returns the parsed string 
let pstringCI (s: string) : Parser<_,_> =
    fun state ->
        let s2 = state.Iter.Read(s.Length)
        if System.String.Equals(s, s2, System.StringComparison.InvariantCultureIgnoreCase) then 
            ConsumedOK s2 (state.Advance(s.Length))
        else EmptyError (expectedError state ("\"" + s + "\" (case insensitive)"))

Is this what you're looking for?

If an overload of System.Globalization.CompareInfo.IsPrefix supported an index for the first argument and returned the length of the matched string, I could efficiently support case insensitive matching directly in the CharStream class, but unfortunately there doesn't seem to be any such a function in the .Net library.

Best regards,
Stephan

By on 1/7/2009 5:02 AM ()

Didn't think about unicode! Fortunately I only have to deal with standard ASCII.
Thanks for the pstringCI, I was about to port it from hsemail but this seems much more efficient.
I take this opportunity to also thank you for FParsec, it's awesome. Loved the comment about Buffer.BlockCopy :-)

By on 1/7/2009 8:01 AM ()

Nevermind, the caseString I mentioned isn't part of parsec, anyway it should be trivial to port it to FParsec.

By on 1/7/2009 4:19 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper