Mercurial Hosting > luan
changeset 1715:ad44e849c60c
add String.regex_compile
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 23 Jul 2022 21:53:04 -0600 |
parents | 31a82b0d0a87 |
children | b82767112d8e |
files | src/luan/modules/String.luan src/luan/modules/StringLuan.java |
diffstat | 2 files changed, 24 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
diff -r 31a82b0d0a87 -r ad44e849c60c src/luan/modules/String.luan --- a/src/luan/modules/String.luan Mon Jul 18 23:49:47 2022 -0600 +++ b/src/luan/modules/String.luan Sat Jul 23 21:53:04 2022 -0600 @@ -14,6 +14,7 @@ String.lower = StringLuan.lower String.match = StringLuan.match String.matches = StringLuan.matches +String.regex_compile = Pattern.compile String.regex_quote = Pattern.quote String.rep = StringLuan.rep String.reverse = StringLuan.reverse
diff -r 31a82b0d0a87 -r ad44e849c60c src/luan/modules/StringLuan.java --- a/src/luan/modules/StringLuan.java Mon Jul 18 23:49:47 2022 -0600 +++ b/src/luan/modules/StringLuan.java Sat Jul 23 21:53:04 2022 -0600 @@ -92,13 +92,14 @@ return s.substring(start,end); } - public static Object[] find(String s,String pattern,Integer init,Boolean plain) { + public static Object[] find(String s,Object pattern,Integer init,Boolean plain) throws LuanException { int start = start(s,init,0); if( Boolean.TRUE.equals(plain) ) { - int i = s.indexOf(pattern,start); - return i == -1 ? null : new Integer[]{i+1,i+pattern.length()}; + String ptn = (String)pattern; + int i = s.indexOf(ptn,start); + return i == -1 ? null : new Integer[]{i+1,i+ptn.length()}; } - Matcher m = Pattern.compile(pattern).matcher(s); + Matcher m = pattern(pattern).matcher(s); if( !m.find(start) ) return null; int n = m.groupCount(); @@ -111,9 +112,9 @@ return rtn; } - public static String[] match(String s,String pattern,Integer init) { + public static String[] match(String s,Object pattern,Integer init) throws LuanException { int start = start(s,init,0); - Matcher m = Pattern.compile(pattern).matcher(s); + Matcher m = pattern(pattern).matcher(s); if( !m.find(start) ) return null; int n = m.groupCount(); @@ -126,10 +127,9 @@ return rtn; } - public static LuanFunction gmatch(String s,String pattern) throws LuanException { + public static LuanFunction gmatch(String s,Object pattern) throws LuanException { Utils.checkNotNull(s); - Utils.checkNotNull(pattern,2); - final Matcher m = Pattern.compile(pattern).matcher(s); + final Matcher m = pattern(pattern).matcher(s); return new LuanFunction() { @Override public Object call(Luan luan,Object[] args) { if( !m.find() ) @@ -146,10 +146,10 @@ }; } - public static Object[] gsub(Luan luan,String s,String pattern,Object repl,Integer n) throws LuanException { + public static Object[] gsub(Luan luan,String s,Object 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); + final Matcher m = pattern(pattern).matcher(s); if( repl instanceof String ) { String replacement = (String)repl; int i = 0; @@ -226,9 +226,19 @@ return null; } - public static boolean matches(String s,String pattern) throws LuanException { + private static Pattern pattern(Object pattern) throws LuanException { + if( pattern instanceof Pattern ) { + return (Pattern)pattern; + } else if( pattern instanceof String ) { + return Pattern.compile((String)pattern); + } else { + throw new LuanException( "bad argument #2 (string or compiled pattern expected)" ); + } + } + + public static boolean matches(String s,Object pattern) throws LuanException { Utils.checkNotNull(s); - return Pattern.compile(pattern).matcher(s).find(); + return pattern(pattern).matcher(s).find(); } public static String[] split(String s,String pattern,Integer limit) throws LuanException {