MCPcopy Index your code
hub / github.com/davidgiven/luje / normalize

Method normalize

lib/java/net/URI.java:1305–1389  ·  view source on GitHub ↗
(String path)

Source from the content-addressed store, hash-verified

1303 * normalize path, and return the resulting string
1304 */
1305 private String normalize(String path) {
1306 // count the number of '/'s, to determine number of segments
1307 int index = -1;
1308 int pathlen = path.length();
1309 int size = 0;
1310 if (pathlen > 0 && path.charAt(0) != '/') {
1311 size++;
1312 }
1313 while ((index = path.indexOf('/', index + 1)) != -1) {
1314 if (index + 1 < pathlen && path.charAt(index + 1) != '/') {
1315 size++;
1316 }
1317 }
1318
1319 String[] seglist = new String[size];
1320 boolean[] include = new boolean[size];
1321
1322 // break the path into segments and store in the list
1323 int current = 0;
1324 int index2 = 0;
1325 index = (pathlen > 0 && path.charAt(0) == '/') ? 1 : 0;
1326 while ((index2 = path.indexOf('/', index + 1)) != -1) {
1327 seglist[current++] = path.substring(index, index2);
1328 index = index2 + 1;
1329 }
1330
1331 // if current==size, then the last character was a slash
1332 // and there are no more segments
1333 if (current < size) {
1334 seglist[current] = path.substring(index);
1335 }
1336
1337 // determine which segments get included in the normalized path
1338 for (int i = 0; i < size; i++) {
1339 include[i] = true;
1340 if (seglist[i].equals("..")) { //$NON-NLS-1$
1341 int remove = i - 1;
1342 // search back to find a segment to remove, if possible
1343 while (remove > -1 && !include[remove]) {
1344 remove--;
1345 }
1346 // if we find a segment to remove, remove it and the ".."
1347 // segment
1348 if (remove > -1 && !seglist[remove].equals("..")) { //$NON-NLS-1$
1349 include[remove] = false;
1350 include[i] = false;
1351 }
1352 } else if (seglist[i].equals(".")) { //$NON-NLS-1$
1353 include[i] = false;
1354 }
1355 }
1356
1357 // put the path back together
1358 StringBuilder newpath = new StringBuilder();
1359 if (path.startsWith("/")) { //$NON-NLS-1$
1360 newpath.append('/');
1361 }
1362

Callers 2

relativizeMethod · 0.95
resolveMethod · 0.95

Calls 14

appendMethod · 0.95
deleteCharAtMethod · 0.95
toStringMethod · 0.95
indexOfMethod · 0.95
insertMethod · 0.95
duplicateMethod · 0.95
setSchemeSpecificPartMethod · 0.95
startsWithMethod · 0.80
endsWithMethod · 0.80
lengthMethod · 0.65
charAtMethod · 0.65
indexOfMethod · 0.65

Tested by

no test coverage detected