]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/String.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / String.scl
diff --git a/bundles/org.simantics.scl.runtime/scl/String.scl b/bundles/org.simantics.scl.runtime/scl/String.scl
new file mode 100644 (file)
index 0000000..11116c5
--- /dev/null
@@ -0,0 +1,82 @@
+import "Prelude" hiding (group)
+
+importJava "java.util.regex.Pattern" where
+    data Pattern
+    
+    compile :: String -> Pattern
+    matcher :: Pattern -> String -> Matcher
+
+importJava "java.util.regex.Matcher" where
+    data Matcher
+
+    matches :: Matcher -> <Proc> Boolean
+    
+    group :: Matcher -> <Proc> String
+
+    @JavaName find
+    matcherFind :: Matcher -> <Proc> Boolean
+    
+    appendReplacement :: Matcher -> StringBuffer -> String -> <Proc> Matcher
+    appendTail :: Matcher -> StringBuffer -> <Proc> StringBuffer
+    
+    @JavaName replaceAll
+    replaceAll_ :: Matcher -> String -> String
+    
+    @JavaName replaceFirst
+    replaceFirst_ :: Matcher -> String -> String
+    
+    quoteReplacement :: String -> String
+
+importJava "java.lang.StringBuffer" where
+    data StringBuffer
+    
+    @JavaName "<init>"
+    newStringBuffer :: () -> StringBuffer
+    
+    @JavaName toString
+    freezeStringBuffer :: StringBuffer -> <Proc> String
+
+"""
+@replaceAll pattern replacement text@ replaces all matches of @pattern@ in the @text@
+by @replacement@.
+"""    
+replaceAll :: String -> String -> String -> String
+replaceAll pattern replacement text = 
+    replaceAll_ (matcher (compile pattern) text) replacement 
+    
+"""
+@replaceFirst pattern replacement text@ replaces the first match of @pattern@ in the @text@
+by @replacement@.
+"""    
+replaceFirst :: String -> String -> String -> String
+replaceFirst pattern replacement text = 
+    replaceFirst_ (matcher (compile pattern) text) replacement 
+
+"""
+@substituteAll pattern f text@ replaces all matches of @pattern@ in the @text@
+by the string returned by @f@ given the matched region of the string as a parameter.
+"""
+substituteAll :: String -> (String -> String) -> String -> String
+substituteAll pattern f text = runProc do
+    m = matcher (compile pattern) text
+    buf = newStringBuffer ()
+    while (matcherFind m) do
+        appendReplacement m buf $ f $ group m
+    appendTail m buf
+    freezeStringBuffer buf
+
+
+"""
+@substituteFirst pattern f text@ replaces the first match of @pattern@ in the @text@
+by the string returned by @f@ given the matched region of the string as a parameter.
+"""
+substituteFirst :: String -> (String -> String) -> String -> String
+substituteFirst pattern f text = runProc do
+    m = matcher (compile pattern) text
+    if (matcherFind m) then do
+        buf = newStringBuffer ()    
+        appendReplacement m buf $ f $ group m
+        appendTail m buf
+        freezeStringBuffer buf
+    else text
+    
\ No newline at end of file