Mercurial Hosting > luan
changeset 1778:1725fdb6c4f5
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 11 Aug 2023 10:44:56 -0600 |
parents | e59349d53fec |
children | 96dbbdf1e368 |
files | src/goodjava/util/GoodUtils.java src/luan/modules/StringLuan.java |
diffstat | 2 files changed, 20 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/src/goodjava/util/GoodUtils.java Thu Aug 10 16:24:20 2023 -0600 +++ b/src/goodjava/util/GoodUtils.java Fri Aug 11 10:44:56 2023 -0600 @@ -69,4 +69,22 @@ return list.subList(0, resultSize).toArray(result); } + // faster than String.replace() + public static String replace(String s,String target,String replacement) { + int i = s.indexOf(target); + if( i == -1 ) + return s; + int starting = 0; + int n = target.length(); + StringBuilder sb = new StringBuilder(); + do { + sb.append(s,starting,i); + sb.append(replacement); + starting = i + n; + i = s.indexOf(target,starting); + } while( i != -1 ); + sb.append(s,starting,s.length()); + return sb.toString(); + } + }
--- a/src/luan/modules/StringLuan.java Thu Aug 10 16:24:20 2023 -0600 +++ b/src/luan/modules/StringLuan.java Fri Aug 11 10:44:56 2023 -0600 @@ -148,32 +148,12 @@ 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); - } -*/ + public static String replace(String s,String target,String replacement) throws LuanException { Utils.checkNotNull(s); Utils.checkNotNull(target,2); Utils.checkNotNull(replacement,3); - int i = s.indexOf(target); - if( i == -1 ) - return s; - int starting = 0; - int n = target.length(); - StringBuilder sb = new StringBuilder(); - do { - sb.append(s,starting,i); - sb.append(replacement); - starting = i + n; - i = s.indexOf(target,starting); - } while( i != -1 ); - sb.append(s,starting,s.length()); - return sb.toString(); + return GoodUtils.replace(s,target,replacement); } }