kucci / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

New overload for Ints.indexOf, etc. to specify range #480

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
java.lang.String has an indexOf(String str, int fromIndex) method for where you 
can find the index of some substring starting from some point in the middle of 
the String.
It would be nice if Chars and the other primitives utilities had this too.

For example, here's something I'm working on right now.  Say I've slurped up a 
file into a char[] buffer named "buf" populated to length "len". I need to find 
the chars "ABC" and then the nearest "XYZ" after "ABC".  As I am lazy, I could 
take the easy route and turn the buffer into a String to find the indexes:
String s = new String(buf, 0, len);
int start = s.indexOf("ABC");
int end   = s.indexOf("XYZ", start);
(...eliding any error handling)
The reason I'm doing this is to output the chars (0, start) and (end+3, len) to 
a Writer, effectively snipping out the data between ABC and XYZ.

However, the char[] buffer could be potentially large, and I'd rather avoid 
transforming it into a String for philosophical reasons as well.

Discovering the first index with the Chars util is easy enough:
char[] ABC_CHARS = "ABC".toCharArray();
int start = Chars.indexOf(buf, ABC_CHARS);

But the second indexOf is not immediately possible.

So, along with Chars.indexOf(char[] array, char[] target), it would be nice to 
have the method Chars.indexOf(char[] array, char[] target, int offset) that 
would trivially skip the first X array elements as specified in the offset.

(Perhaps a different parameter order would be better, I'm not sure.  A 
signature of Chars.indexOf(char[] array, int offset, char[] target) would 
conform to other array-handling methods in Java, but having the offset at the 
end makes it more like String's indexOf().  Whichever...)

It might also be nice to specify an ending index along with a starting index.  
I don't have a use case on me, but it wouldn't be hard to dream one up.

Regards

Original issue reported on code.google.com by stevenro...@gmail.com on 18 Nov 2010 at 6:06

GoogleCodeExporter commented 9 years ago
Note that we have methods that could be turned into what you want by adding 
javadoc, precondition checks, unit tests, etc.

136:   public static int indexOf(int[] array, int target) {
137:     return indexOf(array, target, 0, array.length);
138:   }
139: 
140:   // TODO: consider making this public
141:   private static int indexOf(
142:       int[] array, int target, int start, int end) {
143:     for (int i = start; i < end; i++) {
144:       if (array[i] == target) {
145:         return i;
146:       }
147:     }
148:     return -1;
149:   }

Original comment by kevinb@google.com on 12 Jan 2011 at 10:55

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 6:18