Skip to content

Commit

Permalink
GetChars made faster
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Aug 12, 2023
1 parent 7ae90c2 commit 7faa0db
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ public static int[] countChars(final CharSequence s, final char... chars) {
* @return number of instances of each char in [start, end)
*/
public static int[] countChars(final CharSequence s, int start, int end, final char... chars) {
// Faster specialization for the common single case
if (chars.length == 1) {
return new int[] {countChar(s, start, end, chars[0])};
}

final int[] counts = new int[chars.length];
start = Math.max(0, start);
end = Math.min(end, s.length());
Expand All @@ -232,6 +237,9 @@ public static int countChar(final CharSequence s, final char c) {
return countChar(s, 0, s.length(), c);
}

/**
* Count instances of a single char in a charsequence
*/
public static int countChar(final CharSequence s, int start, int end, final char c) {
start = Math.max(0, start);
end = Math.min(end, s.length());
Expand Down

0 comments on commit 7faa0db

Please sign in to comment.