comparison core/src/luan/modules/StringLuan.java @ 539:473e456444ff

Remove object-oriented primitive methods for string and binary
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 01 Jun 2015 17:53:55 -0600
parents e3fb9768dbb3
children f4dfe9c64c25
comparison
equal deleted inserted replaced
538:919b9410008e 539:473e456444ff
13 import luan.LuanBit; 13 import luan.LuanBit;
14 14
15 15
16 public final class StringLuan { 16 public final class StringLuan {
17 17
18 public static Object __index(LuanBit bit,final String s,Object key) throws LuanException {
19 LuanState luan = bit.luan;
20 LuanTable mod = (LuanTable)PackageLuan.require(luan,"luan:String");
21 Object obj = mod.get(luan,key);
22 if( obj instanceof LuanFunction ) {
23 final LuanFunction fn = (LuanFunction)obj;
24 return new LuanFunction() {
25 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
26 Object[] a = new Object[args.length+1];
27 a[0] = s;
28 System.arraycopy(args,0,a,1,args.length);
29 return fn.call(luan,a);
30 }
31 };
32 }
33 if( luan.hasJava() ) {
34 Object rtn = JavaLuan.__index(bit,s,key,true);
35 if( rtn != JavaLuan.FAIL )
36 return rtn;
37 }
38 if( bit.el != null )
39 throw bit.exception( "invalid index for string in '"+bit.el.text()+"'" ) ;
40 else
41 throw bit.exception( "invalid index for string") ;
42 }
43
44 static int start(String s,int i) { 18 static int start(String s,int i) {
45 int len = s.length(); 19 int len = s.length();
46 return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0); 20 return i==0 ? 0 : i > 0 ? Math.min(i-1,len) : Math.max(len+i,0);
47 } 21 }
48 22
76 a[i] = (char)chars[i]; 50 a[i] = (char)chars[i];
77 } 51 }
78 return new String(a); 52 return new String(a);
79 } 53 }
80 54
81 @LuanMethod public static byte[] to_binary(String s) { 55 @LuanMethod public static byte[] string_to_binary(String s) {
82 return s.getBytes(); 56 return s.getBytes();
83 } 57 }
84 58
85 public static int len(LuanState luan,String s) throws LuanException { 59 public static int len(LuanState luan,String s) throws LuanException {
86 Utils.checkNotNull(luan,s); 60 Utils.checkNotNull(luan,s);
247 221
248 public static String encode(String s) { 222 public static String encode(String s) {
249 return Luan.stringEncode(s); 223 return Luan.stringEncode(s);
250 } 224 }
251 225
252 public static Number to_number(LuanState luan,String s,Integer base) throws LuanException { 226 public static Number string_to_number(LuanState luan,String s,Integer base) throws LuanException {
253 Utils.checkNotNull(luan,s); 227 Utils.checkNotNull(luan,s);
254 try { 228 try {
255 if( base == null ) { 229 if( base == null ) {
256 return Double.valueOf(s); 230 return Double.valueOf(s);
257 } else { 231 } else {