Mercurial Hosting > luan
changeset 1722:7d2ab44f7a59
remove String regex fns
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 29 Jul 2022 14:12:01 -0600 |
parents | 5c69d2e8bd75 |
children | 0246add03d11 |
files | src/luan/modules/String.luan src/luan/modules/StringLuan.java website/src/manual.html.luan |
diffstat | 3 files changed, 0 insertions(+), 258 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/String.luan Tue Jul 26 12:44:52 2022 -0600 +++ b/src/luan/modules/String.luan Fri Jul 29 14:12:01 2022 -0600 @@ -12,14 +12,9 @@ String.ends_with = StringLuan.ends_with String.find = StringLuan.find String.format = StringLuan.format -String.gmatch = StringLuan.gmatch -String.gsub = StringLuan.gsub String.lower = StringLuan.lower -String.match = StringLuan.match -String.matches = StringLuan.matches String.regex = Boot.regex String.regex_quote = Pattern.quote -String.rep = StringLuan.repeated -- remove String.repeated = StringLuan.repeated String.replace = StringLuan.replace String.reverse = StringLuan.reverse
--- a/src/luan/modules/StringLuan.java Tue Jul 26 12:44:52 2022 -0600 +++ b/src/luan/modules/StringLuan.java Fri Jul 29 14:12:01 2022 -0600 @@ -2,8 +2,6 @@ import java.security.NoSuchAlgorithmException; import java.util.Arrays; -import java.util.regex.Pattern; -import java.util.regex.Matcher; import goodjava.util.GoodUtils; import luan.Luan; import luan.LuanTable; @@ -99,100 +97,6 @@ return i == -1 ? null : new Integer[]{i+1,i+s2.length()}; } - public static String[] match(String s,String pattern,Integer init) { - int start = start(s,init,0); - Matcher m = Pattern.compile(pattern).matcher(s); - if( !m.find(start) ) - return null; - int n = m.groupCount(); - if( n == 0 ) - return new String[]{m.group()}; - String[] rtn = new String[n]; - for( int i=0; i<n; i++ ) { - rtn[i] = m.group(i+1); - } - return rtn; - } - - public static LuanFunction gmatch(String s,String pattern) throws LuanException { - Utils.checkNotNull(s); - Utils.checkNotNull(pattern,2); - final Matcher m = Pattern.compile(pattern).matcher(s); - return new LuanFunction() { - @Override public Object call(Luan luan,Object[] args) { - if( !m.find() ) - return null; - final int n = m.groupCount(); - if( n == 0 ) - return m.group(); - String[] rtn = new String[n]; - for( int i=0; i<n; i++ ) { - rtn[i] = m.group(i+1); - } - return rtn; - } - }; - } - - public static Object[] gsub(Luan luan,String s,String pattern,Object repl,Integer n) throws LuanException { - Utils.checkNotNull(s); - int max = n==null ? Integer.MAX_VALUE : n; - final Matcher m = Pattern.compile(pattern).matcher(s); - if( repl instanceof String ) { - String replacement = (String)repl; - int i = 0; - StringBuffer sb = new StringBuffer(); - while( i<max && m.find() ) { - m.appendReplacement(sb,replacement); - i++; - } - m.appendTail(sb); - return new Object[]{ sb.toString(), i }; - } - if( repl instanceof LuanTable ) { - LuanTable t = (LuanTable)repl; - int i = 0; - StringBuffer sb = new StringBuffer(); - while( i<max && m.find() ) { - String match = m.groupCount()==0 ? m.group() : m.group(1); - Object val = t.get(luan,match); - if( val != null ) { - String replacement = luan.luanToString(val); - m.appendReplacement(sb,replacement); - } - i++; - } - m.appendTail(sb); - return new Object[]{ sb.toString(), i }; - } - if( repl instanceof LuanFunction ) { - LuanFunction fn = (LuanFunction)repl; - int i = 0; - StringBuffer sb = new StringBuffer(); - while( i<max && m.find() ) { - Object[] args; - final int count = m.groupCount(); - if( count == 0 ) { - args = new String[]{m.group()}; - } else { - args = new String[count]; - for( int j=0; j<count; j++ ) { - args[j] = m.group(j+1); - } - } - Object val = Luan.first( fn.call(luan,args) ); - if( val != null ) { - String replacement = luan.luanToString(val); - m.appendReplacement(sb,replacement); - } - i++; - } - m.appendTail(sb); - return new Object[]{ sb.toString(), i }; - } - throw new LuanException( "bad argument #3 to 'gsub' (string/function/table expected)" ); - } - // note - String.format() is too stupid to convert between ints and floats. public static String format(String format,Object... args) { return String.format(format,args); @@ -214,11 +118,6 @@ return null; } - public static boolean matches(String s,String pattern) throws LuanException { - Utils.checkNotNull(s); - return Pattern.compile(pattern).matcher(s).find(); - } - public static String[] split(String s,String s2,Integer limit) throws LuanException { Utils.checkNotNull(s); Utils.checkNotNull(s2,2);
--- a/website/src/manual.html.luan Tue Jul 26 12:44:52 2022 -0600 +++ b/website/src/manual.html.luan Fri Jul 29 14:12:01 2022 -0600 @@ -2485,125 +2485,6 @@ <% end } - ["String.gmatch"] = { - title = "<code>String.gmatch (s, pattern)</code>" - content = function() -%> -<p> -Returns an iterator function that, -each time it is called, -returns the next captures from <code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) -over the string <code>s</code>. -If <code>pattern</code> specifies no captures, -then the whole match is produced in each call. -</p> - -<p> -As an example, the following loop -will iterate over all the words from string <code>s</code>, -printing one per line: -</p> -<pre> - local s = "hello world from Lua" - for w in String.gmatch(s, [[\w+]]) do - print(w) - end -</pre> - -<p> -The next example collects all pairs <code>key=value</code> from the -given string into a table: -</p> -<pre> - local t = {} - local s = "from=world, to=Lua" - for k, v in String.gmatch(s, [[(\w+)=(\w+)]]) do - t[k] = v - end -</pre> - -<p> -For this function, a caret '<code>^</code>' at the start of a pattern does not -work as an anchor, as this would prevent the iteration. -</p> -<% - end - } - ["String.gsub"] = { - title = "<code>String.gsub (s, pattern, repl [, n])</code>" - content = function() -%> -<p> -Returns a copy of <code>s</code> -in which all (or the first <code>n</code>, if given) -occurrences of the <code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) have been -replaced by a replacement string specified by <code>repl</code>, -which can be a string, a table, or a function. -<code>gsub</code> also returns, as its second value, -the total number of matches that occurred. -The name <code>gsub</code> comes from <em>Global SUBstitution</em>. -</p> - -<p> -If <code>repl</code> is a string, then its value is used for replacement. -The character <code>\</code> works as an escape character. -Any sequence in <code>repl</code> of the form <code>$<em>d</em></code>, -with <em>d</em> between 1 and 9, -stands for the value of the <em>d</em>-th captured substring. -The sequence <code>$0</code> stands for the whole match. -</p> - -<p> -If <code>repl</code> is a table, then the table is queried for every match, -using the first capture as the key. -</p> - -<p> -If <code>repl</code> is a function, then this function is called every time a -match occurs, with all captured substrings passed as arguments, -in order. -</p> - -<p> -In any case, -if the pattern specifies no captures, -then it behaves as if the whole pattern was inside a capture. -</p> - -<p> -If the value returned by the table query or by the function call -is not <b>nil</b>, -then it is used as the replacement string; -otherwise, if it is <b>nil</b>, -then there is no replacement -(that is, the original match is kept in the string). -</p> - -<p> -Here are some examples: -</p> -<pre> - x = String.gsub("hello world", [[(\w+)]], "$1 $1") - --> x="hello hello world world" - - x = String.gsub("hello world", [[\w+]], "$0 $0", 1) - --> x="hello hello world" - - x = String.gsub("hello world from Luan", [[(\w+)\s*(\w+)]], "$2 $1") - --> x="world hello Luan from" - - x = String.gsub("4+5 = $return 4+5$", [[\$(.*?)\$]], function (s) - return load(s)() - end) - --> x="4+5 = 9" - - local t = {name="lua", version="5.3"} - x = String.gsub("$name-$version.tar.gz", [[\$(\w+)]], t) - --> x="lua-5.3.tar.gz" -</pre> -<% - end - } ["String.lower"] = { title = "<code>String.lower (s)</code>" content = function() @@ -2616,39 +2497,6 @@ <% end } - ["String.match"] = { - title = "<code>String.match (s, pattern [, init])</code>" - content = function() -%> -<p> -Looks for the first <em>match</em> of -<code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) in the string <code>s</code>. -If it finds one, then <code>match</code> returns -the captures from the pattern; -otherwise it returns <b>nil</b>. -If <code>pattern</code> specifies no captures, -then the whole match is returned. -A third, optional numerical argument <code>init</code> specifies -where to start the search; -its default value is 1 and can be negative. -</p> -<% - end - } - ["String.matches"] = { - title = "<code>String.matches (s, pattern)</code>" - content = function() -%> -<p> -Returns a boolean indicating whether the <code>pattern</code> can be found in string <code>s</code>. -This function is equivalent to -</p> -<pre> - return String.match(s,pattern) ~= nil -</pre> -<% - end - } ["String.regex"] = { title = "<code>String.regex (s)</code>" content = function()