From d3903b23ceaf14c5b5039cf3ba4e10a13a266c89 Mon Sep 17 00:00:00 2001 From: Mike Angstadt Date: Sat, 4 Nov 2023 10:19:59 -0400 Subject: [PATCH] Create "startsWithIgnoreCase" method --- src/main/java/biweekly/util/DataUri.java | 10 +++++----- src/main/java/biweekly/util/StringUtils.java | 16 ++++++++++++++++ src/test/java/biweekly/util/StringUtilsTest.java | 9 +++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/biweekly/util/DataUri.java b/src/main/java/biweekly/util/DataUri.java index cc471395..f2237135 100644 --- a/src/main/java/biweekly/util/DataUri.java +++ b/src/main/java/biweekly/util/DataUri.java @@ -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; @@ -95,8 +96,7 @@ private DataUri(String contentType, byte[] data, String text) { public static DataUri parse(String uri) { //Syntax: data:[][;charset=][;base64], - 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); } @@ -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 == ';') { @@ -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) { diff --git a/src/main/java/biweekly/util/StringUtils.java b/src/main/java/biweekly/util/StringUtils.java index 194af331..741ac8de 100644 --- a/src/main/java/biweekly/util/StringUtils.java +++ b/src/main/java/biweekly/util/StringUtils.java @@ -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 diff --git a/src/test/java/biweekly/util/StringUtilsTest.java b/src/test/java/biweekly/util/StringUtilsTest.java index 27bb11eb..260997b6 100644 --- a/src/test/java/biweekly/util/StringUtilsTest.java +++ b/src/test/java/biweekly/util/StringUtilsTest.java @@ -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; @@ -65,6 +67,13 @@ public void afterPrefixIgnoreCase() { expected = StringUtils.afterPrefixIgnoreCase("m", "mailto:"); assertNull(expected); } + + @Test + public void startsWithIgnoreCase() { + assertTrue(StringUtils.startsWithIgnoreCase("MAILTO:email@example.com", "mailto:")); + assertFalse(StringUtils.startsWithIgnoreCase("http://www.google.com", "mailto:")); + assertFalse(StringUtils.startsWithIgnoreCase("m", "mailto:")); + } @Test public void join_multiple() {