annotate src/goodjava/util/GoodUtils.java @ 1809:90187946d1a4

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 12 May 2024 17:15:33 -0600
parents 1725fdb6c4f5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.util;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.UnsupportedEncodingException;
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.util.Base64;
1720
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
5 import java.util.ArrayList;
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 public final class GoodUtils {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 public static byte[] getBytes(String s,String charsetName) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 try {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 return s.getBytes(charsetName);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 } catch(UnsupportedEncodingException e) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 throw new RuntimeException(e);
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 }
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public static String base64Encode(String s) {
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 return Base64.getEncoder().encodeToString(getBytes(s,"UTF-8"));
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
1720
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
21
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
22 public static String[] split(String s,String splitter) {
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
23 return split(s,splitter,-1);
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
24 }
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
25
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
26 // modified from String.java
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
27 public static String[] split(String s,String splitter,int limit) {
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
28 int len = s.length();
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
29 int n = splitter.length();
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
30 int off = 0;
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
31 int next = 0;
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
32 boolean limited = limit > 0;
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
33 ArrayList<String> list = new ArrayList<>();
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
34 if( n==0 ) {
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
35 limit = limited ? Math.min(limit,len) : len;
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
36 for( int i=0; i<limit; i++ ) {
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
37 list.add(s.substring(i, i+1));
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
38 }
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
39 String[] result = new String[limit];
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
40 return list.toArray(result);
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
41 }
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
42 while ((next = s.indexOf(splitter, off)) != -1) {
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
43 if (!limited || list.size() < limit - 1) {
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
44 list.add(s.substring(off, next));
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
45 off = next + n;
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
46 } else { // last one
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
47 //assert (list.size() == limit - 1);
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
48 list.add(s.substring(off, len));
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
49 off = len;
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
50 break;
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
51 }
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
52 }
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
53 // If no match was found, return s
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
54 if (off == 0)
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
55 return new String[]{s};
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
56
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
57 // Add remaining segment
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
58 if (!limited || list.size() < limit)
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
59 list.add(s.substring(off, len));
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
60
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
61 // Construct result
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
62 int resultSize = list.size();
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
63 if (limit == 0) {
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
64 while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
65 resultSize--;
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
66 }
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
67 }
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
68 String[] result = new String[resultSize];
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
69 return list.subList(0, resultSize).toArray(result);
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
70 }
2f4c99c02436 add GoodUtils.split
Franklin Schmidt <fschmidt@gmail.com>
parents: 1583
diff changeset
71
1778
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
72 // faster than String.replace()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
73 public static String replace(String s,String target,String replacement) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
74 int i = s.indexOf(target);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
75 if( i == -1 )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
76 return s;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
77 int starting = 0;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
78 int n = target.length();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
79 StringBuilder sb = new StringBuilder();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
80 do {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
81 sb.append(s,starting,i);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
82 sb.append(replacement);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
83 starting = i + n;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
84 i = s.indexOf(target,starting);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
85 } while( i != -1 );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
86 sb.append(s,starting,s.length());
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
87 return sb.toString();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
88 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1720
diff changeset
89
1583
1cc6c7fa803d mail work
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 }