summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-06-14 02:25:48 +0000
committerPaul Phillips <paulp@improving.org>2011-06-14 02:25:48 +0000
commitf02512706f53ea743a8b8b3df27d20810f14ea4e (patch)
tree35c1284ef506f7f3146732acfdb00d8b7a4b167f /src
parentb6d1953b85bddc7ccd748fa8f8aa2b7d3eb1f194 (diff)
downloadscala-f02512706f53ea743a8b8b3df27d20810f14ea4e.tar.gz
scala-f02512706f53ea743a8b8b3df27d20810f14ea4e.tar.bz2
scala-f02512706f53ea743a8b8b3df27d20810f14ea4e.zip
Performance tweaks for AbstractFile, no review.
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/io/AbstractFile.scala54
1 files changed, 29 insertions, 25 deletions
diff --git a/src/compiler/scala/tools/nsc/io/AbstractFile.scala b/src/compiler/scala/tools/nsc/io/AbstractFile.scala
index 059ca9bd9e..4b196877a3 100644
--- a/src/compiler/scala/tools/nsc/io/AbstractFile.scala
+++ b/src/compiler/scala/tools/nsc/io/AbstractFile.scala
@@ -10,7 +10,7 @@ package io
import java.io.{ FileOutputStream, IOException, InputStream, OutputStream, BufferedOutputStream }
import java.net.URL
import PartialFunction._
-
+import java.io.File.{ separatorChar => separator }
import scala.collection.mutable.ArrayBuffer
/**
@@ -84,7 +84,6 @@ object AbstractFile {
* </p>
*/
abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
-
/** Returns the name of this abstract file. */
def name: String
@@ -137,6 +136,8 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
/** size of this file if it is a concrete file. */
def sizeOption: Option[Int] = None
+ def toURL: URL = if (file == null) null else file.toURI.toURL
+
/** Returns contents of file (if applicable) in a Char array.
* warning: use <code>Global.getSourceFile()</code> to use the proper
* encoding when converting to the char array.
@@ -186,43 +187,47 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
* @return ...
*/
def lookupPath(path: String, directory: Boolean): AbstractFile = {
- lookup((f, p, dir) => f.lookupName(p, dir), path, directory)
+ lookupPathInternal(path, directory, false)
}
/** Return an abstract file that does not check that `path' denotes
* an existing file.
*/
def lookupPathUnchecked(path: String, directory: Boolean): AbstractFile = {
- lookup((f, p, dir) => f.lookupNameUnchecked(p, dir), path, directory)
+ lookupPathInternal(path, directory, true)
}
- private def lookup(getFile: (AbstractFile, String, Boolean) => AbstractFile,
- path0: String,
- directory: Boolean): AbstractFile = {
- val separator = JFile.separatorChar
+ private def lookupPathInternal(path0: String, directory: Boolean, unchecked: Boolean): AbstractFile = {
// trim trailing '/'s
- val path: String = if (path0.last == separator) path0 dropRight 1 else path0
- val length = path.length()
- assert(length > 0 && !(path.last == separator), path)
- var file = this
- var start = 0
- while (true) {
+ val path = (
+ if (path0.charAt(path0.length - 1) != separator) path0
+ else path0.substring(0, path0.length - 1)
+ )
+ def loop(file: AbstractFile, start: Int): AbstractFile = {
val index = path.indexOf(separator, start)
- assert(index < 0 || start < index)
- val name = path.substring(start, if (index < 0) length else index)
- file = getFile(file, name, if (index < 0) directory else true)
- if ((file eq null) || index < 0) return file
- start = index + 1
+ if (index < 0) {
+ if (unchecked) file.lookupNameUnchecked(path, directory)
+ else file.lookupName(path, directory)
+ }
+ else {
+ val name = path.substring(start, index)
+ val next = (
+ if (unchecked) file.lookupNameUnchecked(name, true)
+ else file.lookupName(name, true)
+ )
+ if (next == null) null
+ else loop(next, index + 1)
+ }
}
- file
+ loop(this, 0)
}
- private def fileOrSubdirectoryNamed(name: String, isDir: Boolean): AbstractFile = {
- val lookup = lookupName(name, isDir)
+ private def fileOrSubdirectoryNamed(name: String, directory: Boolean): AbstractFile = {
+ val lookup = lookupName(name, directory)
if (lookup != null) lookup
else {
val jfile = new JFile(file, name)
- if (isDir) jfile.mkdirs() else jfile.createNewFile()
+ if (directory) jfile.mkdirs() else jfile.createNewFile()
new PlainFile(jfile)
}
}
@@ -249,6 +254,5 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
protected def unsupported(msg: String): Nothing = throw new UnsupportedOperationException(msg)
/** Returns the path of this abstract file. */
- override def toString() = path
-
+ override def toString = path
}