annotate lib/ur/string.ur @ 1389:9a721f0722d3

Some more string parsing functions; naughtyDebug
author Adam Chlipala <adam@chlipala.net>
date Tue, 11 Jan 2011 18:04:52 -0500
parents 3913cbfd72e9
children 65fbb250b875
rev   line source
adamc@821 1 type t = Basis.string
adamc@821 2
adamc@1023 3 val str = Basis.str1
adamc@1023 4
adamc@828 5 val length = Basis.strlen
adam@1388 6 val lengthGe = Basis.strlenGe
adamc@828 7 val append = Basis.strcat
adamc@828 8
adamc@821 9 val sub = Basis.strsub
adamc@821 10 val suffix = Basis.strsuffix
adamc@829 11
adamc@829 12 val index = Basis.strindex
adamc@829 13 val atFirst = Basis.strchr
adamc@829 14
adamc@1272 15 fun mindex {Haystack = s, Needle = chs} =
adamc@1272 16 let
adamc@1272 17 val n = Basis.strcspn s chs
adamc@1272 18 in
adamc@1272 19 if n >= length s then
adamc@1272 20 None
adamc@1272 21 else
adamc@1272 22 Some n
adamc@1272 23 end
adamc@831 24
adamc@829 25 fun substring s {Start = start, Len = len} = Basis.substring s start len
adamc@829 26
adam@1389 27 fun seek s ch =
adam@1389 28 case index s ch of
adam@1389 29 None => None
adam@1389 30 | Some i => Some (suffix s (i + 1))
adam@1389 31 fun mseek {Haystack = s, Needle = chs} =
adam@1389 32 case mindex {Haystack = s, Needle = chs} of
adam@1389 33 None => None
adam@1389 34 | Some i => Some (sub s i, suffix s (i + 1))
adam@1389 35
adamc@829 36 fun split s ch =
adamc@829 37 case index s ch of
adamc@829 38 None => None
adamc@829 39 | Some i => Some (substring s {Start = 0, Len = i},
adam@1389 40 suffix s (i + 1))
adam@1389 41 fun split' s ch =
adam@1389 42 case index s ch of
adam@1389 43 None => None
adam@1389 44 | Some i => Some (substring s {Start = 0, Len = i},
adam@1389 45 suffix s i)
adamc@831 46 fun msplit {Haystack = s, Needle = chs} =
adamc@831 47 case mindex {Haystack = s, Needle = chs} of
adamc@831 48 None => None
adamc@831 49 | Some i => Some (substring s {Start = 0, Len = i},
adamc@831 50 sub s i,
adam@1389 51 suffix s (i + 1))
adamc@1057 52
adamc@1057 53 fun all f s =
adamc@1057 54 let
adamc@1057 55 val len = length s
adamc@1057 56
adamc@1057 57 fun al i =
adamc@1057 58 i >= len
adamc@1057 59 || (f (sub s i) && al (i + 1))
adamc@1057 60 in
adamc@1057 61 al 0
adamc@1057 62 end
adamc@1122 63
adamc@1131 64 fun mp f s =
adamc@1131 65 let
adamc@1131 66 fun mp' i acc =
adamc@1131 67 if i < 0 then
adamc@1131 68 acc
adamc@1131 69 else
adamc@1131 70 mp' (i - 1) (str (f (sub s i)) ^ acc)
adamc@1131 71 in
adamc@1131 72 mp' (length s - 1) ""
adamc@1131 73 end
adamc@1131 74
adam@1304 75 fun newlines [ctx] [[Body] ~ ctx] (s : string) : xml ([Body] ++ ctx) [] [] =
adamc@1122 76 case split s #"\n" of
adamc@1122 77 None => cdata s
adamc@1122 78 | Some (s1, s2) => <xml>{[s1]}<br/>{newlines s2}</xml>
adamc@1174 79
adamc@1174 80 fun isPrefix {Full = f, Prefix = p} =
adamc@1174 81 length f >= length p && substring f {Start = 0, Len = length p} = p