Mercurial Hosting > luan
view src/luan/modules/StringLuan.java @ 1722:7d2ab44f7a59
remove String regex fns
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 29 Jul 2022 14:12:01 -0600 |
parents | 5c69d2e8bd75 |
children | e59349d53fec |
line wrap: on
line source
package luan.modules; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import goodjava.util.GoodUtils; import luan.Luan; import luan.LuanTable; import luan.LuanFunction; import luan.LuanException; public final class StringLuan { static int start(String s,int i) { int len = s.length(); return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0); } static int start(String s,Integer i,int dflt) { return i==null ? dflt : start(s,i); } static int end(String s,int i) { int len = s.length(); return i==0 ? 0 : i > 0 ? Math.min(i,len) : Math.max(len+i+1,0); } static int end(String s,Integer i,int dflt) { return i==null ? dflt : end(s,i); } public static Integer[] unicode(String s,Integer i,Integer j) throws LuanException { Utils.checkNotNull(s); int start = start(s,i,0); int end = end(s,j,start+1); Integer[] chars = new Integer[end-start]; for( int k=0; k<chars.length; k++ ) { chars[k] = (int)s.charAt(start+k); } return chars; } public static String char_(int... chars) { char[] a = new char[chars.length]; for( int i=0; i<chars.length; i++ ) { a[i] = (char)chars[i]; } return new String(a); } public static byte[] to_binary(String s) { return s.getBytes(); } public static String lower(String s) throws LuanException { Utils.checkNotNull(s); return s.toLowerCase(); } public static String upper(String s) throws LuanException { Utils.checkNotNull(s); return s.toUpperCase(); } public static String trim(String s) throws LuanException { Utils.checkNotNull(s); return s.trim(); } public static String reverse(String s) throws LuanException { Utils.checkNotNull(s); return new StringBuilder(s).reverse().toString(); } public static String repeated(String s,int n,String sep) { if( n < 1 ) return ""; StringBuilder buf = new StringBuilder(s); while( --n > 0 ) { if( sep != null ) buf.append(sep); buf.append(s); } return buf.toString(); } public static String sub(String s,int i,Integer j) throws LuanException { Utils.checkNotNull(s); int start = start(s,i); int end = end(s,j,s.length()); return s.substring(start,end); } public static Object[] find(String s,String s2,Integer init) { int start = start(s,init,0); int i = s.indexOf(s2,start); return i == -1 ? null : new Integer[]{i+1,i+s2.length()}; } // 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); } public static String encode(String s) { return Luan.stringEncode(s); } public static Number to_number(String s,Integer base) throws LuanException { Utils.checkNotNull(s); try { if( base == null ) { return Double.valueOf(s); } else { return Long.valueOf(s,base); } } catch(NumberFormatException e) {} return null; } public static String[] split(String s,String s2,Integer limit) throws LuanException { Utils.checkNotNull(s); Utils.checkNotNull(s2,2); int n = limit==null ? -1 : limit; return GoodUtils.split(s,s2,n); } public static String digest_message(String algorithm,String input) throws LuanException, NoSuchAlgorithmException { Utils.checkNotNull(algorithm,1); Utils.checkNotNull(input,2); 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); } public static String replace(String s,String target,String replacement) throws LuanException { Utils.checkNotNull(s); Utils.checkNotNull(target,2); Utils.checkNotNull(replacement,3); return s.replace(target,replacement); } }