comparison core/src/luan/modules/StringLuan.java @ 271:82a3ebcfbafa

add internal tests git-svn-id: https://luan-java.googlecode.com/svn/trunk@272 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 31 Oct 2014 04:27:50 +0000
parents 9e0d4452e649
children ae7ae2755b48
comparison
equal deleted inserted replaced
270:b2c20fdcf42a 271:82a3ebcfbafa
16 public static final LuanFunction LOADER = new LuanFunction() { 16 public static final LuanFunction LOADER = new LuanFunction() {
17 @Override public Object call(LuanState luan,Object[] args) { 17 @Override public Object call(LuanState luan,Object[] args) {
18 LuanTable module = Luan.newTable(); 18 LuanTable module = Luan.newTable();
19 try { 19 try {
20 add( module, "to_binary", String.class ); 20 add( module, "to_binary", String.class );
21 add( module, "to_integers", String.class ); 21 module.put( "byte", new LuanJavaFunction(StringLuan.class.getMethod( "byte_", String.class ),null) );
22 module.put( "char", new LuanJavaFunction(StringLuan.class.getMethod( "char_", new int[0].getClass() ),null) ); 22 module.put( "char", new LuanJavaFunction(StringLuan.class.getMethod( "char_", new int[0].getClass() ),null) );
23 add( module, "find", String.class, String.class, Integer.class, Boolean.class ); 23 add( module, "find", String.class, String.class, Integer.class, Boolean.class );
24 add( module, "format", String.class, new Object[0].getClass() ); 24 add( module, "format", String.class, new Object[0].getClass() );
25 add( module, "gmatch", String.class, String.class ); 25 add( module, "gmatch", LuanState.class, String.class, String.class );
26 add( module, "gsub", LuanState.class, String.class, String.class, Object.class, Integer.class ); 26 add( module, "gsub", LuanState.class, String.class, String.class, Object.class, Integer.class );
27 add( module, "len", String.class ); 27 add( module, "len", LuanState.class, String.class );
28 add( module, "lower", String.class ); 28 add( module, "lower", LuanState.class, String.class );
29 add( module, "match", String.class, String.class, Integer.class ); 29 add( module, "match", String.class, String.class, Integer.class );
30 add( module, "rep", String.class, Integer.TYPE, String.class ); 30 add( module, "rep", String.class, Integer.TYPE, String.class );
31 add( module, "reverse", String.class ); 31 add( module, "reverse", LuanState.class, String.class );
32 add( module, "sub", LuanState.class, String.class, Integer.TYPE, Integer.class ); 32 add( module, "sub", LuanState.class, String.class, Integer.TYPE, Integer.class );
33 add( module, "upper", String.class ); 33 add( module, "trim", LuanState.class, String.class );
34 add( module, "upper", LuanState.class, String.class );
34 } catch(NoSuchMethodException e) { 35 } catch(NoSuchMethodException e) {
35 throw new RuntimeException(e); 36 throw new RuntimeException(e);
36 } 37 }
37 return module; 38 return module;
38 } 39 }
81 82
82 public static byte[] to_binary(String s) { 83 public static byte[] to_binary(String s) {
83 return s.getBytes(); 84 return s.getBytes();
84 } 85 }
85 86
86 public static int[] to_integers(String s) { 87 public static int[] byte_(String s) {
87 char[] a = s.toCharArray(); 88 char[] a = s.toCharArray();
88 int[] chars = new int[a.length]; 89 int[] chars = new int[a.length];
89 for( int i=0; i<a.length; i++ ) { 90 for( int i=0; i<a.length; i++ ) {
90 chars[i] = a[i]; 91 chars[i] = a[i];
91 } 92 }
98 a[i] = (char)chars[i]; 99 a[i] = (char)chars[i];
99 } 100 }
100 return new String(a); 101 return new String(a);
101 } 102 }
102 103
103 public static int len(String s) { 104 public static int len(LuanState luan,String s) throws LuanException {
105 Utils.checkNotNull(luan,s);
104 return s.length(); 106 return s.length();
105 } 107 }
106 108
107 public static String lower(String s) { 109 public static String lower(LuanState luan,String s) throws LuanException {
110 Utils.checkNotNull(luan,s);
108 return s.toLowerCase(); 111 return s.toLowerCase();
109 } 112 }
110 113
111 public static String upper(String s) { 114 public static String upper(LuanState luan,String s) throws LuanException {
115 Utils.checkNotNull(luan,s);
112 return s.toUpperCase(); 116 return s.toUpperCase();
113 } 117 }
114 118
115 public static String reverse(String s) { 119 public static String trim(LuanState luan,String s) throws LuanException {
120 Utils.checkNotNull(luan,s);
121 return s.trim();
122 }
123
124 public static String reverse(LuanState luan,String s) throws LuanException {
125 Utils.checkNotNull(luan,s);
116 return new StringBuilder(s).reverse().toString(); 126 return new StringBuilder(s).reverse().toString();
117 } 127 }
118 128
119 public static String rep(String s,int n,String sep) { 129 public static String rep(String s,int n,String sep) {
120 if( n < 1 ) 130 if( n < 1 )
158 rtn[i] = m.group(i+1); 168 rtn[i] = m.group(i+1);
159 } 169 }
160 return rtn; 170 return rtn;
161 } 171 }
162 172
163 public static LuanFunction gmatch(String s,String pattern) { 173 public static LuanFunction gmatch(LuanState luan,String s,String pattern) throws LuanException {
174 Utils.checkNotNull(luan,s);
164 final Matcher m = Pattern.compile(pattern).matcher(s); 175 final Matcher m = Pattern.compile(pattern).matcher(s);
165 return new LuanFunction() { 176 return new LuanFunction() {
166 @Override public Object call(LuanState luan,Object[] args) { 177 @Override public Object call(LuanState luan,Object[] args) {
167 if( !m.find() ) 178 if( !m.find() )
168 return null; 179 return null;