mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-17 04:46:10 +00:00
Fixed #4. Added support for indexOf function
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user