| 34 | } |
| 35 | |
| 36 | public String[] split(CharSequence input, int limit) { |
| 37 | boolean strip; |
| 38 | if (limit < 0) { |
| 39 | strip = false; |
| 40 | limit = Integer.MAX_VALUE; |
| 41 | } else if (limit == 0) { |
| 42 | strip = true; |
| 43 | limit = Integer.MAX_VALUE; |
| 44 | } else { |
| 45 | strip = false; |
| 46 | } |
| 47 | |
| 48 | List<CharSequence> list = new LinkedList<CharSequence>(); |
| 49 | int index = 0; |
| 50 | int trailing = 0; |
| 51 | int patternLength = unescaped.length(); |
| 52 | while (index < input.length() && list.size() < limit - 1) { |
| 53 | int i; |
| 54 | if (patternLength == 0) { |
| 55 | if (list.size() == 0) { |
| 56 | i = 0; |
| 57 | } else { |
| 58 | i = index + 1; |
| 59 | } |
| 60 | } else { |
| 61 | i = indexOf(input, unescaped, index); |
| 62 | } |
| 63 | |
| 64 | if (i >= 0) { |
| 65 | if (patternLength != 0 && i == index) { |
| 66 | ++ trailing; |
| 67 | } else { |
| 68 | trailing = 0; |
| 69 | } |
| 70 | |
| 71 | list.add(input.subSequence(index, i)); |
| 72 | index = i + patternLength; |
| 73 | } else { |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (strip && index > 0 && index == input.length()) { |
| 79 | ++ trailing; |
| 80 | } else { |
| 81 | trailing = 0; |
| 82 | } |
| 83 | list.add(input.subSequence(index, input.length())); |
| 84 | |
| 85 | String[] result = new String[list.size() - trailing]; |
| 86 | int i = 0; |
| 87 | for (Iterator<CharSequence> it = list.iterator(); |
| 88 | it.hasNext() && i < result.length; ++ i) |
| 89 | { |
| 90 | result[i] = it.next().toString(); |
| 91 | } |
| 92 | return result; |
| 93 | } |