Mercurial Hosting > luan
diff src/luan/modules/StringLuan.java @ 1716:b82767112d8e
add String.regex
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 24 Jul 2022 23:43:03 -0600 |
parents | ad44e849c60c |
children | c637a2a1023d |
line wrap: on
line diff
--- a/src/luan/modules/StringLuan.java Sat Jul 23 21:53:04 2022 -0600 +++ b/src/luan/modules/StringLuan.java Sun Jul 24 23:43:03 2022 -0600 @@ -92,14 +92,13 @@ return s.substring(start,end); } - public static Object[] find(String s,Object pattern,Integer init,Boolean plain) throws LuanException { + public static Object[] find(String s,String pattern,Integer init,Boolean plain) { int start = start(s,init,0); if( Boolean.TRUE.equals(plain) ) { - String ptn = (String)pattern; - int i = s.indexOf(ptn,start); - return i == -1 ? null : new Integer[]{i+1,i+ptn.length()}; + int i = s.indexOf(pattern,start); + return i == -1 ? null : new Integer[]{i+1,i+pattern.length()}; } - Matcher m = pattern(pattern).matcher(s); + Matcher m = Pattern.compile(pattern).matcher(s); if( !m.find(start) ) return null; int n = m.groupCount(); @@ -112,9 +111,9 @@ return rtn; } - public static String[] match(String s,Object pattern,Integer init) throws LuanException { + public static String[] match(String s,String pattern,Integer init) { int start = start(s,init,0); - Matcher m = pattern(pattern).matcher(s); + Matcher m = Pattern.compile(pattern).matcher(s); if( !m.find(start) ) return null; int n = m.groupCount(); @@ -127,9 +126,10 @@ return rtn; } - public static LuanFunction gmatch(String s,Object pattern) throws LuanException { + public static LuanFunction gmatch(String s,String pattern) throws LuanException { Utils.checkNotNull(s); - final Matcher m = pattern(pattern).matcher(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() ) @@ -146,10 +146,10 @@ }; } - public static Object[] gsub(Luan luan,String s,Object pattern,Object repl,Integer n) throws LuanException { + 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(pattern).matcher(s); + final Matcher m = Pattern.compile(pattern).matcher(s); if( repl instanceof String ) { String replacement = (String)repl; int i = 0; @@ -226,24 +226,14 @@ return null; } - 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 { + public static boolean matches(String s,String pattern) throws LuanException { Utils.checkNotNull(s); - return pattern(pattern).matcher(s).find(); + return Pattern.compile(pattern).matcher(s).find(); } public static String[] split(String s,String pattern,Integer limit) throws LuanException { Utils.checkNotNull(s); - Utils.checkNotNull(pattern,1); + Utils.checkNotNull(pattern,2); int n = limit==null ? -1 : limit; return s.split(pattern,n); } @@ -254,4 +244,22 @@ return BinaryLuan.to_hex( BinaryLuan.digest_message( algorithm, input.getBytes() ) ); } + public static boolean contains(String s,String s2) throws LuanException { + Utils.checkNotNull(s); + Utils.checkNotNull(s2,2); + return s.contains(s2); + } + + public static boolean starts_with(String s,String s2) throws LuanException { + Utils.checkNotNull(s); + Utils.checkNotNull(s2,2); + return s.startsWith(s2); + } + + public static boolean ends_with(String s,String s2) throws LuanException { + Utils.checkNotNull(s); + Utils.checkNotNull(s2,2); + return s.endsWith(s2); + } + }