(String url)
| 22 | private static final char SPLIT = '/'; |
| 23 | |
| 24 | public static String format(String url) { |
| 25 | int length = url.length(); |
| 26 | StringBuilder sb = new StringBuilder(length); |
| 27 | |
| 28 | for (int index = 0; index < length; ) { |
| 29 | char c = url.charAt(index); |
| 30 | |
| 31 | if (c == SPLIT && index < length - 1) { |
| 32 | sb.append(c); |
| 33 | |
| 34 | StringBuilder nextSection = new StringBuilder(); |
| 35 | boolean isNumber = false; |
| 36 | boolean first = true; |
| 37 | |
| 38 | for (int j = index + 1; j < length; j++) { |
| 39 | char next = url.charAt(j); |
| 40 | |
| 41 | if ((first || isNumber) && next != SPLIT) { |
| 42 | isNumber = isNumber(next); |
| 43 | first = false; |
| 44 | } |
| 45 | |
| 46 | if (next == SPLIT) { |
| 47 | if (isNumber) { |
| 48 | sb.append("{num}"); |
| 49 | } else { |
| 50 | sb.append(nextSection.toString()); |
| 51 | } |
| 52 | index = j; |
| 53 | |
| 54 | break; |
| 55 | } else if (j == length - 1) { |
| 56 | if (isNumber) { |
| 57 | sb.append("{num}"); |
| 58 | } else { |
| 59 | nextSection.append(next); |
| 60 | sb.append(nextSection.toString()); |
| 61 | } |
| 62 | index = j + 1; |
| 63 | break; |
| 64 | } else { |
| 65 | nextSection.append(next); |
| 66 | } |
| 67 | } |
| 68 | } else { |
| 69 | sb.append(c); |
| 70 | index++; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return sb.toString(); |
| 75 | } |
| 76 | |
| 77 | private static boolean isNumber(char c) { |
| 78 | return (c >= '0' && c <= '9') || c == '.' || c == '-' || c == ','; |
no test coverage detected