Browse Source

Moving string utils methods to a utils class

master
anthony restaino 8 years ago
parent
commit
431f5782ff
  1. 56
      app/src/main/java/acr/browser/lightning/utils/AdBlock.java
  2. 99
      app/src/main/java/acr/browser/lightning/utils/StringBuilderUtils.java

56
app/src/main/java/acr/browser/lightning/utils/AdBlock.java

@ -154,24 +154,26 @@ public class AdBlock {
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
lineBuilder.append(line); lineBuilder.append(line);
if (!isEmpty(lineBuilder) && !startsWith(lineBuilder, COMMENT)) { if (!StringBuilderUtils.isEmpty(lineBuilder) &&
replace(lineBuilder, LOCAL_IP_V4, EMPTY); !StringBuilderUtils.startsWith(lineBuilder, COMMENT)) {
replace(lineBuilder, LOCAL_IP_V4_ALT, EMPTY); StringBuilderUtils.replace(lineBuilder, LOCAL_IP_V4, EMPTY);
replace(lineBuilder, LOCAL_IP_V6, EMPTY); StringBuilderUtils.replace(lineBuilder, LOCAL_IP_V4_ALT, EMPTY);
replace(lineBuilder, TAB, EMPTY); StringBuilderUtils.replace(lineBuilder, LOCAL_IP_V6, EMPTY);
StringBuilderUtils.replace(lineBuilder, TAB, EMPTY);
int comment = lineBuilder.indexOf(COMMENT); int comment = lineBuilder.indexOf(COMMENT);
if (comment >= 0) { if (comment >= 0) {
lineBuilder.replace(comment, lineBuilder.length(), EMPTY); lineBuilder.replace(comment, lineBuilder.length(), EMPTY);
} }
trim(lineBuilder); StringBuilderUtils.trim(lineBuilder);
if (!isEmpty(lineBuilder) && !AdBlock.equals(lineBuilder, LOCALHOST)) { if (!StringBuilderUtils.isEmpty(lineBuilder) &&
while (contains(lineBuilder, SPACE)) { !StringBuilderUtils.equals(lineBuilder, LOCALHOST)) {
while (StringBuilderUtils.contains(lineBuilder, SPACE)) {
int space = lineBuilder.indexOf(SPACE); int space = lineBuilder.indexOf(SPACE);
String host = lineBuilder.substring(0, space); String host = lineBuilder.substring(0, space);
replace(lineBuilder, host, EMPTY); StringBuilderUtils.replace(lineBuilder, host, EMPTY);
mBlockedDomainsList.add(host.trim()); mBlockedDomainsList.add(host.trim());
} }
if (lineBuilder.length() > 0) { if (lineBuilder.length() > 0) {
@ -192,40 +194,4 @@ public class AdBlock {
}); });
} }
// TODO: 4/23/17 Move all these methods to a StringUtils class
private static void replace(@NonNull StringBuilder stringBuilder,
@NonNull String toReplace,
@NonNull String replacement) {
int index = stringBuilder.indexOf(toReplace);
if (index >= 0) {
stringBuilder.replace(index, index + toReplace.length(), replacement);
}
}
private static void trim(@NonNull StringBuilder stringBuilder) {
while (stringBuilder.indexOf(SPACE) == 0) {
stringBuilder.replace(0, 1, EMPTY);
}
while (stringBuilder.lastIndexOf(SPACE) == (stringBuilder.length() - 1)) {
stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), EMPTY);
}
}
private static boolean isEmpty(@NonNull StringBuilder stringBuilder) {
return stringBuilder.length() == 0;
}
private static boolean startsWith(@NonNull StringBuilder stringBuilder, @NonNull String start) {
return stringBuilder.indexOf(start) == 0;
}
private static boolean contains(@NonNull StringBuilder stringBuilder, @NonNull String contains) {
return stringBuilder.indexOf(contains) >= 0;
}
private static boolean equals(@NonNull StringBuilder stringBuilder, @NonNull String equal) {
int index = stringBuilder.indexOf(equal);
return index >= 0 && stringBuilder.length() == equal.length();
}
} }

99
app/src/main/java/acr/browser/lightning/utils/StringBuilderUtils.java

@ -0,0 +1,99 @@
package acr.browser.lightning.utils;
import android.support.annotation.NonNull;
/**
* A collection of utils methods for
* {@link StringBuilder} that provides
* API equality with the {@link String}
* API.
*/
public class StringBuilderUtils {
private static final String SPACE = " ";
private static final String EMPTY = "";
/**
* Replace a string in a string
* builder with another string.
*
* @param stringBuilder the string builder.
* @param toReplace the string to replace.
* @param replacement the replacement string.
*/
public static void replace(@NonNull StringBuilder stringBuilder,
@NonNull String toReplace,
@NonNull String replacement) {
int index = stringBuilder.indexOf(toReplace);
if (index >= 0) {
stringBuilder.replace(index, index + toReplace.length(), replacement);
}
}
/**
* Trims a string builder of
* any spaces at the beginning
* and end.
*
* @param stringBuilder the string builder.
*/
public static void trim(@NonNull StringBuilder stringBuilder) {
while (stringBuilder.indexOf(SPACE) == 0) {
stringBuilder.replace(0, 1, EMPTY);
}
while (stringBuilder.lastIndexOf(SPACE) == (stringBuilder.length() - 1)) {
stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), EMPTY);
}
}
/**
* Determines if the string builder is empty.
*
* @param stringBuilder the string builder.
* @return true if the string builder is empty,
* false otherwise.
*/
public static boolean isEmpty(@NonNull StringBuilder stringBuilder) {
return stringBuilder.length() == 0;
}
/**
* Determines if a string builder starts with
* a specific string.
*
* @param stringBuilder the string builder.
* @param start the starting string.
* @return true if the string builder starts
* with the string, false otherwise.
*/
public static boolean startsWith(@NonNull StringBuilder stringBuilder, @NonNull String start) {
return stringBuilder.indexOf(start) == 0;
}
/**
* Determines if a string builder contains a string.
*
* @param stringBuilder the string builder.
* @param contains the string that it might contain.
* @return true if the string builder contains the
* string, false otherwise.
*/
public static boolean contains(@NonNull StringBuilder stringBuilder, @NonNull String contains) {
return stringBuilder.indexOf(contains) >= 0;
}
/**
* Determines equality between a string
* builder and a string.
*
* @param stringBuilder the string builder.
* @param equal the string.
* @return true if the string represented by
* the string builder is equal to the string.
*/
public static boolean equals(@NonNull StringBuilder stringBuilder, @NonNull String equal) {
int index = stringBuilder.indexOf(equal);
return index >= 0 && stringBuilder.length() == equal.length();
}
}
Loading…
Cancel
Save