]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/String.scl
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / String.scl
1 import "Prelude" hiding (group)
2
3 importJava "java.util.regex.Pattern" where
4     data Pattern
5     
6     compile :: String -> Pattern
7     matcher :: Pattern -> String -> Matcher
8
9 importJava "java.util.regex.Matcher" where
10     data Matcher
11
12
13     """
14     Attempts to match the entire region against the pattern.
15     """
16     matches :: Matcher -> <Proc> Boolean
17     
18     """
19     Returns the input subsequence matched by the previous match.
20     """
21     group :: Matcher -> <Proc> String
22     
23     """
24     Returns the number of capturing groups in this matcher's pattern.
25     """
26     groupCount :: Matcher -> <Proc> Integer
27     
28     """
29     Returns the input subsequence captured by the given group during the previous match operation.
30     """ 
31     @JavaName group
32     groupNr :: Matcher -> Integer -> <Proc> Maybe String
33     
34     """
35     Returns the input subsequence captured by the given named group during the previous match operation.
36     """
37     @JavaName group
38     groupNm :: Matcher -> String -> <Proc> Maybe String
39     
40     """
41     Attempts to find the next subsequence of the input sequence that matches the pattern.
42     """ 
43     @JavaName find
44     matcherFind :: Matcher -> <Proc> Boolean
45     
46     appendReplacement :: Matcher -> StringBuffer -> String -> <Proc> Matcher
47     appendTail :: Matcher -> StringBuffer -> <Proc> StringBuffer
48     
49     @JavaName replaceAll
50     replaceAll_ :: Matcher -> String -> String
51     
52     @JavaName replaceFirst
53     replaceFirst_ :: Matcher -> String -> String
54     
55     quoteReplacement :: String -> String
56
57 importJava "java.lang.StringBuffer" where
58     data StringBuffer
59     
60     @JavaName "<init>"
61     newStringBuffer :: () -> StringBuffer
62     
63     @JavaName toString
64     freezeStringBuffer :: StringBuffer -> <Proc> String
65
66 import "IterN"
67
68 """
69 Get parts matched by each group in the pattern, if a match is found.
70 """
71 matchGroups :: Pattern -> String -> Maybe [Maybe String]
72 matchGroups p s = runProc $ if matches m then Just $ mapN (\i -> groupNr m (i+1)) (groupCount m) else Nothing where
73     m = matcher p s
74
75 """
76 `replaceAll pattern replacement text` replaces all matches of `pattern` in the `text`
77 by `replacement`.
78 """    
79 replaceAll :: String -> String -> String -> String
80 replaceAll pattern replacement text = 
81     replaceAll_ (matcher (compile pattern) text) replacement 
82     
83 """
84 `replaceFirst pattern replacement text` replaces the first match of `pattern` in the `text`
85 by `replacement`.
86 """    
87 replaceFirst :: String -> String -> String -> String
88 replaceFirst pattern replacement text = 
89     replaceFirst_ (matcher (compile pattern) text) replacement 
90
91 """
92 `substituteAll pattern f text` replaces all matches of `pattern` in the `text`
93 by the string returned by `f` given the matched region of the string as a parameter.
94 """
95 substituteAll :: String -> (String -> String) -> String -> String
96 substituteAll pattern f text = runProc do
97     m = matcher (compile pattern) text
98     buf = newStringBuffer ()
99     while (matcherFind m) do
100         appendReplacement m buf $ f $ group m
101     appendTail m buf
102     freezeStringBuffer buf
103
104
105 """
106 `substituteFirst pattern f text` replaces the first match of `pattern` in the `text`
107 by the string returned by `f` given the matched region of the string as a parameter.
108 """
109 substituteFirst :: String -> (String -> String) -> String -> String
110 substituteFirst pattern f text = runProc do
111     m = matcher (compile pattern) text
112     if (matcherFind m) then do
113         buf = newStringBuffer ()    
114         appendReplacement m buf $ f $ group m
115         appendTail m buf
116         freezeStringBuffer buf
117     else text
118     
119  
120 importJava "java.lang.String" where
121
122     """
123     `substring string beginIndex` Returns a string that is a substring of given string. The substring begins with the character at the specified index and extends to the end of the string.
124     """ 
125     @JavaName substring
126     substring :: String -> Integer -> String
127     
128     """
129     `substringl string beginIndex endIndex` Returns a string that is a substring of given string. The substring begins at the specified `beginIndex` and extends to the character at index `endIndex - 1`. Thus the length of the substring is endIndex-beginIndex.
130     """
131     @JavaName substring
132     substringl :: String -> Integer -> Integer -> String