Skip to content

Commit

Permalink
Create "startsWithIgnoreCase" method
Browse files Browse the repository at this point in the history
  • Loading branch information
mangstadt committed Nov 4, 2023
1 parent 0b1f5ea commit d3903b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main/java/biweekly/util/DataUri.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @author Michael Angstadt
*/
public final class DataUri {
private static final String SCHEME = "data:";
private final byte[] data;
private final String text;
private final String contentType;
Expand Down Expand Up @@ -95,8 +96,7 @@ private DataUri(String contentType, byte[] data, String text) {
public static DataUri parse(String uri) {
//Syntax: data:[<media type>][;charset=<character set>][;base64],<data>

String scheme = "data:";
if (uri.length() < scheme.length() || !uri.substring(0, scheme.length()).equalsIgnoreCase(scheme)) {
if (!StringUtils.startsWithIgnoreCase(uri, SCHEME)) {
//not a data URI
throw Messages.INSTANCE.getIllegalArgumentException(22);
}
Expand All @@ -105,8 +105,8 @@ public static DataUri parse(String uri) {
String charset = null;
boolean base64 = false;
String dataStr = null;
int tokenStart = scheme.length();
for (int i = scheme.length(); i < uri.length(); i++) {
int tokenStart = SCHEME.length();
for (int i = SCHEME.length(); i < uri.length(); i++) {
char c = uri.charAt(i);

if (c == ';') {
Expand Down Expand Up @@ -211,7 +211,7 @@ public String toString() {
*/
public String toString(String charset) {
StringBuilder sb = new StringBuilder();
sb.append("data:");
sb.append(SCHEME);
sb.append(contentType);

if (data != null) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/biweekly/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ public static void repeat(char c, int count, StringBuilder sb) {
}
}

/**
* Like {@link String#startsWith()}, but using case-insensitive
* comparison.
* @param str the string
* @param prefix the prefix
* @return true if the string begins with the given prefix using
* case-insensitive comparison, false if not
*/
public static boolean startsWithIgnoreCase(String str, String prefix) {
if (str.length() < prefix.length()) {
return false;
}

return str.substring(0, prefix.length()).equalsIgnoreCase(prefix);
}

/**
* Joins a collection of values into a delimited list.
* @param collection the collection of values
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/biweekly/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package biweekly.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -65,6 +67,13 @@ public void afterPrefixIgnoreCase() {
expected = StringUtils.afterPrefixIgnoreCase("m", "mailto:");
assertNull(expected);
}

@Test
public void startsWithIgnoreCase() {
assertTrue(StringUtils.startsWithIgnoreCase("MAILTO:[email protected]", "mailto:"));
assertFalse(StringUtils.startsWithIgnoreCase("http://www.google.com", "mailto:"));
assertFalse(StringUtils.startsWithIgnoreCase("m", "mailto:"));
}

@Test
public void join_multiple() {
Expand Down

0 comments on commit d3903b2

Please sign in to comment.