Mercurial Hosting > luan
comparison src/luan/modules/StringLuan.java @ 1335:e0cf0d108a77
major cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Feb 2019 03:10:45 -0700 |
parents | c88b486a9511 |
children | f5368cd8c056 |
comparison
equal
deleted
inserted
replaced
1334:c88b486a9511 | 1335:e0cf0d108a77 |
---|---|
5 import java.util.regex.Matcher; | 5 import java.util.regex.Matcher; |
6 import luan.Luan; | 6 import luan.Luan; |
7 import luan.LuanTable; | 7 import luan.LuanTable; |
8 import luan.LuanFunction; | 8 import luan.LuanFunction; |
9 import luan.LuanException; | 9 import luan.LuanException; |
10 import luan.LuanMethod; | |
11 | 10 |
12 | 11 |
13 public final class StringLuan { | 12 public final class StringLuan { |
14 | 13 |
15 static int start(String s,int i) { | 14 static int start(String s,int i) { |
28 | 27 |
29 static int end(String s,Integer i,int dflt) { | 28 static int end(String s,Integer i,int dflt) { |
30 return i==null ? dflt : end(s,i); | 29 return i==null ? dflt : end(s,i); |
31 } | 30 } |
32 | 31 |
33 @LuanMethod public static Integer[] unicode(String s,Integer i,Integer j) throws LuanException { | 32 public static Integer[] unicode(String s,Integer i,Integer j) throws LuanException { |
34 Utils.checkNotNull(s); | 33 Utils.checkNotNull(s); |
35 int start = start(s,i,1); | 34 int start = start(s,i,1); |
36 int end = end(s,j,start+1); | 35 int end = end(s,j,start+1); |
37 Integer[] chars = new Integer[end-start]; | 36 Integer[] chars = new Integer[end-start]; |
38 for( int k=0; k<chars.length; k++ ) { | 37 for( int k=0; k<chars.length; k++ ) { |
47 a[i] = (char)chars[i]; | 46 a[i] = (char)chars[i]; |
48 } | 47 } |
49 return new String(a); | 48 return new String(a); |
50 } | 49 } |
51 | 50 |
52 @LuanMethod public static byte[] to_binary(String s) { | 51 public static byte[] to_binary(String s) { |
53 return s.getBytes(); | 52 return s.getBytes(); |
54 } | 53 } |
55 | 54 |
56 public static String lower(String s) throws LuanException { | 55 public static String lower(String s) throws LuanException { |
57 Utils.checkNotNull(s); | 56 Utils.checkNotNull(s); |
90 int start = start(s,i); | 89 int start = start(s,i); |
91 int end = end(s,j,s.length()); | 90 int end = end(s,j,s.length()); |
92 return s.substring(start,end); | 91 return s.substring(start,end); |
93 } | 92 } |
94 | 93 |
95 @LuanMethod public static Object[] find(String s,String pattern,Integer init,Boolean plain) { | 94 public static Object[] find(String s,String pattern,Integer init,Boolean plain) { |
96 int start = start(s,init,0); | 95 int start = start(s,init,0); |
97 if( Boolean.TRUE.equals(plain) ) { | 96 if( Boolean.TRUE.equals(plain) ) { |
98 int i = s.indexOf(pattern,start); | 97 int i = s.indexOf(pattern,start); |
99 return i == -1 ? null : new Integer[]{i+1,i+pattern.length()}; | 98 return i == -1 ? null : new Integer[]{i+1,i+pattern.length()}; |
100 } | 99 } |
109 rtn[2+i] = m.group(i+1); | 108 rtn[2+i] = m.group(i+1); |
110 } | 109 } |
111 return rtn; | 110 return rtn; |
112 } | 111 } |
113 | 112 |
114 @LuanMethod public static String[] match(String s,String pattern,Integer init) { | 113 public static String[] match(String s,String pattern,Integer init) { |
115 int start = start(s,init,0); | 114 int start = start(s,init,0); |
116 Matcher m = Pattern.compile(pattern).matcher(s); | 115 Matcher m = Pattern.compile(pattern).matcher(s); |
117 if( !m.find(start) ) | 116 if( !m.find(start) ) |
118 return null; | 117 return null; |
119 int n = m.groupCount(); | 118 int n = m.groupCount(); |
127 } | 126 } |
128 | 127 |
129 public static LuanFunction gmatch(String s,String pattern) throws LuanException { | 128 public static LuanFunction gmatch(String s,String pattern) throws LuanException { |
130 Utils.checkNotNull(s); | 129 Utils.checkNotNull(s); |
131 final Matcher m = Pattern.compile(pattern).matcher(s); | 130 final Matcher m = Pattern.compile(pattern).matcher(s); |
132 return new LuanFunction() { | 131 return new LuanFunction(false) { |
133 @Override public Object call(Luan luan,Object[] args) { | 132 @Override public Object call(Object[] args) { |
134 if( !m.find() ) | 133 if( !m.find() ) |
135 return null; | 134 return null; |
136 final int n = m.groupCount(); | 135 final int n = m.groupCount(); |
137 if( n == 0 ) | 136 if( n == 0 ) |
138 return m.group(); | 137 return m.group(); |
143 return rtn; | 142 return rtn; |
144 } | 143 } |
145 }; | 144 }; |
146 } | 145 } |
147 | 146 |
148 @LuanMethod public static Object[] gsub(Luan luan,String s,String pattern,Object repl,Integer n) throws LuanException { | 147 public static Object[] gsub(String s,String pattern,Object repl,Integer n) throws LuanException { |
149 Utils.checkNotNull(s); | 148 Utils.checkNotNull(s); |
150 int max = n==null ? Integer.MAX_VALUE : n; | 149 int max = n==null ? Integer.MAX_VALUE : n; |
151 final Matcher m = Pattern.compile(pattern).matcher(s); | 150 final Matcher m = Pattern.compile(pattern).matcher(s); |
152 if( repl instanceof String ) { | 151 if( repl instanceof String ) { |
153 String replacement = (String)repl; | 152 String replacement = (String)repl; |
189 args = new String[count]; | 188 args = new String[count]; |
190 for( int j=0; j<count; j++ ) { | 189 for( int j=0; j<count; j++ ) { |
191 args[j] = m.group(j+1); | 190 args[j] = m.group(j+1); |
192 } | 191 } |
193 } | 192 } |
194 Object val = Luan.first( fn.call(luan,args) ); | 193 Object val = Luan.first( fn.call(args) ); |
195 if( val != null ) { | 194 if( val != null ) { |
196 String replacement = Luan.luanToString(val); | 195 String replacement = Luan.luanToString(val); |
197 m.appendReplacement(sb,replacement); | 196 m.appendReplacement(sb,replacement); |
198 } | 197 } |
199 i++; | 198 i++; |
236 public static boolean matches(String s,String pattern) throws LuanException { | 235 public static boolean matches(String s,String pattern) throws LuanException { |
237 Utils.checkNotNull(s); | 236 Utils.checkNotNull(s); |
238 return Pattern.compile(pattern).matcher(s).find(); | 237 return Pattern.compile(pattern).matcher(s).find(); |
239 } | 238 } |
240 | 239 |
241 @LuanMethod public static String[] split(String s,String pattern,Integer limit) throws LuanException { | 240 public static String[] split(String s,String pattern,Integer limit) throws LuanException { |
242 Utils.checkNotNull(s); | 241 Utils.checkNotNull(s); |
243 int n = limit==null ? -1 : limit; | 242 int n = limit==null ? -1 : limit; |
244 return s.split(pattern,n); | 243 return s.split(pattern,n); |
245 } | 244 } |
246 | 245 |