From f4094f8b3fa44f16808c1875bd34f237f7684bb5 Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Fri, 20 May 2016 19:28:31 +0530 Subject: [PATCH] Fixed #4. Added support for `indexOf` function --- src/main/java/strman/Strman.java | 45 ++++++++++++++++++++-------- src/test/java/strman/StrmanTest.java | 23 ++++++++++++++ 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 7119c99..673ec91 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -450,6 +450,25 @@ public abstract class Strman { return encode(value, 4, 16); } + /** + * The indexOf() method returns the index within the calling String of the first occurrence of the specified value, starting the search at fromIndex. + * Returns -1 if the value is not found. + * + * @param value The input String + * @param needle The search String + * @param offset The offset to start searching from. + * @param caseSensitive boolean to indicate whether search should be case sensitive + * @return Returns position of first occurrence of needle. + */ + public static int indexOf(final String value, final String needle, int offset, boolean caseSensitive) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + if (caseSensitive) { + return value.indexOf(needle, offset); + } + return value.toLowerCase().indexOf(needle.toLowerCase(), offset); + } + + public static String leftPad(final String value, final String pad, final int length) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); if (value.length() >= length) { @@ -458,6 +477,19 @@ public abstract class Strman { return append(Stream.generate(() -> pad).limit(length - value.length()).collect(joining()), value); } + public static String decode(final String value, int digits, int radix) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + return Arrays + .stream(value.split("(?<=\\G.{" + digits + "})")) + .map(data -> String.valueOf(Character.toChars(Integer.parseInt(data, radix)))) + .collect(joining()); + } + + public static String encode(String value, int digits, int radix) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + return value.chars().mapToObj(ch -> leftPad(Integer.toString(ch, radix), "0", digits)).collect(joining()); + } + private static long countSubstr(String value, String subStr, boolean allowOverlapping, long count) { int position = value.indexOf(subStr); if (position == -1) { @@ -471,17 +503,4 @@ public abstract class Strman { } return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count); } - - public static String decode(final String value, int digits, int radix) { - validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); - return Arrays - .stream(value.split("(?<=\\G.{" + digits + "})")) - .map(data -> String.valueOf(Character.toChars(Integer.parseInt(data, radix)))) - .collect(joining()); - } - - public static String encode(String value, int digits, int radix) { - validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); - return value.chars().mapToObj(ch -> leftPad(Integer.toString(ch, radix), "0", digits)).collect(joining()); - } } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 6239b2f..64267a2 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -358,4 +358,27 @@ public class StrmanTest { assertThat(hexEncode("Á"), equalTo("00c1")); assertThat(hexEncode("AA"), equalTo("00410041")); } + + @Test + public void indexOf_shouldBeTrueWhenNeedleExists() throws Exception { + final String value = "foobar"; + assertThat(indexOf(value, "f", 0, true), equalTo(0)); + assertThat(indexOf(value, "o", 0, true), equalTo(1)); + assertThat(indexOf(value, "b", 0, true), equalTo(3)); + assertThat(indexOf(value, "a", 0, true), equalTo(4)); + assertThat(indexOf(value, "r", 0, true), equalTo(5)); + assertThat(indexOf(value, "t", 0, true), equalTo(-1)); + } + + @Test + public void indexOf_shouldBeTrueWhenNeedleExistCaseSensitive() throws Exception { + final String value = "foobar"; + assertThat(indexOf(value, "F", 0, false), equalTo(0)); + assertThat(indexOf(value, "O", 0, false), equalTo(1)); + assertThat(indexOf(value, "B", 0, false), equalTo(3)); + assertThat(indexOf(value, "A", 0, false), equalTo(4)); + assertThat(indexOf(value, "R", 0, false), equalTo(5)); + assertThat(indexOf(value, "T", 0, false), equalTo(-1)); + + } } \ No newline at end of file