Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/com/github/rjeschke/neetutils/Strings.java

Issue 5697499218051072: Usage of new API, cleanups (reduced) (Closed)
Patch Set: Fixed leftover first-batch review issues. Created April 16, 2014, 5:51 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2012 René Jeschke <rene_jeschke@yahoo.de>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.github.rjeschke.neetutils;
17
18 import java.io.UnsupportedEncodingException;
19 import java.nio.charset.Charset;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 /**
24 * String utility methods.
25 *
26 * @author René Jeschke (rene_jeschke@yahoo.de)
27 */
28 public class Strings
29 {
30 /**
31 * Produces a in Java usable string representation of the given String, wrap ping it in quotes and escaping all relevant characters.
32 *
33 * @param str
34 * The String to escape.
35 * @return Escape String or &quote;null&quote; if str is <code>null</code>
36 */
37 public final static String escapeString(final String str)
38 {
39 if (str == null)
40 {
41 return "null";
42 }
43
44 final StringBuilder sb = new StringBuilder();
45
46 sb.append('"');
47 for (int i = 0; i < str.length(); i++)
48 {
49 final char c = str.charAt(i);
50 switch (c)
51 {
52 case '\b':
53 sb.append("\\b");
54 break;
55 case '\f':
56 sb.append("\\f");
57 break;
58 case '\t':
59 sb.append("\\t");
60 break;
61 case '\n':
62 sb.append("\\n");
63 break;
64 case '\r':
65 sb.append("\\r");
66 break;
67 case '"':
68 sb.append("\\\"");
69 break;
70 default:
71 sb.append(c);
72 break;
73 }
74 }
75 sb.append('"');
76
77 return sb.toString();
78 }
79
80 public final static String replace(final String str, final char oldChar, fin al char newChar)
81 {
82 final StringBuilder sb = new StringBuilder();
83
84 for (int i = 0; i < str.length(); i++)
85 {
86 final char ch = str.charAt(i);
87 if (ch == oldChar)
88 {
89 sb.append(newChar);
90 }
91 else
92 {
93 sb.append(ch);
94 }
95 }
96
97 return sb.toString();
98 }
99
100 public final static List<String> split(final String str, final char ch)
101 {
102 final List<String> ret = new ArrayList<String>();
103
104 if (str != null)
105 {
106 int s = 0, e = 0;
107 while (e < str.length())
108 {
109 if (str.charAt(e) == ch)
110 {
111 ret.add(str.substring(s, e));
112 s = e + 1;
113 }
114 e++;
115 }
116 ret.add(str.substring(s, e));
117 }
118
119 return ret;
120 }
121
122 public final static List<String> trim(final List<String> strings)
123 {
124 for (int i = 0; i < strings.size(); i++)
125 {
126 final String s = strings.get(i);
127 if (s != null)
128 {
129 strings.set(i, s.trim());
130 }
131 }
132 return strings;
133 }
134
135 public final static boolean isEmpty(final String str)
136 {
137 return str == null || str.length() == 0;
138 }
139
140 public final static String collapseWhitespace(final String str)
141 {
142 final StringBuilder sb = new StringBuilder();
143 boolean wasWs = false;
144 for (int i = 0; i < str.length(); i++)
145 {
146 final char c = str.charAt(i);
147 if (Character.isWhitespace(c) || Character.isSpaceChar(c))
148 {
149 if (!wasWs)
150 {
151 sb.append(' ');
152 wasWs = true;
153 }
154 }
155 else
156 {
157 wasWs = false;
158 sb.append(c);
159 }
160 }
161 return sb.toString();
162 }
163
164 public final static String join(final Iterable<String> iterable, final Strin g glue)
165 {
166 final StringBuilder sb = new StringBuilder();
167 int index = 0;
168 for (final String s : iterable)
169 {
170 if (index > 0)
171 {
172 sb.append(glue);
173 }
174 sb.append(s);
175 ++index;
176 }
177
178 return sb.toString();
179 }
180
181 public final static String join(final Iterable<String> iterable)
182 {
183 final StringBuilder sb = new StringBuilder();
184
185 for (final String s : iterable)
186 {
187 sb.append(s);
188 }
189
190 return sb.toString();
191 }
192
193 public final static List<String> splitLength(final String str, final int len )
194 {
195 final List<String> ret = new ArrayList<String>();
196 int todo = str.length();
197 int pos = 0;
198 while (todo > 0)
199 {
200 final int n = Math.min(todo, len);
201 ret.add(str.substring(pos, pos + n));
202 pos += n;
203 todo -= n;
204 }
205 return ret;
206 }
207
208 public final static int firstDiffIndex(final String a, final String b)
209 {
210 final int todo = Math.min(a.length(), b.length());
211 int i = 0;
212 for (; i < todo; i++)
213 {
214 if (a.charAt(i) != b.charAt(i))
215 {
216 break;
217 }
218 }
219 return i;
220 }
221
222 public final static String from(final byte[] bytes, final int offs, final in t length, final Charset charset) throws UnsupportedEncodingException
223 {
224 return new String(bytes, offs, length, charset.name());
225 }
226
227 public final static String from(final byte[] bytes, final Charset charset) t hrows UnsupportedEncodingException
228 {
229 return new String(bytes, charset.name());
230 }
231
232 public final static String from(final byte[] bytes, final int offs, final in t length) throws UnsupportedEncodingException
233 {
234 return new String(bytes, offs, length, Charsets.UTF8.name());
235 }
236
237 public final static String from(final byte[] bytes) throws UnsupportedEncodi ngException
238 {
239 return new String(bytes, Charsets.UTF8.name());
240 }
241 }
OLDNEW
« no previous file with comments | « src/com/github/rjeschke/neetutils/Charsets.java ('k') | src/org/adblockplus/ChunkedOutputStream.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld