Use platform independent filename splitting.
@@ -17,6 +17,8 @@ package com.google.j2cl.common;
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
+import com.google.common.base.Splitter;
+import com.google.common.collect.Iterables;
import java.io.File;
import javax.annotation.Nullable;
@@ -87,7 +89,9 @@ public abstract class SourcePosition implements Comparable<SourcePosition> {
/** Returns the file name portion of a path. */
public static String getFileName(String filePath) {
- String[] pathComponents = filePath.split(File.separator);
- return pathComponents[pathComponents.length - 1];
+ // Do not use String.split(File.separator) because the parameter to String.split() is a regex,
+ // therefore, for example on windows, the file separator ("\") would need escaping.
+ // Conversely, Splitter.on() takes a string parameter literally.
+ return Iterables.getLast(Splitter.on(File.separator).split(filePath));
}
}
