From: Marko Luukkainen Date: Mon, 13 May 2019 10:38:50 +0000 (+0300) Subject: SCL functions for regular expression capturing groups X-Git-Tag: v1.43.0~136^2~155^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=69695afdf3ab33a2c304f5afff7b7ff7c08e17cb;p=simantics%2Fplatform.git SCL functions for regular expression capturing groups As a bonus, substring functions gitlab #294 Change-Id: Id40103741d7fe92d823848339e8d1ca28e397fdd --- diff --git a/bundles/org.simantics.scl.runtime/scl/String.scl b/bundles/org.simantics.scl.runtime/scl/String.scl index 11116c5a0..3638553ec 100644 --- a/bundles/org.simantics.scl.runtime/scl/String.scl +++ b/bundles/org.simantics.scl.runtime/scl/String.scl @@ -9,10 +9,37 @@ importJava "java.util.regex.Pattern" where importJava "java.util.regex.Matcher" where data Matcher + + """ + Attempts to match the entire region against the pattern. + """ matches :: Matcher -> Boolean + """ + Returns the input subsequence matched by the previous match. + """ group :: Matcher -> String - + + """ + Returns the number of capturing groups in this matcher's pattern. + """ + groupCount :: Matcher -> Integer + + """ + Returns the input subsequence captured by the given group during the previous match operation. + """ + @JavaName group + groupNr :: Matcher -> Integer -> Maybe String + + """ + Returns the input subsequence captured by the given named group during the previous match operation. + """ + @JavaName group + groupNm :: Matcher -> String -> Maybe String + + """ + Attempts to find the next subsequence of the input sequence that matches the pattern. + """ @JavaName find matcherFind :: Matcher -> Boolean @@ -36,25 +63,34 @@ importJava "java.lang.StringBuffer" where @JavaName toString freezeStringBuffer :: StringBuffer -> String +import "IterN" + """ -@replaceAll pattern replacement text@ replaces all matches of @pattern@ in the @text@ -by @replacement@. +Get parts matched by each group in the pattern, if a match is found. +""" +matchGroups :: Pattern -> String -> Maybe [Maybe String] +matchGroups p s = runProc $ if matches m then Just $ mapN (\i -> groupNr m (i+1)) (groupCount m) else Nothing where + m = matcher p s + +""" +`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 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 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 @@ -67,8 +103,8 @@ substituteAll pattern f text = runProc do """ -@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 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 @@ -79,4 +115,18 @@ substituteFirst pattern f text = runProc do appendTail m buf freezeStringBuffer buf else text - \ No newline at end of file + + +importJava "java.lang.String" where + + """ + `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. + """ + @JavaName substring + substring :: String -> Integer -> String + + """ + `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. + """ + @JavaName substring + substringl :: String -> Integer -> Integer -> String \ No newline at end of file